Include first driver: SHT31
Make I2C reads blocking w/ timeout Add SHT31 driver, roughly :) Fix mg_time() to return a double.
This commit is contained in:
59
src/main.c
59
src/main.c
@ -1,8 +1,61 @@
|
||||
#include "mgos.h"
|
||||
#include "mgos_i2c.h"
|
||||
#include "mgos_sht31.h"
|
||||
#include <fcntl.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#define I2CBUSNR 7
|
||||
|
||||
void i2c_scanner(struct mgos_i2c *i2c) {
|
||||
int i;
|
||||
if (!i2c) {
|
||||
LOG(LL_ERROR, ("No global I2C bus configured"));
|
||||
return;
|
||||
}
|
||||
|
||||
for(i=0x3; i<0x77; i++) {
|
||||
bool ret;
|
||||
ret=mgos_i2c_read(i2c, i, NULL, 0, true);
|
||||
if (ret)
|
||||
LOG(LL_INFO, ("I2C Address 0x%02x %s", i, ret?"true":"false"));
|
||||
}
|
||||
}
|
||||
|
||||
bool i2c_dumpregs(struct mgos_i2c *i2c, uint8_t i2caddr) {
|
||||
uint8_t reg, value;
|
||||
|
||||
for(reg=0; reg<255; reg++) {
|
||||
if (0 != get_i2c_register(i2c, i2caddr, reg, &value)) {
|
||||
LOG(LL_ERROR, ("Could not read register"));
|
||||
} else {
|
||||
LOG(LL_INFO, ("reg=%u value=%u", reg, value));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
mgos_sht31_destroy(&sht31);
|
||||
return true;
|
||||
}
|
||||
|
||||
int main() {
|
||||
struct mgos_i2c *i2c;
|
||||
|
||||
@ -10,11 +63,13 @@ int main() {
|
||||
LOG(LL_ERROR, ("Cannot open I2C bus %u", I2CBUSNR));
|
||||
return -1;
|
||||
}
|
||||
i2c = mgos_i2c_get_global();
|
||||
if (!i2c) {
|
||||
if (!(i2c = mgos_i2c_get_global())) {
|
||||
LOG(LL_ERROR, ("Cannot open I2C bus"));
|
||||
return -2;
|
||||
}
|
||||
|
||||
i2c_scanner(i2c);
|
||||
sht31(i2c, 0x44);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user