Am trying to debug a routine to set the RTC.
My touch screen reads from the main screen menu work fine. i.e. when I touch the "4 button" I get a "4" returned.
For the clock set routine, I drew a 10 key pad with an enter button on the display and get the key "hits" logged reliably.
When I try to read a screen touch from the clock set routine, I keep getting a double "hit" giving me the same number (return) twice for each touch screen event.
Here is the set clock routine
Code: Select all
void setTime()
{
char setBuffer[32];
int setMonth = 0;
int setDay = 0;
int setYear = 2014;
int setHour = 0;
int setMinute = 0;
int setSecond = 0;
tft.fillScreen(RA8875_BLACK);
tft.textMode();
tft.textEnlarge (2);
tft.textSetCursor(80,50);
tft.textTransparent(RA8875_GREEN);
tft.textWrite("Set Time");
tft.textTransparent(RA8875_WHITE);
tft.textSetCursor(60,150);
tft.textWrite("mm/dd/yyyy");
tft.textSetCursor(60,200);
tft.textWrite("hh:mm:ss");
DrawNumKeypad ();
tft.textMode();
sprintf( setBuffer,
"%02d/%02d/%4d %02d:%02d:%02d",
setMonth, setDay, setYear, setHour, setMinute, setSecond);
tft.textSetCursor(60,250);
tft.textTransparent(RA8875_WHITE);
tft.textWrite( setBuffer );
for(int x=0; x<12; x++)
{
GetNumKey ();
if (numKeyPress <0) x--;
delay(200);
tft.textSetCursor(60,250);
tft.textTransparent(RA8875_BLACK);
tft.textWrite( setBuffer );
if (x == 0) setMonth = numKeyPress * 10 , Serial.println ("Set Month tens");
if (x == 1) setMonth = setMonth + numKeyPress, Serial.println ("Set Month ones");
if (x == 2) setDay = numKeyPress * 10 , Serial.println ("Set Day tens");
if (x == 3) setDay = setDay + numKeyPress, Serial.println ("Set Day ones");
if (x == 4) setYear = numKeyPress * 1000, Serial.println ("Set Year thousands");
if (x == 5) setYear = setYear + (numKeyPress * 100), Serial.println ("Set Year hundreds");
if (x == 6) setYear = setYear + (numKeyPress * 10), Serial.println ("Set Year tens");
if (x == 7) setYear = setYear + numKeyPress, Serial.println ("Set Year ones");
if (x == 8) setHour = numKeyPress * 10, Serial.println ("Set Hour tens");
if (x == 9) setHour = setHour + numKeyPress, Serial.println ("Set Hour ones");
if (x == 10) setMinute = numKeyPress * 10, Serial.println ("Set Minutes tens");
if (x == 11) setMinute = setMinute + numKeyPress, Serial.println ("Set Minutes ones");
sprintf( setBuffer,
"%02d/%02d/%4d %02d:%02d:%02d",
setMonth, setDay, setYear, setHour, setMinute, setSecond);
Serial.println(setBuffer);
tft.textSetCursor(60,250);
tft.textTransparent(RA8875_WHITE);
tft.textWrite( setBuffer );
}
tft.graphicsMode();
delay(3000);
welcomeScreen();
}
Code: Select all
int GetNumKey ()
{
keyPress = '*';
numKeyPress = 0;
int keyTouched = 0;
float xScale = 1024.0F/tft.width();
float yScale = 1024.0F/tft.height();
delay(200);
/* Wait around for touch events */
Serial.println("Waiting for touch events ...");
while(keyTouched == 0)
{
if (!digitalRead(RA8875_INT))
//Serial.println("Waiting for interrupt ...");
delay(50);
{
if (tft.touched())
{
//Serial.print("Touch: ");
tft.touchRead(&tx, &ty);
//delay(200);
//Serial.print(tx); Serial.print(", "); Serial.println(ty);
keyTouched = 1;
// break;
}
}
}
if (ty > 342 && ty < 440)
{
if(tx >684 && tx < 750) keyPress = '7';
if(tx >775 && tx < 850) keyPress = '8';
if(tx >872 && tx < 941) keyPress = '9';
}
if (ty > 480 && ty < 583)
{
if(tx >684 && tx < 750) keyPress = '4';
if(tx >775 && tx < 850) keyPress = '5';
if(tx >872 && tx < 941) keyPress = '6';
}
if (ty > 623 && ty < 730)
{
if(tx >684 && tx < 750) keyPress = '1';
if(tx >775 && tx < 850) keyPress = '2';
if(tx >872 && tx < 941) keyPress = '3';
}
if (ty > 764 && ty < 865)
{
if(tx >684 && tx < 750) keyPress = '0';
if(tx >775 && tx < 941) keyPress = 'E';
}
//Serial.print("Key Char = ");Serial.println(keyPress);
// **** Turn keypress into integer ****
numKeyPress = keyPress - 48;
// delay(250);
return (numKeyPress,keyPress);
}
Then instead of doing it again for the next key, the result displays and then goes back to "getNumKey()" again returning the same result without touching the screen. Then it correctly waits for the next touch screen event and double hits that one too.
The loop counter exits when 6 touch events have occurred instead of 12.
The code could probably be better written, but I'm not that good with this language.
Sometimes it's hard to figure out what's going on when a function calls a library that calls another library which calls another library, etc.
Any thoughts would be helpful.