Created
April 11, 2024 20:21
-
-
Save ricklon/1ce0a31fc2882479414a7186c33fff72 to your computer and use it in GitHub Desktop.
Working t-display-s3 touch code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <TFT_eSPI.h> | |
#include <Wire.h> | |
#define TOUCH_MODULES_CST_SELF | |
#include <TouchLib.h> | |
#define PIN_IIC_SDA 18 | |
#define PIN_IIC_SCL 17 | |
#define PIN_TOUCH_RES 16 | |
#define PIN_POWER_ON 15 | |
#define PIN_LCD_BL 38 | |
TouchLib touch(Wire, PIN_IIC_SDA, PIN_IIC_SCL, CTS820_SLAVE_ADDRESS, PIN_TOUCH_RES); | |
TFT_eSPI tft = TFT_eSPI(); | |
const int keyWidth = 50; | |
const int keyHeight = 40; | |
const int keySpacing = 10; | |
const int keysPerRow = 3; | |
const int offsetX = (170 - (keyWidth * keysPerRow + keySpacing * (keysPerRow - 1))) / 2; | |
const int offsetY = (320 - (keyHeight * 4 + keySpacing * 3)) / 2; | |
unsigned long lastTouchTime = 0; | |
const int debounceDelay = 200; | |
void setup() { | |
pinMode(PIN_POWER_ON, OUTPUT); | |
digitalWrite(PIN_POWER_ON, HIGH); | |
pinMode(PIN_TOUCH_RES, OUTPUT); | |
digitalWrite(PIN_TOUCH_RES, LOW); | |
delay(500); | |
digitalWrite(PIN_TOUCH_RES, HIGH); | |
Serial.begin(115200); | |
tft.begin(); | |
tft.setRotation(0); | |
tft.fillScreen(TFT_BLACK); | |
ledcSetup(0, 2000, 8); | |
ledcAttachPin(PIN_LCD_BL, 0); | |
ledcWrite(0, 255); | |
Wire.begin(PIN_IIC_SDA, PIN_IIC_SCL); | |
touch.init(); | |
drawKeypad(); | |
} | |
void loop() { | |
if (touch.read()) { | |
uint8_t n = touch.getPointNum(); | |
for (uint8_t i = 0; i < n; i++) { | |
TP_Point t = touch.getPoint(i); | |
int keyNumber = getKeyNumber(t.x, t.y); | |
if (keyNumber != -1 && millis() - lastTouchTime > debounceDelay) { | |
Serial.println(keyNumber); | |
lastTouchTime = millis(); | |
} | |
} | |
} | |
delay(50); | |
} | |
void drawKeypad() { | |
tft.fillScreen(TFT_BLACK); | |
for (int i = 1; i <= 9; i++) { | |
int row = (i - 1) / keysPerRow; | |
int col = (i - 1) % keysPerRow; | |
int x = offsetX + col * (keyWidth + keySpacing); | |
int y = offsetY + row * (keyHeight + keySpacing); | |
tft.fillRect(x, y, keyWidth, keyHeight, TFT_DARKGREY); | |
tft.drawRect(x, y, keyWidth, keyHeight, TFT_WHITE); | |
tft.setTextColor(TFT_WHITE); | |
tft.setTextSize(2); | |
tft.setCursor(x + keyWidth / 2 - 5, y + keyHeight / 2 - 10); | |
tft.print(i); | |
} | |
// Draw the '0' key | |
int x = offsetX + keysPerRow * (keyWidth + keySpacing) / 2 - keyWidth / 2; | |
int y = offsetY + 3 * (keyHeight + keySpacing); | |
tft.fillRect(x, y, keyWidth, keyHeight, TFT_DARKGREY); | |
tft.drawRect(x, y, keyWidth, keyHeight, TFT_WHITE); | |
tft.setTextColor(TFT_WHITE); | |
tft.setTextSize(2); | |
tft.setCursor(x + keyWidth / 2 - 5, y + keyHeight / 2 - 10); | |
tft.print("0"); | |
} | |
int getKeyNumber(int x, int y) { | |
int col = (x - offsetX) / (keyWidth + keySpacing); | |
int row = (y - offsetY) / (keyHeight + keySpacing); | |
if (col >= 0 && col < keysPerRow && row >= 0 && row < 4) { | |
if (row == 3) { | |
return 0; | |
} else { | |
return row * keysPerRow + col + 1; | |
} | |
} | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment