Thanks Dave!
Using isSwitchPressed works. I need it to flip through a couple of pages - for this use case the method is enough.
Downside: As expected, sometimes it misses a button press (for me roughly 1-2 presses out of 10). This happens whenever the button is pressed & released between the calls of the function referenced by takeOverDisplay. From a usability point of view, the user might think there is a hardware issue since the button press does not lead to any reaction and it needs to be pressed again.
Here is my code as reference for others looking for a similar functionality:
(Screen.h)
struct MyStruct {
int screenCounter;
int currentPage;
int numOfPages;
};
class Screen {
public:
static void showCustomPages(unsigned int encoderValue, RenderPressMode clicked);
private:
static MyStruct myStruct;
};
(Screen.cpp)
#include "Screen.h"
#include "tcmenu_menu.h" // or whatever your tcmenu header is called
MyStruct Screen::myStruct = {0,0,5};
void Screen::showCustomPages(unsigned int encoderValue, RenderPressMode clicked) {
if (myStruct.screenCounter == 0) {
// initial page of the custom screen.
myStruct.infoScreenCounter++;
myStruct.currentPage = 0;
// this example draws a footer with page number on a 240x240 screen
gfx.fillRect(0, 215, 240, 25, 0x4A69);
gfx.setTextColor(ST77XX_WHITE);
gfx.setCursor(20,232);
gfx.setFont(&FreeSans9pt7b);
gfx.print("Page: ");
gfx.print(myStruct.currentPage+1); // count starts at 0, human readable value should start at 1
gfx.print("/");
gfx.print(myStruct.numOfPages);
gfx.print("");
gfx.setCursor(140,232);
gfx.print("<A - Exit>");
}
if (switches.isSwitchPressed(RIGHT_BUTTON_GPIO) ) {
if (myStruct.numOfPages > myStruct.currentPage) {
myStruct.currentPage++;
// draw next page
// this example draws a footer with page number on a 240x240 screen
gfx.fillRect(0, 215, 240, 25, 0x4A69);
gfx.setTextColor(ST77XX_WHITE);
gfx.setCursor(20,232);
gfx.setFont(&FreeSans9pt7b);
gfx.print("Page: ");
gfx.print(myStruct.currentPage+1); // count starts at 0, human readable value should start at 1
gfx.print("/");
gfx.print(myStruct.numOfPages);
gfx.print("");
gfx.setCursor(140,232);
gfx.print("<A - Exit>");
}
}
if (switches.isSwitchPressed(LEFT_BUTTON_GPIO) ) {
if (myStruct.currentPage > 0) {
myStruct.currentPage--;
gfx.fillRect(0, 215, 240, 25, 0x4A69);
gfx.setTextColor(ST77XX_WHITE);
gfx.setCursor(20,232);
gfx.setFont(&FreeSans9pt7b);
gfx.print("Page: ");
gfx.print(myStruct.currentPage+1); // count starts at 0, human readable value should start at 1
gfx.print("/");
gfx.print(myStruct.numOfPages);
gfx.print("");
gfx.setCursor(140,232);
gfx.print("<A - Exit>");
}
}
if (clicked) {
myStruct.infoScreenCounter = 0;
renderer.giveBackDisplay();
}
}