diff --git a/thermo_sensor/main/thermo_sensor.c b/thermo_sensor/main/thermo_sensor.c index b6c24a5..1968504 100644 --- a/thermo_sensor/main/thermo_sensor.c +++ b/thermo_sensor/main/thermo_sensor.c @@ -1,6 +1,8 @@ +#include #include #include #include +#include #include "nvs_flash.h" #include "esp_http_server.h" @@ -21,17 +23,32 @@ static lv_display_t *sh_display = NULL; static lv_obj_t *label = NULL; bmp280_t dev_bmp_280; -void main_loop(void *pvParameters) { +static void lvgl_tick_cb(void *arg) { + lv_tick_inc(10); +} + +void lvgl_task(void *pvParameters) { + while (1) { + lv_timer_handler(); + vTaskDelay(pdMS_TO_TICKS(10)); + } +} + +void sensor_task(void *pvParameters) { // Wait 1s, to avoid corrupted first sensor readout vTaskDelay(pdMS_TO_TICKS(1000)); while (1) { - lv_timer_handler(); - ygg_bmp280_read_sensor(&dev_bmp_280); + ygg_bmp280_read_sensor(&dev_bmp_280); + // Najpierw OLED, potem (blokujący) HTTP — żeby ekran + // odświeżył się nawet gdy serwer nie odpowiada. + char buf[32]; + snprintf(buf, sizeof(buf), "Temp: %.2f C", + CURR_READOUT.temperature); + ygg_set_display_label(sh_display, label, buf); ygg_send_sensor_readouts(BASE_URL, SENSOR_NAME, CURR_READOUT); vTaskDelay(pdMS_TO_TICKS(10000)); - ygg_set_display_label(sh_display, label, "ANOTHER"); } } @@ -65,11 +82,22 @@ void app_main() ESP_ERROR_CHECK(i2cdev_init()); sh_display = ygg_sh1106_init(); + // LVGL potrzebuje regularnego ticka, inaczej redraw nigdy się nie wykona + // (to był powód, dla którego wisiało "FIRST"). + const esp_timer_create_args_t lvgl_tick_args = { + .callback = lvgl_tick_cb, + .name = "lvgl_tick", + }; + esp_timer_handle_t lvgl_tick_handle = NULL; + ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_args, &lvgl_tick_handle)); + ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_handle, 10 * 1000)); + lv_obj_t *scr = lv_display_get_screen_active(sh_display); label = lv_label_create(scr); ygg_set_display_label(sh_display, label, "FIRST"); ygg_bmp280_init(&dev_bmp_280); - xTaskCreatePinnedToCore(main_loop, "bmp280_test", configMINIMAL_STACK_SIZE * 8, NULL, 5, NULL, APP_CPU_NUM); + xTaskCreatePinnedToCore(lvgl_task, "lvgl", 4096, NULL, 4, NULL, APP_CPU_NUM); + xTaskCreatePinnedToCore(sensor_task, "bmp280_test", 8192, NULL, 5, NULL, APP_CPU_NUM); } diff --git a/thermo_sensor/main/ygg_sh1106.c b/thermo_sensor/main/ygg_sh1106.c index 48a746f..d101c77 100644 --- a/thermo_sensor/main/ygg_sh1106.c +++ b/thermo_sensor/main/ygg_sh1106.c @@ -53,12 +53,8 @@ static void example_lvgl_flush_cb(lv_display_t *disp, const lv_area_t *area, uin } } // pass the draw buffer to the driver - ESP_LOGI("FLUSH", - "area: (%d,%d)-(%d,%d), display: %dx%d", - x1, y1, x2, y2, - lv_display_get_horizontal_resolution(disp), - lv_display_get_vertical_resolution(disp)); esp_lcd_panel_draw_bitmap(panel_handle, x1, y1, x2 + 1, y2 + 1, oled_buffer); + lv_display_flush_ready(disp); } lv_display_t* ygg_sh1106_init() { @@ -139,9 +135,10 @@ lv_display_t* ygg_sh1106_init() { return display; } -void ygg_set_display_label(lv_display_t *display, lv_obj_t *label, char *str) { +void ygg_set_display_label(lv_display_t *display, lv_obj_t *label, const char *str) { lv_label_set_text(label, str); /* Size of the screen (if you use rotation 90 or 270, please use lv_display_get_vertical_resolution) */ lv_obj_set_width(label, lv_display_get_horizontal_resolution(display)); + lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, 0); lv_obj_align(label, LV_ALIGN_TOP_MID, 0, 0); } diff --git a/thermo_sensor/main/ygg_sh1106.h b/thermo_sensor/main/ygg_sh1106.h index c811699..3c9ad5c 100644 --- a/thermo_sensor/main/ygg_sh1106.h +++ b/thermo_sensor/main/ygg_sh1106.h @@ -4,6 +4,6 @@ #include "lvgl.h" lv_display_t* ygg_sh1106_init(); -void ygg_set_display_label(lv_display_t *display, lv_obj_t *label, char *str); +void ygg_set_display_label(lv_display_t *display, lv_obj_t *label, const char *str); #endif