gbraad / pxt-chirp-sensor

Extension to read values from the Chirp plant watering alarm over I2C
https://forum.makecode.com/t/extension-for-the-chirp-watering-sensor/1479
1 stars 0 forks source link

Allow to change and use other address #2

Open gbraad opened 4 years ago

gbraad commented 4 years ago

"To change the I2C address of the sensor, write a new address (one byte [1..127]) to register 1; the new address will take effect after reset".

  1. select address 0x20
  2. write to register 1; eg. value 0x30
  3. reset device
  4. connect to address 0x30
gbraad commented 4 years ago

Actually, in the code it says:

                uint8_t newAddress = usiTwiReceiveByte();
                // 1st bit is reserved for protocol.
                // Several addresses in the 7-bit range are reserved
                // https://www.nxp.com/docs/en/user-guide/UM10204.pdf
                if(newAddress >= 8 && newAddress <= 123) {
                    eeprom_write_byte((uint8_t*)0x01, newAddress);
                }

which means that a limited set of address is allowed. In code I allow this check to be performed in the device side, by requesting the new address and determine a boolean state if succesful or not:

https://github.com/gbraad/pxt-chirp-sensor/blob/d38a05fddcfdf5334fc6f4085959a55c3be62381/main.ts#L35-L42