Initial (empty) Si7021 driver

This commit is contained in:
Pim van Pelt
2018-04-02 17:59:41 +02:00
parent 27bba32bd9
commit 2899ee59de
5 changed files with 187 additions and 9 deletions

View File

@ -1,6 +1,7 @@
#include "mgos.h"
#include "mgos_i2c.h"
#include "mgos_sht31.h"
#include "mgos_si7021.h"
#include <fcntl.h>
#include <sys/ioctl.h>
@ -37,25 +38,37 @@ bool i2c_dumpregs(struct mgos_i2c *i2c, uint8_t i2caddr) {
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);
}
temp=mgos_sht31_getTemperature(sht31);
humid=mgos_sht31_getHumidity(sht31);
LOG(LL_INFO, ("SHT31: temperature=%.2fC humidity=%.1f%%", temp, humid));
mgos_sht31_destroy(&sht31);
return true;
}
bool si7021(struct mgos_i2c *i2c, uint8_t i2caddr) {
struct mgos_si7021 *si7021;
float temp, humid;
if (!(si7021 = mgos_si7021_create(i2c, i2caddr))) {
LOG(LL_ERROR, ("Cannot create SI7021 device"));
return false;
}
temp=mgos_si7021_getTemperature(si7021);
humid=mgos_si7021_getHumidity(si7021);
LOG(LL_INFO, ("SI7021: temperature=%.2fC humidity=%.1f%%", temp, humid));
mgos_si7021_destroy(&si7021);
return true;
}
int main() {
struct mgos_i2c *i2c;
@ -69,7 +82,11 @@ int main() {
}
i2c_scanner(i2c);
sht31(i2c, 0x44);
for (;;) {
sht31(i2c, 0x44);
si7021(i2c, 0x44);
sleep(1);
}
return 0;
}