diff --git a/code/nrf-connect/src/main.c b/code/nrf-connect/src/main.c index 77b7756..e2651ce 100644 --- a/code/nrf-connect/src/main.c +++ b/code/nrf-connect/src/main.c @@ -27,12 +27,17 @@ int main(void) { RET_IF_ERR(prst_adc_batt_read(&sensors.batt)); RET_IF_ERR(prst_adc_soil_read(sensors.batt.voltage, &sensors.soil)); RET_IF_ERR(prst_adc_photo_read(sensors.batt.voltage, &sensors.photo)); + RET_IF_ERR(prst_shtc3_read(&sensors.shtc3)) LOG_INF("Batt: %d mV", sensors.batt.millivolts); LOG_INF("Soil: %.0f %% (%.3f mV)", 100 * sensors.soil.percentage, sensors.soil.adc_read.voltage); LOG_INF("Photo: %u lx (%.3f mV)", sensors.photo.brightness, sensors.soil.adc_read.voltage); + LOG_INF("Temp: %f oC (%d)", sensors.shtc3.temp_c, + (int)sensors.shtc3.temp_c); + LOG_INF("Humi: %.0f %%", 100.0 * sensors.shtc3.rel_humi); + LOG_INF("--------------------------------------------------"); k_msleep(500); } } diff --git a/code/nrf-connect/src/prst/shtc3.c b/code/nrf-connect/src/prst/shtc3.c index 1ed3aca..581cf7d 100644 --- a/code/nrf-connect/src/prst/shtc3.c +++ b/code/nrf-connect/src/prst/shtc3.c @@ -4,28 +4,31 @@ #include #include -LOG_MODULE_REGISTER(shtc3, LOG_LEVEL_DBG); +#include "prst/macros.h" + +LOG_MODULE_REGISTER(shtc3, LOG_LEVEL_INF); static const struct i2c_dt_spec shtc3 = I2C_DT_SPEC_GET(DT_NODELABEL(shtc3)); static uint8_t buff[6]; -static void write_cmd(uint16_t command) { +static int write_cmd(uint16_t command) { static uint8_t cmd[2]; cmd[0] = command >> 8; cmd[1] = command & 0xff; - if (i2c_write_dt(&shtc3, cmd, sizeof(cmd)) != 0) { - LOG_ERR("Error writing command"); - } + RET_IF_ERR(i2c_write_dt(&shtc3, cmd, sizeof(cmd))); + return 0; } -prst_shtc3_read_t prst_shtc3_read() { +int prst_shtc3_read(prst_shtc3_read_t *out) { + RET_IF_ERR_MSG(!device_is_ready(shtc3.bus), "SHTC3 is not ready"); + // Wake the sensor up. - write_cmd(PRST_SHTC3_CMD_WAKEUP); + RET_IF_ERR(write_cmd(PRST_SHTC3_CMD_WAKEUP)); k_msleep(1); // Request measurement. - write_cmd(PRST_SHTC3_CMD_MEASURE_TFIRST_NORMAL); + RET_IF_ERR(write_cmd(PRST_SHTC3_CMD_MEASURE_TFIRST_NORMAL)); // Reading in normal (not low power) mode can take up to 12.1 ms, according to // the datasheet. @@ -36,19 +39,17 @@ prst_shtc3_read_t prst_shtc3_read() { } // Put the sensor in sleep mode. - write_cmd(PRST_SHTC3_CMD_SLEEP); + RET_IF_ERR(write_cmd(PRST_SHTC3_CMD_SLEEP)); // TODO: Uninit i2c to save power? // TODO: verify the CRC of the measurements. The function is described in the // datasheet. - float temp_c = -45 + 175 * ((float)((buff[0] << 8) | buff[1])) / (1 << 16); - float humi = ((float)((buff[3] << 8) | buff[4])) / UINT16_MAX; + out->temp_c = -45 + 175 * ((float)((buff[0] << 8) | buff[1])) / (1 << 16); + out->rel_humi = ((float)((buff[3] << 8) | buff[4])) / UINT16_MAX; - prst_shtc3_read_t ret = {.temp_c = temp_c, .rel_humi = humi}; - - LOG_INF("Read temp: %f oC (%d)", ret.temp_c, (int)temp_c); - LOG_INF("Read humi: %.0f %%", 100.0 * ret.rel_humi); - return ret; + LOG_DBG("Read temp: %f oC (%d)", out->temp_c, (int)out->temp_c); + LOG_DBG("Read humi: %.0f %%", 100.0 * out->rel_humi); + return 0; } \ No newline at end of file diff --git a/code/nrf-connect/src/prst/shtc3.h b/code/nrf-connect/src/prst/shtc3.h index 11b12e5..89bb180 100644 --- a/code/nrf-connect/src/prst/shtc3.h +++ b/code/nrf-connect/src/prst/shtc3.h @@ -15,6 +15,6 @@ typedef struct { float rel_humi; } prst_shtc3_read_t; -prst_shtc3_read_t prst_shtc3_read(); +int prst_shtc3_read(prst_shtc3_read_t *out); #endif // _PRST_SHT3C_H_ \ No newline at end of file