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 
164  if(authenticationManager == nullptr) authenticationManager = new NoAuthenticationManager();
165  return authenticationManager;
166  }
167 
174  void setItemCommittedHook(MenuCallbackFn commitCallback) {
175  addChangeNotification(new CommitCallbackObserver(commitCallback));
176  }
177 
182  void valueChanged(int value);
183 
188  void onMenuSelect(bool held);
189 
195  void performDirectionMove(bool dirIsBack);
196 
202  void setItemsInCurrentMenu(int size, int offs = 0);
203 
204  EepromAbstraction* getEepromAbstraction() { return eepromRef; }
205 
210  void setEepromRef(EepromAbstraction* globalRom) {
211  eepromRef = globalRom;
212  }
213 
218  void load(EepromAbstraction& eeprom, uint16_t magicKey = 0xfade, TimerFn onEepromEmpty = nullptr);
219 
220 
225  void load(uint16_t magicKey = 0xfade, TimerFn onEepromEmpty = nullptr);
226 
227 
235  void save(uint16_t magicKey = 0xfade) { if(eepromRef) saveMenuStructure(eepromRef, magicKey); }
236 
242  void save(EepromAbstraction& eeprom, uint16_t magicKey = 0xfade) { saveMenuStructure(&eeprom, magicKey); }
243 
247  MenuItem* findCurrentActive();
248 
255  bool activateMenuItem(MenuItem* item);
256 
260  MenuItem* getRoot() { return navigator.getRoot(); }
261 
265  MenuRenderer* getRenderer() { return renderer; }
266 
270  MenuItem* getCurrentEditor() { return currentEditor; }
271 
276  void setCurrentEditor(MenuItem* editor);
277 
282  void changeMenu(MenuItem* possibleActive=nullptr);
283 
291  void navigateToMenu(MenuItem* theNewItem, MenuItem* possibleActive = nullptr, bool customMenu = false);
292 
296  void resetMenu(bool completeReset);
297 
301  MenuItem* getCurrentMenu() { return navigator.getCurrentRoot(); }
302 
303  MenuItem* getCurrentSubMenu() { return navigator.getCurrentSubMenu(); }
304 
308  MenuItem* getParentAndReset();
309 
315  return switches.getEncoder() != nullptr ? switches.getEncoder()->getCurrentReading() : 0;
316  }
317 
323  SecuredMenuPopup* secureMenuInstance();
324 
325  void stopEditingCurrentItem(bool checkMultiPart);
326 
334  void addMenuAfter(MenuItem* existing, MenuItem* toAdd, bool silent = false);
335 
340  void notifyStructureChanged();
341 
346  void addChangeNotification(MenuManagerObserver* observer);
347 protected:
348  void setupForEditing(MenuItem* item);
349  void actionOnCurrentItem(MenuItem * toEdit);
350  void actionOnSubMenu(MenuItem* nextSub);
351 
352  void notifyEditEnd(MenuItem *pItem);
353  bool notifyEditStarting(MenuItem *pItem);
354 };
355 
359 extern MenuManager menuMgr;
360 
361 #endif // defined header file
Definition: MenuItems.h:329
Definition: tcMenu.h:62
void saveMenuStructure(EepromAbstraction *eeprom, uint16_t magicKey=0xfade)
Definition: EepromItemStorage.cpp:70
void setItemCommittedHook(MenuCallbackFn commitCallback)
Definition: tcMenu.h:174
void setEepromRef(EepromAbstraction *globalRom)
Definition: tcMenu.h:210
Definition: tcMenu.h:85
int getCurrentRangeValue()
Definition: tcMenu.h:314
menuid_t getId() const
Definition: MenuItems.cpp:74
Definition: MenuHistoryNavigator.h:22
void(* MenuCallbackFn)(int id)
Definition: MenuItems.h:40
bool menuEditStarting(MenuItem *) override
Definition: tcMenu.h:71
MenuItem * getCurrentEditor()
Definition: tcMenu.h:270
MenuRenderer * getRenderer()
Definition: tcMenu.h:265
Definition: BaseRenderers.h:176
virtual void structureHasChanged()=0
void structureHasChanged() override
Definition: tcMenu.h:70
void save(uint16_t magicKey=0xfade)
Definition: tcMenu.h:235
void save(EepromAbstraction &eeprom, uint16_t magicKey=0xfade)
Definition: tcMenu.h:242
Definition: SecuredMenuPopup.h:34
MenuItem * getCurrentMenu()
Definition: tcMenu.h:301
Definition: tcMenu.h:35
Definition: RemoteAuthentication.h:33
Definition: RemoteAuthentication.h:166
virtual void menuEditEnded(MenuItem *item)=0
void setRootMenu(MenuItem *menuItem)
Definition: tcMenu.h:148
MenuItem * getRoot()
Definition: tcMenu.h:260
virtual bool menuEditStarting(MenuItem *item)=0
AuthenticationManager * getAuthenticator()
Definition: tcMenu.h:163
void setAuthenticator(AuthenticationManager *manager)
Definition: tcMenu.h:158
MenuManager menuMgr
Definition: tcMenu.cpp:15
void menuEditEnded(MenuItem *item) override
Definition: tcMenu.h:72