Go Down

Topic: TFT LCD problem (Read 69 times) previous topic - next topic

Smiteh

Hi guys,

in the code attached below I'm generating a keypad that writes the # to a text box when pressed, the addition of a clear button allows for rewriting,

the clear button works fine when there is text in the box, however, when I press clear when the text box is empty, is doesn't let me type in any more numbers and I'm not really sure on what the solution is, the clear button if statement is the second code snippit below
Code: [Select]

//#include <Adafruit_TFTLCD.h>
//#include <Adafruit_GFX.h>   
#include <TouchScreen.h>
#include <Elegoo_GFX.h>
#include <Elegoo_TFTLCD.h>

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

#define YP A3
#define XM A2
#define YM 9
#define XP 8

#define BOXSIZE 35 //keypad size

#define TS_MINX 128
#define TS_MINY 131
#define TS_MAXX 951
#define TS_MAXY 914

#define BUTTON_X 15
#define BUTTON_Y 105
#define BUTTON_W 35
#define BUTTON_H 35
#define BUTTON_SPACING_X 10
#define BUTTON_SPACING_Y 10


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

#define TEXT_X 155
#define TEXT_Y 51
#define TEXT_W 145
#define TEXT_H 40

#define STATUS_X 155
#define STATUS_Y 100

#define TEXT_LEN 5
char textfield[TEXT_LEN+1] = "";
uint8_t textfield_i=0;


Elegoo_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 364);

Elegoo_GFX_Button buttons[12];

char buttonlabels[12][4] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "Clr", "0", "."};
uint16_t buttoncolours[12] = {WHITE, WHITE, WHITE,
                             WHITE, WHITE, WHITE,
                             WHITE, WHITE, WHITE,
                             WHITE, WHITE, WHITE};
uint16_t buttontext[12] = {BLACK, BLACK, BLACK,
                             BLACK, BLACK, BLACK,
                             BLACK, BLACK, BLACK,
                             RED, BLACK, BLACK};

char CurrentPage; //Defines page number for use with touch functionality of each page in loop function
int a;            // Variable used for switching x any y axis after mapping, allows for typical axis (x,y)

/*
 * Add in screen commands on setup to work on startup
 */
void setup() {
  Serial.print(9600);
  tft.reset();
  uint16_t identifier = tft.readID();
  tft.begin(0x9341);
  tft.setRotation(1);
  tft.fillScreen(WHITE);
  tft.drawRect(0,0,320,240,BLACK);

// CurrentPage = 1;

  tft.setCursor(35,20);
  tft.setTextColor(BLACK);
  tft.setTextSize(2);
  tft.print("Skull Depth Estimate");

  tft.drawLine(32, 35, 277, 35, BLACK);

  tft.fillRect(230,180, 70, 40, BLUE);
  tft.drawRect(230,180,70,40,BLACK);
  tft.setCursor(241,191);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("Next");
 
  tft.fillRect(155,180, 70, 40, BLUE);
  tft.drawRect(155,180,70,40,BLACK);
  tft.setCursor(166,191);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("Back");

 //create buttons
  for (uint8_t row=0; row<4; row++) {
    for (uint8_t col=0; col<3; col++) {
      buttons[col + row*3].initButton(&tft, BUTTON_X+18+col*(BUTTON_W+BUTTON_SPACING_X),
                 BUTTON_Y-37+row*(BUTTON_H+BUTTON_SPACING_Y),    // x, y, w, h, outline, fill, text
                  BUTTON_W, BUTTON_H, BLACK, buttoncolours[col+row*3], buttontext[col+row*3],
                  buttonlabels[col + row*3], 1);
      buttons[col + row*3].drawButton();
    }
    }

tft.drawRect(TEXT_X,TEXT_Y,TEXT_W,TEXT_H, BLACK);


}

/*void status(const __FlashStringHelper *msg) {
  tft.fillRect(STATUS_X, STATUS_Y, 145, 20, BLACK);
  tft.setCursor(STATUS_X, STATUS_Y);
  tft.setTextColor(BLACK);
  tft.setTextSize(1);
  tft.print(msg);
}
// or charstring
void status(char *msg) {
  tft.fillRect(STATUS_X, STATUS_Y, 240, 8, BLACK);
  tft.setCursor(STATUS_X, STATUS_Y);
  tft.setTextColor(BLACK);
  tft.setTextSize(1);
  tft.print(msg);
*/



#define MINPRESSURE 100
#define MAXPRESSURE 1000

void loop(void) {

TSPoint p = ts.getPoint();               //generates which point on screen is touched
 
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
   
  if (p.z > MINPRESSURE /* && p.z < MAXPRESSURE*/) {
Serial.print("\tPressure = "); Serial.println(p.z);
   p.x = map(p.x, TS_MINX, TS_MAXX, 240, 0);   // maps screen coordinates to appropriate x,y values
   p.y = map(p.y, TS_MINY, TS_MAXY, 320, 0);   // calibration may need altering although currently
   a = p.x;                                    // works for current system, if rotating screen, mapping
   p.x = p.y;                                  // values need changing to accomodate
   p.y = a;

 

  }
 
 
 // go thru all the buttons, checking if they were pressed
  for (uint8_t b=0; b<12; b++) {
    if (buttons[b].contains(p.x, p.y)) {
      //Serial.print("Pressing: "); Serial.println(b);
      buttons[b].press(true);  // tell the button it is pressed
    } else {
      buttons[b].press(false);  // tell the button it is NOT pressed
    }
  }
 

  // now we can ask the buttons if their state has changed
  for (uint8_t b=0; b<12; b++) {
     if (buttons[b].justReleased()) {
     //  Serial.print("Released: "); Serial.println(b);
      buttons[b].drawButton();  // draw normal
    }
   
   
    if (buttons[b].justPressed()) {
        buttons[b].drawButton(true);  // draw invert!
       
        // if a numberpad button, append the relevant # to the textfield
        if (b <9 || b==10 || b==11) {
          if (textfield_i < TEXT_LEN) {
            textfield[textfield_i] = buttonlabels[b][0];
            textfield_i++;
      textfield[textfield_i] = 0; // zero terminate
           
           // fona.playDTMF(buttonlabels[b][0]);
          }
        }
         if (b == 9) {
          textfield[textfield_i] = 0;
          if (textfield > 0) {
            textfield_i--;
            textfield[textfield_i] = ' ';
          }
         
        }

        // clr button! delete char
       
        // update the current text field
        Serial.println(textfield);
        tft.setCursor(TEXT_X + 2, TEXT_Y+10);
        tft.setTextColor(BLACK, WHITE);
        tft.setTextSize(2);
        tft.print(textfield);

         
       

   
       
      delay(100); // UI debouncing
    }
  }
  }
 

 

 


Code: [Select]
if (b == 9) {
          textfield[textfield_i] = 0;
          if (textfield > 0) {
            textfield_i--;
            textfield[textfield_i] = ' ';
          }
         
        }


any advice would be greatly appreciated,

thanks

pylon

I guess

Code: [Select]
          if (textfield > 0) {


should read

Code: [Select]
          if (textfield_i > 0) {

Smiteh

yeah that's solved it, not sure why I didn't try that earlier I've been looking at it so long I must have gone brain dead,

thank you

Go Up