Hello, i am trying to make some project with TFT touch display ST7796 and i have there buttons, it is like multiple page tablet or something like that. When i press and hold the buttons i want them to change the page just once, but when i hold them it skips through every page very quickly.
I think it's because of my display, i am not really sure because when i tried btn.justPressed and btn.justReleased and held finger on button it said in serial monitor repeatedly that it was pressed and released even though it should say it only once.
I tried making latching code for it but because of what it did with btn.justPressed and btn.justReleased it didn't work so last thing I tried was IRQ pin on my display. It made it work, but the thing is when I try to press the button i have to press it multiple times to make it work, it's like because of the IRQ logic it only finds the position of the button once and if it doesn't match it wont work unless i try it multiple times.
I am stuck on this for hours and i can't seem to figure it out myself so i came here.
If there are some questions to clarify my issue I can answer them. Sorry if something didn't make sense but I am just learning about these things and this is my first time making bigger project with this kind of display.
The code i will show is missing #include of TFT_espi library and declaring those buttons and the display, but it is only because I am using Lopaka for design and i have very long variables for some images, also there might be some variables that are unnecessary but i don't want to change them because i want solve some problems my way and even if it is not the correct way i don't really care right now, my only problem is with thoes buttons and I really can't solve that problem.
int value = 1000;
int oldValue = 1000;
int screen = 0;
int oldScreen = 0;
int pocasi = 0;
int oldPocasi = 0;
volatile bool touchFlag = false;
uint16_t calData[5] = { 292, 3663, 214, 3630, 7 };
bool btnPressed = false;
bool btn2Pressed = false;
#define IRQ_PIN 33
void IRAM_ATTR onTouchIRQ() {
touchFlag = true;
}
void setup() {
Serial.begin(9600);
tft.init();
tft.setRotation(1);
tft.fillScreen(0x0);
tft.setTouch(calData);
btn.initButton(&tft, 450 , 165, 70, 320, TFT_BLACK, TFT_BLACK, TFT_BLACK, "", 1);
btn.drawButton();
btn2.initButton(&tft, 20 , 165, 50, 320, TFT_BLACK, TFT_BLACK, TFT_BLACK, "", 1);
btn2.drawButton();
tft.pushImage(0, 32, 20, 256, image_download_3_pixels);
tft.pushImage(460, 32, 20, 256, image_download_copy_pixels);
pinMode(IRQ_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(IRQ_PIN), onTouchIRQ, FALLING);
screen0();
}
void loop() {
setScreen();
}
void setScreen(){
if(touchFlag){
uint16_t x = 0, y = 0;
bool touch = tft.getTouch(&x, &y);
if(touch){
if(btn.contains(x, y)){
if(!btnPressed){
if(screen < 2){
screen++;
}
btnPressed = true;
}
} else {
btnPressed = false;
}
// Tlačítko 2
if(btn2.contains(x, y)){
if(!btn2Pressed){
if(screen > 0){
screen--;
}
btn2Pressed = true;
}
} else {
btn2Pressed = false;
}
} else {
btnPressed = false;
btn2Pressed = false;
}
touchFlag = false;
}
if(oldScreen != screen){
switch(screen){
case 0:
tft.fillScreen(TFT_BLACK);
screen0();
btn.drawButton();
btn2.drawButton();
tft.pushImage(0, 32, 20, 256, image_download_3_pixels);
tft.pushImage(460, 32, 20, 256, image_download_copy_pixels);
break;
case 1:
tft.fillScreen(TFT_BLACK);
screen1();
btn.drawButton();
btn2.drawButton();
tft.pushImage(0, 32, 20, 256, image_download_3_pixels);
tft.pushImage(460, 32, 20, 256, image_download_copy_pixels);
break;
case 2:
tft.fillScreen(TFT_BLACK);
screen2();
btn.drawButton();
btn2.drawButton();
tft.pushImage(0, 32, 20, 256, image_download_3_pixels);
tft.pushImage(460, 32, 20, 256, image_download_copy_pixels);
break;
default:
tft.fillScreen(TFT_BLACK);
screen0();
btn.drawButton();
btn2.drawButton();
tft.pushImage(0, 32, 20, 256, image_download_3_pixels);
tft.pushImage(460, 32, 20, 256, image_download_copy_pixels);
break;
}
oldScreen = screen;
}
}
void screen2(){
tft.setTextColor(0xF800);
tft.setTextSize(10);
tft.setFreeFont();
if(oldValue != value){
tft.fillRect(125,125, 233, 74, TFT_BLACK);
tft.drawString((String)value, 160, 136);
oldValue = value;
}
else{
tft.drawString((String)value, 160, 136);
}
tft.setTextColor(0xFFFF);
tft.setTextSize(4);
tft.drawString("PPM", 206, 222);
}
void screen1(){
tft.fillRect(72, 218, 336, 7, 0xFFFF);
tft.setTextColor(0xFFFF);
tft.setTextSize(7);
tft.setFreeFont();
tft.drawString("23.8 C", 118, 136);
if(oldPocasi != pocasi){
if(pocasi == 0){
tft.fillRect(28, 20, 74, 68, 0x0);
tft.pushImage(50, 20, 68, 64, image_download_pixelss);
}
}
else{
tft.pushImage(50, 20, 68, 64, image_download_pixelss);
}
tft.fillEllipse(309, 143, 7, 7, 0xFFFF);
tft.fillEllipse(309, 143, 5, 5, 0x0);
tft.setTextSize(5);
tft.drawString("41%", 198, 245);
}
void screen0(){
tft.setTextColor(0xFFFF);
tft.setTextSize(7);
//tft.setFreeFont();
tft.drawString("69:69", 139, 139);
tft.fillRect(76, 218, 329, 7, 0xFFFF);
tft.setTextSize(5);
tft.drawString("WED", 89, 250);
tft.drawString("22.OCT", 217, 250);
}