• I wasn't able to get @Alexandr 's code give the correct values so I tried doing it differently.

    I tested the sensor with an Arduino and a library for the HDC1080 and then transferred the whole shield over to the Pixl.js for testing.

    var exports={};
    
    var C = {
      I2C_ADDRESS : 0x40,
    };
    
    function HDC1080(i2c, deviceAddress) {
      this.i2c = i2c;
      this.deviceAddress = deviceAddress;
    }
    
    HDC1080.prototype.read = function() {
      this.i2c.writeTo(this.deviceAddress, 0);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          const data = this.i2c.readFrom(this.deviceAddress, 4);
          const temp = data[1] | data[0] << 8;
          const hum = data[3] | data[2] << 8;
          resolve({
            temp: (temp / 65536) * 165.0 - 40.0,
            humidity: (hum / 65536) * 100.0,
          });
        }, 25);
      });
    };
    
    exports.connect = function (i2c, deviceAddress) {
      return new HDC1080(i2c, deviceAddress || C.I2C_ADDRESS);
    };
    
    I2C1.setup({sda:SDA, scl:SCL});
    var hdc = exports.connect(I2C1);
    setInterval(() => {
      hdc.read().then((e) => {print(e);});
    }, 1000);
    

    It's a bit raw for now but that's how it looks like right now, ready to be done as a module.

    I do have a question for @Gordon , I had to use promises because I needed to wait a few ms for the sensor to be ready after setting the pointer for the read (otherwise I get an I2C exception). There isn't any ready pin for this sensor.

    Would promises be the way to go, or is there some other way to "delay" the I2C read but still maintain a somewhat sychronous flow?

    Edit: obligatory screenshot'


    1 Attachment

    • Annotation 2020-10-04 000704.png
About

Avatar for parasquid @parasquid started