diff --git a/src/mgos_si7021.c b/src/mgos_si7021.c index 0e257bd..5117482 100644 --- a/src/mgos_si7021.c +++ b/src/mgos_si7021.c @@ -19,6 +19,19 @@ #include "mgos_i2c.h" // Private functions follow +static bool mgos_si7021_cmd(struct mgos_si7021 *sensor, uint8_t cmd) { + + if (!sensor || !sensor->i2c) + return false; + + if (!mgos_i2c_write(sensor->i2c, sensor->i2caddr, &cmd, 1, true)) { + LOG(LL_ERROR, ("I2C=0x%02x cmd=%u (0x%02x) write error", sensor->i2caddr, cmd, cmd)); + return false; + } + LOG(LL_DEBUG, ("I2C=0x%02x cmd=%u (0x%02x) write success", sensor->i2caddr, cmd, cmd)); + return true; +} + static uint8_t crc8(const uint8_t *data, int len) { const uint8_t POLYNOMIAL=0x31; @@ -76,10 +89,9 @@ bool mgos_si7021_read(struct mgos_si7021 *sensor) { } // Read out sensor data here // - uint8_t cmd = MGOS_SI7021_MEASRH_NOHOLD_CMD; uint8_t data[3]; - if (!mgos_i2c_write(sensor->i2c, sensor->i2caddr, &cmd, 1, false)) { + if (!mgos_si7021_cmd(sensor, MGOS_SI7021_MEASRH_NOHOLD_CMD)) { LOG(LL_ERROR, ("Could not write command")); return false; } @@ -99,8 +111,7 @@ bool mgos_si7021_read(struct mgos_si7021 *sensor) { humidity -= 6; sensor->humidity = humidity; - cmd=MGOS_SI7021_MEASTEMP_NOHOLD_CMD; - if (!mgos_i2c_write(sensor->i2c, sensor->i2caddr, &cmd, 1, false)) { + if (!mgos_si7021_cmd(sensor, MGOS_SI7021_MEASTEMP_NOHOLD_CMD)) { LOG(LL_ERROR, ("Could not write command")); return false; }