Skip to content

Commit c3c5ca4

Browse files
committed
#42 support for left and right - also added to dfrobot example
1 parent 6999678 commit c3c5ca4

File tree

6 files changed

+88
-11
lines changed

6 files changed

+88
-11
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"version":"1.00","projectName":"C:\\Users\\dave\\Documents\\Arduino\\libraries\\tcMenu\\examples\\analogDfRobot\\analogDfRobot.emf","author":"dave","lastEdited":{"seconds":1573294826,"nanos":836988000},"namingRecursive":false,"items":[{"parentId":0,"type":"analogItem","item":{"maxValue":1024,"offset":0,"divisor":1,"unitName":"","name":"Value A0","id":1,"eepromAddress":-1,"readOnly":false,"localOnly":false}},{"parentId":0,"type":"subMenu","item":{"secured":false,"name":"LED States","id":2,"eepromAddress":-1,"readOnly":false,"localOnly":false}},{"parentId":2,"type":"boolItem","item":{"naming":"ON_OFF","name":"LED 1","id":3,"eepromAddress":2,"functionName":"onLed1","readOnly":false,"localOnly":false}},{"parentId":2,"type":"boolItem","item":{"naming":"ON_OFF","name":"LED 2","id":4,"eepromAddress":3,"functionName":"onLed2","readOnly":false,"localOnly":false}},{"parentId":0,"type":"analogItem","item":{"maxValue":255,"offset":0,"divisor":1,"unitName":"Unit","name":"New AnalogItem","id":5,"eepromAddress":-1,"readOnly":false,"localOnly":false}}],"codeOptions":{"embeddedPlatform":"ARDUINO","lastDisplayUuid":"bcd5fe34-9e9f-4fcb-9edf-f4e3caca0674","lastInputUuid":"7daa6a81-dd09-422e-b83c-bda5045abaef","lastRemoteUuid":"2c101fec-1f7d-4ff3-8d2b-992ad41e7fcb","applicationUUID":"2ba37227-a412-40b7-94e7-42caf9bb0ff4","applicationName":"DfRobot","lastProperties":[],"namingRecursive":false}}
1+
{"version":"1.00","projectName":"C:\\Users\\dave\\Documents\\Arduino\\libraries\\tcMenu\\examples\\analogDfRobot\\analogDfRobot.emf","author":"dave","lastEdited":{"seconds":1574526348,"nanos":899682800},"namingRecursive":false,"items":[{"parentId":0,"type":"analogItem","item":{"maxValue":1024,"offset":0,"divisor":1,"unitName":"","name":"Value A0","id":1,"eepromAddress":-1,"readOnly":false,"localOnly":false}},{"parentId":0,"type":"subMenu","item":{"secured":false,"name":"LED States","id":2,"eepromAddress":-1,"readOnly":false,"localOnly":false}},{"parentId":2,"type":"boolItem","item":{"naming":"ON_OFF","name":"LED 1","id":3,"eepromAddress":2,"functionName":"onLed1","readOnly":false,"localOnly":false}},{"parentId":2,"type":"boolItem","item":{"naming":"ON_OFF","name":"LED 2","id":4,"eepromAddress":3,"functionName":"onLed2","readOnly":false,"localOnly":false}},{"parentId":0,"type":"largeNumItem","item":{"digitsAllowed":8,"decimalPlaces":4,"name":"LgeNum","id":5,"eepromAddress":10,"readOnly":false,"localOnly":false}},{"parentId":0,"type":"textItem","item":{"textLength":6,"itemType":"PLAIN_TEXT","name":"Text","id":6,"eepromAddress":4,"readOnly":false,"localOnly":false}}],"codeOptions":{"embeddedPlatform":"ARDUINO","lastDisplayUuid":"bcd5fe34-9e9f-4fcb-9edf-f4e3caca0674","lastInputUuid":"7daa6a81-dd09-422e-b83c-bda5045abaef","lastRemoteUuid":"2c101fec-1f7d-4ff3-8d2b-992ad41e7fcb","applicationUUID":"2ba37227-a412-40b7-94e7-42caf9bb0ff4","applicationName":"DfRobot","lastProperties":[],"namingRecursive":false}}

examples/analogDfRobot/analogDfRobot.ino

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
#include "analogDfRobot_menu.h"
22

