#include #include "HWCDC.h" #include "lv_conf.h" #include "Touch_CST328.h" #include "elements.h" #include "Arduino_GFX_Library.h" #define LCD_DC 41 #define LCD_CS 42 #define LCD_SCK 40 #define LCD_MOSI 45 #define LCD_RST 39 #define LCD_BL 5 #define LCD_WIDTH 240 #define LCD_HEIGHT 320 #define tickPeriod 2 lv_indev_t* indev; lv_disp_t* disp; CST328_Touch touch; Arduino_DataBus *bus = new Arduino_ESP32SPI(LCD_DC, LCD_CS, LCD_SCK, LCD_MOSI); Arduino_GFX *gfx = new Arduino_ST7789 (bus, LCD_RST, 0, true, LCD_WIDTH, LCD_HEIGHT,0,0,0,0); uint16_t touchX[5] = {0}; uint16_t touchY[5] = {0}; uint16_t strength[5] = {0}; uint8_t touchpad_cnt = 0; uint32_t screenWidth, screenHeight; static uint8_t count = 0; static lv_disp_draw_buf_t drawBuf; 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); #if (LV_COLOR_16_SWAP != 0) gfx->draw16bitBeRGBBitmap(area->x1, area->y1, (uint16_t *)&color_p->full, w, h); #else gfx->draw16bitRGBBitmap(area->x1, area->y1, (uint16_t *)&color_p->full, w, h); #endif lv_disp_flush_ready(disp); } void example_increase_lvgl_tick(void *arg) { /* Tell LVGL how many milliseconds has elapsed */ lv_tick_inc(tickPeriod); } void example_increase_reboot(void *arg) { count++; if (count == 30) { esp_restart(); } } #if LV_USE_LOG != 0 /* Serial debugging */ void my_print(const char *buf) { Serial.printf(buf); Serial.flush(); } #endif void readTouch(lv_indev_drv_t * indev_drv, lv_indev_data_t * data){ uint16_t touchpad_x[5] = {0}; uint16_t touchpad_y[5] = {0}; uint16_t strength[5] = {0}; uint8_t touchpad_cnt = 0; Touch_Read_Data(); uint8_t touchpad_pressed = Touch_Get_XY(touchpad_x, touchpad_y, strength, &touchpad_cnt, CST328_LCD_TOUCH_MAX_POINTS); if (touchpad_pressed && touchpad_cnt > 0) { data->point.x = touchpad_x[0]; data->point.y = touchpad_y[0]; data->state = LV_INDEV_STATE_PR; printf("Touch : X=%u Y=%u num=%d\r\n", touchpad_x[0], touchpad_y[0],touchpad_cnt); } else { data->state = LV_INDEV_STATE_REL; } } static void btn_event_cb(lv_event_t * e) { lv_event_code_t code = lv_event_get_code(e); lv_obj_t * btn = lv_event_get_target(e); if(code == LV_EVENT_CLICKED) { static uint8_t cnt = 0; cnt++; /*Get the first child of the button which is the label and change its text*/ lv_obj_t * label = lv_obj_get_child(btn, 0); lv_label_set_text_fmt(label, "Button: %d", cnt); } } void createButton() { lv_obj_t * btn = lv_btn_create(lv_scr_act()); /*Add a button the current screen*/ lv_obj_set_pos(btn, 10, 10); /*Set its position*/ lv_obj_set_size(btn, 120, 50); lv_obj_add_event_cb(btn, btn_event_cb, LV_EVENT_ALL, NULL); /*Set its size*/ lv_obj_t * label = lv_label_create(btn); /*Add a label to the button*/ lv_label_set_text(label, "Button"); /*Set the labels text*/ lv_obj_center(label); } void setup() { Serial.begin(115200); Touch_Init(); USBSerial.begin(115200); /* prepare for possible serial debug */ gfx->begin(); pinMode(LCD_BL, OUTPUT); digitalWrite(LCD_BL, HIGH); screenWidth = gfx->width(); screenHeight = gfx->height(); lv_init(); lv_color_t *buf1 = (lv_color_t *)heap_caps_malloc(screenWidth * screenHeight / 4 * sizeof(lv_color_t), MALLOC_CAP_DMA); lv_color_t *buf2 = (lv_color_t *)heap_caps_malloc(screenWidth * screenHeight / 4 * sizeof(lv_color_t), MALLOC_CAP_DMA); #if LV_USE_LOG != 0 lv_log_register_print_cb(my_print); /* register print function for debugging */ #endif lv_disp_draw_buf_init(&drawBuf, buf1, buf2, screenWidth * screenHeight / 4); static lv_disp_drv_t disp_drv; lv_disp_drv_init(&disp_drv); /*Change the following line to your display resolution*/ disp_drv.hor_res = screenWidth; disp_drv.ver_res = screenHeight; disp_drv.flush_cb = my_disp_flush; disp_drv.draw_buf = &drawBuf; lv_disp_drv_register(&disp_drv); const esp_timer_create_args_t lvgl_tick_timer_args = { .callback = &example_increase_lvgl_tick, .name = "lvgl_tick" }; const esp_timer_create_args_t reboot_timer_args = { .callback = &example_increase_reboot, .name = "reboot" }; createButton(); lv_indev_drv_t indev_drv; lv_indev_drv_init(&indev_drv); // Initialize the input device driver indev_drv.type = LV_INDEV_TYPE_POINTER; indev_drv.read_cb = readTouch; // Link the custom read function for touch input lv_indev_drv_register(&indev_drv); } void loop() { lv_timer_handler(); /* let the GUI do its work */ delay(5); }