EDIT: included again the code formatted correctly (I hope)
I made a new sketch, just to learn, using the MCUFRIEND:kbv/button_list as starting point.
The goal is to draw 10 buttons arranged like a keypad.
This is running fine, but I'd like to change the backcolor (and possibly the text color) when a button is pressed. I was able to retry the button index and print it on the Serial.
I can not find a method to flip the colors; I made some tries (one you'll see in the Loop), but I think that the button constructor has not something like a 'Fill' method in the libraries. In other words, I do not know how to access the button's parameters.
Second question: when I push a button I get a 'Pressed' and 'JustPressed' events, but they repeat until the push is realesed; I thought that the JustPresses event is raised just once, to avoid the color flashing (if I'm able to change it).
This is my code:
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <TouchScreen.h>
#define MINPRESSURE 200
#define MAXPRESSURE 1000
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
const int XP = 6, XM = A2, YP = A1, YM = 7; //ID=0x9341
const int TS_LEFT = 928, TS_RT = 182, TS_TOP = 962, TS_BOT = 180;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
MCUFRIEND_kbv tft;
Adafruit_GFX_Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
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;
}
void setup() {
Serial.begin(9600);
uint16_t ID = tft.readID();
if (ID == 0xD3D3) ID = 0x9486; // write-only shield
tft.begin(ID);
tft.setRotation(0); //PORTRAIT
tft.fillScreen(BLACK);
btn0.initButton(&tft, 150, 350, 80, 40, WHITE, CYAN, BLACK, "0", 2);
btn1.initButton(&tft, 60, 200, 80, 40, WHITE, CYAN, BLACK, "1", 2);
btn2.initButton(&tft, 150, 200, 80, 40, WHITE, CYAN, BLACK, "2", 2);
btn3.initButton(&tft, 240, 200, 80, 40, WHITE, CYAN, BLACK, "3", 2);
btn4.initButton(&tft, 60, 250, 80, 40, WHITE, CYAN, BLACK, "4", 2);
btn5.initButton(&tft, 150, 250, 80, 40, WHITE, CYAN, BLACK, "5", 2);
btn6.initButton(&tft, 240, 250, 80, 40, WHITE, CYAN, BLACK, "6", 2);
btn7.initButton(&tft, 60, 300, 80, 40, WHITE, CYAN, BLACK, "7", 2);
btn8.initButton(&tft, 150, 300, 80, 40, WHITE, CYAN, BLACK, "8", 2);
btn9.initButton(&tft, 240, 300, 80, 40, WHITE, CYAN, BLACK, "9", 2);
btn0.drawButton(false);
btn1.drawButton(false);
btn2.drawButton(false);
btn3.drawButton(false);
btn4.drawButton(false);
btn5.drawButton(false);
btn6.drawButton(false);
btn7.drawButton(false);
btn8.drawButton(false);
btn9.drawButton(false);
}
// Array of button addresses to behave like a list
Adafruit_GFX_Button *buttons[] = { &btn0, &btn1, &btn2, &btn3, &btn4, &btn5, &btn6, &btn7, &btn8, &btn9, NULL };
bool update_button(Adafruit_GFX_Button *b, bool down) {
b->press(down && b->contains(pixel_x, pixel_y));
if (b->justReleased()) {
b->drawButton(false);
}
if (b->justPressed()) {
b->drawButton(true);
}
return down;
}
bool update_button_list(Adafruit_GFX_Button **pb) {
bool down = Touch_getXY();
for (int i = 0; pb[i] != NULL; i++) {
update_button(pb[i], down);
}
return down;
}
void loop() {
update_button_list(buttons); //use helper function
for (int i = 0; buttons[i] != NULL; i++) {
if (buttons[i]->isPressed()) {
Serial.print("Btn ");
Serial.println(i);
buttons[i]->drawButton(true);
// buttons[i].fill(RED); // <=== here I want to flip color
}
if (buttons[i]->justReleased()) {
buttons[i]->drawButton(false);
}
}
}
Thanks as always