tcMenu
BaseRenderers.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 _BASE_RENDERERS_H_
7 #define _BASE_RENDERERS_H_
8 
9 #include "tcMenu.h"
10 #include "RuntimeMenuItem.h"
11 #include "MenuIterator.h"
12 #include <TaskManager.h>
13 
16 bool isItemActionable(MenuItem* item);
17 
18 // forward reference.
19 class BaseDialog;
20 
27 #ifndef TC_DISPLAY_UPDATES_PER_SECOND
28 #define TC_DISPLAY_UPDATES_PER_SECOND 4
29 #endif // TC_DISPLAY_UPDATES_PER_SECOND
30 
32 #define MAX_TICKS 0xffff
33 
34 bool isItemActionable(MenuItem* item);
35 
41 enum RenderPressMode: uint8_t {
48 };
49 
58 typedef void (*RendererCallbackFn)(unsigned int currentValue, RenderPressMode userClicked);
59 
60 class BaseMenuRenderer;
61 
68 public:
69  virtual ~CustomDrawing() = default;
74  virtual void started(BaseMenuRenderer* currentRenderer) = 0;
78  virtual void reset() = 0;
85  virtual void renderLoop(unsigned int currentValue, RenderPressMode userClick) = 0;
86 };
87 
92 typedef void (*ResetCallbackFn)();
93 
104 class TitleWidget {
105 private:
106  const uint8_t* const* iconData;
107  volatile uint8_t currentState;
108  volatile bool changed;
109  uint8_t width;
110  uint8_t height;
111  uint8_t maxStateIcons;
112  TitleWidget* next;
113 public:
115  TitleWidget(const uint8_t* const* icons, uint8_t maxStateIcons, uint8_t width, uint8_t height, TitleWidget* next = NULL);
117  uint8_t getCurrentState() const {return currentState;}
119 #ifdef __AVR__
120  const uint8_t* getCurrentIcon() {
121  changed = false;
122  return (const uint8_t *)pgm_read_ptr(&iconData[currentState]);
123  }
124  const uint8_t* getIcon(int num) {
125  if(num >= maxStateIcons) num = 0;
126  return (const uint8_t *)pgm_read_ptr(&iconData[num]);
127  }
128 #else
129  const uint8_t* getCurrentIcon() {
130  changed = false;
131  return iconData[currentState];
132  }
133  const uint8_t* getIcon(int num) {
134  if(num >= maxStateIcons) num = 0;
135  return iconData[num];
136  }
137 #endif
138 
140  void setCurrentState(uint8_t state) {
141  // if outside of allowable icons or value hasn't changed just return.
142  if (state >= maxStateIcons || currentState == state) return;
143 
144  this->currentState = state;
145  this->changed = true;
146  }
147 
149  bool isChanged() {return this->changed;}
150 
152  void setChanged(bool ch) { changed = ch; }
153 
155  uint8_t getWidth() {return width;}
156 
158  uint8_t getHeight() {return height;}
159 
161  uint8_t getMaxValue() { return maxStateIcons; }
162 
164  TitleWidget* getNext() {return next;}
165 
167  void setNext(TitleWidget* next) {this->next = next;}
168 };
169 
170 enum RendererType: uint8_t { RENDER_TYPE_NOLOCAL, RENDER_TYPE_BASE, RENDER_TYPE_CONFIGURABLE };
171 
177 protected:
178  static MenuRenderer* theInstance;
179  char *buffer;
180  uint8_t bufferSize;
181  RendererType rendererType;
182 public:
183  MenuRenderer(RendererType rendererType, int bufferSize) {
184  buffer = new char[bufferSize+1];
185  this->bufferSize = bufferSize;
186  this->rendererType = rendererType;
187  }
192  virtual void initialise() = 0;
193 
200  virtual bool tryTakeSelectIfNeeded(int currentReading, RenderPressMode press) = 0;
201 
206  virtual BaseDialog* getDialog() = 0;
207 
209  virtual ~MenuRenderer() { }
210 
211  /* Gets the rendering instance */
212  static MenuRenderer* getInstance() { return theInstance; }
213 
217  char* getBuffer() {return buffer;}
218 
222  uint8_t getBufferSize() {return bufferSize;}
223 
225  RendererType getRendererType() { return rendererType; }
226 };
227 
232 enum MenuRedrawState: uint8_t {
233  MENUDRAW_NO_CHANGE = 0, MENUDRAW_EDITOR_CHANGE, MENUDRAW_COMPLETE_REDRAW
234 };
235 
236 enum MenuDrawJustification: uint8_t { JUSTIFY_TEXT_LEFT, JUSTIFY_TEXT_RIGHT };
237 
241 class NoRenderer : public MenuRenderer {
242 private:
243  BaseDialog* dialog;
244 public:
245  NoRenderer() : MenuRenderer(RENDER_TYPE_NOLOCAL, 20) { MenuRenderer::theInstance = this; dialog = NULL;}
246  ~NoRenderer() override { }
247  bool tryTakeSelectIfNeeded(int, RenderPressMode) override { return false; }
248  void initialise() override { }
249  BaseDialog* getDialog() override;
250 };
251 
252 class RemoteMenuItem; // forward reference.
253 
262 class BaseMenuRenderer : public MenuRenderer, Executable {
263 public:
264  enum DisplayTakeoverMode { NOT_TAKEN_OVER, TAKEN_OVER_FN, START_CUSTOM_DRAW, RUNNING_CUSTOM_DRAW };
265 
266 protected:
267  uint8_t lastOffset;
268  uint8_t updatesPerSecond;
269  uint16_t ticksToReset;
270  uint16_t resetValInTicks;
271  MenuRedrawState redrawMode;
272  TitleWidget* firstWidget;
273  union {
274  ResetCallbackFn resetCallback;
275  CustomDrawing *customDrawing;
276  };
277  bool isCustomDrawing;
278  DisplayTakeoverMode displayTakenMode;
279  BaseDialog* dialog;
280 
281  RenderPressMode renderFnPressType;
282  RendererCallbackFn renderCallback;
283 public:
288  BaseMenuRenderer(int bufferSize, RendererType rType = RENDER_TYPE_BASE);
289 
293  ~BaseMenuRenderer() override {delete buffer;}
294 
299  void initialise() override;
300 
305  void setUpdatesPerSecond(int updatesSec) {
306  updatesPerSecond = updatesSec;
307  resetValInTicks = 30 * updatesSec;
308  }
309 
316  void setResetIntervalTimeSeconds(uint16_t interval) {
317  resetValInTicks = interval * updatesPerSecond;
318  }
319 
326  isCustomDrawing = false;
327  resetCallback = resetFn;
328  }
329 
336  void setCustomDrawingHandler(CustomDrawing* customDrawingParam) {
337  isCustomDrawing= true;
338  customDrawing = customDrawingParam;
339  }
340 
344  void exec() override;
345 
351  virtual void render() = 0;
352 
360  bool tryTakeSelectIfNeeded(int currentReading, RenderPressMode pressMode) override;
361 
368  virtual MenuItem *getMenuItemAtIndex(MenuItem *pItem, uint8_t idx);
369 
375  virtual int findActiveItem(MenuItem* root);
376 
383  virtual uint8_t itemCount(MenuItem* item, bool includeNonVisible);
384 
389  void setFirstWidget(TitleWidget* widget);
390 
395  void menuAltered() { ticksToReset = resetValInTicks; }
396 
405  void takeOverDisplay(RendererCallbackFn displayFn = nullptr);
406 
411  void giveBackDisplay();
412 
416  void resetToDefault();
417 
422  void redrawRequirement(MenuRedrawState state) { if (state > redrawMode) redrawMode = state; }
423 
429  void menuValueToText(MenuItem* item, MenuDrawJustification justification);
430 
434  void invalidateAll() { redrawMode = MENUDRAW_COMPLETE_REDRAW; }
435 
436 protected:
440  void countdownToDefaulting();
441 };
442 
443 #endif
uint8_t getHeight()
Definition: BaseRenderers.h:158
Definition: MenuItems.h:329
void setUpdatesPerSecond(int updatesSec)
Definition: BaseRenderers.h:305
void setResetCallback(ResetCallbackFn resetFn)
Definition: BaseRenderers.h:325
bool isChanged()
Definition: BaseRenderers.h:149
virtual ~MenuRenderer()
Definition: BaseRenderers.h:209
void setResetIntervalTimeSeconds(uint16_t interval)
Definition: BaseRenderers.h:316
void invalidateAll()
Definition: BaseRenderers.h:434
void redrawRequirement(MenuRedrawState state)
Definition: BaseRenderers.h:422
MenuRedrawState
Definition: BaseRenderers.h:232
char * getBuffer()
Definition: BaseRenderers.h:217
uint8_t getBufferSize()
Definition: BaseRenderers.h:222
uint8_t getWidth()
Definition: BaseRenderers.h:155
Definition: BaseRenderers.h:45
bool isItemActionable(MenuItem *item)
Definition: BaseRenderers.cpp:229
void setCurrentState(uint8_t state)
Definition: BaseRenderers.h:140
Definition: BaseRenderers.h:47
~BaseMenuRenderer() override
Definition: BaseRenderers.h:293
virtual void started(BaseMenuRenderer *currentRenderer)=0
void setNext(TitleWidget *next)
Definition: BaseRenderers.h:167
const uint8_t * getCurrentIcon()
Definition: BaseRenderers.h:120
virtual void reset()=0
Definition: BaseRenderers.h:67
uint8_t itemCount(MenuItem *item, bool includeNonVisble=false)
Definition: MenuIterator.cpp:164
RendererType getRendererType()
Definition: BaseRenderers.h:225
uint8_t getCurrentState() const
Definition: BaseRenderers.h:117
Definition: BaseRenderers.h:176
uint8_t getMaxValue()
Definition: BaseRenderers.h:161
Definition: BaseRenderers.h:241
virtual void renderLoop(unsigned int currentValue, RenderPressMode userClick)=0
Definition: BaseDialog.h:105
TitleWidget * getNext()
Definition: BaseRenderers.h:164
void initialise() override
Definition: BaseRenderers.h:248
Definition: BaseRenderers.h:43
void(* RendererCallbackFn)(unsigned int currentValue, RenderPressMode userClicked)
Definition: BaseRenderers.h:58
void setChanged(bool ch)
Definition: BaseRenderers.h:152
Definition: BaseRenderers.h:262
bool tryTakeSelectIfNeeded(int, RenderPressMode) override
Definition: BaseRenderers.h:247
RenderPressMode
Definition: BaseRenderers.h:41
Definition: BaseRenderers.h:104
void menuAltered()
Definition: BaseRenderers.h:395
void(* ResetCallbackFn)()
Definition: BaseRenderers.h:92
Definition: RemoteMenuItem.h:31
void setCustomDrawingHandler(CustomDrawing *customDrawingParam)
Definition: BaseRenderers.h:336