Skip to content

Commit 6087491

Browse files
author
dave
committed
#125 Allow the top level title text to be overridden.
1 parent 284bd3a commit 6087491

File tree

10 files changed

+86
-18
lines changed

10 files changed

+86
-18
lines changed

examples/dynamicMenuItems/ThemeCoolBlueTraditional.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
// tcMenu drawing properties take a 4 color palette for items, titles and actions.
99
// this renderer shares the color configuration for items and actions.
10-
const color_t coolBlueTitlePalette[] = {RGB(0,0,0), RGB(20,132,255), RGB(192,192,192), RGB(0,133,255)};
10+
const color_t coolBlueTitlePalette[] = {RGB(0,0,0), RGB(20,132,255), RGB(192,192,192), RGB(64, 64, 64)};
1111
const color_t coolBlueItemPalette[] = {RGB(255, 255, 255), RGB(0,64,135), RGB(20,133,255), RGB(31,100,178)};
1212

1313
void installCoolBlueTraditionalTheme(GraphicsDeviceRenderer& bgr, const MenuFontDef& itemFont, const MenuFontDef& titleFont, bool needEditingIcons) {

examples/dynamicMenuItems/dynamicMenuItems.emf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,10 @@
405405
"saveToSrc": false,
406406
"useCppMain": false,
407407
"eepromDefinition": "",
408-
"authenticatorDefinition": ""
408+
"authenticatorDefinition": "",
409+
"projectIoExpanders": [
410+
"deviceIO:",
411+
"pcf8574:io8574:32:0"
412+
]
409413
}
410414
}

examples/dynamicMenuItems/dynamicMenuItems.ino

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
*/
66

77
#include "dynamicMenuItems_menu.h"
8+
#include <BaseDialog.h>
89
#include <IoAbstractionWire.h>
9-
10-
// we declare a reference to an io expander as our encoder is on I2C
11-
IoAbstractionRef io8574 = ioFrom8574(0x20, 0); // on addr 0x20 and interrupt pin 0
10+
#include <graphics/RuntimeTitleMenuItem.h>
11+
#include <tcMenuVersion.h>
1212

1313
// Pizza toppings that we forward referenced in the scroll choice item, when in RAM or EEPROM then they are declared in
1414
// a flat array based on the item size.
@@ -97,13 +97,34 @@ void prepareOvenMenuAtRuntime() {
9797
menuMgr.addMenuAfter(&menuBackOven, &menuOvenTempItem);
9898
}
9999

100+
//
101+
// We register a handler that will be called when the title menu item is selected, see setup where we set the callback
102+
//
103+
void onTitlePressed(int /*id*/) {
104+
withMenuDialogIfAvailable([] (MenuBasedDialog* dlg) {
105+
dlg->setButtons(BTNTYPE_CLOSE, BTNTYPE_NONE);
106+
dlg->showRam("Title clicked", false);
107+
char szVer[10];
108+
tccore::copyTcMenuVersion(szVer, sizeof szVer);
109+
dlg->copyIntoBuffer(szVer);
110+
});
111+
}
112+
100113
void setup() {
114+
// If we use wire or serial, it's our responsibility to prepare it.
101115
Serial.begin(115200);
102116
Wire.begin();
103117
setupMenu();
118+
119+
// Here we add a menu observer that is notified as events take place on the menu.
104120
menuMgr.addChangeNotification(&myMgrObserver);
121+
122+
// Now we set up the runtime parameters of various menu items
105123
prepareOvenMenuAtRuntime();
106124
menuList.setNumberOfRows(numListItems);
125+
126+
// and lastly we add the title callback that is notified of the title being selected
127+
appTitleMenuItem.setCallback(onTitlePressed);
107128
}
108129

