Added BME280 DHT sensor driver.
This commit is contained in:
parent
7595f35cc6
commit
f18edbb049
|
@ -15,8 +15,10 @@ set(C_SOURCES
|
|||
"src/impl/user_st7789_impl.c"
|
||||
"src/impl/user_lvgl_impl.c"
|
||||
"src/impl/user_stick_impl.c"
|
||||
"src/impl/user_bme280_impl.c"
|
||||
"src/tasks/user_lvgl_task.c"
|
||||
"src/tasks/user_clock_task.c"
|
||||
"src/tasks/user_dht_task.c"
|
||||
"src/tasks/user_base_task.c"
|
||||
"src/utils/user_log_util.c"
|
||||
"src/assets/encode_sans_sc_light_24.c"
|
||||
|
@ -28,6 +30,7 @@ set(C_SOURCES
|
|||
set(C_INCLUDES
|
||||
"lib/LittleVGL"
|
||||
"lib/st7789_lcd"
|
||||
"lib/bme280_dht"
|
||||
"include"
|
||||
)
|
||||
|
||||
|
@ -46,6 +49,7 @@ set(C_LIBRARIES
|
|||
"config"
|
||||
"lvgl"
|
||||
"st7789"
|
||||
"bme280"
|
||||
)
|
||||
|
||||
add_subdirectory(lib)
|
||||
|
|
|
@ -6,7 +6,7 @@ which runs on Linux.
|
|||
|
||||
## Supported sensors
|
||||
|
||||
- [ ] Bosch SensorTec BME280 digital temperature/humidity/pressure sensor
|
||||
- [x] Bosch SensorTec BME280 digital temperature/humidity/pressure sensor
|
||||
- [ ] AMS(Sciosense) CCS811 eTVOC sensor
|
||||
- [ ] MH-Z19 series NDIR eCO2 sensor
|
||||
- [ ] TDK InvenSense 6-Axis gyro/accelerometer
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
#ifndef USER_BME280_IMPL_H
|
||||
#define USER_BME280_IMPL_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "bme280_dht.h"
|
||||
|
||||
typedef struct {
|
||||
int i2cdev_fd;
|
||||
uint8_t i2c_addr;
|
||||
} user_bme280_impl_t;
|
||||
|
||||
int user_bme280_impl_init(user_bme280_impl_t *impl);
|
||||
|
||||
int user_bme280_impl_deinit(user_bme280_impl_t *impl);
|
||||
|
||||
bme280_ret_t user_bme280_impl_read_register_cb(user_bme280_impl_t *impl,
|
||||
uint8_t reg, uint8_t *data,
|
||||
uint8_t len);
|
||||
|
||||
bme280_ret_t user_bme280_impl_write_register_cb(user_bme280_impl_t *impl,
|
||||
uint8_t reg, uint8_t data);
|
||||
|
||||
#endif
|
|
@ -7,6 +7,9 @@ int user_lvgl_task_deinit(void);
|
|||
int user_clock_task_init(void);
|
||||
int user_clock_task_deinit(void);
|
||||
|
||||
int user_dht_task_init(void);
|
||||
int user_dht_task_deinit(void);
|
||||
|
||||
int user_base_task_init(void);
|
||||
int user_base_task_deinit(void);
|
||||
|
||||
|
|
|
@ -17,4 +17,11 @@ set(ST7789_LCD_SOURCES
|
|||
)
|
||||
|
||||
add_library(st7789 STATIC ${ST7789_LCD_SOURCES})
|
||||
target_compile_definitions(st7789 PRIVATE ${ST7789_LCD_DEFINES})
|
||||
target_compile_definitions(st7789 PRIVATE ${ST7789_LCD_DEFINES})
|
||||
|
||||
set(BME280_DHT_SOURCES
|
||||
"bme280_dht/bme280_dht.c"
|
||||
)
|
||||
|
||||
add_library(bme280 STATIC ${BME280_DHT_SOURCES})
|
||||
target_compile_definitions(bme280 PRIVATE ${BME280_DHT_DEFINES})
|
||||
|
|
|
@ -0,0 +1,213 @@
|
|||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/i2c-dev.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#include "bme280_dht.h"
|
||||
|
||||
bme280_ret_t _bme280_read_trim_data(bme280_t *bme);
|
||||
|
||||
double _bme280_compensate_T(bme280_t *bme, int32_t adc_T);
|
||||
double _bme280_compensate_P(bme280_t *bme, int32_t adc_P);
|
||||
double _bme280_compensate_H(bme280_t *bme, int32_t adc_H);
|
||||
|
||||
bme280_ret_t bme280_init(bme280_t *bme) {
|
||||
if(!bme) return BME_FAIL;
|
||||
if(_bme280_read_trim_data(bme) == BME_FAIL) return BME_FAIL;
|
||||
return BME_OK;
|
||||
}
|
||||
|
||||
bme280_ret_t bme280_preset_config(bme280_config_t *config, bme280_mode_preset_t preset) {
|
||||
switch(preset) {
|
||||
case BME_PRESET_WEATHER:
|
||||
config->mode = BME_MODE_FORCED;
|
||||
config->osrs_t = BME_OS_1;
|
||||
config->osrs_p = BME_OS_1;
|
||||
config->osrs_h = BME_OS_1;
|
||||
config->filter = BME_FILTER_OFF;
|
||||
break;
|
||||
case BME_PRESET_HUMIDITY:
|
||||
config->mode = BME_MODE_FORCED;
|
||||
config->osrs_t = BME_OS_1;
|
||||
config->osrs_p = BME_OS_SKIP;
|
||||
config->osrs_h = BME_OS_1;
|
||||
config->filter = BME_FILTER_OFF;
|
||||
break;
|
||||
case BME_PRESET_INDOOR_NAV:
|
||||
config->mode = BME_MODE_NORMAL;
|
||||
config->osrs_t = BME_OS_2;
|
||||
config->osrs_p = BME_OS_16;
|
||||
config->osrs_h = BME_OS_1;
|
||||
config->filter = BME_FILTER_16;
|
||||
break;
|
||||
case BME_PRESET_GAMING:
|
||||
config->mode = BME_MODE_NORMAL;
|
||||
config->osrs_t = BME_OS_1;
|
||||
config->osrs_p = BME_OS_4;
|
||||
config->osrs_h = BME_OS_SKIP;
|
||||
config->filter = BME_FILTER_4;
|
||||
break;
|
||||
}
|
||||
return BME_OK;
|
||||
}
|
||||
|
||||
bme280_ret_t bme280_apply_config(bme280_t *bme, bme280_config_t *config) {
|
||||
uint8_t reg_config = config->filter << 0x02;
|
||||
uint8_t reg_ctrl_measure = (config->osrs_t << 0x05) | (config->osrs_p << 0x02) | config->mode;
|
||||
uint8_t reg_ctrl_hum = config->osrs_h;
|
||||
bme->cb.write_register_cb(bme->user_data, 0xF5, reg_config);
|
||||
bme->cb.write_register_cb(bme->user_data, 0xF4, reg_ctrl_measure);
|
||||
bme->cb.write_register_cb(bme->user_data, 0xF2, reg_ctrl_hum);
|
||||
return BME_OK;
|
||||
}
|
||||
|
||||
bme280_ret_t bme280_measure(bme280_t *bme, bme280_result_t *result) {
|
||||
uint32_t raw_P = 0x00;
|
||||
uint32_t raw_T = 0x00;
|
||||
uint32_t raw_H = 0x00;
|
||||
uint8_t measure_data[8];
|
||||
uint8_t status = 0;
|
||||
uint8_t ctrl_meas;
|
||||
uint8_t loop_count = 0;
|
||||
bme->cb.read_register_cb(bme->user_data, 0xF4, &ctrl_meas, 0x01);
|
||||
bme->cb.write_register_cb(bme->user_data, 0xF4, ctrl_meas | BME_MODE_FORCED);
|
||||
do {
|
||||
bme->cb.read_register_cb(bme->user_data, 0xF3, &status, 0x01);
|
||||
loop_count++;
|
||||
usleep(100 * 1000);
|
||||
} while(status & 0x08 && (loop_count < 12));
|
||||
bme->cb.read_register_cb(bme->user_data, 0xF7, measure_data, 0x08);
|
||||
|
||||
raw_P = measure_data[0] << 12 | measure_data[1] << 0x04 | measure_data[2];
|
||||
raw_T = measure_data[3] << 12 | measure_data[4] << 0x04 | measure_data[5];
|
||||
raw_H = measure_data[6] << 8 | measure_data[7];
|
||||
result->temperature = _bme280_compensate_T(bme, raw_T);
|
||||
result->pressure = _bme280_compensate_P(bme, raw_P);
|
||||
result->humidity = _bme280_compensate_H(bme, raw_H);
|
||||
return BME_OK;
|
||||
}
|
||||
|
||||
bme280_ret_t _bme280_read_trim_data(bme280_t *bme) {
|
||||
bme280_ret_t ret;
|
||||
uint8_t rx_buf[2];
|
||||
ret = bme->cb.read_register_cb(bme->user_data, 0x88, rx_buf, 0x02); // T1
|
||||
if(ret == BME_FAIL) return ret;
|
||||
bme->trim.dig_T1 = rx_buf[1] << 0x08 | rx_buf[0];
|
||||
|
||||
ret = bme->cb.read_register_cb(bme->user_data, 0x8A, rx_buf, 0x02); // T2
|
||||
if(ret == BME_FAIL) return ret;
|
||||
bme->trim.dig_T2 = rx_buf[1] << 0x08 | rx_buf[0];
|
||||
|
||||
ret = bme->cb.read_register_cb(bme->user_data, 0x8C, rx_buf, 0x02); // T3
|
||||
if(ret == BME_FAIL) return ret;
|
||||
bme->trim.dig_T3 = rx_buf[1] << 0x08 | rx_buf[0];
|
||||
|
||||
ret = bme->cb.read_register_cb(bme->user_data, 0x8E, rx_buf, 0x02); // P1
|
||||
if(ret == BME_FAIL) return ret;
|
||||
bme->trim.dig_P1 = rx_buf[1] << 0x08 | rx_buf[0];
|
||||
|
||||
ret = bme->cb.read_register_cb(bme->user_data, 0x90, rx_buf, 0x02); // P2
|
||||
if(ret == BME_FAIL) return ret;
|
||||
bme->trim.dig_P2 = rx_buf[1] << 0x08 | rx_buf[0];
|
||||
|
||||
ret = bme->cb.read_register_cb(bme->user_data, 0x92, rx_buf, 0x02); // P3
|
||||
if(ret == BME_FAIL) return ret;
|
||||
bme->trim.dig_P3 = rx_buf[1] << 0x08 | rx_buf[0];
|
||||
|
||||
ret = bme->cb.read_register_cb(bme->user_data, 0x94, rx_buf, 0x02); // P4
|
||||
if(ret == BME_FAIL) return ret;
|
||||
bme->trim.dig_P4 = rx_buf[1] << 0x08 | rx_buf[0];
|
||||
|
||||
ret = bme->cb.read_register_cb(bme->user_data, 0x96, rx_buf, 0x02); // P5
|
||||
if(ret == BME_FAIL) return ret;
|
||||
bme->trim.dig_P5 = rx_buf[1] << 0x08 | rx_buf[0];
|
||||
|
||||
ret = bme->cb.read_register_cb(bme->user_data, 0x98, rx_buf, 0x02); // P5
|
||||
if(ret == BME_FAIL) return ret;
|
||||
bme->trim.dig_P6 = rx_buf[1] << 0x08 | rx_buf[0];
|
||||
|
||||
ret = bme->cb.read_register_cb(bme->user_data, 0x9A, rx_buf, 0x02); // P7
|
||||
if(ret == BME_FAIL) return ret;
|
||||
bme->trim.dig_P7 = rx_buf[1] << 0x08 | rx_buf[0];
|
||||
|
||||
ret = bme->cb.read_register_cb(bme->user_data, 0x9C, rx_buf, 0x02); // P8
|
||||
if(ret == BME_FAIL) return ret;
|
||||
bme->trim.dig_P8 = rx_buf[1] << 0x08 | rx_buf[0];
|
||||
|
||||
ret = bme->cb.read_register_cb(bme->user_data, 0x9E, rx_buf, 0x02); // P9
|
||||
if(ret == BME_FAIL) return ret;
|
||||
bme->trim.dig_P9 = rx_buf[1] << 0x08 | rx_buf[0];
|
||||
|
||||
ret = bme->cb.read_register_cb(bme->user_data, 0xA1, rx_buf, 0x01); // H1
|
||||
if(ret == BME_FAIL) return ret;
|
||||
bme->trim.dig_H1 = rx_buf[0];
|
||||
|
||||
ret = bme->cb.read_register_cb(bme->user_data, 0xE1, rx_buf, 0x02); // H2
|
||||
if(ret == BME_FAIL) return ret;
|
||||
bme->trim.dig_H2 = rx_buf[1] << 0x08 | rx_buf[0];
|
||||
|
||||
ret = bme->cb.read_register_cb(bme->user_data, 0xE3, rx_buf, 0x01); // H3
|
||||
if(ret == BME_FAIL) return ret;
|
||||
bme->trim.dig_H3 = rx_buf[0];
|
||||
|
||||
ret = bme->cb.read_register_cb(bme->user_data, 0xE4, rx_buf, 0x02); // H4
|
||||
if(ret == BME_FAIL) return ret;
|
||||
bme->trim.dig_H4 = (rx_buf[0] << 0x04) | (rx_buf[1] & 0x0F);
|
||||
|
||||
ret = bme->cb.read_register_cb(bme->user_data, 0xE5, rx_buf, 0x02); // H5
|
||||
if(ret == BME_FAIL) return ret;
|
||||
bme->trim.dig_H5 = (rx_buf[1] << 0x04) | (rx_buf[0] >> 0x04);
|
||||
|
||||
ret = bme->cb.read_register_cb(bme->user_data, 0xE7, rx_buf, 0x01); // H6
|
||||
if(ret == BME_FAIL) return ret;
|
||||
bme->trim.dig_H6 = rx_buf[0];
|
||||
return BME_OK;
|
||||
}
|
||||
|
||||
double _bme280_compensate_T(bme280_t *bme, int32_t adc_T) {
|
||||
double var1, var2, T;
|
||||
var1 = (((double)adc_T) / 16384.0 - ((double)bme->trim.dig_T1) / 1024.0) * ((double)bme->trim.dig_T2);
|
||||
var2 = ((((double)adc_T) / 131072.0 - ((double)bme->trim.dig_T1) / 8192.0) *
|
||||
(((double)adc_T) / 131072.0 - ((double)bme->trim.dig_T1) / 8192.0)) *
|
||||
((double)bme->trim.dig_T3);
|
||||
bme->t_fine = (int32_t)(var1 + var2);
|
||||
T = (var1 + var2) / 5120.0;
|
||||
return T;
|
||||
}
|
||||
|
||||
double _bme280_compensate_P(bme280_t *bme, int32_t adc_P) {
|
||||
double var1, var2, p;
|
||||
var1 = ((double)bme->t_fine / 2.0) - 64000.0;
|
||||
var2 = var1 * var1 * ((double)bme->trim.dig_P6) / 32768.0;
|
||||
var2 = var2 + var1 * ((double)bme->trim.dig_P5) * 2.0;
|
||||
var2 = (var2 / 4.0) + (((double)bme->trim.dig_P4) * 65536.0);
|
||||
var1 = (((double)bme->trim.dig_P3) * var1 * var1 / 524288.0 + ((double)bme->trim.dig_P2) * var1) / 524288.0;
|
||||
var1 = (1.0 + var1 / 32768.0) * ((double)bme->trim.dig_P1);
|
||||
if (var1 == 0.0)
|
||||
{
|
||||
return 0; // avoid exception caused by division by zero
|
||||
}
|
||||
p = 1048576.0 - (double)adc_P;
|
||||
p = (p - (var2 / 4096.0)) * 6250.0 / var1;
|
||||
var1 = ((double)bme->trim.dig_P9) * p * p / 2147483648.0;
|
||||
var2 = p * ((double)bme->trim.dig_P8) / 32768.0;
|
||||
p = p + (var1 + var2 + ((double)bme->trim.dig_P7)) / 16.0;
|
||||
return p;
|
||||
}
|
||||
|
||||
double _bme280_compensate_H(bme280_t *bme, int32_t adc_H) {
|
||||
double var_H;
|
||||
var_H = (((double)bme->t_fine) - 76800.0);
|
||||
var_H = (adc_H - (((double)bme->trim.dig_H4) * 64.0 + ((double)bme->trim.dig_H5) / 16384.0 *
|
||||
var_H)) *
|
||||
(((double)bme->trim.dig_H2) / 65536.0 * (1.0 + ((double)bme->trim.dig_H6) / 67108864.0 * var_H * (1.0 + ((double)bme->trim.dig_H3) / 67108864.0 * var_H)));
|
||||
var_H = var_H * (1.0 - ((double)bme->trim.dig_H1) * var_H / 524288.0);
|
||||
if (var_H > 100.0)
|
||||
var_H = 100.0;
|
||||
else if (var_H < 0.0)
|
||||
var_H = 0.0;
|
||||
return var_H;
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
#ifndef BME280_DHT_H
|
||||
#define BME280_DHT_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef int bme280_ret_t;
|
||||
|
||||
#define BME_OK 0
|
||||
#define BME_FAIL -1
|
||||
|
||||
#define BME_MODE_SLEEP 0x00
|
||||
#define BME_MODE_FORCED 0x01
|
||||
#define BME_MODE_NORMAL 0x03
|
||||
|
||||
#define BME_OS_SKIP 0x00
|
||||
#define BME_OS_1 0x01
|
||||
#define BME_OS_2 0x02
|
||||
#define BME_OS_4 0x03
|
||||
#define BME_OS_8 0x04
|
||||
#define BME_OS_16 0x05
|
||||
|
||||
#define BME_FILTER_OFF 0x00
|
||||
#define BME_FILTER_2 0x01
|
||||
#define BME_FILTER_4 0x02
|
||||
#define BME_FILTER_8 0x03
|
||||
#define BME_FILTER_16 0x04
|
||||
|
||||
typedef enum {
|
||||
BME_PRESET_WEATHER,
|
||||
BME_PRESET_HUMIDITY,
|
||||
BME_PRESET_INDOOR_NAV,
|
||||
BME_PRESET_GAMING
|
||||
} bme280_mode_preset_t;
|
||||
|
||||
typedef struct {
|
||||
bme280_ret_t (*read_register_cb)(void *handle, uint8_t reg, uint8_t *data, uint8_t len);
|
||||
bme280_ret_t (*write_register_cb)(void *handle, uint8_t reg, uint8_t data);
|
||||
} bme280_cb_t;
|
||||
|
||||
typedef struct {
|
||||
unsigned short dig_T1;
|
||||
short dig_T2;
|
||||
short dig_T3;
|
||||
unsigned short dig_P1;
|
||||
short dig_P2;
|
||||
short dig_P3;
|
||||
short dig_P4;
|
||||
short dig_P5;
|
||||
short dig_P6;
|
||||
short dig_P7;
|
||||
short dig_P8;
|
||||
short dig_P9;
|
||||
unsigned char dig_H1;
|
||||
short dig_H2;
|
||||
unsigned char dig_H3;
|
||||
short dig_H4;
|
||||
short dig_H5;
|
||||
char dig_H6;
|
||||
} bme280_trim_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t mode;
|
||||
uint8_t osrs_t;
|
||||
uint8_t osrs_p;
|
||||
uint8_t osrs_h;
|
||||
uint8_t t_sb;
|
||||
uint8_t filter;
|
||||
} bme280_config_t;
|
||||
|
||||
typedef struct {
|
||||
double temperature;
|
||||
double pressure;
|
||||
double humidity;
|
||||
} bme280_result_t;
|
||||
|
||||
typedef struct {
|
||||
void *user_data;
|
||||
bme280_trim_t trim;
|
||||
bme280_cb_t cb;
|
||||
int32_t t_fine;
|
||||
} bme280_t;
|
||||
|
||||
bme280_ret_t bme280_init(bme280_t *bme);
|
||||
bme280_ret_t bme280_preset_config(bme280_config_t *config, bme280_mode_preset_t preset);
|
||||
bme280_ret_t bme280_apply_config(bme280_t *bme, bme280_config_t *config);
|
||||
bme280_ret_t bme280_measure(bme280_t *bme, bme280_result_t *result);
|
||||
|
||||
#endif
|
|
@ -5,13 +5,14 @@ agent: {
|
|||
log_level = 1;
|
||||
};
|
||||
|
||||
drivers: {
|
||||
i2c: {
|
||||
path = "/dev/i2c-1";
|
||||
};
|
||||
};
|
||||
|
||||
devices: {
|
||||
dht: {
|
||||
i2c: {
|
||||
path = "/dev/i2c-1";
|
||||
addr = 0x76;
|
||||
};
|
||||
};
|
||||
|
||||
lcd: {
|
||||
spi: {
|
||||
path = "/dev/spidev0.0";
|
||||
|
@ -30,6 +31,7 @@ agent: {
|
|||
line = 231;
|
||||
};
|
||||
};
|
||||
|
||||
stick: {
|
||||
up: {
|
||||
path = "/dev/gpiochip2";
|
||||
|
|
|
@ -5,13 +5,14 @@ agent: {
|
|||
log_level = 1;
|
||||
};
|
||||
|
||||
drivers: {
|
||||
i2c: {
|
||||
path = "/dev/i2c-1";
|
||||
};
|
||||
};
|
||||
|
||||
devices: {
|
||||
dht: {
|
||||
i2c: {
|
||||
path = "/dev/i2c-1";
|
||||
addr = 0x76;
|
||||
};
|
||||
};
|
||||
|
||||
lcd: {
|
||||
spi: {
|
||||
path = "/dev/spidev0.1";
|
||||
|
@ -30,6 +31,7 @@ agent: {
|
|||
line = 68;
|
||||
};
|
||||
};
|
||||
|
||||
stick: {
|
||||
up: {
|
||||
path = "/dev/gpiochip2";
|
||||
|
|
|
@ -5,13 +5,14 @@ agent: {
|
|||
log_level = 1;
|
||||
};
|
||||
|
||||
drivers: {
|
||||
i2c: {
|
||||
path = "/dev/i2c-1";
|
||||
};
|
||||
};
|
||||
|
||||
devices: {
|
||||
dht: {
|
||||
i2c: {
|
||||
path = "/dev/i2c-1";
|
||||
addr = 0x76;
|
||||
};
|
||||
};
|
||||
|
||||
lcd: {
|
||||
spi: {
|
||||
path = "/dev/spidev0.1";
|
||||
|
@ -30,6 +31,7 @@ agent: {
|
|||
line = 12;
|
||||
};
|
||||
};
|
||||
|
||||
stick: {
|
||||
up: {
|
||||
path = "/dev/gpiochip2";
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
#include <fcntl.h>
|
||||
#include <linux/i2c-dev.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "drivers/user_config_driver.h"
|
||||
|
||||
#include "utils/user_log_util.h"
|
||||
|
||||
#include "impl/user_bme280_impl.h"
|
||||
|
||||
extern user_config_t g_config;
|
||||
|
||||
int user_bme280_impl_init(user_bme280_impl_t *impl) {
|
||||
char *i2cdev_path =
|
||||
user_config_lookup_string(&g_config, "agent.devices.dht.i2c.path");
|
||||
if(i2cdev_path == NULL) return -1;
|
||||
int i2c_slaveaddr = 0;
|
||||
if(user_config_lookup_int(&g_config, "agent.devices.dht.i2c.addr",
|
||||
&i2c_slaveaddr) != 0) {
|
||||
USER_LOG(USER_LOG_ERROR, "Failed to lookup I2C device address.");
|
||||
return -2;
|
||||
}
|
||||
|
||||
if((impl->i2cdev_fd = open(i2cdev_path, O_RDWR)) < 0) {
|
||||
USER_LOG(USER_LOG_ERROR, "Failed to open I2C device.");
|
||||
return -3;
|
||||
}
|
||||
|
||||
USER_LOG(USER_LOG_INFO, "Opened %s for read-write, fd=%d.", i2cdev_path, impl->i2cdev_fd);
|
||||
|
||||
impl->i2c_addr = i2c_slaveaddr & 0xFFU;
|
||||
|
||||
if(ioctl(impl->i2cdev_fd, I2C_SLAVE, impl->i2c_addr) < 0) {
|
||||
int err = errno;
|
||||
USER_LOG(USER_LOG_ERROR, "Failed to select I2C slave 0x%x, %s.", impl->i2c_addr, strerror(err));
|
||||
close(impl->i2cdev_fd);
|
||||
return -3;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int user_bme280_impl_deinit(user_bme280_impl_t *impl) {
|
||||
close(impl->i2cdev_fd);
|
||||
}
|
||||
|
||||
bme280_ret_t user_bme280_impl_read_register_cb(user_bme280_impl_t *impl,
|
||||
uint8_t reg, uint8_t *data,
|
||||
uint8_t len) {
|
||||
if(write(impl->i2cdev_fd, ®, 0x01) < 1) {
|
||||
USER_LOG(USER_LOG_WARN, "I2C write failed, reg=%d.", reg);
|
||||
return BME_FAIL;
|
||||
}
|
||||
|
||||
if(read(impl->i2cdev_fd, data, len) < len) {
|
||||
USER_LOG(USER_LOG_WARN, "I2C read failed, reg=%d.", reg);
|
||||
return BME_FAIL;
|
||||
}
|
||||
|
||||
return BME_OK;
|
||||
}
|
||||
|
||||
bme280_ret_t user_bme280_impl_write_register_cb(user_bme280_impl_t *impl,
|
||||
uint8_t reg, uint8_t data) {
|
||||
uint8_t tx_buf[2] = {reg, data};
|
||||
if(write(impl->i2cdev_fd, tx_buf, 0x02) < 2) {
|
||||
USER_LOG(USER_LOG_WARN, "I2C write failed, reg=%d.", reg);
|
||||
return BME_FAIL;
|
||||
}
|
||||
return BME_OK;
|
||||
}
|
|
@ -41,12 +41,14 @@ int main(int argc, const char *argv[]) {
|
|||
user_lvgl_task_init();
|
||||
user_base_task_init();
|
||||
user_clock_task_init();
|
||||
user_dht_task_init();
|
||||
|
||||
USER_LOG(USER_LOG_INFO, "Initialized, main thread sleeping.");
|
||||
while(g_running) {
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
user_dht_task_deinit();
|
||||
user_clock_task_deinit();
|
||||
user_base_task_deinit();
|
||||
user_lvgl_task_deinit();
|
||||
|
|
|
@ -62,4 +62,6 @@ void *user_base_task(void *arguments) {
|
|||
while(g_running) {
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
|
@ -78,4 +78,6 @@ void *user_clock_task(void *arguments) {
|
|||
pthread_mutex_unlock(&g_lvgl_mutex);
|
||||
usleep(200 * 1000);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
#include <stdint.h>
|
||||
|
||||
#include "impl/user_bme280_impl.h"
|
||||
|
||||
#include "tasks/user_task_lvgl_common.h"
|
||||
|
||||
void *user_dht_task(void *arguments);
|
||||
pthread_t user_dht_task_thread;
|
||||
|
||||
int user_dht_task_init(void) {
|
||||
int ret;
|
||||
|
||||
ret = pthread_create(&user_dht_task_thread, NULL, user_dht_task, NULL);
|
||||
if(ret) return ret;
|
||||
}
|
||||
|
||||
int user_dht_task_deinit(void) {
|
||||
pthread_join(user_dht_task_thread, NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *user_dht_task(void *arguments) {
|
||||
while(g_running && !g_lvgl_ready) {
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
user_bme280_impl_t bme_impl;
|
||||
user_bme280_impl_init(&bme_impl);
|
||||
|
||||
bme280_t bme = {
|
||||
.cb =
|
||||
{
|
||||
.read_register_cb =
|
||||
(bme280_ret_t(*)(void *, uint8_t, uint8_t *,
|
||||
uint8_t))user_bme280_impl_read_register_cb,
|
||||
.write_register_cb = (bme280_ret_t(*)(void *, uint8_t, uint8_t))
|
||||
user_bme280_impl_write_register_cb,
|
||||
},
|
||||
.user_data = &bme_impl
|
||||
};
|
||||
|
||||
bme280_config_t cfg;
|
||||
|
||||
bme280_init(&bme);
|
||||
bme280_preset_config(&cfg, BME_PRESET_WEATHER);
|
||||
bme280_apply_config(&bme, &cfg);
|
||||
|
||||
bme280_result_t res;
|
||||
|
||||
while(g_running) {
|
||||
bme280_measure(&bme, &res);
|
||||
USER_LOG(USER_LOG_INFO, "BME: %.02f, %.02f, %.02f", res.temperature, res.humidity, res.pressure);
|
||||
sleep(1);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue