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

@ -19,13 +19,13 @@
#ifndef __MGOS_MOCK_H #ifndef __MGOS_MOCK_H
#define __MGOS_MOCK_H #define __MGOS_MOCK_H
#include "mgos.h" #include "mgos.h"
#include <time.h> #include <time.h>
#include <sys/time.h> #include <sys/time.h>
// mgos_log // mgos_log
enum cs_log_level { enum cs_log_level
{
LL_NONE = -1, LL_NONE = -1,
LL_ERROR = 0, LL_ERROR = 0,
LL_WARN = 1, LL_WARN = 1,
@ -40,14 +40,13 @@ enum cs_log_level {
int log_print_prefix(enum cs_log_level l, const char *func, const char *file); int log_print_prefix(enum cs_log_level l, const char *func, const char *file);
#define LOG(l, x) \ #define LOG(l, x) \
do { \ do \
{ \
if (log_print_prefix(l, __func__, __FILE__)) printf x; \ if (log_print_prefix(l, __func__, __FILE__)) printf x; \
printf("\r\n"); \ printf("\r\n"); \
} while (0) } while (0)
double mg_time(); double mg_time();
void mgos_usleep(uint32_t usecs); void mgos_usleep(uint32_t usecs);
#endif // __MGOS_MOCK_H #endif // __MGOS_MOCK_H

View File

@ -40,6 +40,7 @@ void i2c_scanner(struct mgos_i2c *i2c) {
} }
} }
bool i2c_dumpregs(struct mgos_i2c *i2c, uint8_t i2caddr) { bool i2c_dumpregs(struct mgos_i2c *i2c, uint8_t i2caddr) {
uint16_t reg; uint16_t reg;
int value; 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); value=mgos_i2c_read_reg_b(i2c, i2caddr, reg);
if (value<0) { if (value<0) {
printf(" XX"); printf(" XX");
} else { }
else {
printf(" %02x", value); printf(" %02x", value);
} }
if (reg%16==15) printf("\n"); if (reg%16==15) printf("\n");
@ -57,6 +59,7 @@ bool i2c_dumpregs(struct mgos_i2c *i2c, uint8_t i2caddr) {
return true; return true;
} }
bool do_sht31(struct mgos_sht31 *sensor) { bool do_sht31(struct mgos_sht31 *sensor) {
float temp, humid; float temp, humid;
@ -69,6 +72,7 @@ bool do_sht31(struct mgos_sht31 *sensor) {
return true; return true;
} }
bool do_si7021(struct mgos_si7021 *sensor) { bool do_si7021(struct mgos_si7021 *sensor) {
float temp, humid; float temp, humid;
@ -81,6 +85,7 @@ bool do_si7021(struct mgos_si7021 *sensor) {
return true; return true;
} }
bool do_htu21df(struct mgos_htu21df *sensor) { bool do_htu21df(struct mgos_htu21df *sensor) {
float temp, humid; float temp, humid;
@ -93,6 +98,7 @@ bool do_htu21df(struct mgos_htu21df *sensor) {
return true; return true;
} }
bool do_mcp9808(struct mgos_mcp9808 *sensor) { bool do_mcp9808(struct mgos_mcp9808 *sensor) {
float temp; float temp;
@ -104,6 +110,7 @@ bool do_mcp9808(struct mgos_mcp9808 *sensor) {
return true; return true;
} }
int main() { int main() {
struct mgos_i2c *i2c; struct mgos_i2c *i2c;
struct mgos_si7021 *si7021; struct mgos_si7021 *si7021;

View File

@ -35,6 +35,7 @@ static bool mgos_htu21df_cmd(struct mgos_htu21df *sensor, uint8_t cmd) {
return true; 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; const uint8_t poly=0x31;
uint8_t crc=0x00; uint8_t crc=0x00;
@ -46,6 +47,8 @@ static uint8_t crc8(const uint8_t *data, int len) {
} }
return crc; return crc;
} }
// Private functions end // Private functions end
// Public functions follow // Public functions follow
@ -80,6 +83,7 @@ struct mgos_htu21df *mgos_htu21df_create(struct mgos_i2c *i2c, uint8_t i2caddr)
return NULL; return NULL;
} }
void mgos_htu21df_destroy(struct mgos_htu21df **sensor) { void mgos_htu21df_destroy(struct mgos_htu21df **sensor) {
if (!*sensor) return; if (!*sensor) return;
free (*sensor); free (*sensor);
@ -87,6 +91,7 @@ void mgos_htu21df_destroy(struct mgos_htu21df **sensor) {
return; return;
} }
bool mgos_htu21df_read(struct mgos_htu21df *sensor) { bool mgos_htu21df_read(struct mgos_htu21df *sensor) {
double now = mg_time(); double now = mg_time();
@ -139,19 +144,24 @@ bool mgos_htu21df_read(struct mgos_htu21df *sensor) {
return true; return true;
} }
float mgos_htu21df_getTemperature(struct mgos_htu21df *sensor) { float mgos_htu21df_getTemperature(struct mgos_htu21df *sensor) {
if (!mgos_htu21df_read(sensor)) return NAN; if (!mgos_htu21df_read(sensor)) return NAN;
return sensor->temperature; return sensor->temperature;
} }
float mgos_htu21df_getHumidity(struct mgos_htu21df *sensor) { float mgos_htu21df_getHumidity(struct mgos_htu21df *sensor) {
if (!mgos_htu21df_read(sensor)) return NAN; if (!mgos_htu21df_read(sensor)) return NAN;
return sensor->humidity; return sensor->humidity;
} }
bool mgos_htu21df_i2c_init(void) { bool mgos_htu21df_i2c_init(void) {
return true; return true;
} }
// Public functions end // Public functions end

View File

@ -29,12 +29,12 @@
#define MGOS_HTU21DF_READREG (0xE7) #define MGOS_HTU21DF_READREG (0xE7)
#define MGOS_HTU21DF_RESET (0xFE) #define MGOS_HTU21DF_RESET (0xFE)
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
struct mgos_htu21df { struct mgos_htu21df
{
struct mgos_i2c *i2c; struct mgos_i2c *i2c;
uint8_t i2caddr; uint8_t i2caddr;
double last_read_time; double last_read_time;

View File

@ -28,7 +28,8 @@
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
struct mgos_i2c { struct mgos_i2c
{
int fd; int fd;
uint16_t read_timeout_ms; // in msec uint16_t read_timeout_ms; // in msec
char *filename; char *filename;
@ -55,6 +56,7 @@ static size_t i2c_read_timeout(struct mgos_i2c *i2c, void *data, size_t len) {
return len; return len;
} }
bool mgos_i2c_read(struct mgos_i2c *i2c, uint16_t addr, void *data, size_t len, bool stop) { bool mgos_i2c_read(struct mgos_i2c *i2c, uint16_t addr, void *data, size_t len, bool stop) {
size_t ret; size_t ret;
@ -75,6 +77,7 @@ bool mgos_i2c_read(struct mgos_i2c *i2c, uint16_t addr, void *data, size_t len,
return true; return true;
} }
bool mgos_i2c_write(struct mgos_i2c *i2c, uint16_t addr, const void *data, size_t len, bool stop) { bool mgos_i2c_write(struct mgos_i2c *i2c, uint16_t addr, const void *data, size_t len, bool stop) {
size_t ret; size_t ret;
@ -95,19 +98,23 @@ bool mgos_i2c_write(struct mgos_i2c *i2c, uint16_t addr, const void *data, size_
return true; return true;
} }
void mgos_i2c_stop(struct mgos_i2c *i2c) { void mgos_i2c_stop(struct mgos_i2c *i2c) {
return; return;
} }
int mgos_i2c_get_freq(struct mgos_i2c *i2c) { int mgos_i2c_get_freq(struct mgos_i2c *i2c) {
return MGOS_I2C_FREQ_100KHZ; return MGOS_I2C_FREQ_100KHZ;
} }
bool mgos_i2c_set_freq(struct mgos_i2c *i2c, int freq) { bool mgos_i2c_set_freq(struct mgos_i2c *i2c, int freq) {
if (freq==MGOS_I2C_FREQ_100KHZ) return true; if (freq==MGOS_I2C_FREQ_100KHZ) return true;
return false; return false;
} }
int mgos_i2c_read_reg_b(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg) { int mgos_i2c_read_reg_b(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg) {
uint8_t value; 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; return value;
} }
int mgos_i2c_read_reg_w(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg) { int mgos_i2c_read_reg_w(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg) {
uint16_t value; uint16_t value;
uint8_t data[2]; 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; return value;
} }
bool mgos_i2c_read_reg_n(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, size_t n, uint8_t *buf) { 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; uint8_t outbuf;
struct i2c_rdwr_ioctl_data packets; struct i2c_rdwr_ioctl_data packets;
@ -166,6 +175,7 @@ bool mgos_i2c_read_reg_n(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, size_
return true; return true;
} }
bool mgos_i2c_write_reg_w(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, uint16_t value) { bool mgos_i2c_write_reg_w(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, uint16_t value) {
uint8_t data[2]; uint8_t data[2];
data[0]=value>>8; 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); 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) { 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); 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) { 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]; unsigned char outbuf[n+1];
struct i2c_rdwr_ioctl_data packets; 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; return true;
} }
void mgos_i2c_close(struct mgos_i2c *i2c) { void mgos_i2c_close(struct mgos_i2c *i2c) {
return; return;
} }
struct mgos_i2c *mgos_i2c_get_global(void) { struct mgos_i2c *mgos_i2c_get_global(void) {
return s_global_i2c_bus; return s_global_i2c_bus;
} }
// User provided function to interface with Linux I2C driver // User provided function to interface with Linux I2C driver
bool mgos_i2c_open(int busnr) { bool mgos_i2c_open(int busnr) {
int fd; 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)); return ((manufacturer_id == 0x0054) && (device_id == 0x0400));
} }
static void mgos_mcp9808_reset(struct mgos_mcp9808 *sensor) { static void mgos_mcp9808_reset(struct mgos_mcp9808 *sensor) {
if (!sensor) return; if (!sensor) return;
mgos_i2c_write_reg_w(sensor->i2c, sensor->i2caddr, MGOS_MCP9808_REG_CONFIG, 0x0); mgos_i2c_write_reg_w(sensor->i2c, sensor->i2caddr, MGOS_MCP9808_REG_CONFIG, 0x0);
} }
// Private functions end // Private functions end
// Public functions follow // Public functions follow
@ -60,6 +63,7 @@ struct mgos_mcp9808 *mgos_mcp9808_create(struct mgos_i2c *i2c, uint8_t i2caddr)
return sensor; return sensor;
} }
void mgos_mcp9808_destroy(struct mgos_mcp9808 **sensor) { void mgos_mcp9808_destroy(struct mgos_mcp9808 **sensor) {
if (!*sensor) return; if (!*sensor) return;
free (*sensor); free (*sensor);
@ -67,6 +71,7 @@ void mgos_mcp9808_destroy(struct mgos_mcp9808 **sensor) {
return; return;
} }
bool mgos_mcp9808_read(struct mgos_mcp9808 *sensor) { bool mgos_mcp9808_read(struct mgos_mcp9808 *sensor) {
double now = mg_time(); double now = mg_time();
@ -90,12 +95,14 @@ bool mgos_mcp9808_read(struct mgos_mcp9808 *sensor) {
return true; return true;
} }
float mgos_mcp9808_getTemperature(struct mgos_mcp9808 *sensor) { float mgos_mcp9808_getTemperature(struct mgos_mcp9808 *sensor) {
if (!mgos_mcp9808_read(sensor)) return NAN; if (!mgos_mcp9808_read(sensor)) return NAN;
return sensor->temperature; return sensor->temperature;
} }
void mgos_mcp9808_enable(struct mgos_mcp9808 *sensor, bool enable) { 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); uint16_t conf_reg = mgos_i2c_read_reg_w(sensor->i2c, sensor->i2caddr, MGOS_MCP9808_REG_CONFIG);
if (enable) 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); mgos_i2c_write_reg_w(sensor->i2c, sensor->i2caddr, MGOS_MCP9808_REG_CONFIG, conf_reg);
} }
bool mgos_mcp9808_i2c_init(void) { bool mgos_mcp9808_i2c_init(void) {
return true; return true;
} }
// Public functions end // Public functions end

View File

@ -45,7 +45,8 @@
extern "C" { extern "C" {
#endif #endif
struct mgos_mcp9808 { struct mgos_mcp9808
{
struct mgos_i2c *i2c; struct mgos_i2c *i2c;
uint8_t i2caddr; uint8_t i2caddr;
double last_read_time; double last_read_time;

View File

@ -48,6 +48,7 @@ int log_print_prefix(enum cs_log_level l, const char *func, const char *file) {
return 1; return 1;
} }
double mg_time() { double mg_time() {
struct timespec ts; struct timespec ts;
double ret; double ret;
@ -60,6 +61,7 @@ double mg_time() {
return ret; return ret;
} }
void mgos_usleep(uint32_t usecs) { void mgos_usleep(uint32_t usecs) {
usleep(usecs); usleep(usecs);
} }

View File

@ -39,8 +39,8 @@ static bool mgos_sht31_cmd(struct mgos_sht31 *sensor, uint16_t cmd) {
return true; 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; const uint8_t poly=0x31;
uint8_t crc=0xFF; uint8_t crc=0xFF;
@ -52,6 +52,7 @@ static uint8_t crc8(const uint8_t *data, int len)
return crc; return crc;
} }
static uint16_t mgos_sht31_status(struct mgos_sht31 *sensor) { static uint16_t mgos_sht31_status(struct mgos_sht31 *sensor) {
uint8_t data[3]; uint8_t data[3];
uint16_t value; uint16_t value;
@ -67,6 +68,8 @@ static uint16_t mgos_sht31_status(struct mgos_sht31 *sensor) {
return value; return value;
} }
// Private functions end // Private functions end
// Public functions follow // 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); mgos_sht31_cmd(sensor, MGOS_SHT31_SOFTRESET);
// Toggle heater on and off, which shows up in status register bit 13 (0=Off, 1=On) // 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); 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); 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)) { if (((status0 & 0x2000) == 0) && ((status1 & 0x2000) != 0) && ((status2 & 0x2000) == 0)) {
LOG(LL_INFO, ("SHT31 created at I2C 0x%02x", i2caddr)); 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; return NULL;
} }
void mgos_sht31_destroy(struct mgos_sht31 **sensor) { void mgos_sht31_destroy(struct mgos_sht31 **sensor) {
if (!*sensor) return; if (!*sensor) return;
free (*sensor); free (*sensor);
@ -108,6 +115,7 @@ void mgos_sht31_destroy(struct mgos_sht31 **sensor) {
return; return;
} }
bool mgos_sht31_read(struct mgos_sht31 *sensor) { bool mgos_sht31_read(struct mgos_sht31 *sensor) {
double now = mg_time(); double now = mg_time();
@ -149,19 +157,24 @@ bool mgos_sht31_read(struct mgos_sht31 *sensor) {
return true; return true;
} }
float mgos_sht31_getTemperature(struct mgos_sht31 *sensor) { float mgos_sht31_getTemperature(struct mgos_sht31 *sensor) {
if (!mgos_sht31_read(sensor)) return NAN; if (!mgos_sht31_read(sensor)) return NAN;
return sensor->temperature; return sensor->temperature;
} }
float mgos_sht31_getHumidity(struct mgos_sht31 *sensor) { float mgos_sht31_getHumidity(struct mgos_sht31 *sensor) {
if (!mgos_sht31_read(sensor)) return NAN; if (!mgos_sht31_read(sensor)) return NAN;
return sensor->humidity; return sensor->humidity;
} }
bool mgos_sht31_i2c_init(void) { bool mgos_sht31_i2c_init(void) {
return true; return true;
} }
// Public functions end // Public functions end

View File

@ -39,7 +39,8 @@
extern "C" { extern "C" {
#endif #endif
struct mgos_sht31 { struct mgos_sht31
{
struct mgos_i2c *i2c; struct mgos_i2c *i2c;
uint8_t i2caddr; uint8_t i2caddr;
double last_read_time; double last_read_time;

View File

@ -35,8 +35,8 @@ static bool mgos_si7021_cmd(struct mgos_si7021 *sensor, uint8_t cmd) {
return true; 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; const uint8_t poly=0x31;
uint8_t crc=0x00; uint8_t crc=0x00;
@ -48,6 +48,7 @@ static uint8_t crc8(const uint8_t *data, int len)
return crc; return crc;
} }
// Private functions end // Private functions end
// Public functions follow // Public functions follow
@ -74,6 +75,7 @@ struct mgos_si7021 *mgos_si7021_create(struct mgos_i2c *i2c, uint8_t i2caddr) {
return sensor; return sensor;
} }
void mgos_si7021_destroy(struct mgos_si7021 **sensor) { void mgos_si7021_destroy(struct mgos_si7021 **sensor) {
if (!*sensor) return; if (!*sensor) return;
free (*sensor); free (*sensor);
@ -81,6 +83,7 @@ void mgos_si7021_destroy(struct mgos_si7021 **sensor) {
return; return;
} }
bool mgos_si7021_read(struct mgos_si7021 *sensor) { bool mgos_si7021_read(struct mgos_si7021 *sensor) {
double now = mg_time(); double now = mg_time();
@ -139,19 +142,24 @@ bool mgos_si7021_read(struct mgos_si7021 *sensor) {
return true; return true;
} }
float mgos_si7021_getTemperature(struct mgos_si7021 *sensor) { float mgos_si7021_getTemperature(struct mgos_si7021 *sensor) {
if (!mgos_si7021_read(sensor)) return NAN; if (!mgos_si7021_read(sensor)) return NAN;
return sensor->temperature; return sensor->temperature;
} }
float mgos_si7021_getHumidity(struct mgos_si7021 *sensor) { float mgos_si7021_getHumidity(struct mgos_si7021 *sensor) {
if (!mgos_si7021_read(sensor)) return NAN; if (!mgos_si7021_read(sensor)) return NAN;
return sensor->humidity; return sensor->humidity;
} }
bool mgos_si7021_i2c_init(void) { bool mgos_si7021_i2c_init(void) {
return true; return true;
} }
// Public functions end // Public functions end

View File

@ -41,7 +41,8 @@
extern "C" { extern "C" {
#endif #endif
struct mgos_si7021 { struct mgos_si7021
{
struct mgos_i2c *i2c; struct mgos_i2c *i2c;
uint8_t i2caddr; uint8_t i2caddr;
double last_read_time; double last_read_time;