109130
void loop() {
@@ -152,17 +173,24 @@ void CALLBACK_FUNCTION onDialogInfo(int id) {
152173
}
153174

154175
//
155-
// Shows a question dialog that gets the title from const/program memory. Notice that in this case we have a completion handler
156-
// that accepts the final value, so that we could do something with the result.
176+
// Shows a question dialog that gets the title from const/program memory. Notice that in this case we have a completion
177+
// handler that accepts the final value, so that we could do something with the result.
157178
//
158-
const char pgmQuestionHeader[] PROGMEM = {"Question.."};
179+
const char pgmQuestionHeader[] PROGMEM = {"Override the title?"};
180+
const char pgmTitleOverride[] PROGMEM = {"Title Overridden"};
159181
taskid_t questionTask = TASKMGR_INVALIDID;
160182
void CALLBACK_FUNCTION onDialogQuestion(int id) {
161183
withMenuDialogIfAvailable([] (MenuBasedDialog* dlg) {
162-
dlg->setButtons(BTNTYPE_ACCEPT, BTNTYPE_CANCEL);
184+
dlg->setButtons(BTNTYPE_OK, BTNTYPE_CANCEL);
163185
dlg->show(pgmQuestionHeader, true, [](ButtonType btn, void* data) {
164186
// this is the completion task that runs when the dialog is dismissed.
165187
Serial.print("Question result was "); Serial.println(btn);
188+
if(btn == BTNTYPE_OK) {
189+
appTitleMenuItem.setTitleOverridePgm(pgmTitleOverride);
190+
}
191+
else {
192+
appTitleMenuItem.clearTitleOverride();
193+
}
166194
taskManager.cancelTask(questionTask);
167195
});
168196

@@ -179,7 +207,6 @@ void CALLBACK_FUNCTION onDialogQuestion(int id) {
179207
// In this case we create a controller based dialog that increments and decrements a number that is stored within
180208
// an Analog item. We add the analog item and also an extra button to the dialog.
181209
//
182-
183210
class MyDialogController : public BaseDialogController {
184211
private:
185212
// create an extra button (buttonNum = 2)

examples/dynamicMenuItems/dynamicMenuItems_menu.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
// Global variable declarations
1616
const ConnectorLocalInfo applicationInfo = { "Dynamic Menus", "5f22995e-8da2-49c4-9ec8-d055901003af" };
17-
17+
IoAbstractionRef ioexp_io8574 = ioFrom8574(0x20, 0);
1818
Adafruit_ST7735 gfx(6, 7, 3);
1919
AdafruitDrawable gfxDrawable(&gfx);
2020
GraphicsDeviceRenderer renderer(30, applicationInfo.name, &gfxDrawable);
@@ -72,7 +72,7 @@ void setupMenu() {
7272
gfx.initR(INITR_BLACKTAB);
7373
gfx.setRotation(0);
7474
renderer.setUpdatesPerSecond(5);
75-
switches.initialiseInterrupt(io8574, true);
75+
switches.initialiseInterrupt(ioexp_io8574, true);
7676
menuMgr.initForEncoder(&renderer, &menuMainPower, 7, 6, 5);
7777
renderer.setTitleMode(BaseGraphicalRenderer::TITLE_FIRST_ROW);
7878
renderer.setUseSliderForAnalog(true);

examples/dynamicMenuItems/dynamicMenuItems_menu.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616
#include "tcMenuAdaFruitGfx.h"
1717
#include <RuntimeMenuItem.h>
1818
#include <ScrollChoiceMenuItem.h>
19+
#include <IoAbstraction.h>
20+
#include <IoAbstractionWire.h>
1921

2022
// variables we declare that you may need to access
2123
extern const PROGMEM ConnectorLocalInfo applicationInfo;
2224
extern Adafruit_ST7735 gfx;
2325
extern AdafruitDrawable gfxDrawable;
2426
extern GraphicsDeviceRenderer renderer;
25-
extern IoAbstractionRef io8574;
27+
28+
// Any externals needed by IO expanders, EEPROMs etc
29+
extern IoAbstractionRef ioexp_io8574;
2630

2731
// Global Menu Item exports
2832
extern ListRuntimeMenuItem menuList;

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"authors": "DaveTCC"
2323
}
2424
],
25-
"version": "2.2.3",
25+
"version": "2.2.4",
2626
"license": "Apache-2.0",
2727
"frameworks": "arduino, mbed",
2828
"platforms": "*"

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55

66
name=tcMenu
7-
version=2.2.3
7+
version=2.2.4
88
maintainer=www.thecoderscorner.com
99
author=davetcc
1010
category=Other

src/graphics/RuntimeTitleMenuItem.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "PlatformDetermination.h"
77
#include "RuntimeTitleMenuItem.h"
8+
89
namespace tcgfx {
910

1011
RuntimeTitleMenuItem appTitleMenuItem(0, nullptr);

src/graphics/RuntimeTitleMenuItem.h

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,57 @@ namespace tcgfx {
2626

2727
/**
2828
* This menu item extension class handles the title row, for the root menu. It stores a header in program memory
29-
* and a possible callback function for when it is actioned.
29+
* and a possible callback function for when it is actioned. This menu item also allows the header text to be
30+
* overridden too.
3031
*/
3132
class RuntimeTitleMenuItem : public RuntimeMenuItem {
3233
private:
3334
const char *titleHeaderPgm;
35+
const char *titleOverridePgm;
3436
MenuCallbackFn callback;
3537
public:
38+
/**
39+
* There should only be one instance of this class, it is constructed globally by the library, you can access
40+
* this instance as
41+
* @param id
42+
* @param next
43+
*/
3644
RuntimeTitleMenuItem(uint16_t id, MenuItem *next) : RuntimeMenuItem(MENUTYPE_TITLE_ITEM, id,
3745
appTitleRenderingFn, 0, 1, next) {
3846
titleHeaderPgm = nullptr;
47+
titleOverridePgm = nullptr;
3948
callback = nullptr;
4049
}
4150

51+
/**
52+
* This should only be called by the menu library, to override what's displayed on the title you use
53+
* the `setTitleOverridePgm` instead.
54+
* @param header the new header text
55+
*/
4256
void setTitleHeaderPgm(const char *header) {
4357
titleHeaderPgm = header;
58+
setChanged(true);
59+
}
60+
61+
/**
62+
* In order to override the title with user specific text that overrides the regular title set use this method.
63+
* @param overrideTitle the text to override
64+
*/
65+
void setTitleOverridePgm(const char* overrideTitle) {
66+
titleOverridePgm = overrideTitle;
67+
setChanged(true);
68+
}
69+
70+
/**
71+
* Call this to clear an override that was previously set, so that the regular header text is displayed instead.
72+
*/
73+
void clearTitleOverride() {
74+
titleOverridePgm = nullptr;
75+
setChanged(true);
4476
}
4577

4678
const char *getTitleHeaderPgm() {
47-
return titleHeaderPgm;
79+
return (titleOverridePgm != nullptr) ? titleOverridePgm : titleHeaderPgm;
4880
}
4981

5082
void setCallback(MenuCallbackFn titleCb) {

src/tcMenuVersion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace tccore {
1717
// here we define the version as both a string and separate field
1818
#define TCMENU_MAJOR 2
1919
#define TCMENU_MINOR 2
20-
#define TCMENU_PATCH 3
20+
#define TCMENU_PATCH 4
2121

2222
/**
2323
* A helper to generate the major minor version numbers used in the protocol

0 commit comments

Comments
 (0)