In my understanding next two methods must perform the same write action regarding I2C device and they both are correct
private void writeWrong(int addr, byte data) throws IOException, InterruptedException {
ByteBuffer buf = ByteBuffer.allocate(1);
buf.put(data);
i2cdevice.begin();
i2cdevice.write(addr, 1, buf);
i2cdevice.end();
Thread.sleep(2000);
}
private void write(int addr, byte data) throws IOException, InterruptedException {
byte[] bytes = new byte[]{
data
};
i2cdevice.begin();
i2cdevice.write(addr, 1, ByteBuffer.wrap(bytes));
i2cdevice.end();
Thread.sleep(2000);
}
However, if we read immediately after write operation to verify the data, it occurs that writeWrong method does not actually write any data
private byte read(int addr) throws IOException {
byte[] bytes = new byte[1];
i2cdevice.read(addr, 1, ByteBuffer.wrap(bytes));
return bytes[0];
}
so i.e.
int value = 16;
writeWrong(0x00, (byte) value);
read(0x00) = 0
write(0x00, (byte) value);
read(0x00) = 16
private void writeWrong(int addr, byte data) throws IOException, InterruptedException {
ByteBuffer buf = ByteBuffer.allocate(1);
buf.put(data);
i2cdevice.begin();
i2cdevice.write(addr, 1, buf);
i2cdevice.end();
Thread.sleep(2000);
}
private void write(int addr, byte data) throws IOException, InterruptedException {
byte[] bytes = new byte[]{
data
};
i2cdevice.begin();
i2cdevice.write(addr, 1, ByteBuffer.wrap(bytes));
i2cdevice.end();
Thread.sleep(2000);
}
However, if we read immediately after write operation to verify the data, it occurs that writeWrong method does not actually write any data
private byte read(int addr) throws IOException {
byte[] bytes = new byte[1];
i2cdevice.read(addr, 1, ByteBuffer.wrap(bytes));
return bytes[0];
}
so i.e.
int value = 16;
writeWrong(0x00, (byte) value);
read(0x00) = 0
write(0x00, (byte) value);
read(0x00) = 16