It's rather confusing, but as I understand it a 'device', and the i2c object in Arduino represents a single sensor(or whatever) that is connected to the i2c bus . Meanwhile, the 'device'/i2c object in Espruino is the i2c port on the Espruino board(it might have several). This means all calls to read and write on Arduino are automatically prefixed with the address, wheras on Espruino you have to supply the address in every function call.
I would advise you to look at the function documentation and try to understand what is being done, instead of trying to translate every function argument from language 1 to language 2.
If you review the function arguments of write_then_read , it can be seen that a call of write_then_read(buffer, 1, buffer, 6) sends the first byte of buffer to the device, then reads 6 bytes into the same memory. Perhaps you are not aware of this, but in C/C++, arrays do not know their own size(an 'array' variable is actually just the memory address of the first entry) so the size must always be supplied separately.
write_then_read(buffer, 1, buffer, 6)
Your code sends the entire buffer plus a byte of value of '1' to the device, then reads 6 bytes into 'val2'.
© Espruino, powered by microcosm.
Report a problem