diff --git a/include/mgos_htu21df.h b/include/mgos_htu21df.h new file mode 100644 index 0000000..aeb21c9 --- /dev/null +++ b/include/mgos_htu21df.h @@ -0,0 +1,64 @@ +#pragma once + +#include "mgos.h" +#include "mgos_i2c.h" + +#define MGOS_HTU21DF_READ_DELAY (2) + +#ifdef __cplusplus +extern "C" { +#endif + +struct mgos_htu21df; + +/* + * Initialize a HTU21DF on the I2C bus `i2c` at address specified in `i2caddr` + * parameter (default HTU21DF is on address 0x40). The sensor will be polled for + * validity, upon success a new `struct mgos_htu21df` is allocated and + * returned. If the device could not be found, NULL is returned. + */ +struct mgos_htu21df *mgos_htu21df_create(struct mgos_i2c *i2c, uint8_t i2caddr); + +/* + * Destroy the data structure associated with a HTU21DF device. The reference + * to the pointer of the `struct mgos_htu21df` has to be provided, and upon + * successful destruction, its associated memory will be freed and the pointer + * set to NULL. + */ +void mgos_htu21df_destroy(struct mgos_htu21df **sensor); + +/* + * The sensor will be polled for its temperature and humidity data. If the poll + * has occured in the last `MGOS_HTU21DF_READ_DELAY` seconds, the cached data is + * used (so as not to repeatedly poll the bus upon subsequent calls). + */ +bool mgos_htu21df_read(struct mgos_htu21df *sensor); + +/* + * The sensor will be polled for its temperature and humidity data. If the poll + * has occured in the last `MGOS_HTU21DF_READ_DELAY` seconds, the cached data is + * used (so as not to repeatedly poll the bus upon subsequent calls). + * + * The return value is the temperature of the sensor in Celsius, or NAN if no + * data was found. + */ +float mgos_htu21df_getTemperature(struct mgos_htu21df *sensor); + +/* + * The sensor will be polled for its temperature and humidity data. If the poll + * has occured in the last `MGOS_HTU21DF_READ_DELAY` seconds, the cached data is + * used (so as not to repeatedly poll the bus upon subsequent calls). + * + * The return value is the humidity of the sensor in percent relative humidity, + * or NAN if no data was found. + */ +float mgos_htu21df_getHumidity(struct mgos_htu21df *sensor); + +/* + * Initialization function for MGOS -- currently a noop. + */ +bool mgos_htu21df_i2c_init(void); + +#ifdef __cplusplus +} +#endif diff --git a/src/mgos_htu21df.c b/src/mgos_htu21df.c new file mode 100644 index 0000000..0de346a --- /dev/null +++ b/src/mgos_htu21df.c @@ -0,0 +1,82 @@ +/* + * 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_htu21df_internal.h" +#include "mgos_i2c.h" + +// Datasheet: +// https://cdn-shop.adafruit.com/datasheets/1899_HTU21D.pdf + +// Private functions follow +// Private functions end + +// Public functions follow +struct mgos_htu21df *mgos_htu21df_create(struct mgos_i2c *i2c, uint8_t i2caddr) { + struct mgos_htu21df *sensor; + + if (!i2c) return NULL; + + sensor=calloc(1, sizeof(struct mgos_htu21df)); + if (!sensor) return NULL; + sensor->i2caddr=i2caddr; + sensor->i2c=i2c; + sensor->last_read_time=0; + + LOG(LL_INFO, ("HTU21DF created at I2C 0x%02x", i2caddr)); + return sensor; +} + +void mgos_htu21df_destroy(struct mgos_htu21df **sensor) { + if (!*sensor) return; + free (*sensor); + *sensor=NULL; + return; +} + +bool mgos_htu21df_read(struct mgos_htu21df *sensor) { + double now = mg_time(); + + if (!sensor || !sensor->i2c) + return false; + + if (now - sensor->last_read_time < MGOS_HTU21DF_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_htu21df_getTemperature(struct mgos_htu21df *sensor) { + if (!mgos_htu21df_read(sensor)) return NAN; + + return sensor->temperature; +} + +float mgos_htu21df_getHumidity(struct mgos_htu21df *sensor) { + if (!mgos_htu21df_read(sensor)) return NAN; + + return sensor->humidity; +} + +bool mgos_htu21df_i2c_init(void) { + return true; +} +// Public functions end diff --git a/src/mgos_htu21df_internal.h b/src/mgos_htu21df_internal.h new file mode 100644 index 0000000..8ee22ab --- /dev/null +++ b/src/mgos_htu21df_internal.h @@ -0,0 +1,31 @@ +#pragma once + +#include "mgos.h" +#include "mgos_i2c.h" +#include "mgos_htu21df.h" +#include + +#define MGOS_HTU21DF_DEFAULT_I2CADDR (0x40) + +#define MGOS_HTU21DF_READTEMP (0xE3) +#define MGOS_HTU21DF_READHUM (0xE5) +#define MGOS_HTU21DF_WRITEREG (0xE6) +#define MGOS_HTU21DF_READREG (0xE7) +#define MGOS_HTU21DF_RESET (0xFE) + + +#ifdef __cplusplus +extern "C" { +#endif + +struct mgos_htu21df { + struct mgos_i2c *i2c; + uint8_t i2caddr; + double last_read_time; + + float humidity, temperature; +}; + +#ifdef __cplusplus +} +#endif