0

I am very new to TFT screens and I am developing some code to display on a little information board, however I have come across an issue. Once I call tft.fillScreen(BLACK);, it writes testing but then redraws the buttons. How would I go about preventing this? Thanks Adam

This is the full code:

#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#define MINPRESSURE 200
#define MAXPRESSURE 1000

const int XP=6,XM=A2,YP=A1,YM=7; //ID=0x9341
const int TS_LEFT=960,TS_RT=90,TS_TOP=138,TS_BOT=904;

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

Adafruit_GFX_Button on_btn, off_btn, on_btn2, off_btn2, on_btn3, off_btn3;
int i=0;
int pixel_x, pixel_y;     //Touch_getXY() updates global vars
bool Touch_getXY(void)
{
    TSPoint p = ts.getPoint();
    pinMode(YP, OUTPUT);      //restore shared pins
    pinMode(XM, OUTPUT);
    digitalWrite(YP, HIGH);   //because TFT control pins
    digitalWrite(XM, HIGH);
    bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
    if (pressed) {
        pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //.kbv makes sense to me
        pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
    }
    return pressed;
}

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

void setup(void)
{
    Serial.begin(9600);
    uint16_t ID = tft.readID();
    Serial.print("TFT ID = 0x");
    Serial.println(ID, HEX);
    Serial.println("Calibrate for your Touch Panel");
    if (ID == 0xD3D3) ID = 0x9486; // write-only shield
    tft.reset();
    tft.begin(ID);
    tft.setRotation(1);            //PORTRAIT
    tft.fillScreen(BLACK);
    on_btn.initButton(&tft,  150, 50, 200, 60, WHITE, CYAN, BLACK, "cpu", 2);
    off_btn.initButton(&tft, 350, 50, 200, 60, WHITE, CYAN, BLACK, "memory", 2);
    on_btn2.initButton(&tft,  150, 150, 200, 60, WHITE, CYAN, BLACK, "storage", 2);
    off_btn2.initButton(&tft, 350, 150, 200, 60, WHITE, CYAN, BLACK, "motherboard", 2);
    on_btn3.initButton(&tft,  150, 250, 200, 60, WHITE, CYAN, BLACK, "power supply", 2);
    off_btn3.initButton(&tft, 350, 250, 200, 60, WHITE, CYAN, BLACK, "graphics card", 2);
    on_btn.drawButton(false);
    off_btn.drawButton(false);
    on_btn2.drawButton(false);
    off_btn2.drawButton(false);
    on_btn3.drawButton(false);
    off_btn3.drawButton(false);
}

/* two buttons are quite simple
 */
void loop(void)
{
    bool down = Touch_getXY();
    on_btn.press(down && on_btn.contains(pixel_x, pixel_y));
    off_btn.press(down && off_btn.contains(pixel_x, pixel_y));
    on_btn2.press(down && on_btn2.contains(pixel_x, pixel_y));
    off_btn2.press(down && off_btn2.contains(pixel_x, pixel_y));
    on_btn3.press(down && on_btn3.contains(pixel_x, pixel_y));
    off_btn3.press(down && off_btn3.contains(pixel_x, pixel_y));


    if (on_btn.justReleased())
      on_btn.drawButton();
      if(on_btn.justPressed()){
          on_btn.drawButton(true);
          tft.fillScreen(BLACK); // clear the screen
          tft.setCursor(20,20); 
          tft.setTextColor(MAGENTA);
          tft.setTextSize(1);
          tft.print("testing");
      }



    if (off_btn.justReleased())
        off_btn.drawButton();


    if (on_btn2.justReleased())
        on_btn2.drawButton();


    if (off_btn2.justReleased())
        off_btn2.drawButton();

    if (on_btn3.justReleased())
        on_btn3.drawButton();


    if (off_btn3.justReleased())
        off_btn3.drawButton();

}

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.