From e8c2db5642dd63e4fc419549c1d9445efe121361 Mon Sep 17 00:00:00 2001 From: cosmicpsyop Date: Wed, 29 May 2024 14:35:49 -0700 Subject: [PATCH] t-hmi add touchhandler class --- src/TouchHandler.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++ src/TouchHandler.h | 30 ++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 src/TouchHandler.cpp create mode 100644 src/TouchHandler.h diff --git a/src/TouchHandler.cpp b/src/TouchHandler.cpp new file mode 100644 index 0000000..b45f141 --- /dev/null +++ b/src/TouchHandler.cpp @@ -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; +} diff --git a/src/TouchHandler.h b/src/TouchHandler.h new file mode 100644 index 0000000..4d3821c --- /dev/null +++ b/src/TouchHandler.h @@ -0,0 +1,30 @@ +#ifndef _TOUCHHANDLER_H_ +#define _TOUCHHANDLER_H_ + +#include // TFT display library +#include // 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 \ No newline at end of file