tcMenu
MenuItems.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 
17 #ifndef _MENUITEMS_h
18 #define _MENUITEMS_h
19 
20 #include "tcUtil.h"
21 
22 #define INFO_LOCATION_PGM true
23 #define INFO_LOCATION_RAM false
24 
26 #define MAXIMUM_ID_ALLOWED 32000
27 
28 typedef uint16_t menuid_t;
29 
31 #define INVALID_MENU_ID 0xffff
32 
34 #define NAME_SIZE_T 20
35 
37 #define NO_CALLBACK NULL
38 
40 typedef void (*MenuCallbackFn)(int id);
41 
46 struct AnyMenuInfo {
48  char name[NAME_SIZE_T];
50  menuid_t id;
52  uint16_t eepromAddr;
54  uint16_t maxValue;
57 };
58 
68  char name[NAME_SIZE_T];
70  menuid_t id;
72  uint16_t eepromAddr;
74  uint16_t maxValue;
77 
82  int16_t offset;
89  uint16_t divisor;
93  char unitName[5];
94 };
95 
103 struct EnumMenuInfo {
105  char name[NAME_SIZE_T];
107  menuid_t id;
109  uint16_t eepromAddr;
111  uint16_t maxValue;
114 
123  const char * const *menuItems;
124 };
125 
129 enum BooleanNaming : uint8_t {
136 };
137 
146  char name[NAME_SIZE_T];
148  menuid_t id;
150  uint16_t eepromAddr;
152  uint16_t maxValue;
155 
161 };
162 
169 struct SubMenuInfo {
171  char name[NAME_SIZE_T];
173  menuid_t id;
175  uint16_t eepromAddr;
177  uint16_t maxValue;
180 };
181 
188  char name[NAME_SIZE_T];
190  menuid_t id;
192  uint16_t eepromAddr;
197 };
198 
202 enum Flags : uint8_t {
231 };
232 
233 #define MENUITEM_ALL_REMOTES 0xFC00
234 
242 enum MenuType : uint8_t {
269 
282 };
283 
287 enum RenderFnMode : uint8_t {
302 };
303 
304 // forward reference
305 class RuntimeMenuItem;
306 
318 typedef int(*RuntimeRenderingFn)(RuntimeMenuItem* item, uint8_t row, RenderFnMode mode, char* buffer, int bufferSize);
319 
329 class MenuItem {
330 protected:
331  uint16_t flags;
332  MenuItem* next;
334  union {
335  const AnyMenuInfo *info;
336  RuntimeRenderingFn renderFn;
337  };
338  MenuType menuType;
339 public:
340 
346  uint8_t copyNameToBuffer(char* sz, int size) const { return copyNameToBuffer(sz, 0, size);}
347 
355  uint8_t copyNameToBuffer(char* sz, int offset, int size) const;
357  menuid_t getId() const;
359  uint16_t getMaximumValue() const;
361  uint16_t getEepromPosition() const;
363  MenuType getMenuType() const { return menuType; }
365  void triggerCallback() const;
366 
367  bool isInfoProgMem() const { return bitRead(flags, MENUITEM_INFO_STRUCT_PGM); }
368 
370  void setChanged(bool changed) { bitWrite(flags, MENUITEM_CHANGED, changed); }
372  bool isChanged() const { return bitRead(flags, MENUITEM_CHANGED); }
374  bool isSendRemoteNeeded(uint8_t remoteNo) const;
376  void setSendRemoteNeededAll();
378  void clearSendRemoteNeededAll();
380  void setSendRemoteNeeded(uint8_t remoteNo, bool needed);
381 
383  void setActive(bool active);
385  bool isActive() const { return bitRead(flags, MENUITEM_ACTIVE); }
386 
388  void setEditing(bool active);
390  bool isEditing() const { return bitRead(flags, MENUITEM_EDITING); }
391 
393  void setReadOnly(bool active) { bitWrite(flags, MENUITEM_READONLY, active); }
395  bool isReadOnly() const { return bitRead(flags, MENUITEM_READONLY); }
396 
398  void setLocalOnly(bool localOnly) { bitWrite(flags, MENUITEM_LOCAL_ONLY, localOnly); }
400  bool isLocalOnly() const { return bitRead(flags, MENUITEM_LOCAL_ONLY); }
401 
403  void setSecured(bool secured) { bitWrite(flags, MENUITEM_PIN_SECURED, secured); }
405  bool isSecured() const { return bitRead(flags, MENUITEM_PIN_SECURED); }
406 
408  void setVisible(bool visible) { bitWrite(flags, MENUITEM_PIN_VISIBLE, visible); }
410  bool isVisible() const { return bitRead(flags, MENUITEM_PIN_VISIBLE); }
411 
413  MenuItem* getNext() const { return next; }
415  void setNext(MenuItem* next) { this->next = next; }
416 
420  void changeOccurred(bool silent);
421 protected:
425  MenuItem(MenuType menuType, const AnyMenuInfo* menuInfo, MenuItem* next, bool infoProgMem);
426 
427 };
428 
433 class ValueMenuItem : public MenuItem {
434 protected:
435  uint16_t currentValue;
436 
438  ValueMenuItem(MenuType menuType, const AnyMenuInfo* info, uint16_t defaultVal, MenuItem* next, bool infoPgm)
439  : MenuItem(menuType, info, next, infoPgm) {
440  this->currentValue = defaultVal;
441  }
442 public:
444  void setCurrentValue(uint16_t val, bool silent = false);
445 
447  uint16_t getCurrentValue() const { return currentValue; }
448 };
449 
455 public:
456  WholeAndFraction() {
457  this->whole = this->fraction = this->negative = 0;
458  }
459 
460  WholeAndFraction(const WholeAndFraction& that) {
461  this->whole = that.whole;
462  this->fraction = that.fraction;
463  this->negative = that.negative;
464  }
465 
466  WholeAndFraction(uint16_t whole, uint16_t fract, bool negative) {
467  this->whole = whole;
468  this->fraction = fract;
469  this->negative = negative;
470  }
471  uint32_t whole: 15;
472  uint32_t fraction: 16;
473  uint32_t negative: 1;
474 };
475 
486 public:
494  AnalogMenuItem(const AnalogMenuInfo* info, uint16_t defaultVal, MenuItem* next = nullptr, bool infoInPgm = INFO_LOCATION_PGM)
495  : ValueMenuItem(MENUTYPE_INT_VALUE, (const AnyMenuInfo*)info, defaultVal, next, infoInPgm) {;}
496 
498  int getOffset() const;
500  uint16_t getDivisor() const;
502  int unitNameLength() const;
504  void copyUnitToBuffer(char* unitBuff, uint8_t size = 5) const;
505 
511  void copyValue(char* buffer, uint8_t bufferSize) const;
512 
513 
519  float getAsFloatingPointValue() const;
520 
525  void setFromFloatingPointValue(float value);
526 
532  WholeAndFraction getWholeAndFraction() const;
533 
539  void setFromWholeAndFraction(WholeAndFraction wf);
540 
544  uint8_t getDecimalPlacesForDivisor() const;
545 
549  uint16_t getActualDecimalDivisor() const;
550 };
551 
558 class EnumMenuItem : public ValueMenuItem {
559 public:
567  EnumMenuItem(const EnumMenuInfo *info, uint8_t defaultVal, MenuItem* next = nullptr, bool infoInPgm = INFO_LOCATION_PGM)
568  : ValueMenuItem(MENUTYPE_ENUM_VALUE, (const AnyMenuInfo*)info, defaultVal, next, infoInPgm) {;}
569 
575  void copyEnumStrToBuffer(char* buffer, int size, int idx) const;
576 
581  int getLengthOfEnumStr(int idx) const;
582 };
583 
590 public:
598  BooleanMenuItem(const BooleanMenuInfo* info, bool defaultVal, MenuItem* next = nullptr, bool infoInPgm = INFO_LOCATION_PGM)
599  : ValueMenuItem(MENUTYPE_BOOLEAN_VALUE, (const AnyMenuInfo*)info, defaultVal, next, infoInPgm) {;}
600 
604  BooleanNaming getBooleanNaming() const;
605 
607  bool getBoolean() const {return currentValue != 0;}
609  void setBoolean(bool b, bool silent = false) {setCurrentValue(b, silent);}
610 };
611 
617 class FloatMenuItem : public MenuItem {
618 private:
619  float currValue;
620 public:
627  FloatMenuItem(const FloatMenuInfo* info, MenuItem* next, bool infoInPgm = INFO_LOCATION_PGM)
628  : MenuItem(MENUTYPE_FLOAT_VALUE, (const AnyMenuInfo*)info, next, infoInPgm) { currValue = 0; }
629 
633  int getDecimalPlaces() const;
634 
638  void setFloatValue(float newVal, bool silent = false);
639 
643  float getFloatValue() const { return currValue; }
644 };
645 
646 
652 class ActionMenuItem : public MenuItem {
653 public:
660  ActionMenuItem(const AnyMenuInfo* info, MenuItem* next, bool infoInProgmem = INFO_LOCATION_PGM)
661  : MenuItem(MENUTYPE_ACTION_VALUE, info, next, infoInProgmem) {;}
662 };
663 
664 // forward reference
665 class RuntimeMenuItem;
666 
673 void copyMenuItemValue(const MenuItem* item, char* buffer, size_t bufferSize);
674 
682 void copyMenuItemNameAndValue(const MenuItem* item, char* buffer, size_t bufferSize, char additionalSep = ':');
683 
687 inline bool isMenuBasedOnValueItem(const MenuItem* item) {
688  if(item == nullptr) return false;
689  auto ty = uint8_t(item->getMenuType());
690  return ty == MENUTYPE_INT_VALUE || ty == MENUTYPE_ENUM_VALUE || ty == MENUTYPE_BOOLEAN_VALUE;
691 }
692 
696 inline bool isMenuRuntime(const MenuItem* t) {
697  return t && (uint8_t(t->getMenuType()) >= uint8_t(MENUTYPE_RUNTIME_VALUE));
698 }
699 
703 inline bool isMenuRuntimeMultiEdit(const MenuItem* t) {
704  return t && (uint8_t(t->getMenuType()) >= uint8_t(MENUTYPE_TEXT_VALUE));
705 }
706 
712 inline const RuntimeMenuItem* asRuntimeItem(const MenuItem* i) {
713  return reinterpret_cast<const RuntimeMenuItem*>(i);
714 }
715 
716 #endif
Definition: MenuItems.h:652
MenuCallbackFn callback
Definition: MenuItems.h:76
RenderFnMode
Definition: MenuItems.h:287
bool isEditing() const
Definition: MenuItems.h:390
uint16_t divisor
Definition: MenuItems.h:89
Definition: MenuItems.h:329
Definition: MenuItems.h:293
Definition: MenuItems.h:204
Definition: MenuItems.h:144
MenuItem * getNext() const
Definition: MenuItems.h:413
uint16_t eepromAddr
Definition: MenuItems.h:150
bool isReadOnly() const
Definition: MenuItems.h:395
Definition: MenuItems.h:46
void setReadOnly(bool active)
Definition: MenuItems.h:393
Definition: MenuItems.h:433
Definition: MenuItems.h:103
Definition: MenuItems.h:246
bool isVisible() const
Definition: MenuItems.h:410
bool isChanged() const
Definition: MenuItems.h:372
menuid_t id
Definition: MenuItems.h:70
Definition: MenuItems.h:485
const RuntimeMenuItem * asRuntimeItem(const MenuItem *i)
Definition: MenuItems.h:712
Definition: MenuItems.h:258
Definition: MenuItems.h:244
uint16_t eepromAddr
Definition: MenuItems.h:109
menuid_t id
Definition: MenuItems.h:107
Definition: MenuItems.h:214
MenuCallbackFn callback
Definition: MenuItems.h:113
Definition: MenuItems.h:617
bool isMenuRuntimeMultiEdit(const MenuItem *t)
Definition: MenuItems.h:703
MenuType getMenuType() const
Definition: MenuItems.h:363
Definition: MenuItems.h:208
void setBoolean(bool b, bool silent=false)
Definition: MenuItems.h:609
Definition: MenuItems.h:281
Definition: MenuItems.h:220
void setChanged(bool changed)
Definition: MenuItems.h:370
const char *const * menuItems
Definition: MenuItems.h:123
Definition: MenuItems.h:289
Flags
Definition: MenuItems.h:202
Definition: MenuItems.h:133
Definition: MenuItems.h:262
void(* MenuCallbackFn)(int id)
Definition: MenuItems.h:40
ValueMenuItem(MenuType menuType, const AnyMenuInfo *info, uint16_t defaultVal, MenuItem *next, bool infoPgm)
Definition: MenuItems.h:438
bool isLocalOnly() const
Definition: MenuItems.h:400
menuid_t id
Definition: MenuItems.h:50
MenuType
Definition: MenuItems.h:242
Definition: MenuItems.h:260
Definition: MenuItems.h:131
void copyMenuItemValue(const MenuItem *item, char *buffer, size_t bufferSize)
Definition: MenuItems.cpp:315
void copyMenuItemNameAndValue(const MenuItem *item, char *buffer, size_t bufferSize, char additionalSep=':')
Definition: MenuItems.cpp:306
BooleanNaming
Definition: MenuItems.h:129
float getFloatValue() const
Definition: MenuItems.h:643
Definition: MenuItems.h:186
MenuCallbackFn callback
Definition: MenuItems.h:56
Definition: MenuItems.h:291
Definition: MenuItems.h:558
Definition: MenuItems.h:224
uint16_t eepromAddr
Definition: MenuItems.h:175
Definition: MenuItems.h:216
Definition: MenuItems.h:454
Definition: MenuItems.h:252
void setVisible(bool visible)
Definition: MenuItems.h:408
FloatMenuItem(const FloatMenuInfo *info, MenuItem *next, bool infoInPgm=INFO_LOCATION_PGM)
Definition: MenuItems.h:627
AnalogMenuItem(const AnalogMenuInfo *info, uint16_t defaultVal, MenuItem *next=nullptr, bool infoInPgm=INFO_LOCATION_PGM)
Definition: MenuItems.h:494
Definition: MenuItems.h:66
uint16_t numDecimalPlaces
Definition: MenuItems.h:194
Definition: MenuItems.h:222
MenuCallbackFn callback
Definition: MenuItems.h:196
Definition: MenuItems.h:295
ActionMenuItem(const AnyMenuInfo *info, MenuItem *next, bool infoInProgmem=INFO_LOCATION_PGM)
Definition: MenuItems.h:660
Definition: MenuItems.h:226
Definition: MenuItems.h:297
menuid_t id
Definition: MenuItems.h:190
Definition: MenuItems.h:279
uint8_t copyNameToBuffer(char *sz, int size) const
Definition: MenuItems.h:346
void setNext(MenuItem *next)
Definition: MenuItems.h:415
uint16_t maxValue
Definition: MenuItems.h:54
Definition: MenuItems.h:250
int16_t offset
Definition: MenuItems.h:82
Definition: MenuItems.h:589
BooleanMenuItem(const BooleanMenuInfo *info, bool defaultVal, MenuItem *next=nullptr, bool infoInPgm=INFO_LOCATION_PGM)
Definition: MenuItems.h:598
uint16_t maxValue
Definition: MenuItems.h:111
Definition: MenuItems.h:230
uint16_t eepromAddr
Definition: MenuItems.h:52
EnumMenuItem(const EnumMenuInfo *info, uint8_t defaultVal, MenuItem *next=nullptr, bool infoInPgm=INFO_LOCATION_PGM)
Definition: MenuItems.h:567
Definition: MenuItems.h:266
Definition: MenuItems.h:248
bool isSecured() const
Definition: MenuItems.h:405
Definition: MenuItems.h:275
uint16_t maxValue
Definition: MenuItems.h:177
bool isMenuRuntime(const MenuItem *t)
Definition: MenuItems.h:696
Definition: MenuItems.h:273
Definition: MenuItems.h:212
Definition: RuntimeMenuItem.h:73
uint16_t eepromAddr
Definition: MenuItems.h:192
Definition: MenuItems.h:301
uint16_t maxValue
Definition: MenuItems.h:74
uint16_t maxValue
Definition: MenuItems.h:152
Definition: MenuItems.h:210
MenuCallbackFn callback
Definition: MenuItems.h:179
Definition: MenuItems.h:264
Definition: MenuItems.h:218
char name[NAME_SIZE_T]
Definition: MenuItems.h:48
Definition: MenuItems.h:256
Definition: MenuItems.h:268
Definition: MenuItems.h:228
Definition: MenuItems.h:169
Definition: MenuItems.h:254
uint16_t eepromAddr
Definition: MenuItems.h:72
BooleanNaming naming
Definition: MenuItems.h:160
MenuCallbackFn callback
Definition: MenuItems.h:154
menuid_t id
Definition: MenuItems.h:173
void setLocalOnly(bool localOnly)
Definition: MenuItems.h:398
Definition: MenuItems.h:299
Definition: MenuItems.h:135
menuid_t id
Definition: MenuItems.h:148
Definition: MenuItems.h:271
void setSecured(bool secured)
Definition: MenuItems.h:403
bool isActive() const
Definition: MenuItems.h:385
bool isMenuBasedOnValueItem(const MenuItem *item)
Definition: MenuItems.h:687
int(* RuntimeRenderingFn)(RuntimeMenuItem *item, uint8_t row, RenderFnMode mode, char *buffer, int bufferSize)
Definition: MenuItems.h:318
uint16_t getCurrentValue() const
Definition: MenuItems.h:447
Definition: MenuItems.h:206
bool getBoolean() const
Definition: MenuItems.h:607
Definition: MenuItems.h:277