Description
I’m trying to display a sensor data in a display with LVGL
What MCU/Processor/Board and compiler are you using?
ESP32 NodeMCU and ILI9341 and Arduino IDE
What LVGL version are you using?
v.7
What do you want to achieve?
Update sensor data every 2 seconds and learn how to use the library
What have you tried so far?
Try to set the text in of a label in the loop() with the sensor data but doesn’t appear nothing in the tft
Code to reproduce
#include <lvgl.h>
#include <TFT_eSPI.h>
#include <Wire.h>
#include <string.h>.
#include "SparkFun_SCD30_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_SCD30
SCD30 airSensor;
TFT_eSPI tft = TFT_eSPI(); /* TFT instance */
static lv_disp_buf_t disp_buf;
static lv_color_t buf[LV_HOR_RES_MAX * 10];
static lv_obj_t * bg_top;
static lv_obj_t * bg_middle1;
static lv_obj_t * bg_bottom;
static lv_obj_t * label_status;
static lv_obj_t * label_co2;
static lv_obj_t * label_temp;
static lv_obj_t * label_hum;
#if USE_LV_LOG != 0
/* Serial debugging */
void my_print(lv_log_level_t level, const char * file, uint32_t line, const char * dsc)
{
Serial.printf("%s@%d->%s\r\n", file, line, dsc);
Serial.flush();
}
#endif
/* Display flushing */
void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p)
{
uint32_t w = (area->x2 - area->x1 + 1);
uint32_t h = (area->y2 - area->y1 + 1);
tft.startWrite();
tft.setAddrWindow(area->x1, area->y1, w, h);
tft.pushColors(&color_p->full, w * h, true);
tft.endWrite();
lv_disp_flush_ready(disp);
}
/* Reading input device (simulated encoder here) */
bool read_encoder(lv_indev_drv_t * indev, lv_indev_data_t * data)
{
static int32_t last_diff = 0;
int32_t diff = 0; /* Dummy - no movement */
int btn_state = LV_INDEV_STATE_REL; /* Dummy - no press */
data->enc_diff = diff - last_diff;;
data->state = btn_state;
last_diff = diff;
return false;
}
void setup()
{
pinMode(15, OUTPUT);
digitalWrite(15, LOW);
delay(1);
Serial.begin(115200); /* prepare for possible serial debug */
airSensor.begin();
delay(100);
airSensor.setMeasurementInterval(2);
Serial.println("SCD30 Example");
Wire.begin(25,26);
lv_init();
#if USE_LV_LOG != 0
lv_log_register_print_cb(my_print); /* register print function for debugging */
#endif
tft.begin(); /* TFT init */
tft.setRotation(2); /* Landscape orientation */
lv_disp_buf_init(&disp_buf, buf, NULL, LV_HOR_RES_MAX * 10);
/*Initialize the display*/
lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv);
disp_drv.hor_res = 240;
disp_drv.ver_res = 320;
disp_drv.flush_cb = my_disp_flush;
disp_drv.buffer = &disp_buf;
lv_disp_drv_register(&disp_drv);
/*Initialize the (dummy) input device driver*/
lv_indev_drv_t indev_drv;
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_ENCODER;
indev_drv.read_cb = read_encoder;
lv_indev_drv_register(&indev_drv);
lv_main();
}
void loop()
{
lv_task_handler(); /* let the GUI do its work */
delay(5);
String co2 = String(airSensor.getCO2());
String temp = String(airSensor.getTemperature());
String hum = String(airSensor.getHumidity());
int c = co2.length();
int t = temp.length();
int h = hum.length();
char co2C[c+1];
char tempC[t+1];
char humC[h+ 1];
strcpy(co2C, co2.c_str());
strcpy(tempC, temp.c_str());
strcpy(humC, hum.c_str());
lv_label_set_text(label_co2, co2C);
lv_label_set_text(label_temp, tempC);
lv_label_set_text(label_hum, humC);
delay(2000);
}
static void lv_main(){
//LV_THEME_MATERIAL_FLAG_LIGHT
//LV_THEME_MATERIAL_FLAG_DARK
lv_theme_t * th = lv_theme_material_init(LV_THEME_DEFAULT_COLOR_PRIMARY, LV_THEME_DEFAULT_COLOR_SECONDARY, LV_THEME_MATERIAL_FLAG_LIGHT, LV_THEME_DEFAULT_FONT_SMALL , LV_THEME_DEFAULT_FONT_NORMAL, LV_THEME_DEFAULT_FONT_SUBTITLE, LV_THEME_DEFAULT_FONT_TITLE);
lv_theme_set_act(th);
lv_obj_t * scr = lv_obj_create(NULL, NULL);
lv_scr_load(scr);
bg_top = lv_obj_create(scr, NULL);
lv_obj_clean_style_list(bg_top, LV_OBJ_PART_MAIN);
lv_obj_set_style_local_bg_opa(bg_top, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT,LV_OPA_COVER);
lv_obj_set_style_local_bg_color(bg_top, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT,LV_COLOR_NAVY);
lv_obj_set_size(bg_top, LV_HOR_RES, 50);
bg_middle1 = lv_obj_create(scr, NULL);
lv_obj_clean_style_list(bg_middle1, LV_OBJ_PART_MAIN);
lv_obj_set_style_local_bg_opa(bg_middle1, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT,LV_OPA_COVER);
lv_obj_set_style_local_bg_color(bg_middle1, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT,LV_COLOR_WHITE);
lv_obj_set_pos(bg_middle1, 0, 50);
lv_obj_set_size(bg_middle1, LV_HOR_RES, 240);
label_co2 = lv_label_create(bg_middle1, NULL);
lv_label_set_long_mode(label_co2, LV_LABEL_LONG_SROLL_CIRC);
lv_obj_set_width(label_co2, LV_HOR_RES - 20);
lv_label_set_text(label_co2, "");
lv_obj_align(label_co2, NULL,LV_LABEL_ALIGN_LEFT, 10, 20);
label_temp = lv_label_create(bg_middle1, NULL);
lv_label_set_long_mode(label_temp, LV_LABEL_LONG_SROLL_CIRC);
lv_obj_set_width(label_temp, LV_HOR_RES - 20);
lv_label_set_text(label_temp, "");
lv_obj_align(label_temp, NULL,LV_LABEL_ALIGN_LEFT, 10, 40);
label_hum = lv_label_create(bg_middle1, NULL);
lv_label_set_long_mode(label_hum, LV_LABEL_LONG_SROLL_CIRC);
lv_obj_set_width(label_hum, LV_HOR_RES - 20);
lv_label_set_text(label_hum, "");
lv_obj_align(label_temp, NULL,LV_LABEL_ALIGN_LEFT, 10, 60);
bg_bottom = lv_obj_create(scr, NULL);
lv_obj_clean_style_list(bg_bottom, LV_OBJ_PART_MAIN);
lv_obj_set_style_local_bg_opa(bg_bottom, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT,LV_OPA_COVER);
lv_obj_set_style_local_bg_color(bg_bottom, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT,LV_COLOR_ORANGE);
lv_obj_set_pos(bg_bottom, 0, 290);
lv_obj_set_size(bg_bottom, LV_HOR_RES, 30);
label_status = lv_label_create(bg_bottom, NULL);
lv_label_set_long_mode(label_status, LV_LABEL_LONG_SROLL_CIRC);
lv_obj_set_width(label_status, LV_HOR_RES - 20);
lv_label_set_text(label_status, "");
lv_obj_align(label_status, NULL, LV_ALIGN_CENTER, 0, 0);
LV_IMG_DECLARE(logo);
lv_obj_t * img_src = lv_img_create(lv_scr_act(), NULL); /*Crate an image object*/
lv_img_set_src(img_src, &logo); /*Set the created file as image (a red fl ower)*/
lv_obj_set_pos(img_src, 45, 5); /*Set the positions*/
lv_obj_set_drag(img_src, true);
}