Copyright © https://mongoose-os.com

Mongoose OS Forum

frame
ATTENTION! This forum has moved to:

https://community.mongoose-os.com

Do not post any new messages.

Javascript lib i2c

CCesarCCesar Recife, Brasil

Hi folks.

Have anyone used the javascript lib i2c?
I have some question about that, for example the read function:
I2C.read(handle, addr, data, len, stop)
I don't understand what does data means? and the return that function are true or false, I supposed that when I call that function the return is a value.

Sorry for my stupid question if it's.

Thank you!

Comments

  • Example: (read temperature and humidity from SHT30 device)

    load('api_i2c.js');
    let myI2C = I2C.get_default();
    I2C.write(myI2C, 0x45, "\x21\x30", 2, true);  // initiate periodic measurement of SHT30, address 0x45
    let result = "00000000000000000000"; //here the result will be saved, large enough
    I2C.write(myI2C, 0x45, "\xe0\x00", 2, true);  //Initiate date delivery of SHt30
    let success= I2C.read(myI2C, 0x45, result, 6, true);  //Read data (6 Bytes) and release bus
    if(success==false)
    {
    print("Error");
    }
    let temp = ((((result.at(0) << 8) + result.at(1)) / 65535) * 175) - 45;  //Convert to Values
    let hum = ((((result.at(0) << 8) + result.at(1)) / 65535) * 100);
    print(temp);
    print(hum);

    From the file api_i2c.js
    // ## **I2C.read(handle, addr, data, len, stop)
    **
      // Read specified number of
      // bytes from the specified address.
      // If stop is true, the bus will be released at the end.
      // Return value: success, true/false.
      read: ffi('bool mgos_i2c_read(void *, int, char *, int, bool)'),
  • CCesarCCesar Recife, Brasil

    Hi Capsicum!
    First at all thank you very much.
    I did a mistake when I was using the i2c.write function
    I was using like that:
    I2C.write(i2c, addr, "00", 2, true);
    but like you said the correct is:
    I2C.write(i2c, addr, "\x00", 2, true);

    I understand as well what data means, actually data are bytes read by the i2c bus.

    Thank you

  • Hello,

    the api_i2c.js has changed.

    Now you have to use:
    let result= I2C.read(myI2C, 0x45, 6, true); //Read data (6 Bytes) and release bus

  • CCesarCCesar Recife, Brasil

    Thanks guys!

    Very much clear now.
    Well done

Sign In or Register to comment.