Avatar for user149798

user149798

Member since Oct 2022 • Last active Oct 2022
  • 0 conversations
  • 2 comments

Most recent activity

  • in Electronics
    Avatar for user149798

    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.

    Your code sends the entire buffer plus a byte of value of '1' to the device, then reads 6 bytes into 'val2'.

  • in Electronics
    Avatar for user149798

    I would advise you to review the Espruino documentation on the I2C.writeTo function:

    Call type:
    function I2C.writeTo(address, data, ...)
    Parameters
    
    address - The 7 bit address of the device to transmit to, or an object of the form {address:12, stop:false} to send this data without a STOP signal.
    
    data, ... - One or more items to write. May be ints, strings, arrays, or special objects (see E.toUint8Array for more info).
    

    and also what the Adafruit writeRegister8 function does, namely pack the two arguments together and send them both to the device.

    void Adafruit_MMA8451::writeRegister8(uint8_t­ reg, uint8_t value) {
      uint8_t buffer[2] = {reg, value};
      i2c_dev->write(buffer, 2);
    }
    

    In short, your existing code appears to read and write from a variety of random device addresses, none of which have any corresponding I2C bus device apart from 29(0x1D).

Actions