tcMenu
BaseRenderers.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry).
3  * This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
4  */
5 
11 #ifndef _BASE_RENDERERS_H_
12 #define _BASE_RENDERERS_H_
13 
14 #include "tcMenu.h"
15 #include "RuntimeMenuItem.h"
16 #include "MenuIterator.h"
17 #include <TaskManager.h>
18 
21 bool isItemActionable(MenuItem* item);
22 
23 // forward reference.
24 class BaseDialog;
25 
32 #ifndef TC_DISPLAY_UPDATES_PER_SECOND
33 #define TC_DISPLAY_UPDATES_PER_SECOND 4
34 #endif // TC_DISPLAY_UPDATES_PER_SECOND
35 
36 // The updates per second value that indicates the display is not updating, and doesn't schedule rendering.
37 #define UPDATES_SEC_DISPLAY_OFF 0xFFU
38 
39 #define RENDERER_MAXIMUM_TICK_MASK 0x1FFF
40 #define RENDERER_RESET_OFF 0x8000
41 #define RENDERER_RESET_NOTIFY_ONLY 0x4000
42 
43 bool isItemActionable(MenuItem* item);
44 
50 enum RenderPressMode: uint8_t {
56  RPRESS_HELD = 2
57 };
58 
67 typedef void (*RendererCallbackFn)(unsigned int currentValue, RenderPressMode userClicked);
68 
69 class BaseMenuRenderer;
70 
77 public:
78  virtual ~CustomDrawing() = default;
83  virtual void started(BaseMenuRenderer* currentRenderer) = 0;
87  virtual void reset() = 0;
94  virtual void renderLoop(unsigned int currentValue, RenderPressMode userClick) = 0;
95 };
96 
101 typedef void (*ResetCallbackFn)();
102 
108  ResetCallbackFn resetFn;
109 public:
111  void started(BaseMenuRenderer* /*currentRenderer*/) override { }
112  void reset() override {resetFn();}
113  void renderLoop(unsigned int /*currentValue*/, RenderPressMode /*userClick*/) override {}
114 };
115 
126 class TitleWidget {
127 private:
128  const uint8_t* const* iconData;
129  volatile uint8_t currentState;
130  volatile bool changed;
131  uint8_t width;
132  uint8_t height;
133  uint8_t maxStateIcons;
134  TitleWidget* next;
135 public:
137  TitleWidget(const uint8_t* const* icons, uint8_t maxStateIcons, uint8_t width, uint8_t height, TitleWidget* next = NULL);
139  uint8_t getCurrentState() const {return currentState;}
141 #ifdef __AVR__
142  const uint8_t* getCurrentIcon() {
143  changed = false;
144  return (const uint8_t *)pgm_read_ptr(&iconData[currentState]);
145  }
146  const uint8_t* getIcon(int num) {
147  if(num >= maxStateIcons) num = 0;
148  return (const uint8_t *)pgm_read_ptr(&iconData[num]);
149  }
150 #else
151  const uint8_t* getCurrentIcon() {
152  changed = false;
153  return iconData[currentState];
154  }
155  const uint8_t* getIcon(int num) {
156  if(num >= maxStateIcons) num = 0;
157  return iconData[num];
158  }
159 #endif
160 
162  void setCurrentState(uint8_t state) {
163  // if outside of allowable icons or value hasn't changed just return.
164  if (state >= maxStateIcons || currentState == state) return;
165 
166  this->currentState = state;
167  this->changed = true;
168  }
169 
171  bool isChanged() {return this->changed;}
172 
174  void setChanged(bool ch) { changed = ch; }
175 
177  uint8_t getWidth() {return width;}
178 
180  uint8_t getHeight() {return height;}
181 
183  uint8_t getMaxValue() { return maxStateIcons; }
184 
186  TitleWidget* getNext() {return next;}
187 
189  void setNext(TitleWidget* next) {this->next = next;}
190 };
191 
192 enum RendererType: uint8_t { RENDER_TYPE_NOLOCAL, RENDER_TYPE_BASE, RENDER_TYPE_CONFIGURABLE };
193 
199 protected:
200  static MenuRenderer* theInstance;
201  char *buffer;
202  uint8_t bufferSize;
203  RendererType rendererType;
204 public:
205  MenuRenderer(RendererType rendererType, int bufferSize) {
206  buffer = new char[bufferSize+1];
207  this->bufferSize = bufferSize;
208  this->rendererType = rendererType;
209  }
214  virtual void initialise() = 0;
215 
222  virtual bool tryTakeSelectIfNeeded(int currentReading, RenderPressMode press) = 0;
223 
228  virtual BaseDialog* getDialog() = 0;
229 
231  virtual ~MenuRenderer() { }
232 
233  /* Gets the rendering instance */
234  static MenuRenderer* getInstance() { return theInstance; }
235 
239  char* getBuffer() {return buffer;}
240 
244  uint8_t getBufferSize() {return bufferSize;}
245 
247  RendererType getRendererType() { return rendererType; }
248 };
249 
254 enum MenuRedrawState: uint8_t {
255  MENUDRAW_NO_CHANGE = 0, MENUDRAW_EDITOR_CHANGE, MENUDRAW_COMPLETE_REDRAW
256 };
257 
261 class NoRenderer : public MenuRenderer {
262 private:
263  BaseDialog* dialog;
264 public:
265  NoRenderer() : MenuRenderer(RENDER_TYPE_NOLOCAL, 20) { MenuRenderer::theInstance = this; dialog = nullptr;}
266  ~NoRenderer() override = default;
267  bool tryTakeSelectIfNeeded(int, RenderPressMode) override { return false; }
268  void initialise() override { }
269  BaseDialog* getDialog() override;
270 };
271 
272 class RemoteMenuItem; // forward reference.
273 
282 class BaseMenuRenderer : public MenuRenderer, Executable {
283 public:
284  enum DisplayTakeoverMode { NOT_TAKEN_OVER, TAKEN_OVER_FN, START_CUSTOM_DRAW, RUNNING_CUSTOM_DRAW };
285 
286 protected:
287  uint8_t lastOffset;
288  uint8_t updatesPerSecond;
289  uint8_t displayNumber = 0;
290  MenuRedrawState redrawMode;
291  uint16_t ticksToReset;
292  uint16_t resetValInTicks;
293  TitleWidget* firstWidget;
294  CustomDrawing *customDrawing;
295  BaseDialog* dialog;
296  DisplayTakeoverMode displayTakenMode;
297  RenderPressMode renderFnPressType;
298  RendererCallbackFn renderCallback;
299  MenuItem* activeItem = nullptr;
300 public:
305  BaseMenuRenderer(int bufferSize, RendererType rType = RENDER_TYPE_BASE, uint8_t displayNum = 0);
306 
310  ~BaseMenuRenderer() override {delete buffer;}
311 
316  void initialise() override;
317 
323  void setUpdatesPerSecond(int updatesSec);
324 
329  void turnOffDisplayUpdates(bool /*updatesEnabled*/) {
330  updatesPerSecond = UPDATES_SEC_DISPLAY_OFF;
331  }
332 
339  void setResetIntervalTimeSeconds(uint16_t interval) {
340  resetValInTicks = (interval * updatesPerSecond) & RENDERER_MAXIMUM_TICK_MASK;
341  }
342 
347  resetValInTicks = RENDERER_RESET_OFF;
348  }
349 
355  void resetNotifiesOnly(uint16_t ticks) {
356  resetValInTicks = (ticks & RENDERER_MAXIMUM_TICK_MASK) | RENDERER_RESET_NOTIFY_ONLY;
357  }
358 
365  customDrawing = new ResetCallbackFunctionCustomDraw(resetFn);
366  }
367 
374  void setCustomDrawingHandler(CustomDrawing* customDrawingParam) {
375  customDrawing = customDrawingParam;
376  }
377 
382  return customDrawing;
383  }
384 
388  void exec() override;
389 
395  virtual void render() = 0;
396 
404  bool tryTakeSelectIfNeeded(int currentReading, RenderPressMode pressMode) override;
405 
412  virtual MenuItem *getMenuItemAtIndex(MenuItem *pItem, uint8_t idx);
413 
420  virtual int findItemIndex(MenuItem *root, MenuItem *toFind);
421 
428  virtual uint8_t itemCount(MenuItem* item, bool includeNonVisible);
429 
436  virtual uint8_t setActiveItem(MenuItem* item);
437 
438  MenuItem* getActiveItem() { return activeItem; }
439 
445  int findActiveItem(MenuItem* root);
446 
447 
452  void setFirstWidget(TitleWidget* widget);
453  TitleWidget* getFirstWidget() { return this->firstWidget; }
454 
459  void menuAltered() { ticksToReset = resetValInTicks; }
460 
471  void takeOverDisplay(RendererCallbackFn displayFn = nullptr);
472 
477  void giveBackDisplay();
478 
482  void resetToDefault();
483 
488  void redrawRequirement(MenuRedrawState state) { if (state > redrawMode) redrawMode = state; }
489 
493  void invalidateAll() { redrawMode = MENUDRAW_COMPLETE_REDRAW; }
494 
495  static BaseMenuRenderer* getInstance() { return reinterpret_cast<BaseMenuRenderer *>(theInstance); }
496 
497 protected:
501  void countdownToDefaulting();
502 };
503 
504 bool isCardLayoutActive(MenuItem* root);
505 
506 #endif
RenderPressMode
Definition: BaseRenderers.h:50
@ RPRESS_NONE
Definition: BaseRenderers.h:52
@ RPRESS_PRESSED
Definition: BaseRenderers.h:54
@ RPRESS_HELD
Definition: BaseRenderers.h:56
bool isItemActionable(MenuItem *item)
Definition: BaseRenderers.cpp:249
void(* RendererCallbackFn)(unsigned int currentValue, RenderPressMode userClicked)
Definition: BaseRenderers.h:67
MenuRedrawState
Definition: BaseRenderers.h:254
void(* ResetCallbackFn)()
Definition: BaseRenderers.h:101
Provides a number of utility functions for the processing of menu item structures.
Contains definitions of menu items that can be fully defined at runtime with no need for prog mem str...
Definition: BaseDialog.h:102
Definition: BaseRenderers.h:282
void resetToDefault()
Definition: BaseRenderers.cpp:110
void setUpdatesPerSecond(int updatesSec)
Definition: BaseRenderers.cpp:212
CustomDrawing * getCurrentCustomDrawing()
Definition: BaseRenderers.h:381
void countdownToDefaulting()
Definition: BaseRenderers.cpp:126
BaseMenuRenderer(int bufferSize, RendererType rType=RENDER_TYPE_BASE, uint8_t displayNum=0)
Definition: BaseRenderers.cpp:38
void turnOffDisplayUpdates(bool)
Definition: BaseRenderers.h:329
virtual MenuItem * getMenuItemAtIndex(MenuItem *pItem, uint8_t idx)
Definition: BaseRenderers.cpp:196
void setResetIntervalTimeSeconds(uint16_t interval)
Definition: BaseRenderers.h:339
void initialise() override
Definition: BaseRenderers.cpp:57
~BaseMenuRenderer() override
Definition: BaseRenderers.h:310
void turnOffResetLogic()
Definition: BaseRenderers.h:346
void resetNotifiesOnly(uint16_t ticks)
Definition: BaseRenderers.h:355
void takeOverDisplay(RendererCallbackFn displayFn=nullptr)
Definition: BaseRenderers.cpp:140
virtual void render()=0
virtual uint8_t itemCount(MenuItem *item, bool includeNonVisible)
Definition: BaseRenderers.cpp:187
void redrawRequirement(MenuRedrawState state)
Definition: BaseRenderers.h:488
void exec() override
Definition: BaseRenderers.cpp:82
void invalidateAll()
Definition: BaseRenderers.h:493
virtual uint8_t setActiveItem(MenuItem *item)
Definition: BaseRenderers.cpp:162
int findActiveItem(MenuItem *root)
Definition: BaseRenderers.cpp:183
void giveBackDisplay()
Definition: BaseRenderers.cpp:148
void setFirstWidget(TitleWidget *widget)
Definition: BaseRenderers.cpp:157
void menuAltered()
Definition: BaseRenderers.h:459
void setCustomDrawingHandler(CustomDrawing *customDrawingParam)
Definition: BaseRenderers.h:374
void setResetCallback(ResetCallbackFn resetFn)
Definition: BaseRenderers.h:364
virtual int findItemIndex(MenuItem *root, MenuItem *toFind)
Definition: BaseRenderers.cpp:170
bool tryTakeSelectIfNeeded(int currentReading, RenderPressMode pressMode) override
Definition: BaseRenderers.cpp:68
Definition: BaseRenderers.h:76
virtual void renderLoop(unsigned int currentValue, RenderPressMode userClick)=0
virtual void reset()=0
virtual void started(BaseMenuRenderer *currentRenderer)=0
Definition: MenuItems.h:329
Definition: BaseRenderers.h:198
virtual BaseDialog * getDialog()=0
char * getBuffer()
Definition: BaseRenderers.h:239
uint8_t getBufferSize()
Definition: BaseRenderers.h:244
virtual bool tryTakeSelectIfNeeded(int currentReading, RenderPressMode press)=0
virtual ~MenuRenderer()
Definition: BaseRenderers.h:231
RendererType getRendererType()
Definition: BaseRenderers.h:247
virtual void initialise()=0
Definition: BaseRenderers.h:261
BaseDialog * getDialog() override
Definition: BaseRenderers.cpp:242
bool tryTakeSelectIfNeeded(int, RenderPressMode) override
Definition: BaseRenderers.h:267
void initialise() override
Definition: BaseRenderers.h:268
Definition: RemoteMenuItem.h:30
Definition: BaseRenderers.h:107
void reset() override
Definition: BaseRenderers.h:112
void renderLoop(unsigned int, RenderPressMode) override
Definition: BaseRenderers.h:113
void started(BaseMenuRenderer *) override
Definition: BaseRenderers.h:111
Definition: BaseRenderers.h:126
uint8_t getHeight()
Definition: BaseRenderers.h:180
uint8_t getWidth()
Definition: BaseRenderers.h:177
void setChanged(bool ch)
Definition: BaseRenderers.h:174
TitleWidget(const uint8_t *const *icons, uint8_t maxStateIcons, uint8_t width, uint8_t height, TitleWidget *next=NULL)
Definition: BaseRenderers.cpp:223
uint8_t getCurrentState() const
Definition: BaseRenderers.h:139
TitleWidget * getNext()
Definition: BaseRenderers.h:186
uint8_t getMaxValue()
Definition: BaseRenderers.h:183
void setNext(TitleWidget *next)
Definition: BaseRenderers.h:189
const uint8_t * getCurrentIcon()
Definition: BaseRenderers.h:142
bool isChanged()
Definition: BaseRenderers.h:171
void setCurrentState(uint8_t state)
Definition: BaseRenderers.h:162
The menu manager is responsible for managing a set of menu items, and is configured with a renderer a...