Transfers data over the SPI bus @param out bytes to send @return bytes read in (array is the same length as out) @webref
(byte[] out)
| 162 | * @webref |
| 163 | */ |
| 164 | public byte[] transfer(byte[] out) { |
| 165 | if (NativeInterface.isSimulated()) { |
| 166 | return new byte[out.length]; |
| 167 | } |
| 168 | |
| 169 | // track the current setting per device across multiple instances |
| 170 | String curSettings = maxSpeed + "-" + dataOrder + "-" + mode; |
| 171 | if (!curSettings.equals(settings.get(dev))) { |
| 172 | int ret = NativeInterface.setSpiSettings(handle, maxSpeed, dataOrder, mode); |
| 173 | if (ret < 0) { |
| 174 | System.err.println(NativeInterface.getError(handle)); |
| 175 | throw new RuntimeException("Error updating device configuration"); |
| 176 | } |
| 177 | settings.put(dev, curSettings); |
| 178 | } |
| 179 | |
| 180 | byte[] in = new byte[out.length]; |
| 181 | int transferred = NativeInterface.transferSpi(handle, out, in); |
| 182 | if (transferred < 0) { |
| 183 | throw new RuntimeException(NativeInterface.getError(transferred)); |
| 184 | } else if (transferred < out.length) { |
| 185 | throw new RuntimeException("Fewer bytes transferred than requested: " + transferred); |
| 186 | } |
| 187 | return in; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | /** |
nothing calls this directly
no test coverage detected