TouchScreen as a single function

Is it possible to write the TouchScreen routine into a single function, with a menu, so it is not ‘visible’ to the rest of the functions/sketch?
On the XPT2046_touchscreen you can use ‘break’ after the menu call due to the presence of a while(true) statement but I can’t see something similar within the TouchScreen routine.

of course it's possible.
but you need to post your code to understand why you mention "menu".

I stuck at it and solved the problem late tonight,

//updated 10.30pm 28sept2022
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <TouchScreen.h>   //.kbv
#define MINPRESSURE 200
#define MAXPRESSURE 1000

const int XP = 9, XM = A3, YP = A2, YM = 8; //ID=0x9341
const int TS_LEFT = 155, TS_RT = 900, TS_TOP = 90, TS_BOT = 955;  //TS_LEFT = 179, TS_RT = 911, TS_TOP = 64, TS_BOT = 949;;

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

int thick = 3;
MCUFRIEND_kbv tft;

#define BLACK 0x0000
#define BLUE 0x001F
#define GREEN 0x07E0
#define CYAN 0x07FF
#define RED 0xF800
#define YELLOW 0xFFE0
#define WHITE 0xFFFF

int x, y;

void setup()
{
  Serial.begin(9600);
  tft.begin(0x9488);
  tft.setRotation(3);  //was 1
  tft.setTextSize(2);
  tft.fillScreen(BLACK);
  read_touch_buttons();
}

int read_touch_buttons()
{

  //tft.fillRect(310, 20, 150, 60, WHITE);
  tft.setCursor(325, 40);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("First ");

  // tft.fillRect(310, 135, 150, 60, WHITE);
  tft.setCursor(330, 158);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("Next ");

  //tft.fillRect(310, 250, 150, 60, BLUE);
  tft.setCursor(320, 272);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("File");

  // tft.fillRect(10, 255, 185, 60, YELLOW);
  tft.setCursor(18, 277);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("ENTER/CONTINUE");
  tft.setTextColor(WHITE);

  //Rectangle outlines
  for (int i = 0; i < thick; i++) {
    tft.drawRect(310 + i, 20 + i, 150 - i * 2, 60 - i * 2, GREEN);
    tft.drawRect(310 + i, 135 + i, 150 - i * 2, 60 - i * 2, CYAN);
    tft.drawRect(310 + i, 250 + i, 150 - i * 2, 60 - i * 2, YELLOW);
    tft.drawRect(10 + i, 255 + i, 185 - i * 2, 60 - i * 2, RED);
  }

  while (1) {

    TSPoint p;
    do {
      p = ts.getPoint();
      pinMode(XM, OUTPUT);
      pinMode(YP, OUTPUT);
    } while ((p.z < MINPRESSURE ) || (p.z > MAXPRESSURE));
    p.x = map(p.x, TS_LEFT, TS_RT, 0, 320); //320
    p.y = map(p.y, TS_BOT, TS_TOP, 0, 480); //480
    // return p;

    y = p.x; x = p.y;

    {
      delay(100);

      // Serial.print(x); Serial.print(","); Serial.println(y);

      if (x > 310 && x < 470)  //First File
      {
        if (y > 20 && y < 80)
        {
          tft.setCursor(20, 100);
          tft.print("First");
          delay(100);
          secons();
          break;

        }
        if (y > 135 && y < 200)  //Next File
        {
          tft.setCursor(20, 140);
          tft.print("Next");
          delay(100);
          thirds();
          break;
        }
        if (y > 250 && y < 320)  //Stream File
        {
          tft.setCursor(20, 180);
          tft.print("File");
          delay(100);
          fourths();
          break;
        }
      }
      if (x > 10 && x < 250)
      {
        if (y > 255  && y < 320) //ENTER/CONTINUE
        {
          tft.fillRect(0, 0, 200, 200, BLACK);
          delay(100);
          
        }
      }
    }
  }
}
void loop() {


}

void secons() {
  Serial.print(x); Serial.print(","); Serial.println(y);
  tft.fillScreen(BLACK);
  delay(1000);
  read_touch_buttons();
}


void thirds() {
  Serial.print(x); Serial.print(","); Serial.println(y);
  tft.fillScreen(BLACK);
  delay(1000);
  read_touch_buttons();
}


void fourths() {
  Serial.print(x); Serial.print(","); Serial.println(y);
  tft.fillScreen(BLACK);
  delay(1000);
  read_touch_buttons();
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.