SI7021
Add mgos_si7021_cmd() mimicking the SHT31 driver.
This commit is contained in:
@ -19,6 +19,19 @@
|
|||||||
#include "mgos_i2c.h"
|
#include "mgos_i2c.h"
|
||||||
|
|
||||||
// Private functions follow
|
// 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)
|
static uint8_t crc8(const uint8_t *data, int len)
|
||||||
{
|
{
|
||||||
const uint8_t POLYNOMIAL=0x31;
|
const uint8_t POLYNOMIAL=0x31;
|
||||||
@ -76,10 +89,9 @@ bool mgos_si7021_read(struct mgos_si7021 *sensor) {
|
|||||||
}
|
}
|
||||||
// Read out sensor data here
|
// Read out sensor data here
|
||||||
//
|
//
|
||||||
uint8_t cmd = MGOS_SI7021_MEASRH_NOHOLD_CMD;
|
|
||||||
uint8_t data[3];
|
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"));
|
LOG(LL_ERROR, ("Could not write command"));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -99,8 +111,7 @@ bool mgos_si7021_read(struct mgos_si7021 *sensor) {
|
|||||||
humidity -= 6;
|
humidity -= 6;
|
||||||
sensor->humidity = humidity;
|
sensor->humidity = humidity;
|
||||||
|
|
||||||
cmd=MGOS_SI7021_MEASTEMP_NOHOLD_CMD;
|
if (!mgos_si7021_cmd(sensor, MGOS_SI7021_MEASTEMP_NOHOLD_CMD)) {
|
||||||
if (!mgos_i2c_write(sensor->i2c, sensor->i2caddr, &cmd, 1, false)) {
|
|
||||||
LOG(LL_ERROR, ("Could not write command"));
|
LOG(LL_ERROR, ("Could not write command"));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user