From d2e64c0f453618a677ccac52c3a783639ecdf052 Mon Sep 17 00:00:00 2001 From: Pim van Pelt Date: Mon, 9 Apr 2018 18:30:46 +0200 Subject: [PATCH] Reformat with bcpp Used commandline: bcpp -i 2 -yq -ya -s -bcl -tbcl $i --- include/mgos_htu21df.h | 90 ++++++++++++++++++------------------- include/mgos_mcp9808.h | 82 ++++++++++++++++----------------- include/mgos_mock.h | 13 +++--- include/mgos_sht31.h | 90 ++++++++++++++++++------------------- include/mgos_si7021.h | 90 ++++++++++++++++++------------------- src/main.c | 15 +++++-- src/mgos_htu21df.c | 16 +++++-- src/mgos_htu21df_internal.h | 18 ++++---- src/mgos_i2c.c | 33 ++++++++++---- src/mgos_mcp9808.c | 12 ++++- src/mgos_mcp9808_internal.h | 17 +++---- src/mgos_mock.c | 4 +- src/mgos_sht31.c | 25 ++++++++--- src/mgos_sht31_internal.h | 17 +++---- src/mgos_si7021.c | 14 ++++-- src/mgos_si7021_internal.h | 17 +++---- 16 files changed, 310 insertions(+), 243 deletions(-) diff --git a/include/mgos_htu21df.h b/include/mgos_htu21df.h index 281dd07..ebde7f3 100644 --- a/include/mgos_htu21df.h +++ b/include/mgos_htu21df.h @@ -23,58 +23,58 @@ #ifdef __cplusplus extern "C" { -#endif + #endif -struct mgos_htu21df; + 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); + /* + * 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); + /* + * 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). + */ + 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 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); + /* + * 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); + /* + * Initialization function for MGOS -- currently a noop. + */ + bool mgos_htu21df_i2c_init(void); -#ifdef __cplusplus + #ifdef __cplusplus } #endif diff --git a/include/mgos_mcp9808.h b/include/mgos_mcp9808.h index 2353b64..decb8ed 100644 --- a/include/mgos_mcp9808.h +++ b/include/mgos_mcp9808.h @@ -23,54 +23,54 @@ #ifdef __cplusplus extern "C" { -#endif + #endif -struct mgos_mcp9808; + struct mgos_mcp9808; -/* - * Initialize a MCP9808 on the I2C bus `i2c` at address specified in `i2caddr` - * parameter (default MCP9808 is on address 0x18). The sensor will be polled for - * validity, upon success a new `struct mgos_mcp9808` is allocated and - * returned. If the device could not be found, NULL is returned. - */ -struct mgos_mcp9808 *mgos_mcp9808_create(struct mgos_i2c *i2c, uint8_t i2caddr); + /* + * Initialize a MCP9808 on the I2C bus `i2c` at address specified in `i2caddr` + * parameter (default MCP9808 is on address 0x18). The sensor will be polled for + * validity, upon success a new `struct mgos_mcp9808` is allocated and + * returned. If the device could not be found, NULL is returned. + */ + struct mgos_mcp9808 *mgos_mcp9808_create(struct mgos_i2c *i2c, uint8_t i2caddr); -/* - * Destroy the data structure associated with a MCP9808 device. The reference - * to the pointer of the `struct mgos_mcp9808` has to be provided, and upon - * successful destruction, its associated memory will be freed and the pointer - * set to NULL. - */ -void mgos_mcp9808_destroy(struct mgos_mcp9808 **sensor); + /* + * Destroy the data structure associated with a MCP9808 device. The reference + * to the pointer of the `struct mgos_mcp9808` has to be provided, and upon + * successful destruction, its associated memory will be freed and the pointer + * set to NULL. + */ + void mgos_mcp9808_destroy(struct mgos_mcp9808 **sensor); -/* - * The sensor will be polled for its temperature and humidity data. If the poll - * has occured in the last `MGOS_MCP9808_READ_DELAY` seconds, the cached data is - * used (so as not to repeatedly poll the bus upon subsequent calls). - */ -bool mgos_mcp9808_read(struct mgos_mcp9808 *sensor); + /* + * The sensor will be polled for its temperature and humidity data. If the poll + * has occured in the last `MGOS_MCP9808_READ_DELAY` seconds, the cached data is + * used (so as not to repeatedly poll the bus upon subsequent calls). + */ + bool mgos_mcp9808_read(struct mgos_mcp9808 *sensor); -/* - * The sensor will be polled for its temperature and humidity data. If the poll - * has occured in the last `MGOS_MCP9808_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_mcp9808_getTemperature(struct mgos_mcp9808 *sensor); + /* + * The sensor will be polled for its temperature and humidity data. If the poll + * has occured in the last `MGOS_MCP9808_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_mcp9808_getTemperature(struct mgos_mcp9808 *sensor); -/* - * The sensor will be enabled (true) or disabled and put into deep sleep (false) - * based on the `enable` argument. - */ -void mgos_mcp9808_enable(struct mgos_mcp9808 *sensor, bool enable); + /* + * The sensor will be enabled (true) or disabled and put into deep sleep (false) + * based on the `enable` argument. + */ + void mgos_mcp9808_enable(struct mgos_mcp9808 *sensor, bool enable); -/* - * Initialization function for MGOS -- currently a noop. - */ -bool mgos_mcp9808_i2c_init(void); + /* + * Initialization function for MGOS -- currently a noop. + */ + bool mgos_mcp9808_i2c_init(void); -#ifdef __cplusplus + #ifdef __cplusplus } #endif diff --git a/include/mgos_mock.h b/include/mgos_mock.h index 8286082..07516a4 100644 --- a/include/mgos_mock.h +++ b/include/mgos_mock.h @@ -19,13 +19,13 @@ #ifndef __MGOS_MOCK_H #define __MGOS_MOCK_H - #include "mgos.h" #include #include // mgos_log -enum cs_log_level { +enum cs_log_level +{ LL_NONE = -1, LL_ERROR = 0, LL_WARN = 1, @@ -39,15 +39,14 @@ enum cs_log_level { int log_print_prefix(enum cs_log_level l, const char *func, const char *file); -#define LOG(l, x) \ - do { \ +#define LOG(l, x) \ + do \ + { \ if (log_print_prefix(l, __func__, __FILE__)) printf x; \ printf("\r\n"); \ } while (0) - double mg_time(); void mgos_usleep(uint32_t usecs); - -#endif // __MGOS_MOCK_H +#endif // __MGOS_MOCK_H diff --git a/include/mgos_sht31.h b/include/mgos_sht31.h index edf8756..adc152c 100644 --- a/include/mgos_sht31.h +++ b/include/mgos_sht31.h @@ -23,58 +23,58 @@ #ifdef __cplusplus extern "C" { -#endif + #endif -struct mgos_sht31; + struct mgos_sht31; -/* - * Initialize a SHT31 on the I2C bus `i2c` at address specified in `i2caddr` - * parameter (default SHT31 is on address 0x44). The sensor will be polled for - * validity, upon success a new `struct mgos_sht31` is allocated and - * returned. If the device could not be found, NULL is returned. - */ -struct mgos_sht31 *mgos_sht31_create(struct mgos_i2c *i2c, uint8_t i2caddr); + /* + * Initialize a SHT31 on the I2C bus `i2c` at address specified in `i2caddr` + * parameter (default SHT31 is on address 0x44). The sensor will be polled for + * validity, upon success a new `struct mgos_sht31` is allocated and + * returned. If the device could not be found, NULL is returned. + */ + struct mgos_sht31 *mgos_sht31_create(struct mgos_i2c *i2c, uint8_t i2caddr); -/* - * Destroy the data structure associated with a SHT31 device. The reference - * to the pointer of the `struct mgos_sht31` has to be provided, and upon - * successful destruction, its associated memory will be freed and the pointer - * set to NULL. - */ -void mgos_sht31_destroy(struct mgos_sht31 **sensor); + /* + * Destroy the data structure associated with a SHT31 device. The reference + * to the pointer of the `struct mgos_sht31` has to be provided, and upon + * successful destruction, its associated memory will be freed and the pointer + * set to NULL. + */ + void mgos_sht31_destroy(struct mgos_sht31 **sensor); -/* - * The sensor will be polled for its temperature and humidity data. If the poll - * has occured in the last `MGOS_SHT31_READ_DELAY` seconds, the cached data is - * used (so as not to repeatedly poll the bus upon subsequent calls). - */ -bool mgos_sht31_read(struct mgos_sht31 *sensor); + /* + * The sensor will be polled for its temperature and humidity data. If the poll + * has occured in the last `MGOS_SHT31_READ_DELAY` seconds, the cached data is + * used (so as not to repeatedly poll the bus upon subsequent calls). + */ + bool mgos_sht31_read(struct mgos_sht31 *sensor); -/* - * The sensor will be polled for its temperature and humidity data. If the poll - * has occured in the last `MGOS_SHT31_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_sht31_getTemperature(struct mgos_sht31 *sensor); + /* + * The sensor will be polled for its temperature and humidity data. If the poll + * has occured in the last `MGOS_SHT31_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_sht31_getTemperature(struct mgos_sht31 *sensor); -/* - * The sensor will be polled for its temperature and humidity data. If the poll - * has occured in the last `MGOS_SHT31_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_sht31_getHumidity(struct mgos_sht31 *sensor); + /* + * The sensor will be polled for its temperature and humidity data. If the poll + * has occured in the last `MGOS_SHT31_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_sht31_getHumidity(struct mgos_sht31 *sensor); -/* - * Initialization function for MGOS -- currently a noop. - */ -bool mgos_sht31_i2c_init(void); + /* + * Initialization function for MGOS -- currently a noop. + */ + bool mgos_sht31_i2c_init(void); -#ifdef __cplusplus + #ifdef __cplusplus } #endif diff --git a/include/mgos_si7021.h b/include/mgos_si7021.h index 4d9525c..bf60608 100644 --- a/include/mgos_si7021.h +++ b/include/mgos_si7021.h @@ -23,58 +23,58 @@ #ifdef __cplusplus extern "C" { -#endif + #endif -struct mgos_si7021; + struct mgos_si7021; -/* - * Initialize a Si7021 on the I2C bus `i2c` at address specified in `i2caddr` - * parameter (default Si7021 is on address 0x40). The sensor will be polled for - * validity, upon success a new `struct mgos_si7021` is allocated and - * returned. If the device could not be found, NULL is returned. - */ -struct mgos_si7021 *mgos_si7021_create(struct mgos_i2c *i2c, uint8_t i2caddr); + /* + * Initialize a Si7021 on the I2C bus `i2c` at address specified in `i2caddr` + * parameter (default Si7021 is on address 0x40). The sensor will be polled for + * validity, upon success a new `struct mgos_si7021` is allocated and + * returned. If the device could not be found, NULL is returned. + */ + struct mgos_si7021 *mgos_si7021_create(struct mgos_i2c *i2c, uint8_t i2caddr); -/* - * Destroy the data structure associated with a Si7021 device. The reference - * to the pointer of the `struct mgos_si7021` has to be provided, and upon - * successful destruction, its associated memory will be freed and the pointer - * set to NULL. - */ -void mgos_si7021_destroy(struct mgos_si7021 **sensor); + /* + * Destroy the data structure associated with a Si7021 device. The reference + * to the pointer of the `struct mgos_si7021` has to be provided, and upon + * successful destruction, its associated memory will be freed and the pointer + * set to NULL. + */ + void mgos_si7021_destroy(struct mgos_si7021 **sensor); -/* - * The sensor will be polled for its temperature and humidity data. If the poll - * has occured in the last `MGOS_SI7021_READ_DELAY` seconds, the cached data is - * used (so as not to repeatedly poll the bus upon subsequent calls). - */ -bool mgos_si7021_read(struct mgos_si7021 *sensor); + /* + * The sensor will be polled for its temperature and humidity data. If the poll + * has occured in the last `MGOS_SI7021_READ_DELAY` seconds, the cached data is + * used (so as not to repeatedly poll the bus upon subsequent calls). + */ + bool mgos_si7021_read(struct mgos_si7021 *sensor); -/* - * The sensor will be polled for its temperature and humidity data. If the poll - * has occured in the last `MGOS_SI7021_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_si7021_getTemperature(struct mgos_si7021 *sensor); + /* + * The sensor will be polled for its temperature and humidity data. If the poll + * has occured in the last `MGOS_SI7021_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_si7021_getTemperature(struct mgos_si7021 *sensor); -/* - * The sensor will be polled for its temperature and humidity data. If the poll - * has occured in the last `MGOS_SI7021_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_si7021_getHumidity(struct mgos_si7021 *sensor); + /* + * The sensor will be polled for its temperature and humidity data. If the poll + * has occured in the last `MGOS_SI7021_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_si7021_getHumidity(struct mgos_si7021 *sensor); -/* - * Initialization function for MGOS -- currently a noop. - */ -bool mgos_si7021_i2c_init(void); + /* + * Initialization function for MGOS -- currently a noop. + */ + bool mgos_si7021_i2c_init(void); -#ifdef __cplusplus + #ifdef __cplusplus } #endif diff --git a/src/main.c b/src/main.c index ab6acd5..6ba1bf8 100644 --- a/src/main.c +++ b/src/main.c @@ -40,6 +40,7 @@ void i2c_scanner(struct mgos_i2c *i2c) { } } + bool i2c_dumpregs(struct mgos_i2c *i2c, uint8_t i2caddr) { uint16_t reg; int value; @@ -48,7 +49,8 @@ bool i2c_dumpregs(struct mgos_i2c *i2c, uint8_t i2caddr) { value=mgos_i2c_read_reg_b(i2c, i2caddr, reg); if (value<0) { printf(" XX"); - } else { + } + else { printf(" %02x", value); } if (reg%16==15) printf("\n"); @@ -57,6 +59,7 @@ bool i2c_dumpregs(struct mgos_i2c *i2c, uint8_t i2caddr) { return true; } + bool do_sht31(struct mgos_sht31 *sensor) { float temp, humid; @@ -69,6 +72,7 @@ bool do_sht31(struct mgos_sht31 *sensor) { return true; } + bool do_si7021(struct mgos_si7021 *sensor) { float temp, humid; @@ -81,6 +85,7 @@ bool do_si7021(struct mgos_si7021 *sensor) { return true; } + bool do_htu21df(struct mgos_htu21df *sensor) { float temp, humid; @@ -93,6 +98,7 @@ bool do_htu21df(struct mgos_htu21df *sensor) { return true; } + bool do_mcp9808(struct mgos_mcp9808 *sensor) { float temp; @@ -104,6 +110,7 @@ bool do_mcp9808(struct mgos_mcp9808 *sensor) { return true; } + int main() { struct mgos_i2c *i2c; struct mgos_si7021 *si7021; @@ -125,13 +132,13 @@ int main() { if (!(sht31 = mgos_sht31_create(i2c, 0x44))) LOG(LL_ERROR, ("Cannot create SHT31 device")); - if (!(si7021 = mgos_si7021_create(i2c, 0x40))) + if (!(si7021 = mgos_si7021_create(i2c, 0x40))) LOG(LL_ERROR, ("Cannot create SI7021 device")); - if (!(htu21df = mgos_htu21df_create(i2c, 0x40))) + if (!(htu21df = mgos_htu21df_create(i2c, 0x40))) LOG(LL_ERROR, ("Cannot create HTU21DF device")); - if (!(mcp9808 = mgos_mcp9808_create(i2c, 0x18))) + if (!(mcp9808 = mgos_mcp9808_create(i2c, 0x18))) LOG(LL_ERROR, ("Cannot create MCP9808 device")); for (;;) { diff --git a/src/mgos_htu21df.c b/src/mgos_htu21df.c index e97fcac..d2849cf 100644 --- a/src/mgos_htu21df.c +++ b/src/mgos_htu21df.c @@ -35,6 +35,7 @@ static bool mgos_htu21df_cmd(struct mgos_htu21df *sensor, uint8_t cmd) { return true; } + static uint8_t crc8(const uint8_t *data, int len) { const uint8_t poly=0x31; uint8_t crc=0x00; @@ -46,6 +47,8 @@ static uint8_t crc8(const uint8_t *data, int len) { } return crc; } + + // Private functions end // Public functions follow @@ -80,6 +83,7 @@ struct mgos_htu21df *mgos_htu21df_create(struct mgos_i2c *i2c, uint8_t i2caddr) return NULL; } + void mgos_htu21df_destroy(struct mgos_htu21df **sensor) { if (!*sensor) return; free (*sensor); @@ -87,6 +91,7 @@ void mgos_htu21df_destroy(struct mgos_htu21df **sensor) { return; } + bool mgos_htu21df_read(struct mgos_htu21df *sensor) { double now = mg_time(); @@ -96,7 +101,7 @@ bool mgos_htu21df_read(struct mgos_htu21df *sensor) { if (now - sensor->last_read_time < MGOS_HTU21DF_READ_DELAY) { return true; } - // Read out sensor data here + // Read out sensor data here // uint8_t data[3]; @@ -109,7 +114,7 @@ bool mgos_htu21df_read(struct mgos_htu21df *sensor) { LOG(LL_ERROR, ("CRC error on temperature data")); return false; } - + uint16_t temp = (data[0]<<8)+data[1]; float temperature = temp; temperature *= 175.72; @@ -126,7 +131,7 @@ bool mgos_htu21df_read(struct mgos_htu21df *sensor) { LOG(LL_ERROR, ("CRC error on temperature data")); return false; } - + uint16_t hum = (data[0]<<8)+data[1]; float humidity = hum; humidity *= 125; @@ -139,19 +144,24 @@ bool mgos_htu21df_read(struct mgos_htu21df *sensor) { 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 index f6bd6b2..f5f08d2 100644 --- a/src/mgos_htu21df_internal.h +++ b/src/mgos_htu21df_internal.h @@ -29,19 +29,19 @@ #define MGOS_HTU21DF_READREG (0xE7) #define MGOS_HTU21DF_RESET (0xFE) - #ifdef __cplusplus extern "C" { -#endif + #endif -struct mgos_htu21df { - struct mgos_i2c *i2c; - uint8_t i2caddr; - double last_read_time; + struct mgos_htu21df + { + struct mgos_i2c *i2c; + uint8_t i2caddr; + double last_read_time; - float humidity, temperature; -}; + float humidity, temperature; + }; -#ifdef __cplusplus + #ifdef __cplusplus } #endif diff --git a/src/mgos_i2c.c b/src/mgos_i2c.c index c22581d..e771005 100644 --- a/src/mgos_i2c.c +++ b/src/mgos_i2c.c @@ -28,9 +28,10 @@ #include #include -struct mgos_i2c { +struct mgos_i2c +{ int fd; - uint16_t read_timeout_ms; // in msec + uint16_t read_timeout_ms; // in msec char *filename; }; @@ -49,12 +50,13 @@ static size_t i2c_read_timeout(struct mgos_i2c *i2c, void *data, size_t len) { ret = read(i2c->fd, data, len); } if (ret!=len) { -// LOG(LL_ERROR, ("Read timeout on I2C")); + // LOG(LL_ERROR, ("Read timeout on I2C")); return -1; } return len; } + bool mgos_i2c_read(struct mgos_i2c *i2c, uint16_t addr, void *data, size_t len, bool stop) { size_t ret; @@ -69,12 +71,13 @@ bool mgos_i2c_read(struct mgos_i2c *i2c, uint16_t addr, void *data, size_t len, } ret = i2c_read_timeout(i2c, data, len); if (ret != len) { -// LOG(LL_DEBUG, ("RECV %ld bytes (wanted %lu) from 0x%02x: %s", ret, len, addr, strerror(errno))); + // LOG(LL_DEBUG, ("RECV %ld bytes (wanted %lu) from 0x%02x: %s", ret, len, addr, strerror(errno))); return false; } return true; } + bool mgos_i2c_write(struct mgos_i2c *i2c, uint16_t addr, const void *data, size_t len, bool stop) { size_t ret; @@ -89,25 +92,29 @@ bool mgos_i2c_write(struct mgos_i2c *i2c, uint16_t addr, const void *data, size_ } ret = write(i2c->fd, data, len); if (ret != len) { -// LOG(LL_DEBUG, ("XMIT %ld bytes (wanted %lu) from 0x%02x: %s", ret, len, addr, strerror(errno))); + // LOG(LL_DEBUG, ("XMIT %ld bytes (wanted %lu) from 0x%02x: %s", ret, len, addr, strerror(errno))); return false; } return true; } + void mgos_i2c_stop(struct mgos_i2c *i2c) { return; } + int mgos_i2c_get_freq(struct mgos_i2c *i2c) { return MGOS_I2C_FREQ_100KHZ; } + bool mgos_i2c_set_freq(struct mgos_i2c *i2c, int freq) { if (freq==MGOS_I2C_FREQ_100KHZ) return true; return false; } + int mgos_i2c_read_reg_b(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg) { uint8_t value; @@ -116,6 +123,7 @@ int mgos_i2c_read_reg_b(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg) { return value; } + int mgos_i2c_read_reg_w(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg) { uint16_t value; uint8_t data[2]; @@ -126,6 +134,7 @@ int mgos_i2c_read_reg_w(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg) { return value; } + bool mgos_i2c_read_reg_n(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, size_t n, uint8_t *buf) { uint8_t outbuf; struct i2c_rdwr_ioctl_data packets; @@ -141,10 +150,10 @@ bool mgos_i2c_read_reg_n(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, size_ return -1; } /* - * In order to read a register, we first do a "dummy write" by writing - * 0 bytes to the register we want to read from. This is similar to - * the packet in set_i2c_register, except it's 1 byte rather than 2. - */ + * In order to read a register, we first do a "dummy write" by writing + * 0 bytes to the register we want to read from. This is similar to + * the packet in set_i2c_register, except it's 1 byte rather than 2. + */ outbuf = reg; messages[0].addr = addr; messages[0].flags = 0; @@ -166,6 +175,7 @@ bool mgos_i2c_read_reg_n(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, size_ return true; } + bool mgos_i2c_write_reg_w(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, uint16_t value) { uint8_t data[2]; data[0]=value>>8; @@ -174,10 +184,12 @@ bool mgos_i2c_write_reg_w(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, uint return mgos_i2c_write_reg_n(i2c, addr, reg, 2, data); } + bool mgos_i2c_write_reg_b(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, uint8_t value) { return mgos_i2c_write_reg_n(i2c, addr, reg, 1, &value); } + bool mgos_i2c_write_reg_n(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, size_t n, const uint8_t *buf) { unsigned char outbuf[n+1]; struct i2c_rdwr_ioctl_data packets; @@ -209,14 +221,17 @@ bool mgos_i2c_write_reg_n(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, size return true; } + void mgos_i2c_close(struct mgos_i2c *i2c) { return; } + struct mgos_i2c *mgos_i2c_get_global(void) { return s_global_i2c_bus; } + // User provided function to interface with Linux I2C driver bool mgos_i2c_open(int busnr) { int fd; diff --git a/src/mgos_mcp9808.c b/src/mgos_mcp9808.c index 95af064..3f1963a 100644 --- a/src/mgos_mcp9808.c +++ b/src/mgos_mcp9808.c @@ -31,10 +31,13 @@ static bool is_mcp9808(struct mgos_i2c *i2c, uint8_t i2caddr) { return ((manufacturer_id == 0x0054) && (device_id == 0x0400)); } + static void mgos_mcp9808_reset(struct mgos_mcp9808 *sensor) { if (!sensor) return; mgos_i2c_write_reg_w(sensor->i2c, sensor->i2caddr, MGOS_MCP9808_REG_CONFIG, 0x0); } + + // Private functions end // Public functions follow @@ -60,6 +63,7 @@ struct mgos_mcp9808 *mgos_mcp9808_create(struct mgos_i2c *i2c, uint8_t i2caddr) return sensor; } + void mgos_mcp9808_destroy(struct mgos_mcp9808 **sensor) { if (!*sensor) return; free (*sensor); @@ -67,6 +71,7 @@ void mgos_mcp9808_destroy(struct mgos_mcp9808 **sensor) { return; } + bool mgos_mcp9808_read(struct mgos_mcp9808 *sensor) { double now = mg_time(); @@ -76,7 +81,7 @@ bool mgos_mcp9808_read(struct mgos_mcp9808 *sensor) { if (now - sensor->last_read_time < MGOS_MCP9808_READ_DELAY) { return true; } - // Read out sensor data here + // Read out sensor data here // uint16_t temp_reg = mgos_i2c_read_reg_w(sensor->i2c, sensor->i2caddr, MGOS_MCP9808_REG_AMBIENT_TEMP); @@ -90,12 +95,14 @@ bool mgos_mcp9808_read(struct mgos_mcp9808 *sensor) { return true; } + float mgos_mcp9808_getTemperature(struct mgos_mcp9808 *sensor) { if (!mgos_mcp9808_read(sensor)) return NAN; return sensor->temperature; } + void mgos_mcp9808_enable(struct mgos_mcp9808 *sensor, bool enable) { uint16_t conf_reg = mgos_i2c_read_reg_w(sensor->i2c, sensor->i2caddr, MGOS_MCP9808_REG_CONFIG); if (enable) @@ -105,7 +112,10 @@ void mgos_mcp9808_enable(struct mgos_mcp9808 *sensor, bool enable) { mgos_i2c_write_reg_w(sensor->i2c, sensor->i2caddr, MGOS_MCP9808_REG_CONFIG, conf_reg); } + bool mgos_mcp9808_i2c_init(void) { return true; } + + // Public functions end diff --git a/src/mgos_mcp9808_internal.h b/src/mgos_mcp9808_internal.h index 4f5665c..4a7b67d 100644 --- a/src/mgos_mcp9808_internal.h +++ b/src/mgos_mcp9808_internal.h @@ -43,16 +43,17 @@ #ifdef __cplusplus extern "C" { -#endif + #endif -struct mgos_mcp9808 { - struct mgos_i2c *i2c; - uint8_t i2caddr; - double last_read_time; + struct mgos_mcp9808 + { + struct mgos_i2c *i2c; + uint8_t i2caddr; + double last_read_time; - float temperature; -}; + float temperature; + }; -#ifdef __cplusplus + #ifdef __cplusplus } #endif diff --git a/src/mgos_mock.c b/src/mgos_mock.c index 958dc14..2cbd5c8 100644 --- a/src/mgos_mock.c +++ b/src/mgos_mock.c @@ -41,13 +41,14 @@ int log_print_prefix(enum cs_log_level l, const char *func, const char *file) { case LL_VERBOSE_DEBUG: strncpy(ll_str, "VERB", sizeof(ll_str)); break; - default: // LL_NONE + default: // LL_NONE return 0; } printf ("%-5s %-20s %-40s| ", ll_str, file, func); return 1; } + double mg_time() { struct timespec ts; double ret; @@ -60,6 +61,7 @@ double mg_time() { return ret; } + void mgos_usleep(uint32_t usecs) { usleep(usecs); } diff --git a/src/mgos_sht31.c b/src/mgos_sht31.c index 9ac4757..abdb39e 100644 --- a/src/mgos_sht31.c +++ b/src/mgos_sht31.c @@ -39,8 +39,8 @@ static bool mgos_sht31_cmd(struct mgos_sht31 *sensor, uint16_t 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 poly=0x31; uint8_t crc=0xFF; @@ -52,6 +52,7 @@ static uint8_t crc8(const uint8_t *data, int len) return crc; } + static uint16_t mgos_sht31_status(struct mgos_sht31 *sensor) { uint8_t data[3]; uint16_t value; @@ -67,6 +68,8 @@ static uint16_t mgos_sht31_status(struct mgos_sht31 *sensor) { return value; } + + // Private functions end // Public functions follow @@ -85,11 +88,14 @@ struct mgos_sht31 *mgos_sht31_create(struct mgos_i2c *i2c, uint8_t i2caddr) { mgos_sht31_cmd(sensor, MGOS_SHT31_SOFTRESET); // Toggle heater on and off, which shows up in status register bit 13 (0=Off, 1=On) - status0=mgos_sht31_status(sensor); // heater is off, bit13 is 0 + // heater is off, bit13 is 0 + status0=mgos_sht31_status(sensor); mgos_sht31_cmd(sensor, MGOS_SHT31_HEATEREN); - status1=mgos_sht31_status(sensor); // heater is on, bit13 is 1 + // heater is on, bit13 is 1 + status1=mgos_sht31_status(sensor); mgos_sht31_cmd(sensor, MGOS_SHT31_HEATERDIS); - status2=mgos_sht31_status(sensor); // heater is off, bit13 is 0 + // heater is off, bit13 is 0 + status2=mgos_sht31_status(sensor); if (((status0 & 0x2000) == 0) && ((status1 & 0x2000) != 0) && ((status2 & 0x2000) == 0)) { LOG(LL_INFO, ("SHT31 created at I2C 0x%02x", i2caddr)); @@ -101,6 +107,7 @@ struct mgos_sht31 *mgos_sht31_create(struct mgos_i2c *i2c, uint8_t i2caddr) { return NULL; } + void mgos_sht31_destroy(struct mgos_sht31 **sensor) { if (!*sensor) return; free (*sensor); @@ -108,6 +115,7 @@ void mgos_sht31_destroy(struct mgos_sht31 **sensor) { return; } + bool mgos_sht31_read(struct mgos_sht31 *sensor) { double now = mg_time(); @@ -117,7 +125,7 @@ bool mgos_sht31_read(struct mgos_sht31 *sensor) { if (now - sensor->last_read_time < MGOS_SHT31_READ_DELAY) { return true; } - // Read out sensor data here + // Read out sensor data here // uint8_t data[6]; float humidity, temperature; @@ -149,19 +157,24 @@ bool mgos_sht31_read(struct mgos_sht31 *sensor) { return true; } + float mgos_sht31_getTemperature(struct mgos_sht31 *sensor) { if (!mgos_sht31_read(sensor)) return NAN; return sensor->temperature; } + float mgos_sht31_getHumidity(struct mgos_sht31 *sensor) { if (!mgos_sht31_read(sensor)) return NAN; return sensor->humidity; } + bool mgos_sht31_i2c_init(void) { return true; } + + // Public functions end diff --git a/src/mgos_sht31_internal.h b/src/mgos_sht31_internal.h index 87585dd..46f7570 100644 --- a/src/mgos_sht31_internal.h +++ b/src/mgos_sht31_internal.h @@ -37,16 +37,17 @@ #ifdef __cplusplus extern "C" { -#endif + #endif -struct mgos_sht31 { - struct mgos_i2c *i2c; - uint8_t i2caddr; - double last_read_time; + struct mgos_sht31 + { + struct mgos_i2c *i2c; + uint8_t i2caddr; + double last_read_time; - float humidity, temperature; -}; + float humidity, temperature; + }; -#ifdef __cplusplus + #ifdef __cplusplus } #endif diff --git a/src/mgos_si7021.c b/src/mgos_si7021.c index 144da7d..78d07f3 100644 --- a/src/mgos_si7021.c +++ b/src/mgos_si7021.c @@ -35,8 +35,8 @@ static bool mgos_si7021_cmd(struct mgos_si7021 *sensor, uint8_t 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 poly=0x31; uint8_t crc=0x00; @@ -48,6 +48,7 @@ static uint8_t crc8(const uint8_t *data, int len) return crc; } + // Private functions end // Public functions follow @@ -74,6 +75,7 @@ struct mgos_si7021 *mgos_si7021_create(struct mgos_i2c *i2c, uint8_t i2caddr) { return sensor; } + void mgos_si7021_destroy(struct mgos_si7021 **sensor) { if (!*sensor) return; free (*sensor); @@ -81,6 +83,7 @@ void mgos_si7021_destroy(struct mgos_si7021 **sensor) { return; } + bool mgos_si7021_read(struct mgos_si7021 *sensor) { double now = mg_time(); @@ -90,7 +93,7 @@ bool mgos_si7021_read(struct mgos_si7021 *sensor) { if (now - sensor->last_read_time < MGOS_SI7021_READ_DELAY) { return true; } - // Read out sensor data here + // Read out sensor data here // uint8_t data[3]; @@ -139,19 +142,24 @@ bool mgos_si7021_read(struct mgos_si7021 *sensor) { 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 index 11c5a07..3237928 100644 --- a/src/mgos_si7021_internal.h +++ b/src/mgos_si7021_internal.h @@ -39,16 +39,17 @@ #ifdef __cplusplus extern "C" { -#endif + #endif -struct mgos_si7021 { - struct mgos_i2c *i2c; - uint8_t i2caddr; - double last_read_time; + struct mgos_si7021 + { + struct mgos_i2c *i2c; + uint8_t i2caddr; + double last_read_time; - float humidity, temperature; -}; + float humidity, temperature; + }; -#ifdef __cplusplus + #ifdef __cplusplus } #endif