Musicmanager:
In my first post I wrote .. .. " I know this snippet is wrong, I've written it like that so that you can see what I'm trying to do .. .. .. .. "
What we understood is that you knew there was a bug, not that you had written in pseudo code..
I dug in an old test I had written, it does not use the button library and it basically creates 4 rectangle on the screen and tracks clicks. if you touch within one of the rectangle, then the rectangle is framed in white until you exit the rectangle or release the touch.
there is no event generated or whatever smartness in any of this, that was just to test drawing and touching.
may be this helps get you started? (you can remove lines related to BigFont, I don't display text)
#include <UTFT.h>
#include <SPI.h>
#include <URTouch.h>
extern uint8_t BigFont[];
#define TOUCH_ORIENTATION LANDSCAPE
UTFT myGLCD(ITDB32S, 38, 39, 40, 41);
URTouch myTouch(6, 5, 4, 3, 2);
int screenW, screenH;
const uint8_t nbButtons = 4;
const int buttonSpacing = 30;
struct rectangleButton {
int x1, y1, x2, y2;
uint16_t bgcolor;
uint16_t fcolor;
uint8_t id;
} buttons[nbButtons];
void fillButton(const rectangleButton* r)
{
myGLCD.setColor(r->bgcolor);
myGLCD.fillRoundRect(r->x1, r->y1, r->x2, r->y2);
}
void frameButton(const rectangleButton* r)
{
myGLCD.setColor(r->fcolor);
for (int8_t i = 3; i >= 0; i--) // 4 pixel wide frame
myGLCD.drawRoundRect(r->x1 + i, r->y1 + i, r->x2 - i, r->y2 - i);
}
void drawAllButtons()
{
for (uint8_t i = 0; i < nbButtons; i++) fillButton(&(buttons[i]));
}
inline bool testHitButton(const rectangleButton& r, const int x, const int y)
{
return ((r.x1 < x) && (x < r.x2) && (r.y1 < y) && (y < r.y2));
}
rectangleButton* testHit()
{
rectangleButton* r = NULL;
int x , y;
if (myTouch.dataAvailable()) {
myTouch.read();
x = myTouch.getX();
y = myTouch.getY();
Serial.print(x); Serial.write(' '); Serial.println(y);
if ((x >= 0) && (y >= 0)) {
for (uint8_t i = 0; i < nbButtons; i++) {
if (testHitButton(buttons[i], x, y)) {
r = &(buttons[i]);
break;
}
}
}
}
return r;
}
void trackTouch()
{
static rectangleButton* oldR = NULL;
rectangleButton* newR = testHit();
if (newR) { // if we touched within a rectangle
if (newR != oldR) { // and this is not the currently highlighted one
if (oldR) fillButton(oldR); // if there was a previous rectangle highlighted, remove the frame
frameButton(newR); // and frame the new one
oldR = newR; // remember this one is framed now
}
} else { // we released the touch
if (oldR) fillButton(oldR); // if there was a previous rectangle highlighted, remove the frame
oldR = NULL; // and remember no rectangle is hit at the moment
}
}
void setup() {
Serial.begin(115200);
myGLCD.InitLCD(TOUCH_ORIENTATION); // Setup the LCD
myGLCD.setFont(BigFont);
myTouch.InitTouch(TOUCH_ORIENTATION);
myTouch.setPrecision(PREC_MEDIUM);
screenW = myGLCD.getDisplayXSize();
screenH = myGLCD.getDisplayYSize();
myGLCD.clrScr();
int buttonWidth = (screenW - 3 * buttonSpacing) / 2;
int buttonHeight = (screenH - 3 * buttonSpacing) / 2;
buttons[0].x1 = buttonSpacing;
buttons[0].y1 = buttonSpacing;
buttons[0].x2 = buttons[0].x1 + buttonWidth;
buttons[0].y2 = buttons[0].y1 + buttonHeight;
buttons[0].bgcolor = VGA_RED;
buttons[0].fcolor = VGA_WHITE;
buttons[0].id = 0;
buttons[1].x1 = buttonSpacing;
buttons[1].y1 = 2 * buttonSpacing + buttonHeight;
buttons[1].x2 = buttons[1].x1 + buttonWidth;
buttons[1].y2 = buttons[1].y1 + buttonHeight;
buttons[1].bgcolor = VGA_BLUE;
buttons[1].fcolor = VGA_WHITE;
buttons[1].id = 1;
buttons[2].x1 = 2 * buttonSpacing + buttonWidth;
buttons[2].y1 = buttonSpacing;
buttons[2].x2 = buttons[2].x1 + buttonWidth;
buttons[2].y2 = buttons[2].y1 + buttonHeight;
buttons[2].bgcolor = VGA_FUCHSIA;
buttons[2].fcolor = VGA_WHITE;
buttons[2].id = 2;
buttons[3].x1 = 2 * buttonSpacing + buttonWidth;
buttons[3].y1 = 2 * buttonSpacing + buttonHeight;
buttons[3].x2 = buttons[3].x1 + buttonWidth;
buttons[3].y2 = buttons[3].y1 + buttonHeight;
buttons[3].bgcolor = VGA_GREEN;
buttons[3].fcolor = VGA_WHITE;
buttons[3].id = 3;
drawAllButtons();
Serial.println(F("GO!"));
}
void loop()
{
trackTouch();
}