Hi guys,
I’m using this really nice IPS display (Makerfabs clone 800x480 ESP32-8048S043) Arduino IDE and SquareLine to generate gui (apart from this example, which is stripped back and all included in the ino file).
Everything works including touch.
But when I try to connect to a WiFi network, the display glitches a couple of times like in this video:
No glitching when scanning for networks.
I’ve tried putting the lvgl loop in a separate thread, i’ve tried the WiFi connect routine in a separate thread with different priorities, no change: without fail it will glitch the screen every time I try to connect to a network.
I’ve reduced the BL intensity in case it’s a power issue: I don’t believe it is.
This is mentioned on this blog, which may point to the problem:
Since the RGB Display method uses the ESP32’s PSRAM for his buffer,
slowdowns and flickering may occur.
I’m not experienced enough in the inner workings of frame buffers to figure out how to fix this, do you kind folks have any ideas?
I don’t mind pausing the screen buffering and resuming it while connecting if it means I can hide the glitching. But I’d like to get to the real solution if possible.
Here’s the .ino code, and I’m using a touch.h header file next to the main ino for touch.
#include "WiFi.h"
#include <lvgl.h>
#include <vector>
#include <Arduino_GFX_Library.h>
#define TFT_BL 2
#define GFX_BL DF_GFX_BL // default backlight pin, you may replace DF_GFX_BL to actual backlight pin
/* More data bus class: https://github.com/moononournation/Arduino_GFX/wiki/Data-Bus-Class */
//Arduino_DataBus *bus = create_default_Arduino_DataBus();
/* More display class: https://github.com/moononournation/Arduino_GFX/wiki/Display-Class */
//Arduino_GFX *gfx = new Arduino_ILI9341(bus, DF_GFX_RST, 0 /* rotation */, false /* IPS */);
Arduino_ESP32RGBPanel *bus = new Arduino_ESP32RGBPanel(
GFX_NOT_DEFINED /* CS */, GFX_NOT_DEFINED /* SCK */, GFX_NOT_DEFINED /* SDA */,
40 /* DE */, 41 /* VSYNC */, 39 /* HSYNC */, 42 /* PCLK */,
45 /* R0 */, 48 /* R1 */, 47 /* R2 */, 21 /* R3 */, 14 /* R4 */,
5 /* G0 */, 6 /* G1 */, 7 /* G2 */, 15 /* G3 */, 16 /* G4 */, 4 /* G5 */,
8 /* B0 */, 3 /* B1 */, 46 /* B2 */, 9 /* B3 */, 1 /* B4 */
);
// ST7262 IPS LCD 800x480
Arduino_RPi_DPI_RGBPanel *gfx = new Arduino_RPi_DPI_RGBPanel(
bus,
800 /* width */, 0 /* hsync_polarity */, 8 /* hsync_front_porch */, 4 /* hsync_pulse_width */, 8 /* hsync_back_porch */,
480 /* height */, 0 /* vsync_polarity */, 8 /* vsync_front_porch */, 4 /* vsync_pulse_width */, 8 /* vsync_back_porch */,
1 /* pclk_active_neg */, 12000000 /* prefer_speed */, true /* auto_flush */);
#include "time.h"
const char *ntpServer = "pool.ntp.org";
const long gmtOffset_sec = -8 * 60 * 60; // Set your timezone here
const int daylightOffset_sec = 0;
#include "touch.h"
//#include <FT6236.h>
//#define SDA_FT6236 21
//#define SCL_FT6236 22
//FT6236 ts = FT6236();
typedef enum {
NONE,
NETWORK_SEARCHING,
NETWORK_CONNECTED_POPUP,
NETWORK_CONNECTED,
NETWORK_CONNECT_FAILED
} Network_Status_t;
Network_Status_t networkStatus = NONE;
/*Change to your screen resolution*/
static uint32_t screenWidth;
static uint32_t screenHeight;
static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[800 * 10];
static lv_style_t label_style;
static lv_obj_t *headerLabel;
static lv_disp_drv_t disp_drv;
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 my_touchpad_read(lv_indev_drv_t *indev_driver, lv_indev_data_t *data)
{
if (touch_has_signal())
{
if (touch_touched())
{
data->state = LV_INDEV_STATE_PR;
/*Set the coordinates*/
data->point.x = touch_last_x;
data->point.y = touch_last_y;
}
else if (touch_released())
{
data->state = LV_INDEV_STATE_REL;
}
}
else
{
data->state = LV_INDEV_STATE_REL;
}
}
static lv_style_t border_style;
static lv_style_t popupBox_style;
static lv_obj_t *timeLabel;
static lv_obj_t *settings;
static lv_obj_t *settingBtn;
static lv_obj_t *settingCloseBtn;
static lv_obj_t *settingWiFiSwitch;
static lv_obj_t *wfList;
static lv_obj_t *settinglabel;
static lv_obj_t *mboxConnect;
static lv_obj_t *mboxTitle;
static lv_obj_t *mboxPassword;
static lv_obj_t *mboxConnectBtn;
static lv_obj_t *mboxCloseBtn;
static lv_obj_t *keyboard;
static lv_obj_t *popupBox;
static lv_obj_t *popupBoxCloseBtn;
static lv_timer_t *timer;
static int foundNetworks = 0;
unsigned long networkTimeout = 10 * 1000;
String ssidName, ssidPW;
// TaskHandle_t ntScanTaskHandler, ntConnectTaskHandler;
TaskHandle_t ntScanTaskHandler;
std::vector<String> foundWifiList;
void setup() {
Serial.begin(115200);
gfx->begin();
pinMode(TFT_BL, OUTPUT);
analogWrite(TFT_BL, 100);
lv_init();
delay(10);
touch_init();
screenWidth = gfx->width();
screenHeight = gfx->height();
static lv_color_t *disp_draw_buf;
static lv_disp_drv_t disp_drv;
disp_draw_buf = (lv_color_t *)heap_caps_malloc(sizeof(lv_color_t) * screenWidth * screenHeight/4 , MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
lv_disp_draw_buf_init(&draw_buf, disp_draw_buf, NULL, screenWidth * screenHeight/4);
/* Initialize the display */
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 = &draw_buf;
//disp_drv.rotated = LV_DISP_ROT_270;
lv_disp_drv_register(&disp_drv);
disp_drv.hor_res = 800;
disp_drv.ver_res = 480;
/* Initialize the (dummy) 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);
setStyle();
makeKeyboard();
buildStatusBar();
buildPWMsgBox();
buildBody();
buildSettings();
tryPreviousNetwork();
}
void loop() {
lv_timer_handler();
delay(5);
}
void tryPreviousNetwork() {
//loadWIFICredentialEEPROM();
}
static void setStyle() {
lv_style_init(&border_style);
lv_style_set_border_width(&border_style, 2);
lv_style_set_border_color(&border_style, lv_color_black());
lv_style_init(&popupBox_style);
lv_style_set_radius(&popupBox_style, 10);
lv_style_set_bg_opa(&popupBox_style, LV_OPA_COVER);
lv_style_set_border_color(&popupBox_style, lv_palette_main(LV_PALETTE_BLUE));
lv_style_set_border_width(&popupBox_style, 5);
}
static void buildStatusBar() {
static lv_style_t style_btn;
lv_style_init(&style_btn);
lv_style_set_bg_color(&style_btn, lv_color_hex(0xC5C5C5));
lv_style_set_bg_opa(&style_btn, LV_OPA_50);
lv_obj_t *statusBar = lv_obj_create(lv_scr_act());
lv_obj_set_size(statusBar, 800, 30);
lv_obj_align(statusBar, LV_ALIGN_TOP_MID, 0, 0);
lv_obj_remove_style(statusBar, NULL, LV_PART_SCROLLBAR | LV_STATE_ANY);
timeLabel = lv_label_create(statusBar);
lv_obj_set_size(timeLabel,800 - 50, 30);
lv_label_set_text(timeLabel, "WiFi Not Connected! " LV_SYMBOL_CLOSE);
lv_obj_align(timeLabel, LV_ALIGN_LEFT_MID, 8, 4);
settingBtn = lv_btn_create(statusBar);
lv_obj_set_size(settingBtn, 30, 30);
lv_obj_align(settingBtn, LV_ALIGN_RIGHT_MID, 0, 0);
lv_obj_add_event_cb(settingBtn, btn_event_cb, LV_EVENT_ALL, NULL);
lv_obj_t *label = lv_label_create(settingBtn); /*Add a label to the button*/
lv_label_set_text(label, LV_SYMBOL_SETTINGS); /*Set the labels text*/
lv_obj_center(label);
}
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) {
if (btn == settingBtn) {
lv_obj_clear_flag(settings, LV_OBJ_FLAG_HIDDEN);
} else if (btn == settingCloseBtn) {
lv_obj_add_flag(settings, LV_OBJ_FLAG_HIDDEN);
} else if (btn == mboxConnectBtn) {
ssidPW = String(lv_textarea_get_text(mboxPassword));
beginWIFITask();
//networkConnector();
lv_obj_move_background(mboxConnect);
popupMsgBox("Connecting!", "Attempting to connect to the selected network.");
} else if (btn == mboxCloseBtn) {
lv_obj_move_background(mboxConnect);
} else if (btn == popupBoxCloseBtn) {
lv_obj_move_background(popupBox);
}
} else if (code == LV_EVENT_VALUE_CHANGED) {
if (btn == settingWiFiSwitch) {
if (lv_obj_has_state(btn, LV_STATE_CHECKED)) {
if (ntScanTaskHandler == NULL) {
networkStatus = NETWORK_SEARCHING;
networkScanner();
timer = lv_timer_create(timerForNetwork, 1000, wfList);
lv_list_add_text(wfList, "WiFi: Looking for Networks...");
}
} else {
if (ntScanTaskHandler != NULL) {
networkStatus = NONE;
vTaskDelete(ntScanTaskHandler);
ntScanTaskHandler = NULL;
lv_timer_del(timer);
lv_obj_clean(wfList);
}
if (WiFi.status() == WL_CONNECTED) {
WiFi.disconnect(true);
lv_label_set_text(timeLabel, "WiFi Not Connected! " LV_SYMBOL_CLOSE);
}
}
}
}
}
static void timerForNetwork(lv_timer_t *timer) {
LV_UNUSED(timer);
switch (networkStatus) {
case NETWORK_SEARCHING:
showingFoundWiFiList();
break;
case NETWORK_CONNECTED_POPUP:
popupMsgBox("WiFi Connected!", "Now you'll get the current time soon.");
networkStatus = NETWORK_CONNECTED;
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
break;
case NETWORK_CONNECTED:
showingFoundWiFiList();
updateLocalTime();
break;
case NETWORK_CONNECT_FAILED:
networkStatus = NETWORK_SEARCHING;
popupMsgBox("Oops!", "Please check your wifi password and try again.");
break;
default:
break;
}
}
static void showingFoundWiFiList() {
if (foundWifiList.size() == 0 || foundNetworks == foundWifiList.size())
return;
lv_obj_clean(wfList);
lv_list_add_text(wfList, foundWifiList.size() > 1 ? "WiFi: Found Networks" : "WiFi: Not Found!");
for (std::vector<String>::iterator item = foundWifiList.begin(); item != foundWifiList.end(); ++item) {
lv_obj_t *btn = lv_list_add_btn(wfList, LV_SYMBOL_WIFI, (*item).c_str());
lv_obj_add_event_cb(btn, list_event_handler, LV_EVENT_CLICKED, NULL);
delay(1);
}
foundNetworks = foundWifiList.size();
}
static void buildBody() {
lv_obj_t *bodyScreen = lv_obj_create(lv_scr_act());
lv_obj_add_style(bodyScreen, &border_style, 0);
lv_obj_set_size(bodyScreen, 800, 480 - 34);
lv_obj_align(bodyScreen, LV_ALIGN_BOTTOM_MID, 0, 0);
lv_obj_t *label = lv_label_create(bodyScreen);
lv_label_set_text(label, "Your Contents");
lv_obj_center(label);
}
static void buildSettings() {
settings = lv_obj_create(lv_scr_act());
lv_obj_add_style(settings, &border_style, 0);
lv_obj_set_size(settings, 800 - 100, 480 - 40);
lv_obj_align(settings, LV_ALIGN_TOP_RIGHT, -20, 20);
settinglabel = lv_label_create(settings);
lv_label_set_text(settinglabel, "Settings " LV_SYMBOL_SETTINGS);
lv_obj_align(settinglabel, LV_ALIGN_TOP_LEFT, 0, 0);
settingCloseBtn = lv_btn_create(settings);
lv_obj_set_size(settingCloseBtn, 30, 30);
lv_obj_align(settingCloseBtn, LV_ALIGN_TOP_RIGHT, 0, -10);
lv_obj_add_event_cb(settingCloseBtn, btn_event_cb, LV_EVENT_ALL, NULL);
lv_obj_t *btnSymbol = lv_label_create(settingCloseBtn);
lv_label_set_text(btnSymbol, LV_SYMBOL_CLOSE);
lv_obj_center(btnSymbol);
settingWiFiSwitch = lv_switch_create(settings);
lv_obj_add_event_cb(settingWiFiSwitch, btn_event_cb, LV_EVENT_ALL, NULL);
lv_obj_align_to(settingWiFiSwitch, settinglabel, LV_ALIGN_TOP_RIGHT, 60, -10);
lv_obj_add_flag(settings, LV_OBJ_FLAG_HIDDEN);
wfList = lv_list_create(settings);
lv_obj_set_size(wfList, 800 - 140, 210);
lv_obj_align_to(wfList, settinglabel, LV_ALIGN_TOP_LEFT, 0, 30);
}
static void list_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_CLICKED) {
String selectedItem = String(lv_list_get_btn_text(wfList, obj));
for (int i = 0; i < selectedItem.length() - 1; i++) {
if (selectedItem.substring(i, i + 2) == " (") {
ssidName = selectedItem.substring(0, i);
lv_label_set_text_fmt(mboxTitle, "Selected WiFi SSID: %s", ssidName);
lv_obj_move_foreground(mboxConnect);
break;
}
}
}
}
/*
* NETWORK TASKS
*/
static void networkScanner() {
xTaskCreate(scanWIFITask,
"ScanWIFITask",
4096,
NULL,
1,
&ntScanTaskHandler);
}
//static void networkConnector() {
// xTaskCreate(beginWIFITask,
// "beginWIFITask",
// 4096,
// NULL,
// 1,
// &ntConnectTaskHandler);
//}
static void scanWIFITask(void *pvParameters) {
while (1) {
foundWifiList.clear();
int n = WiFi.scanNetworks();
vTaskDelay(10);
for (int i = 0; i < n; ++i) {
String item = WiFi.SSID(i) + " (" + WiFi.RSSI(i) + ") " + ((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*");
foundWifiList.push_back(item);
vTaskDelay(10);
}
vTaskDelay(5000);
}
}
//void beginWIFITask(void *pvParameters) {
void beginWIFITask() {
unsigned long startingTime = millis();
WiFi.mode(WIFI_STA);
WiFi.disconnect();
//vTaskDelay(100);
WiFi.begin(ssidName.c_str(), ssidPW.c_str());
while (WiFi.status() != WL_CONNECTED && (millis() - startingTime) < networkTimeout) {
//vTaskDelay(250);
}
if (WiFi.status() == WL_CONNECTED) {
networkStatus = NETWORK_CONNECTED_POPUP;
//saveWIFICredentialEEPROM(1, ssidName + " " + ssidPW);
} else {
networkStatus = NETWORK_CONNECT_FAILED;
//saveWIFICredentialEEPROM(0, "");
}
//vTaskDelete(NULL);
}
static void buildPWMsgBox() {
mboxConnect = lv_obj_create(lv_scr_act());
lv_obj_add_style(mboxConnect, &border_style, 0);
lv_obj_set_size(mboxConnect, 800 * 2 / 3, 480 / 2);
lv_obj_center(mboxConnect);
mboxTitle = lv_label_create(mboxConnect);
lv_label_set_text(mboxTitle, "Selected WiFi SSID: ThatProject");
lv_obj_align(mboxTitle, LV_ALIGN_TOP_LEFT, 0, 0);
mboxPassword = lv_textarea_create(mboxConnect);
lv_obj_set_size(mboxPassword, 800 / 2, 40);
lv_obj_align_to(mboxPassword, mboxTitle, LV_ALIGN_TOP_LEFT, 0, 30);
lv_textarea_set_placeholder_text(mboxPassword, "Password?");
lv_obj_add_event_cb(mboxPassword, text_input_event_cb, LV_EVENT_ALL, keyboard);
mboxConnectBtn = lv_btn_create(mboxConnect);
lv_obj_add_event_cb(mboxConnectBtn, btn_event_cb, LV_EVENT_ALL, NULL);
lv_obj_align(mboxConnectBtn, LV_ALIGN_BOTTOM_LEFT, 0, 0);
lv_obj_t *btnLabel = lv_label_create(mboxConnectBtn);
lv_label_set_text(btnLabel, "Connect");
lv_obj_center(btnLabel);
mboxCloseBtn = lv_btn_create(mboxConnect);
lv_obj_add_event_cb(mboxCloseBtn, btn_event_cb, LV_EVENT_ALL, NULL);
lv_obj_align(mboxCloseBtn, LV_ALIGN_BOTTOM_RIGHT, 0, 0);
lv_obj_t *btnLabel2 = lv_label_create(mboxCloseBtn);
lv_label_set_text(btnLabel2, "Cancel");
lv_obj_center(btnLabel2);
}
static void text_input_event_cb(lv_event_t *e) {
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t *ta = lv_event_get_target(e);
if (code == LV_EVENT_FOCUSED) {
lv_obj_move_foreground(keyboard);
lv_keyboard_set_textarea(keyboard, ta);
lv_obj_clear_flag(keyboard, LV_OBJ_FLAG_HIDDEN);
}
if (code == LV_EVENT_DEFOCUSED) {
lv_keyboard_set_textarea(keyboard, NULL);
lv_obj_add_flag(keyboard, LV_OBJ_FLAG_HIDDEN);
}
}
static void makeKeyboard() {
keyboard = lv_keyboard_create(lv_scr_act());
lv_obj_add_flag(keyboard, LV_OBJ_FLAG_HIDDEN);
}
static void popupMsgBox(String title, String msg) {
if (popupBox != NULL) {
lv_obj_del(popupBox);
}
popupBox = lv_obj_create(lv_scr_act());
lv_obj_add_style(popupBox, &popupBox_style, 0);
lv_obj_set_size(popupBox, 800 * 2 / 3, 480 / 2);
lv_obj_center(popupBox);
lv_obj_t *popupTitle = lv_label_create(popupBox);
lv_label_set_text(popupTitle, title.c_str());
lv_obj_set_width(popupTitle, 800 * 2 / 3 - 50);
lv_obj_align(popupTitle, LV_ALIGN_TOP_LEFT, 0, 0);
lv_obj_t *popupMSG = lv_label_create(popupBox);
lv_obj_set_width(popupMSG, 800 * 2 / 3 - 50);
lv_label_set_text(popupMSG, msg.c_str());
lv_obj_align(popupMSG, LV_ALIGN_TOP_LEFT, 0, 40);
popupBoxCloseBtn = lv_btn_create(popupBox);
lv_obj_add_event_cb(popupBoxCloseBtn, btn_event_cb, LV_EVENT_ALL, NULL);
lv_obj_align(popupBoxCloseBtn, LV_ALIGN_BOTTOM_RIGHT, 0, 0);
lv_obj_t *btnLabel = lv_label_create(popupBoxCloseBtn);
lv_label_set_text(btnLabel, "Okay");
lv_obj_center(btnLabel);
}
void updateLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
return;
}
char hourMin[6];
strftime(hourMin, 6, "%H:%M", &timeinfo);
String hourMinWithSymbol = String(hourMin);
hourMinWithSymbol += " ";
hourMinWithSymbol += LV_SYMBOL_WIFI;
lv_label_set_text(timeLabel, hourMinWithSymbol.c_str());
}
touch.h
/*******************************************************************************
* Touch libraries:
* FT6X36: https://github.com/strange-v/FT6X36.git
* GT911: https://github.com/TAMCTec/gt911-arduino.git
* XPT2046: https://github.com/PaulStoffregen/XPT2046_Touchscreen.git
******************************************************************************/
/* uncomment for FT6X36 */
// #define TOUCH_FT6X36
// #define TOUCH_FT6X36_SCL 19
// #define TOUCH_FT6X36_SDA 18
// #define TOUCH_FT6X36_INT 39
// #define TOUCH_SWAP_XY
// #define TOUCH_MAP_X1 480
// #define TOUCH_MAP_X2 0
// #define TOUCH_MAP_Y1 0
// #define TOUCH_MAP_Y2 320
/* uncomment for GT911 */
#define TOUCH_GT911
#define TOUCH_GT911_SCL 20
#define TOUCH_GT911_SDA 19
#define TOUCH_GT911_INT -1
#define TOUCH_GT911_RST 38
#define TOUCH_GT911_ROTATION ROTATION_NORMAL
#define TOUCH_MAP_X1 480
#define TOUCH_MAP_X2 0
#define TOUCH_MAP_Y1 272
#define TOUCH_MAP_Y2 0
/* uncomment for XPT2046 */
// #define TOUCH_XPT2046
// #define TOUCH_XPT2046_SCK 12
// #define TOUCH_XPT2046_MISO 13
// #define TOUCH_XPT2046_MOSI 11
// #define TOUCH_XPT2046_CS 38
// #define TOUCH_XPT2046_INT 18
// #define TOUCH_XPT2046_ROTATION 0
// #define TOUCH_MAP_X1 4000
// #define TOUCH_MAP_X2 100
// #define TOUCH_MAP_Y1 100
// #define TOUCH_MAP_Y2 4000
int touch_last_x = 0, touch_last_y = 0;
#if defined(TOUCH_FT6X36)
#include <Wire.h>
#include <FT6X36.h>
FT6X36 ts(&Wire, TOUCH_FT6X36_INT);
bool touch_touched_flag = true, touch_released_flag = true;
#elif defined(TOUCH_GT911)
#include <Wire.h>
#include <TAMC_GT911.h>
TAMC_GT911 ts = TAMC_GT911(TOUCH_GT911_SDA, TOUCH_GT911_SCL, TOUCH_GT911_INT, TOUCH_GT911_RST, max(TOUCH_MAP_X1, TOUCH_MAP_X2), max(TOUCH_MAP_Y1, TOUCH_MAP_Y2));
#elif defined(TOUCH_XPT2046)
#include <XPT2046_Touchscreen.h>
#include <SPI.h>
XPT2046_Touchscreen ts(TOUCH_XPT2046_CS, TOUCH_XPT2046_INT);
#endif
#if defined(TOUCH_FT6X36)
void touch(TPoint p, TEvent e)
{
if (e != TEvent::Tap && e != TEvent::DragStart && e != TEvent::DragMove && e != TEvent::DragEnd)
{
return;
}
// translation logic depends on screen rotation
#if defined(TOUCH_SWAP_XY)
touch_last_x = map(p.y, TOUCH_MAP_X1, TOUCH_MAP_X2, 0, gfx->width());
touch_last_y = map(p.x, TOUCH_MAP_Y1, TOUCH_MAP_Y2, 0, gfx->height());
#else
touch_last_x = map(p.x, TOUCH_MAP_X1, TOUCH_MAP_X2, 0, gfx->width());
touch_last_y = map(p.y, TOUCH_MAP_Y1, TOUCH_MAP_Y2, 0, gfx->height());
#endif
switch (e)
{
case TEvent::Tap:
Serial.println("Tap");
touch_touched_flag = true;
touch_released_flag = true;
break;
case TEvent::DragStart:
Serial.println("DragStart");
touch_touched_flag = true;
break;
case TEvent::DragMove:
Serial.println("DragMove");
touch_touched_flag = true;
break;
case TEvent::DragEnd:
Serial.println("DragEnd");
touch_released_flag = true;
break;
default:
Serial.println("UNKNOWN");
break;
}
}
#endif
void touch_init()
{
#if defined(TOUCH_FT6X36)
Wire.begin(TOUCH_FT6X36_SDA, TOUCH_FT6X36_SCL);
ts.begin();
ts.registerTouchHandler(touch);
#elif defined(TOUCH_GT911)
Wire.begin(TOUCH_GT911_SDA, TOUCH_GT911_SCL);
ts.begin();
ts.setRotation(TOUCH_GT911_ROTATION);
#elif defined(TOUCH_XPT2046)
SPI.begin(TOUCH_XPT2046_SCK, TOUCH_XPT2046_MISO, TOUCH_XPT2046_MOSI, TOUCH_XPT2046_CS);
ts.begin();
ts.setRotation(TOUCH_XPT2046_ROTATION);
#endif
}
bool touch_has_signal()
{
#if defined(TOUCH_FT6X36)
ts.loop();
return touch_touched_flag || touch_released_flag;
#elif defined(TOUCH_GT911)
return true;
#elif defined(TOUCH_XPT2046)
return ts.tirqTouched();
#else
return false;
#endif
}
bool touch_touched()
{
#if defined(TOUCH_FT6X36)
if (touch_touched_flag)
{
touch_touched_flag = false;
return true;
}
else
{
return false;
}
#elif defined(TOUCH_GT911)
ts.read();
if (ts.isTouched)
{
#if defined(TOUCH_SWAP_XY)
touch_last_x = map(ts.points[0].y, TOUCH_MAP_X1, TOUCH_MAP_X2, 0, gfx->width() - 1);
touch_last_y = map(ts.points[0].x, TOUCH_MAP_Y1, TOUCH_MAP_Y2, 0, gfx->height() - 1);
#else
touch_last_x = map(ts.points[0].x, TOUCH_MAP_X1, TOUCH_MAP_X2, 0, gfx->width() - 1);
touch_last_y = map(ts.points[0].y, TOUCH_MAP_Y1, TOUCH_MAP_Y2, 0, gfx->height() - 1);
#endif
return true;
}
else
{
return false;
}
#elif defined(TOUCH_XPT2046)
if (ts.touched())
{
TS_Point p = ts.getPoint();
#if defined(TOUCH_SWAP_XY)
touch_last_x = map(p.y, TOUCH_MAP_X1, TOUCH_MAP_X2, 0, gfx->width() - 1);
touch_last_y = map(p.x, TOUCH_MAP_Y1, TOUCH_MAP_Y2, 0, gfx->height() - 1);
#else
touch_last_x = map(p.x, TOUCH_MAP_X1, TOUCH_MAP_X2, 0, gfx->width() - 1);
touch_last_y = map(p.y, TOUCH_MAP_Y1, TOUCH_MAP_Y2, 0, gfx->height() - 1);
#endif
return true;
}
else
{
return false;
}
#else
return false;
#endif
}
bool touch_released()
{
#if defined(TOUCH_FT6X36)
if (touch_released_flag)
{
touch_released_flag = false;
return true;
}
else
{
return false;
}
#elif defined(TOUCH_GT911)
return true;
#elif defined(TOUCH_XPT2046)
return true;
#else
return false;
#endif
}
Thanks for any help/pointers!