33
void setup() {
4+
// first we setup the menu
45
setupMenu();
6+
7+
// we are going to toggle the built in LED, so set it as output.
58
pinMode(LED_BUILTIN, OUTPUT);
9+
10+
// now we read the value of A0 every 200millis and set it onto a menu item
611
menuValueA0.setReadOnly(true);
712
taskManager.scheduleFixedRate(200, [] {
813
menuValueA0.setCurrentValue(analogRead(A0));
914
});
15+
16+
// Finally we set up back (left) and next (right) functionality.
17+
menuMgr.setBackButton(DF_KEY_LEFT);
18+
menuMgr.setNextButton(DF_KEY_RIGHT);
1019
}
1120

1221
void loop() {
@@ -21,5 +30,3 @@ void CALLBACK_FUNCTION onLed2(int id) {
2130
// TODO: write your own second LED function..
2231
// Called whenever you change the LED2 menu item..
2332
}
24-
25-

examples/analogDfRobot/analogDfRobot_menu.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@ LiquidCrystalRenderer renderer(lcd, 16, 2);
1818

1919
// Global Menu Item declarations
2020

21+
RENDERING_CALLBACK_NAME_INVOKE(fnTextRtCall, textItemRenderFn, "Text", 4, NULL)
22+
TextMenuItem menuText(fnTextRtCall, 6, 6, NULL);
23+
RENDERING_CALLBACK_NAME_INVOKE(fnLgeNumRtCall, largeNumItemRenderFn, "LgeNum", 10, NULL)
24+
EditableLargeNumberMenuItem menuLgeNum(fnLgeNumRtCall, 5, 8, 4, &menuText);
2125
const PROGMEM BooleanMenuInfo minfoLED2 = { "LED 2", 4, 3, 1, onLed2, NAMING_ON_OFF };
2226
BooleanMenuItem menuLED2(&minfoLED2, false, NULL);
2327
const PROGMEM BooleanMenuInfo minfoLED1 = { "LED 1", 3, 2, 1, onLed1, NAMING_ON_OFF };
2428
BooleanMenuItem menuLED1(&minfoLED1, false, &menuLED2);
2529
RENDERING_CALLBACK_NAME_INVOKE(fnLEDStatesRtCall, backSubItemRenderFn, "LED States", -1, NULL)
2630
const PROGMEM SubMenuInfo minfoLEDStates = { "LED States", 2, 0xffff, 0, NO_CALLBACK };
2731
BackMenuItem menuBackLEDStates(fnLEDStatesRtCall, &menuLED1);
28-
SubMenuItem menuLEDStates(&minfoLEDStates, &menuBackLEDStates, NULL);
32+
SubMenuItem menuLEDStates(&minfoLEDStates, &menuBackLEDStates, &menuLgeNum);
2933
const PROGMEM AnalogMenuInfo minfoValueA0 = { "Value A0", 1, 0xffff, 1024, NO_CALLBACK, 0, 1, "" };
3034
AnalogMenuItem menuValueA0(&minfoValueA0, 0, &menuLEDStates);
3135
const PROGMEM ConnectorLocalInfo applicationInfo = { "DfRobot", "2ba37227-a412-40b7-94e7-42caf9bb0ff4" };

examples/analogDfRobot/analogDfRobot_menu.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <LiquidCrystalIO.h>
1616
#include <IoAbstraction.h>
1717
#include <DfRobotInputAbstraction.h>
18+
#include <RuntimeMenuItem.h>
19+
#include <EditableLargeNumberMenuItem.h>
1820
#include "tcMenuLiquidCrystal.h"
1921

2022
// all define statements needed
@@ -25,6 +27,8 @@ extern LiquidCrystal lcd;
2527
extern LiquidCrystalRenderer renderer;
2628

2729
// all menu item forward references.
30+
extern TextMenuItem menuText;
31+
extern EditableLargeNumberMenuItem menuLgeNum;
2832
extern BooleanMenuItem menuLED2;
2933
extern BooleanMenuItem menuLED1;
3034
extern BackMenuItem menuBackLEDStates;

src/tcMenu.cpp

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,49 @@ void MenuManager::initForEncoder(MenuRenderer* renderer, MenuItem* root, uint8_
3333
renderer->initialise();
3434
}
3535

36+
void MenuManager::setBackButton(uint8_t backButtonPin) {
37+
switches.addSwitch(backButtonPin, [](uint8_t, bool held){
38+
if(!held) menuMgr.performDirectionMove(true);
39+
});
40+
}
41+
42+
void MenuManager::setNextButton(uint8_t nextButtonPin) {
43+
switches.addSwitch(nextButtonPin, [](uint8_t, bool held){
44+
if(!held) menuMgr.performDirectionMove(false);
45+
});
46+
}
47+
48+
void MenuManager::performDirectionMove(bool dirIsBack) {
49+
if(currentEditor != NULL && isMenuRuntimeMultiEdit(currentEditor)) {
50+
EditableMultiPartMenuItem<void*>* editableItem = reinterpret_cast<EditableMultiPartMenuItem<void*>*>(currentEditor);
51+
52+
int editorRange = dirIsBack ? editableItem->previousPart() : editableItem->nextPart();
53+
if (editorRange != 0) {
54+
switches.changeEncoderPrecision(editorRange, editableItem->getPartValueAsInt());
55+
}
56+
else {
57+
currentEditor->setEditing(false);
58+
currentEditor->setActive(true);
59+
currentEditor = NULL;
60+
}
61+
}
62+
else if(currentEditor != NULL) {
63+
currentEditor->setEditing(false);
64+
currentEditor->setActive(true);
65+
currentEditor = NULL;
66+
}
67+
else if(currentEditor == NULL && dirIsBack) {
68+
setCurrentMenu(getParentAndReset());
69+
}
70+
else if(currentEditor == NULL && !dirIsBack) {
71+
MenuItem* currentActive = menuMgr.findCurrentActive();
72+
if(currentActive != NULL && currentActive->getMenuType() == MENUTYPE_SUB_VALUE) {
73+
setCurrentMenu(currentActive);
74+
}
75+
}
76+
77+
}
78+
3679
void MenuManager::initWithoutInput(MenuRenderer* renderer, MenuItem* root) {
3780
this->renderer = renderer;
3881
this->currentRoot = this->rootMenu = root;
@@ -79,13 +122,13 @@ void MenuManager::onMenuSelect(bool held) {
79122
if (renderer->tryTakeSelectIfNeeded(0, held ? RPRESS_HELD : RPRESS_PRESSED)) return;
80123

81124
if (held) {
82-
if (currentEditor != NULL && isMenuRuntimeMultiEdit(currentEditor)) {
83-
setCurrentMenu(currentRoot);
84-
}
85-
else {
86-
setCurrentMenu(getParentAndReset());
87-
}
88-
}
125+
if (currentEditor != NULL && isMenuRuntimeMultiEdit(currentEditor)) {
126+
setCurrentMenu(currentRoot);
127+
}
128+
else {
129+
setCurrentMenu(getParentAndReset());
130+
}
131+
}
89132
else if (getCurrentEditor() != NULL) {
90133
stopEditingCurrentItem();
91134
}

src/tcMenu.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ class MenuManager {
8080
*/
8181
void initWithoutInput(MenuRenderer* renderer, MenuItem* root);
8282

83+
/**
84+
* You can add a back button that generally performs the back or left function
85+
* @param backButtonPin the pin on which the back button is assigned.
86+
*/
87+
void setBackButton(uint8_t backButtonPin);
88+
89+
/**
90+
* YOu can add a next button that generally performs the next or right function
91+
* @param nextButtonPin the pin to which the next button is assigned
92+
*/
93+
void setNextButton(uint8_t nextButtonPin);
94+
8395
/**
8496
* Sometimes you need to use the menu structure before everything is initialised, in this case
8597
* you can call this function early on to set up the root menu item.
@@ -108,6 +120,13 @@ class MenuManager {
108120
*/
109121
void onMenuSelect(bool held);
110122

123+
/**
124+
* This provides support for next and back (left, right) functionality by making the menu
125+
* structure respond to such functions in a reasonable way.
126+
* @param dirIsBack true for back (left), false for next (right)
127+
*/
128+
void performDirectionMove(bool dirIsBack);
129+
111130
/**
112131
* Sets the number of items and offset of the items in the current menu
113132
* @param size the number of items

0 commit comments

Comments
 (0)