#include <lvgl.h>
#include <TFT_eSPI.h>
#include <ui.h> // Auto-generated by Squareline Studio
/* Change to your screen resolution */
static const uint16_t screenWidth = 320;
static const uint16_t screenHeight = 240;
static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[screenWidth * screenHeight / 10];
TFT_eSPI tft = TFT_eSPI(screenWidth, screenHeight); /* TFT instance */
/* Define LED Pin */
#define LED_PIN 2
#if LV_USE_LOG != 0
/* Serial debugging */
void my_print(const char *buf)
{
Serial.printf(buf);
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((uint16_t *)&color_p->full, w * h, true);
tft.endWrite();
lv_disp_flush_ready(disp);
}
/* Read the touchpad */
/* Read the touchpad */
void my_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data) {
// Simulate touch interaction
static bool toggle_touch = false; // A flag to simulate touch press/release
static uint16_t touchX = 100; // Simulated X coordinate
static uint16_t touchY = 75; // Simulated Y coordinate
// Toggle touch state every loop to mimic touch press and release
toggle_touch = !toggle_touch;
if (toggle_touch) {
data->state = LV_INDEV_STATE_PR; // Press state
data->point.x = touchX; // Simulated X coordinate
data->point.y = touchY; // Simulated Y coordinate
} else {
data->state = LV_INDEV_STATE_REL; // Release state
}
// Print the simulated coordinates for debugging
Serial.print("Simulated Touch: State = ");
Serial.print(toggle_touch ? "PRESSED" : "RELEASED");
if (toggle_touch) {
Serial.print(", X = ");
Serial.print(touchX);
Serial.print(", Y = ");
Serial.println(touchY);
} else {
Serial.println();
}
}
void setup()
{
Serial.begin(115200); // Initialize Serial Monitor
/* Initialize LED Pin */
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW); // Start with LED OFF
Serial.println("Initializing LVGL and Display");
lv_init();
#if LV_USE_LOG != 0
lv_log_register_print_cb(my_print); /* Register print function for debugging */
#endif
/* Initialize TFT */
tft.begin();
tft.setRotation(3); /* Landscape orientation, flipped */
/* Initialize LVGL display buffer */
lv_disp_draw_buf_init(&draw_buf, buf, NULL, screenWidth * screenHeight / 10);
/* Initialize the display 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 = my_disp_flush;
disp_drv.draw_buf = &draw_buf;
lv_disp_drv_register(&disp_drv);
/* Initialize the touch 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 = my_touchpad_read;
lv_indev_drv_register(&indev_drv);
/* Initialize the GUI from Squareline Studio */
ui_init();
Serial.println("Setup complete!");
}
void loop()
{
lv_timer_handler(); /* Let LVGL do its work */
delay(5); // Small delay for stability
}
Loading
ili9341-cap-touch
ili9341-cap-touch