From f3a99c3bc63e38edde592aefc9f05916a36b8421 Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 20 Apr 2020 18:54:01 +0200 Subject: [PATCH] Fix rgb value ordering Enable sensor usage Display now uses measured values --- .settings/org.eclipse.core.resources.prefs | 2 - components/display/display.c | 57 +++-- main/main.c | 274 +++++++++++---------- 3 files changed, 179 insertions(+), 154 deletions(-) delete mode 100644 .settings/org.eclipse.core.resources.prefs diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index 366e3bb..0000000 --- a/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,2 +0,0 @@ -eclipse.preferences.version=1 -encoding//main/main.c=ISO-8859-1 diff --git a/components/display/display.c b/components/display/display.c index fc16368..5b97d0c 100644 --- a/components/display/display.c +++ b/components/display/display.c @@ -64,7 +64,7 @@ layout_t create_layout() layout.datetimeBaseline = (layout.datetimebarH - layout.datetimeA) / 2; layout.innenW = TFT_getStringWidth("Innen"); - layout.aussenW = TFT_getStringWidth("Außen"); + layout.aussenW = TFT_getStringWidth("Aussen"); layout.locA = TFT_getfontheight(); TFT_setFont(DEJAVU18_FONT, NULL); @@ -117,15 +117,38 @@ layout_t create_layout() 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) { + // Calc temperature pre and post comma values + int32_t temp_pre = temp_raw / 100; + int32_t temp_post = (abs(temp_raw) % 100) / 10; + char temp_str[12]; + sprintf(temp_str, "% 2.2d,%.1d", temp_pre, temp_post); + int32_t temp2_pre = temp2_raw / 100; + int32_t temp2_post = (abs(temp2_raw) % 100) / 10; + char temp2_str[12]; + sprintf(temp2_str, "% 2.2d,%.1d", temp2_pre, temp2_post); + + // 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; + char humid_str[12]; + sprintf(humid_str, "%2.2d,%.1d", humid_pre, humid_post); + uint32_t humid2_pre = humidity2_raw / 1024; + uint32_t humid2_post = (humidity2_raw - humid2_pre*1024) * 10 / 1024; + char humid2_str[12]; + sprintf(humid2_str, "%2.2d,%.1d", humid2_pre, humid2_post); + + // Calc pressure values + uint32_t press = pressure_raw / 100; + char press_str[12]; + sprintf(press_str, "%d", press); + uint32_t press2 = pressure2_raw / 100; + char press2_str[12]; + sprintf(press2_str, "%d", press2); + TFT_setFont(UBUNTU16_FONT, NULL); TFT_fillScreen(TFT_BLACK); tft_fg = TFT_WHITE; - const color_t red = {0, 0, 255}; - const color_t green = {0, 255, 0}; - const color_t blue = {255, 0, 0}; - const color_t yellow = {255, 255, 0}; - TFT_print("25.12.2031 08:31", layout.dateTimeLeft, layout.datetimeBaseline); TFT_drawFastHLine(0, 20, 160, TFT_WHITE); @@ -133,21 +156,21 @@ void display_data(int32_t temp_raw, uint32_t pressure_raw, uint32_t humidity_raw TFT_print("Aussen", layout.aussenLeft, layout.aussenBaseline); TFT_setFont(DEJAVU18_FONT, NULL); - tft_fg = red; - TFT_print("-42,2", layout.innenLeft, layout.tempBaseline); - tft_fg = yellow; - TFT_print("-35,2", layout.aussenLeft, layout.tempBaseline); + tft_fg = TFT_RED; + TFT_print(temp_str, layout.innenLeft, layout.tempBaseline); + tft_fg = TFT_YELLOW; + TFT_print(temp2_str, layout.aussenLeft, layout.tempBaseline); tft_fg = TFT_WHITE; TFT_print(" C", layout.unitLeft, layout.tempBaseline); TFT_drawCircle(layout.unitLeft+3, layout.tempBaseline+3, 3, TFT_WHITE); TFT_setFont(UBUNTU16_FONT, NULL); - TFT_print("25,2", layout.innenLeft, layout.humBaseline); - TFT_print("42,2", layout.aussenLeft, layout.humBaseline); + TFT_print(humid_str, layout.innenLeft, layout.humBaseline); + TFT_print(humid2_str, layout.aussenLeft, layout.humBaseline); TFT_print("%", layout.unitLeft, layout.humBaseline); - TFT_print("1288", layout.innenLeft, layout.pressBaseline); - TFT_print("2563", layout.aussenLeft, layout.pressBaseline); + TFT_print(press_str, layout.innenLeft, layout.pressBaseline); + TFT_print(press2_str, layout.aussenLeft, layout.pressBaseline); TFT_print("hPa", layout.unitLeft, layout.pressBaseline); } @@ -158,9 +181,9 @@ esp_err_t init_display() spi_lobo_device_handle_t spi; spi_lobo_bus_config_t buscfg={ - .miso_io_num=PIN_NUM_MISO, // set SPI MISO pin - .mosi_io_num=PIN_NUM_MOSI, // set SPI MOSI pin - .sclk_io_num=PIN_NUM_CLK, // set SPI CLK pin + .miso_io_num=PIN_NUM_MISO, + .mosi_io_num=PIN_NUM_MOSI, + .sclk_io_num=PIN_NUM_CLK, .quadwp_io_num=-1, .quadhd_io_num=-1, .max_transfer_sz = 6*1024, diff --git a/main/main.c b/main/main.c index e1b64ed..1c5f42e 100644 --- a/main/main.c +++ b/main/main.c @@ -1,6 +1,6 @@ -//#include "bme280.h" -//#include "bme280_defs.h" -//#include "driver/i2c.h" +#include "bme280.h" +#include "bme280_defs.h" +#include "driver/i2c.h" #include "display.h" #include #include @@ -240,123 +240,123 @@ // ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler)); // 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 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) { @@ -458,23 +458,23 @@ void app_main(void) 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); + 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); + 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 @@ -498,6 +498,10 @@ void app_main(void) while (1) { + read_sensor(&dev, &temp, &pressure, &humidity); + read_sensor(&dev2, &temp2, &pressure2, &humidity2); + printf("%i °C, %i hPa, %i %%\r\n", temp, pressure, humidity); + printf("%i °C, %i hPa, %i %%\r\n", temp2, pressure2, humidity2); display_data(temp, pressure, humidity, temp2, pressure2, humidity2); vTaskDelay(1000 / portTICK_PERIOD_MS); }