t-hmi add touchhandler class

This commit is contained in:
cosmicpsyop 2024-05-29 14:35:49 -07:00
parent e8ee3b80c2
commit e8c2db5642
2 changed files with 96 additions and 0 deletions

66
src/TouchHandler.cpp Normal file
View File

@ -0,0 +1,66 @@
#include "drivers/displays/display.h"
#include "TouchHandler.h"
// Global variable declaration
extern unsigned int lowerScreen;
TouchHandler::~TouchHandler() {
}
TouchHandler::TouchHandler(TFT_eSPI& tft, uint8_t csPin, uint8_t irqPin, SPIClass& spi)
: tft(tft), csPin(csPin), irqPin(irqPin), spi(spi), lastTouchTime(0),
screenSwitchCallback(nullptr), touch(spi, csPin, irqPin) {
}
void TouchHandler::begin(uint16_t xres, uint16_t yres) {
spi.begin(TOUCH_CLK, TOUCH_MISO, TOUCH_MOSI);
touch.begin(xres, yres);
}
void TouchHandler::setScreenSwitchCallback(void (*callback)()) {
screenSwitchCallback = callback;
}
uint16_t TouchHandler::isTouched() {
// XXX - move touch_x, touch_y to private and min_x, min_y,max_x, max_y
uint16_t touch_x, touch_y, code = 0;
if (touch.pressed()) {
touch_x = touch.RawX();
touch_y = touch.RawY();
// Perform actions based on touch coordinates
// if (y < y_min + (y_max - y_min) / 4) {
if (touch_x < 200 + (1700 - 200) / 4) {
// bottom
code = 1;
if (debounce())
lowerScreen = 3 - lowerScreen;
} else {
// top
code = 2;
if (debounce() && screenSwitchCallback) {
screenSwitchCallback();
}
}
if (code) {
if (code == 1)
Serial.print("Touch bottom\n");
else
Serial.print("Touch top\n");
}
}
return code;
}
bool TouchHandler::debounce() {
unsigned long currentTime = millis();
if (currentTime - lastTouchTime >= 2000) {
lastTouchTime = currentTime;
return true;
}
return false;
}

30
src/TouchHandler.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef _TOUCHHANDLER_H_
#define _TOUCHHANDLER_H_
#include <TFT_eSPI.h> // TFT display library
#include <xpt2046.h> // https://github.com/liangyingy/arduino_xpt2046_library
class TouchHandler {
public:
TouchHandler();
~TouchHandler();
TouchHandler(TFT_eSPI& tft, uint8_t csPin, uint8_t irqPin, SPIClass& spi);
void begin(uint16_t xres, uint16_t yres);
uint16_t isTouched();
void setScreenSwitchCallback(void (*callback)());
private:
bool debounce();
TFT_eSPI& tft;
XPT2046 touch;
uint8_t csPin;
uint8_t irqPin;
SPIClass& spi;
unsigned long lastTouchTime;
// unsigned int lower_switch;
void (*screenSwitchCallback)();
};
#endif