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() {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 protected:
266  uint8_t lastOffset;
267  uint8_t updatesPerSecond;
268  uint16_t ticksToReset;
269  uint16_t resetValInTicks;
270  MenuRedrawState redrawMode;
271  TitleWidget* firstWidget;
272  union {
273  ResetCallbackFn resetCallback;
274  CustomDrawing *customDrawing;
275  };
276  bool isCustomDrawing;
277  DisplayTakeoverMode displayTakenMode;
278  BaseDialog* dialog;
279 
280  RenderPressMode renderFnPressType;
281  RendererCallbackFn renderCallback;
282 public:
287  BaseMenuRenderer(int bufferSize, RendererType rType = RENDER_TYPE_BASE);
288 
292  ~BaseMenuRenderer() override {delete buffer;}
293 
298  void initialise() override;
299 
304  void setUpdatesPerSecond(int updatesSec) {
305  updatesPerSecond = updatesSec;
306  resetValInTicks = 30 * updatesSec;
307  }
308 
315  void setResetIntervalTimeSeconds(uint16_t interval) {
316  resetValInTicks = interval * updatesPerSecond;
317  }
318 
325  isCustomDrawing = false;
326  resetCallback = resetFn;
327  }
328 
335  void setCustomDrawingHandler(CustomDrawing* customDrawingParam) {
336  isCustomDrawing= true;
337  customDrawing = customDrawingParam;
338  }
339 
343  void exec() override;
344 
350  virtual void render() = 0;
351 
359  bool tryTakeSelectIfNeeded(int currentReading, RenderPressMode pressMode) override;
360 
365  void setFirstWidget(TitleWidget* widget);
366 
371  void menuAltered() { ticksToReset = resetValInTicks; }
372 
381  void takeOverDisplay(RendererCallbackFn displayFn = nullptr);
382 
387  void giveBackDisplay();
388 
392  void resetToDefault();
393 
398  void prepareNewSubmenu();
399 
404  void redrawRequirement(MenuRedrawState state) { if (state > redrawMode) redrawMode = state; }
405 
411  void menuValueToText(MenuItem* item, MenuDrawJustification justification);
412 
416  void invalidateAll() { redrawMode = MENUDRAW_COMPLETE_REDRAW; }
417 
418 protected:
422  void countdownToDefaulting();
423 };
424 
425 #endif
uint8_t getHeight()
Definition: BaseRenderers.h:158
Definition: MenuItems.h:316
void setUpdatesPerSecond(int updatesSec)
Definition: BaseRenderers.h:304
void setResetCallback(ResetCallbackFn resetFn)
Definition: BaseRenderers.h:324
bool isChanged()
Definition: BaseRenderers.h:149
virtual ~MenuRenderer()
Definition: BaseRenderers.h:209
void setResetIntervalTimeSeconds(uint16_t interval)
Definition: BaseRenderers.h:315
void invalidateAll()
Definition: BaseRenderers.h:416
void redrawRequirement(MenuRedrawState state)
Definition: BaseRenderers.h:404
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:206
void setCurrentState(uint8_t state)
Definition: BaseRenderers.h:140
Definition: BaseRenderers.h:47
~BaseMenuRenderer() override
Definition: BaseRenderers.h:292
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
RendererType getRendererType()
Definition: BaseRenderers.h:225
Definition: BaseRenderers.h:176
uint8_t getMaxValue()
Definition: BaseRenderers.h:161
uint8_t getCurrentState()
Definition: BaseRenderers.h:117
Definition: BaseRenderers.h:241
virtual void renderLoop(unsigned int currentValue, RenderPressMode userClick)=0
Definition: BaseDialog.h:60
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:371
void(* ResetCallbackFn)()
Definition: BaseRenderers.h:92
Definition: RemoteMenuItem.h:30
void setCustomDrawingHandler(CustomDrawing *customDrawingParam)
Definition: BaseRenderers.h:335