I am trying to read data from the I2C bus. I am using a wt32-sc01 board which has a touchscreen with ESP32 on the back. I am using lvgl 8.1.0 for this project. I wanted to read i2c data (from an adafruit ADS1X15) and show them on my screen. I basically have a simple function to read i2c (readInp), no matter where I put the code, it freezes the LCD. I have tried to put it in the main loop, the screen froze. I tried putting it in the lv_main, the screen froze.
The code block(s) should be formatted like:
void setup() {
Serial.begin(115200); /* prepare for possible serial debug */
lcd.init(); // Initialize LovyanGFX
lv_init(); // Initialize lvgl
Wire.begin(); // Wire communication begin
if (!ads.begin(0x48)) {
Serial.println("Failed to initialize ADS.");
}
// Setting display to landscape
if (lcd.width() < lcd.height())
lcd.setRotation(0);
/* LVGL : Setting up buffer to use for display */
lv_disp_draw_buf_init(&draw_buf, buf, NULL, screenWidth * 10);
/*** LVGL : Setup & Initialize the display device driver ***/
static lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv);
disp_drv.hor_res = screenWidth;
disp_drv.ver_res = screenHeight;
disp_drv.flush_cb = display_flush;
disp_drv.draw_buf = &draw_buf;
lv_disp_drv_register(&disp_drv);
/*** LVGL : Setup & Initialize the input device driver ***/
static lv_indev_drv_t indev_drv;
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.read_cb = touchpad_read;
lv_indev_drv_register(&indev_drv);
/*** Create simple label and show LVGL version ***/
String LVGL_Arduino = "WT32-SC01 with LVGL ";
LVGL_Arduino += String('v') + lv_version_major() + "." + lv_version_minor() + "." + lv_version_patch();
lv_obj_t *label = lv_label_create(lv_scr_act()); // full screen as the parent
lv_label_set_text(label, LVGL_Arduino.c_str()); // set label text
lv_obj_align(label, LV_ALIGN_TOP_MID, 0, 20); // Center but 20 from the top
lv_main();
}
void loop() {
lv_timer_handler(); /* let the GUI do its work */
delay(5);
}
/*** Display callback to flush the buffer to screen ***/
void display_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);
lcd.startWrite();
lcd.setAddrWindow(area->x1, area->y1, w, h);
lcd.pushColors((uint16_t *)&color_p->full, w * h, true);
lcd.endWrite();
lv_disp_flush_ready(disp);
}
/*** Touchpad callback to read the touchpad ***/
void touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data)
{
uint16_t touchX, touchY;
bool touched = lcd.getTouch(&touchX, &touchY);
if (!touched)
{
data->state = LV_INDEV_STATE_REL;
}
else
{
data->state = LV_INDEV_STATE_PR;
/*Set the coordinates*/
data->point.x = touchX;
data->point.y = touchY;
Serial.printf("Touch (x,y): (%03d,%03d)\n",touchX,touchY );
}
}
static void event_handler(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * obj = lv_event_get_target(e);
if(code == LV_EVENT_VALUE_CHANGED) {
LV_LOG_USER("State: %s\n", lv_obj_has_state(obj, LV_STATE_CHECKED) ? "On" : "Off");
}
}
void readInp(void)
{
int16_t joy0;
int16_t adc0, adc1, adc2, adc3;
adc0 = ads.readADC_SingleEnded(0);
//Serial.print("AIN0: "); Serial.println(adc0);
joy0 = map(adc0, 0, 270, 50, 1800);
Serial.println(joy0);
}
void lv_main(void)
{
lv_obj_set_flex_flow(lv_scr_act(), LV_FLEX_FLOW_COLUMN);
lv_obj_align(lv_scr_act(), LV_ALIGN_DEFAULT,0, 0);
lv_obj_t * sw;
sw = lv_switch_create(lv_scr_act());
lv_obj_add_event_cb(sw, event_handler, LV_EVENT_ALL, NULL);
sw = lv_switch_create(lv_scr_act());
lv_obj_add_state(sw, LV_STATE_CHECKED);
lv_obj_add_event_cb(sw, event_handler, LV_EVENT_ALL, NULL);
}