tcMenu
tcMenu.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 https://www.thecoderscorner.com (Nutricherry LTD).
3  * This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
4  */
5 
6 #ifndef TCMENU_MANAGER_H
7 #define TCMENU_MANAGER_H
8 
9 #include <IoAbstraction.h>
10 #include "tcUtil.h"
11 #include "MenuItems.h"
12 #include "MenuHistoryNavigator.h"
13 #include "RuntimeMenuItem.h"
14 #include "BaseRenderers.h"
15 #include "RemoteAuthentication.h"
16 #include "EepromItemStorage.h"
17 
18 // forward reference
19 class MenuRenderer;
20 
29 class SecuredMenuPopup;
30 
36 public:
41  virtual void structureHasChanged()=0;
42 
48  virtual bool menuEditStarting(MenuItem* item)=0;
49 
56  virtual void menuEditEnded(MenuItem* item)=0;
57 };
58 
63 private:
64  MenuCallbackFn commitCb;
65 public:
67  commitCb = callbackFn;
68  }
69 
70  void structureHasChanged() override {}
71  bool menuEditStarting(MenuItem*) override { return true; }
72  void menuEditEnded(MenuItem* item) override {
73  commitCb(item->getId());
74  }
75 };
76 
77 #ifndef MAX_MENU_NOTIFIERS
78 #define MAX_MENU_NOTIFIERS 4
79 #endif
80 
85 class MenuManager {
86 private:
87  tcnav::MenuNavigationStore navigator{};
88  MenuItem* currentEditor;
89  MenuRenderer* renderer;
90  SecuredMenuPopup* securedMenuPopup;
91  AuthenticationManager *authenticationManager;
92  EepromAbstraction* eepromRef;
93  MenuManagerObserver* structureNotifier[MAX_MENU_NOTIFIERS];
94 public:
95  MenuManager();
96 
106  void initForEncoder(MenuRenderer* renderer, MenuItem* root, pinid_t encoderPinA, pinid_t encoderPinB, pinid_t encoderButton, EncoderType type = FULL_CYCLE);
107 
116  void initForUpDownOk(MenuRenderer* renderer, MenuItem* root, pinid_t downPin, pinid_t upPin, pinid_t okPin);
117 
130  void initWithoutInput(MenuRenderer* renderer, MenuItem* root);
131 
136  void setBackButton(pinid_t backButtonPin);
137 
142  void setNextButton(pinid_t nextButtonPin);
143 
148  void setRootMenu(MenuItem* menuItem) {
149  navigator.setRootItem(menuItem);
150  }
151 
158  void setAuthenticator(AuthenticationManager* manager) { authenticationManager = manager; }
159 
166  void setItemCommittedHook(MenuCallbackFn commitCallback) {
167  addChangeNotification(new CommitCallbackObserver(commitCallback));
168  }
169 
174  void valueChanged(int value);
175 
180  void onMenuSelect(bool held);
181 
187  void performDirectionMove(bool dirIsBack);
188 
194  void setItemsInCurrentMenu(int size, int offs = 0);
195 
196  EepromAbstraction* getEepromAbstraction() { return eepromRef; }
197 
202  void setEepromRef(EepromAbstraction* globalRom) {
203  eepromRef = globalRom;
204  }
205 
210  void load(EepromAbstraction& eeprom, uint16_t magicKey = 0xfade, TimerFn onEepromEmpty = nullptr);
211 
212 
217  void load(uint16_t magicKey = 0xfade, TimerFn onEepromEmpty = nullptr);
218 
219 
227  void save(uint16_t magicKey = 0xfade) { if(eepromRef) saveMenuStructure(eepromRef, magicKey); }
228 
234  void save(EepromAbstraction& eeprom, uint16_t magicKey = 0xfade) { saveMenuStructure(&eeprom, magicKey); }
235 
239  MenuItem* findCurrentActive();
240 
247  bool activateMenuItem(MenuItem* item);
248 
252  MenuItem* getRoot() { return navigator.getRoot(); }
253 
257  MenuRenderer* getRenderer() { return renderer; }
258 
262  MenuItem* getCurrentEditor() { return currentEditor; }
263 
268  void setCurrentEditor(MenuItem* editor);
269 
274  void changeMenu(MenuItem* possibleActive=nullptr);
275 
283  void navigateToMenu(MenuItem* theNewItem, MenuItem* possibleActive = nullptr, bool customMenu = false);
284 
288  void resetMenu(bool completeReset);
289 
293  MenuItem* getCurrentMenu() { return navigator.getCurrentRoot(); }
294 
295  MenuItem* getCurrentSubMenu() { return navigator.getCurrentSubMenu(); }
296 
300  MenuItem* getParentAndReset();
301 
307  return switches.getEncoder() != nullptr ? switches.getEncoder()->getCurrentReading() : 0;
308  }
309 
315  SecuredMenuPopup* secureMenuInstance();
316 
317  void stopEditingCurrentItem(bool checkMultiPart);
318 
326  void addMenuAfter(MenuItem* existing, MenuItem* toAdd, bool silent = false);
327 
332  void notifyStructureChanged();
333 
338  void addChangeNotification(MenuManagerObserver* observer);
339 protected:
340  void setupForEditing(MenuItem* item);
341  void actionOnCurrentItem(MenuItem * toEdit);
342  void actionOnSubMenu(MenuItem* nextSub);
343 
344  void notifyEditEnd(MenuItem *pItem);
345  bool notifyEditStarting(MenuItem *pItem);
346 };
347 
351 extern MenuManager menuMgr;
352 
353 #endif // defined header file
Definition: MenuItems.h:324
Definition: tcMenu.h:62
void saveMenuStructure(EepromAbstraction *eeprom, uint16_t magicKey=0xfade)
Definition: EepromItemStorage.cpp:70
void setItemCommittedHook(MenuCallbackFn commitCallback)
Definition: tcMenu.h:166
void setEepromRef(EepromAbstraction *globalRom)
Definition: tcMenu.h:202
Definition: tcMenu.h:85
int getCurrentRangeValue()
Definition: tcMenu.h:306
uint16_t getId() const
Definition: MenuItems.cpp:74
Definition: MenuHistoryNavigator.h:22
void(* MenuCallbackFn)(int id)
Definition: MenuItems.h:35
bool menuEditStarting(MenuItem *) override
Definition: tcMenu.h:71
MenuItem * getCurrentEditor()
Definition: tcMenu.h:262
MenuRenderer * getRenderer()
Definition: tcMenu.h:257
Definition: BaseRenderers.h:176
virtual void structureHasChanged()=0
void structureHasChanged() override
Definition: tcMenu.h:70
void save(uint16_t magicKey=0xfade)
Definition: tcMenu.h:227
void save(EepromAbstraction &eeprom, uint16_t magicKey=0xfade)
Definition: tcMenu.h:234
Definition: SecuredMenuPopup.h:34
MenuItem * getCurrentMenu()
Definition: tcMenu.h:293
Definition: tcMenu.h:35
Definition: RemoteAuthentication.h:29
virtual void menuEditEnded(MenuItem *item)=0
void setRootMenu(MenuItem *menuItem)
Definition: tcMenu.h:148
MenuItem * getRoot()
Definition: tcMenu.h:252
virtual bool menuEditStarting(MenuItem *item)=0
void setAuthenticator(AuthenticationManager *manager)
Definition: tcMenu.h:158
MenuManager menuMgr
Definition: tcMenu.cpp:14
void menuEditEnded(MenuItem *item) override
Definition: tcMenu.h:72