mirror of
https://github.com/YuzuZensai/MCUFRIEND_kbv.git
synced 2026-01-30 20:24:15 +00:00
separate files for each approach
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
#if 1
|
||||
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <MCUFRIEND_kbv.h>
|
||||
MCUFRIEND_kbv tft;
|
||||
@@ -58,8 +60,6 @@ void setup(void)
|
||||
}
|
||||
|
||||
/* two buttons are quite simple
|
||||
* rename to loop() function to loop_old()
|
||||
* and loop_new() to loop() to see another way
|
||||
*/
|
||||
void loop(void)
|
||||
{
|
||||
@@ -79,51 +79,6 @@ void loop(void)
|
||||
tft.fillRect(40, 80, 160, 80, RED);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Unused functions to illustrate:
|
||||
* updating multiple buttons from a list
|
||||
* anything more than two buttons gets repetitive
|
||||
*
|
||||
* compare the simplicity of update_button_list()
|
||||
*/
|
||||
|
||||
// Array of button addresses to behave like a list
|
||||
Adafruit_GFX_Button *buttons[] = {&on_btn, &off_btn, NULL};
|
||||
|
||||
void loop_new(void)
|
||||
{
|
||||
update_button_list(buttons); //use helper function
|
||||
if (on_btn.justPressed()) {
|
||||
tft.fillRect(40, 80, 160, 80, GREEN);
|
||||
}
|
||||
if (off_btn.justPressed()) {
|
||||
tft.fillRect(40, 80, 160, 80, RED);
|
||||
}
|
||||
}
|
||||
|
||||
/* update the state of a button and redraw as reqd
|
||||
*
|
||||
* main program can use isPressed(), justPressed() etc
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/* most screens have different sets of buttons
|
||||
* life is easier if you process whole list in one go
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user