Reformat with bcpp

Used commandline: bcpp -i 2 -yq -ya -s -bcl -tbcl $i
This commit is contained in:
Pim van Pelt
2018-04-09 18:30:46 +02:00
parent 0337487a0c
commit d2e64c0f45
16 changed files with 310 additions and 243 deletions

View File

@ -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

View File

@ -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

View File

@ -19,13 +19,13 @@
#ifndef __MGOS_MOCK_H
#define __MGOS_MOCK_H
#include "mgos.h"
#include <time.h>
#include <sys/time.h>
// 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

View File

@ -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

View File

@ -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

View File

@ -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;

View File

@ -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();
@ -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

View File

@ -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

View File

@ -28,9 +28,10 @@
#include <unistd.h>
#include <errno.h>
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;

View File

@ -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();
@ -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

View File

@ -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

View File

@ -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);
}

View File

@ -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();
@ -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

View File

@ -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

View File

@ -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();
@ -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

View File

@ -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