From 8c93743548f4f4415b2e60be641ae0aa2e7632ac Mon Sep 17 00:00:00 2001 From: Christian Loch Date: Tue, 21 Apr 2020 00:37:16 +0200 Subject: [PATCH] Refactor sensor control to separate component --- components/display/display.c | 52 ----- components/sensors/CMakeLists.txt | 3 + {main => components/sensors}/bme280.c | 0 {main => components/sensors}/bme280.h | 0 {main => components/sensors}/bme280_defs.h | 0 components/sensors/sensors.c | 145 ++++++++++++ components/sensors/sensors.h | 19 ++ main/CMakeLists.txt | 2 +- main/main.c | 242 +-------------------- 9 files changed, 176 insertions(+), 287 deletions(-) create mode 100644 components/sensors/CMakeLists.txt rename {main => components/sensors}/bme280.c (100%) rename {main => components/sensors}/bme280.h (100%) rename {main => components/sensors}/bme280_defs.h (100%) create mode 100644 components/sensors/sensors.c create mode 100644 components/sensors/sensors.h diff --git a/components/display/display.c b/components/display/display.c index 089f858..ce3ea05 100644 --- a/components/display/display.c +++ b/components/display/display.c @@ -240,51 +240,6 @@ void print_humid2(uint32_t humidity_raw) TFT_print(humid_str, layout.aussenLeft, layout.humBaseline); } -int32_t last_temp_raw = 0; -uint32_t last_pressure_raw = 0; -uint32_t last_humidity_raw = 0; -int32_t last_temp2_raw = 0; -uint32_t last_pressure2_raw = 0; -uint32_t last_humidity2_raw = 0; - -void update_data(int32_t temp_raw, uint32_t pressure_raw, uint32_t humidity_raw, - int32_t temp2_raw, uint32_t pressure2_raw, uint32_t humidity2_raw) -{ - if (temp_raw != last_temp_raw) { - print_temp1(temp_raw); - last_temp_raw = temp_raw; - } - if (temp2_raw != last_temp2_raw) { - print_temp2(temp2_raw); - last_temp2_raw = temp2_raw; - } - - if (pressure_raw != last_pressure_raw) { - print_press1(pressure_raw); - last_pressure_raw = pressure_raw; - } - if (pressure2_raw != last_pressure2_raw) { - print_press2(pressure2_raw); - last_pressure2_raw = pressure2_raw; - } - - if (humidity_raw != last_humidity_raw) { - print_humid1(humidity_raw); - last_humidity_raw = humidity_raw; - } - if (humidity2_raw != last_humidity2_raw) { - print_humid2(humidity2_raw); - last_humidity2_raw = humidity2_raw; - } - - last_pressure_raw = pressure_raw; - last_humidity_raw = humidity_raw; - last_temp2_raw = temp2_raw; - last_pressure2_raw = pressure2_raw; - last_humidity2_raw = humidity2_raw; -} - - void display_data(int32_t temp_raw, uint32_t pressure_raw, uint32_t humidity_raw, int32_t temp2_raw, uint32_t pressure2_raw, uint32_t humidity2_raw) { @@ -314,13 +269,6 @@ void display_data(int32_t temp_raw, uint32_t pressure_raw, uint32_t humidity_raw TFT_setFont(UBUNTU16_FONT, NULL); TFT_print("%", layout.unitLeft, layout.humBaseline); TFT_print("hPa", layout.unitLeft, layout.pressBaseline); - - last_temp_raw = temp_raw; - last_pressure_raw = pressure_raw; - last_humidity_raw = humidity_raw; - last_temp2_raw = temp2_raw; - last_pressure2_raw = pressure2_raw; - last_humidity2_raw = humidity2_raw; } esp_err_t init_display() diff --git a/components/sensors/CMakeLists.txt b/components/sensors/CMakeLists.txt new file mode 100644 index 0000000..68a867f --- /dev/null +++ b/components/sensors/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "bme280.c" "sensors.c" + INCLUDE_DIRS "." + REQUIRES driver ) \ No newline at end of file diff --git a/main/bme280.c b/components/sensors/bme280.c similarity index 100% rename from main/bme280.c rename to components/sensors/bme280.c diff --git a/main/bme280.h b/components/sensors/bme280.h similarity index 100% rename from main/bme280.h rename to components/sensors/bme280.h diff --git a/main/bme280_defs.h b/components/sensors/bme280_defs.h similarity index 100% rename from main/bme280_defs.h rename to components/sensors/bme280_defs.h diff --git a/components/sensors/sensors.c b/components/sensors/sensors.c new file mode 100644 index 0000000..ee251c7 --- /dev/null +++ b/components/sensors/sensors.c @@ -0,0 +1,145 @@ +/* + * sensors.c + * + * Created on: 20 Apr 2020 + * Author: Chris + */ + +#include "sensors.h" + +static struct bme280_dev* dev; +static struct bme280_dev* dev2; + +void i2c_setup() +{ + printf("Setting up I2C driver on port 1... "); + i2c_config_t config; + config.mode = I2C_MODE_MASTER; + config.sda_io_num = 33; + config.sda_pullup_en = GPIO_PULLUP_ENABLE; + config.scl_io_num = 32; + config.scl_pullup_en = GPIO_PULLUP_ENABLE; + config.master.clk_speed = 100000; + i2c_param_config(I2C_NUM_0, &config); + printf("Set driver parameters... "); + esp_err_t err = i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0); + if (err == ESP_OK) + printf("Driver installed!\n"); + else if (err == ESP_ERR_INVALID_ARG) + printf("Driver install failed, invalid arguments!\n"); + else + printf("Driver install failed!\n"); +} + +int8_t i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len) { + i2c_cmd_handle_t cmd = i2c_cmd_link_create(); + i2c_master_start(cmd); + i2c_master_write_byte(cmd, dev_id << 1 | I2C_MASTER_WRITE, 1); + i2c_master_write_byte(cmd, reg_addr, 1); + i2c_master_start(cmd); + i2c_master_write_byte(cmd, dev_id << 1 | I2C_MASTER_READ, 1); + if (len > 1) { + i2c_master_read(cmd, data, len - 1, I2C_MASTER_ACK); + } + i2c_master_read_byte(cmd, data + len - 1, I2C_MASTER_NACK); + i2c_master_stop(cmd); + i2c_master_cmd_begin(I2C_NUM_0, cmd, 500 / portTICK_RATE_MS); + i2c_cmd_link_delete(cmd); + return 0; +} + +int8_t i2c_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len) { + //printf("Writing to bus: dev_id=%x, reg_addr=%x, data=%p, length=%u\n", dev_id, reg_addr, data, len); + i2c_cmd_handle_t cmd = i2c_cmd_link_create(); + i2c_master_start(cmd); + i2c_master_write_byte(cmd, (dev_id << 1) | I2C_MASTER_WRITE, 1); + i2c_master_write_byte(cmd, reg_addr, 1); + i2c_master_write(cmd, data, len, 1); + i2c_master_stop(cmd); + i2c_master_cmd_begin(I2C_NUM_0, cmd, 500 / portTICK_RATE_MS); + i2c_cmd_link_delete(cmd); + return 0; +} + +void i2c_delay(uint32_t period) { + vTaskDelay(period / portTICK_PERIOD_MS); +} + +void i2c_shutdown() +{ + printf("Shutting down I�C bus... "); + esp_err_t err = i2c_driver_delete(I2C_NUM_0); + if (err == ESP_ERR_INVALID_ARG) + printf("Failed, invalid arguments!\n"); + else + printf("Success!\n"); +} + +void read_sensor(int32_t* temp, uint32_t* pressure, uint32_t* humidity) { + + uint8_t settings_sel; + uint32_t req_delay; + struct bme280_data comp_data; + dev->settings.osr_h = BME280_OVERSAMPLING_16X; + dev->settings.osr_p = BME280_OVERSAMPLING_16X; + dev->settings.osr_t = BME280_OVERSAMPLING_16X; + dev->settings.filter = BME280_FILTER_COEFF_16; + + settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL; + bme280_set_sensor_settings(settings_sel, dev); + + req_delay = 12*bme280_cal_meas_delay(&(dev->settings)); + + bme280_set_sensor_mode(BME280_FORCED_MODE, dev); + dev->delay_ms(req_delay / portTICK_PERIOD_MS); + bme280_get_sensor_data(BME280_ALL, &comp_data, dev); + *temp = comp_data.temperature; + *pressure = comp_data.pressure; + *humidity = comp_data.humidity; +} + +void read_sensor2(int32_t* temp, uint32_t* pressure, uint32_t* humidity) { + + uint8_t settings_sel; + uint32_t req_delay; + struct bme280_data comp_data; + + dev2->settings.osr_h = BME280_OVERSAMPLING_16X; + dev2->settings.osr_p = BME280_OVERSAMPLING_16X; + dev2->settings.osr_t = BME280_OVERSAMPLING_16X; + dev2->settings.filter = BME280_FILTER_COEFF_16; + + settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL; + bme280_set_sensor_settings(settings_sel, dev2); + + req_delay = 12*bme280_cal_meas_delay(&(dev2->settings)); + + bme280_set_sensor_mode(BME280_FORCED_MODE, dev2); + dev2->delay_ms(req_delay / portTICK_PERIOD_MS); + bme280_get_sensor_data(BME280_ALL, &comp_data, dev2); + *temp = comp_data.temperature; + *pressure = comp_data.pressure; + *humidity = comp_data.humidity; +} + +void init_sensors() +{ + // INIT SENSOR + i2c_setup(); + dev = malloc(sizeof(struct bme280_dev)); + dev->dev_id = 0x76; + dev->intf = BME280_I2C_INTF; + dev->read = i2c_read; + dev->write = i2c_write; + dev->delay_ms = i2c_delay; + bme280_init(dev); + + // INIT SENSOR2 + dev2 = malloc(sizeof(struct bme280_dev)); + dev2->dev_id = 0x77; + dev2->intf = BME280_I2C_INTF; + dev2->read = i2c_read; + dev2->write = i2c_write; + dev2->delay_ms = i2c_delay; + bme280_init(dev2); +} diff --git a/components/sensors/sensors.h b/components/sensors/sensors.h new file mode 100644 index 0000000..b23870d --- /dev/null +++ b/components/sensors/sensors.h @@ -0,0 +1,19 @@ +/* + * sensors.h + * + * Created on: 20 Apr 2020 + * Author: Chris + */ + +#ifndef COMPONENTS_SENSORS_SENSORS_H_ +#define COMPONENTS_SENSORS_SENSORS_H_ + +#include "bme280.h" +#include "bme280_defs.h" +#include "driver/i2c.h" + +void init_sensors(); +void read_sensor2(int32_t* temp, uint32_t* pressure, uint32_t* humidity); +void read_sensor(int32_t* temp, uint32_t* pressure, uint32_t* humidity); + +#endif /* COMPONENTS_SENSORS_SENSORS_H_ */ diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index fb7306a..e6c20ac 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -2,7 +2,7 @@ set(COMPONENT_REQUIRES ) set(COMPONENT_PRIV_REQUIRES ) -set(COMPONENT_SRCS "main.c" "bme280.c") +set(COMPONENT_SRCS "main.c") set(COMPONENT_ADD_INCLUDEDIRS "") register_component() diff --git a/main/main.c b/main/main.c index 39d1023..a18fdef 100644 --- a/main/main.c +++ b/main/main.c @@ -1,10 +1,10 @@ -#include "bme280.h" -#include "bme280_defs.h" -#include "driver/i2c.h" + #include "display.h" +#include "sensors.h" #include #include #include + //#include //#include "freertos/event_groups.h" //#include "nvs_flash.h" @@ -241,213 +241,6 @@ // vEventGroupDelete(s_wifi_event_group); //} -void i2c_setup() -{ - printf("Setting up I�C driver on port 1... "); - i2c_config_t config; - config.mode = I2C_MODE_MASTER; - config.sda_io_num = 33; - config.sda_pullup_en = GPIO_PULLUP_ENABLE; - config.scl_io_num = 32; - config.scl_pullup_en = GPIO_PULLUP_ENABLE; - config.master.clk_speed = 100000; - i2c_param_config(I2C_NUM_0, &config); - printf("Set driver parameters... "); - esp_err_t err = i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0); - if (err == ESP_OK) - printf("Driver installed!\n"); - else if (err == ESP_ERR_INVALID_ARG) - printf("Driver install failed, invalid arguments!\n"); - else - printf("Driver install failed!\n"); -} - -int8_t i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len) { - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); - i2c_master_start(cmd); - i2c_master_write_byte(cmd, dev_id << 1 | I2C_MASTER_WRITE, 1); - i2c_master_write_byte(cmd, reg_addr, 1); - i2c_master_start(cmd); - i2c_master_write_byte(cmd, dev_id << 1 | I2C_MASTER_READ, 1); - if (len > 1) { - i2c_master_read(cmd, data, len - 1, I2C_MASTER_ACK); - } - i2c_master_read_byte(cmd, data + len - 1, I2C_MASTER_NACK); - i2c_master_stop(cmd); - i2c_master_cmd_begin(I2C_NUM_0, cmd, 500 / portTICK_RATE_MS); - i2c_cmd_link_delete(cmd); - return 0; -} - -int8_t i2c_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len) { - //printf("Writing to bus: dev_id=%x, reg_addr=%x, data=%p, length=%u\n", dev_id, reg_addr, data, len); - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); - i2c_master_start(cmd); - i2c_master_write_byte(cmd, (dev_id << 1) | I2C_MASTER_WRITE, 1); - i2c_master_write_byte(cmd, reg_addr, 1); - i2c_master_write(cmd, data, len, 1); - i2c_master_stop(cmd); - i2c_master_cmd_begin(I2C_NUM_0, cmd, 500 / portTICK_RATE_MS); - i2c_cmd_link_delete(cmd); - return 0; -} - -void i2c_delay(uint32_t period) { - vTaskDelay(period / portTICK_PERIOD_MS); -} - -void i2c_shutdown() -{ - printf("Shutting down I�C bus... "); - esp_err_t err = i2c_driver_delete(I2C_NUM_0); - if (err == ESP_ERR_INVALID_ARG) - printf("Failed, invalid arguments!\n"); - else - printf("Success!\n"); -} - -void read_sensor(struct bme280_dev* dev, int32_t* temp, uint32_t* pressure, uint32_t* humidity) { - - uint8_t settings_sel; - uint32_t req_delay; - struct bme280_data comp_data; - - dev->settings.osr_h = BME280_OVERSAMPLING_16X; - dev->settings.osr_p = BME280_OVERSAMPLING_16X; - dev->settings.osr_t = BME280_OVERSAMPLING_16X; - dev->settings.filter = BME280_FILTER_COEFF_16; - - settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL; - bme280_set_sensor_settings(settings_sel, dev); - - req_delay = 12*bme280_cal_meas_delay(&(dev->settings)); - - /* Continuously stream sensor data */ - bme280_set_sensor_mode(BME280_FORCED_MODE, dev); - /* Wait for the measurement to complete and print data @25Hz */ - dev->delay_ms(req_delay / portTICK_PERIOD_MS); - bme280_get_sensor_data(BME280_ALL, &comp_data, dev); - *temp = comp_data.temperature; - *pressure = comp_data.pressure; - *humidity = comp_data.humidity; -} - -void read_sensor2(struct bme280_dev* dev, int32_t* temp, uint32_t* pressure, uint32_t* humidity) { - - uint8_t settings_sel; - uint32_t req_delay; - struct bme280_data comp_data; - - dev->settings.osr_h = BME280_OVERSAMPLING_16X; - dev->settings.osr_p = BME280_OVERSAMPLING_16X; - dev->settings.osr_t = BME280_OVERSAMPLING_16X; - dev->settings.filter = BME280_FILTER_COEFF_16; - - settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL; - bme280_set_sensor_settings(settings_sel, dev); - /*Calculate the minimum delay required between consecutive measurement based upon the sensor enabled - * and the oversampling configuration. */ - req_delay = 12*bme280_cal_meas_delay(&(dev->settings)); - - bme280_set_sensor_mode(BME280_FORCED_MODE, dev); - /* Wait for the measurement to complete and print data @25Hz */ - dev->delay_ms(req_delay / portTICK_PERIOD_MS); - bme280_get_sensor_data(BME280_ALL, &comp_data, dev); - *temp = comp_data.temperature; - *pressure = comp_data.pressure; - *humidity = comp_data.humidity; -} - -//void print_data(u8g2_t* u8g2, int32_t temp_raw, uint32_t pressure_raw, uint32_t humidity_raw, -// int32_t temp2_raw, uint32_t pressure2_raw, uint32_t humidity2_raw) { -// // Calc temperature pre and post comma values -// int32_t temp_pre = temp_raw / 100; -// int32_t temp_post = (abs(temp_raw) % 100) / 10; -// int32_t temp2_pre = temp2_raw / 100; -// int32_t temp2_post = (abs(temp2_raw) % 100) / 10; -// -// // Calc pressure values -// uint32_t press = pressure_raw / 100; -// uint32_t press2 = pressure2_raw / 100; -// -// // Calc humidity pre and post comma values -// uint32_t humid_pre = humidity_raw / 1024; -// uint32_t humid_post = (humidity_raw - humid_pre*1024) * 10 / 1024; -// uint32_t humid2_pre = humidity2_raw / 1024; -// uint32_t humid2_post = (humidity2_raw - humid2_pre*1024) * 10 / 1024; -// -// // Format temperatures -// char temp_str[2*(sizeof(int)*8+1)+5] = ""; // "" -// char temp_pre_str[sizeof(int)*8+1]; -// itoa(temp_pre, temp_pre_str, 10); -// char temp_post_str[sizeof(int)*8+1]; -// itoa(temp_post, temp_post_str, 10); -// char temp2_pre_str[sizeof(int)*8+1]; -// itoa(temp2_pre, temp2_pre_str, 10); -// char temp2_post_str[sizeof(int)*8+1]; -// itoa(temp2_post, temp2_post_str, 10); -// if (temp_pre < 10) -// strcat(temp_str, " "); // Add space if first temperatur is just one digit long " " -// strcat(temp_str, temp_pre_str); // " 1" -// strcat(temp_str, ","); // " 1," -// strcat(temp_str, temp_post_str); // " 1,3" -// strcat(temp_str, " "); // " 1,3 " -// if (temp2_pre >= 0) -// strcat(temp_str, " "); // Add space if there is no minus sign " 1,3 " -// if (temp2_pre < 10) -// strcat(temp_str, " "); // Add space if second temperatur is just one digit long " 1,3 " -// strcat(temp_str, temp2_pre_str); // " 1,3 7" -// strcat(temp_str, ","); // " 1,3 7," -// strcat(temp_str, temp2_post_str); // " 1,3 7,2" -// strcat(temp_str, " �C"); // " 1,3 7,2 �C" -// -// // Format temperatures -// char humid_str[2*(sizeof(int)*8+1)+5] = ""; // "" -// char humid_pre_str[sizeof(int)*8+1]; -// itoa(humid_pre, humid_pre_str, 10); -// char humid_post_str[sizeof(int)*8+1]; -// itoa(humid_post, humid_post_str, 10); -// char humid2_pre_str[sizeof(int)*8+1]; -// itoa(humid2_pre, humid2_pre_str, 10); -// char humid2_post_str[sizeof(int)*8+1]; -// itoa(humid2_post, humid2_post_str, 10); -// strcat(humid_str, humid_pre_str); // "12" -// strcat(humid_str, ","); // "12," -// strcat(humid_str, humid_post_str); // "12,5" -// strcat(humid_str, " "); // "12,5 " -// strcat(humid_str, humid2_pre_str); // "12,5 45" -// strcat(humid_str, ","); // "12,5 45," -// strcat(humid_str, humid2_post_str); // "12,5 45,23" -// strcat(humid_str, " %"); // "12,5 45,23 %" -// -// // Format pressure -// char pressure_str[2*(sizeof(int)*8+1)+5] = ""; // "" -// char press1_str[sizeof(int)*8+1]; -// itoa(press, press1_str, 10); -// char press2_str[sizeof(int)*8+1]; -// itoa(press2, press2_str, 10); -// if (press < 1000) -// strcat(pressure_str, " "); -// strcat(pressure_str, press1_str); -// strcat(pressure_str, " "); -// if (press2 < 1000) -// strcat(pressure_str, " "); -// strcat(pressure_str, press2_str); -// strcat(pressure_str, " hPa"); -// -// u8g2_ClearBuffer(u8g2); -// u8g2_SetFont(u8g2, u8g2_font_profont17_mf); -// int8_t fontheight = u8g2_GetAscent(u8g2); -// int8_t fontmargin = abs(u8g2_GetDescent(u8g2))+2; -// u8g2_DrawStr(u8g2, 0, fontheight, " IN OUT "); -// u8g2_DrawStr(u8g2, 0, 2*fontheight + fontmargin, temp_str); -// u8g2_DrawStr(u8g2, 0, 3*fontheight + 2*fontmargin, humid_str); -// u8g2_DrawStr(u8g2, 0, 4*fontheight + 3*fontmargin, pressure_str); -// u8g2_SendBuffer(u8g2); -//} - - - void app_main(void) { int32_t temp = -12; @@ -457,25 +250,6 @@ void app_main(void) uint32_t pressure2 = 0; uint32_t humidity2 = 0; - // INIT SENSOR - i2c_setup(); - struct bme280_dev dev; - dev.dev_id = 0x76; - dev.intf = BME280_I2C_INTF; - dev.read = i2c_read; - dev.write = i2c_write; - dev.delay_ms = i2c_delay; - bme280_init(&dev); - - // INIT SENSOR2 - struct bme280_dev dev2; - dev2.dev_id = 0x77; - dev2.intf = BME280_I2C_INTF; - dev2.read = i2c_read; - dev2.write = i2c_write; - dev2.delay_ms = i2c_delay; - bme280_init(&dev2); - // INIT WIFI //Initialize NVS // esp_err_t ret = nvs_flash_init(); @@ -493,14 +267,14 @@ void app_main(void) // ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server)); // ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server)); // server = start_webserver(); - + init_sensors(); init_display(); - read_sensor(&dev, &temp, &pressure, &humidity); - read_sensor(&dev2, &temp2, &pressure2, &humidity2); + read_sensor(&temp, &pressure, &humidity); + read_sensor2(&temp2, &pressure2, &humidity2); display_data(temp, pressure, humidity, temp2, pressure2, humidity2); while (1) { - read_sensor(&dev, &temp, &pressure, &humidity); - read_sensor(&dev2, &temp2, &pressure2, &humidity2); + read_sensor(&temp, &pressure, &humidity); + read_sensor2(&temp2, &pressure2, &humidity2); display_data(temp, pressure, humidity, temp2, pressure2, humidity2); vTaskDelay(1000 / portTICK_PERIOD_MS); }