diff --git a/include/mgos_si7021.h b/include/mgos_si7021.h new file mode 100644 index 0000000..425b57e --- /dev/null +++ b/include/mgos_si7021.h @@ -0,0 +1,46 @@ +#pragma once + +#include "mgos.h" +#include "mgos_i2c.h" + +#define MGOS_SI7021_READ_DELAY (2) + +#ifdef __cplusplus +extern "C" { +#endif + +struct mgos_si7021; + +/* + * + */ +struct mgos_si7021 *mgos_si7021_create(struct mgos_i2c *i2c, uint8_t i2caddr); + +/* + * + */ +void mgos_si7021_destroy(struct mgos_si7021 **sensor); + +/* + * + */ +bool mgos_si7021_read(struct mgos_si7021 *sensor); + +/* + * + */ +float mgos_si7021_getTemperature(struct mgos_si7021 *sensor); + +/* + * + */ +float mgos_si7021_getHumidity(struct mgos_si7021 *sensor); + +/* + * + */ +bool mgos_si7021_i2c_init(void); + +#ifdef __cplusplus +} +#endif diff --git a/mgos_si7021.c b/mgos_si7021.c new file mode 100644 index 0000000..e69de29 diff --git a/src/main.c b/src/main.c index b810cb7..eafe0ae 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,7 @@ #include "mgos.h" #include "mgos_i2c.h" #include "mgos_sht31.h" +#include "mgos_si7021.h" #include #include @@ -37,25 +38,37 @@ bool i2c_dumpregs(struct mgos_i2c *i2c, uint8_t i2caddr) { bool sht31(struct mgos_i2c *i2c, uint8_t i2caddr) { struct mgos_sht31 *sht31; float temp, humid; - int num; if (!(sht31 = mgos_sht31_create(i2c, i2caddr))) { LOG(LL_ERROR, ("Cannot create SHT31 device")); return false; } - num=1000; - while (num--) { - temp=mgos_sht31_getTemperature(sht31); - humid=mgos_sht31_getHumidity(sht31); - LOG(LL_INFO, ("SHT31: temperature=%.2fC humidity=%.1f%%", temp, humid)); - sleep(1); - } + temp=mgos_sht31_getTemperature(sht31); + humid=mgos_sht31_getHumidity(sht31); + LOG(LL_INFO, ("SHT31: temperature=%.2fC humidity=%.1f%%", temp, humid)); mgos_sht31_destroy(&sht31); return true; } +bool si7021(struct mgos_i2c *i2c, uint8_t i2caddr) { + struct mgos_si7021 *si7021; + float temp, humid; + + if (!(si7021 = mgos_si7021_create(i2c, i2caddr))) { + LOG(LL_ERROR, ("Cannot create SI7021 device")); + return false; + } + + temp=mgos_si7021_getTemperature(si7021); + humid=mgos_si7021_getHumidity(si7021); + LOG(LL_INFO, ("SI7021: temperature=%.2fC humidity=%.1f%%", temp, humid)); + + mgos_si7021_destroy(&si7021); + return true; +} + int main() { struct mgos_i2c *i2c; @@ -69,7 +82,11 @@ int main() { } i2c_scanner(i2c); - sht31(i2c, 0x44); + for (;;) { + sht31(i2c, 0x44); + si7021(i2c, 0x44); + sleep(1); + } return 0; } diff --git a/src/mgos_si7021.c b/src/mgos_si7021.c new file mode 100644 index 0000000..fb0b7d1 --- /dev/null +++ b/src/mgos_si7021.c @@ -0,0 +1,77 @@ +/* + * Copyright 2018 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mgos.h" +#include "mgos_si7021_internal.h" +#include "mgos_i2c.h" + +// Private functions follow +// Private functions end + +// Public functions follow +struct mgos_si7021 *mgos_si7021_create(struct mgos_i2c *i2c, uint8_t i2caddr) { + struct mgos_si7021 *sensor; + + if (!i2c) return NULL; + + sensor=calloc(1, sizeof(struct mgos_si7021)); + if (!sensor) return NULL; + sensor->i2caddr=i2caddr; + sensor->i2c=i2c; + sensor->last_read_time=0; + return sensor; +} + +void mgos_si7021_destroy(struct mgos_si7021 **sensor) { + if (!*sensor) return; + free (*sensor); + *sensor=NULL; + return; +} + +bool mgos_si7021_read(struct mgos_si7021 *sensor) { + double now = mg_time(); + + if (!sensor || !sensor->i2c) + return false; + + if (now - sensor->last_read_time < MGOS_SI7021_READ_DELAY) { + return true; + } + // Read out sensor data here + // + + LOG(LL_DEBUG, ("temperature=%.2fC humidity=%.1f%%", sensor->temperature, sensor->humidity)); + sensor->last_read_time=now; + return true; +} + +float mgos_si7021_getTemperature(struct mgos_si7021 *sensor) { + if (!mgos_si7021_read(sensor)) return NAN; + + return sensor->temperature; +} + +float mgos_si7021_getHumidity(struct mgos_si7021 *sensor) { + if (!mgos_si7021_read(sensor)) return NAN; + + return sensor->humidity; +} + +bool mgos_si7021_i2c_init(void) { + return true; +} +// Public functions end diff --git a/src/mgos_si7021_internal.h b/src/mgos_si7021_internal.h new file mode 100644 index 0000000..7518f2d --- /dev/null +++ b/src/mgos_si7021_internal.h @@ -0,0 +1,38 @@ +#pragma once + +#include "mgos.h" +#include "mgos_i2c.h" +#include "mgos_si7021.h" +#include + +#define MGOS_SI7021_DEFAULT_I2CADDR (0x40) + +#define MGOS_SI7021_MEASRH_HOLD_CMD (0xE5) +#define MGOS_SI7021_MEASRH_NOHOLD_CMD (0xF5) +#define MGOS_SI7021_MEASTEMP_HOLD_CMD (0xE3) +#define MGOS_SI7021_MEASTEMP_NOHOLD_CMD (0xF3) +#define MGOS_SI7021_READPREVTEMP_CMD (0xE0) +#define MGOS_SI7021_RESET_CMD (0xFE) +#define MGOS_SI7021_WRITERHT_REG_CMD (0xE6) +#define MGOS_SI7021_READRHT_REG_CMD (0xE7) +#define MGOS_SI7021_WRITEHEATER_REG_CMD (0x51) +#define MGOS_SI7021_READHEATER_REG_CMD (0x11) +#define MGOS_SI7021_ID1_CMD (0xFA0F) +#define MGOS_SI7021_ID2_CMD (0xFCC9) +#define MGOS_SI7021_FIRMVERS_CMD (0x84B8) + +#ifdef __cplusplus +extern "C" { +#endif + +struct mgos_si7021 { + struct mgos_i2c *i2c; + uint8_t i2caddr; + double last_read_time; + + float humidity, temperature; +}; + +#ifdef __cplusplus +} +#endif