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 
29 #define NAME_SIZE_T 20
30 
32 #define NO_CALLBACK NULL
33 
35 typedef void (*MenuCallbackFn)(int id);
36 
41 struct AnyMenuInfo {
43  char name[NAME_SIZE_T];
45  uint16_t id;
47  uint16_t eepromAddr;
49  uint16_t maxValue;
52 };
53 
63  char name[NAME_SIZE_T];
65  uint16_t id;
67  uint16_t eepromAddr;
69  uint16_t maxValue;
72 
77  int16_t offset;
84  uint16_t divisor;
88  char unitName[5];
89 };
90 
98 struct EnumMenuInfo {
100  char name[NAME_SIZE_T];
102  uint16_t id;
104  uint16_t eepromAddr;
106  uint16_t maxValue;
109 
118  const char * const *menuItems;
119 };
120 
124 enum BooleanNaming : uint8_t {
131 };
132 
141  char name[NAME_SIZE_T];
143  uint16_t id;
145  uint16_t eepromAddr;
147  uint16_t maxValue;
150 
156 };
157 
164 struct SubMenuInfo {
166  char name[NAME_SIZE_T];
168  uint16_t id;
170  uint16_t eepromAddr;
172  uint16_t maxValue;
175 };
176 
183  char name[NAME_SIZE_T];
185  uint16_t id;
187  uint16_t eepromAddr;
192 };
193 
197 enum Flags : uint8_t {
226 };
227 
228 #define MENUITEM_ALL_REMOTES 0xFC00
229 
237 enum MenuType : uint8_t {
264 
277 };
278 
282 enum RenderFnMode : uint8_t {
297 };
298 
299 // forward reference
300 class RuntimeMenuItem;
301 
313 typedef int(*RuntimeRenderingFn)(RuntimeMenuItem* item, uint8_t row, RenderFnMode mode, char* buffer, int bufferSize);
314 
324 class MenuItem {
325 protected:
326  uint16_t flags;
327  MenuItem* next;
329  union {
330  const AnyMenuInfo *info;
331  RuntimeRenderingFn renderFn;
332  };
333  MenuType menuType;
334 public:
335 
341  uint8_t copyNameToBuffer(char* sz, int size) const { return copyNameToBuffer(sz, 0, size);}
342 
350  uint8_t copyNameToBuffer(char* sz, int offset, int size) const;
352  uint16_t getId() const;
354  uint16_t getMaximumValue() const;
356  uint16_t getEepromPosition() const;
358  MenuType getMenuType() const { return menuType; }
360  void triggerCallback() const;
361 
362  bool isInfoProgMem() const { return bitRead(flags, MENUITEM_INFO_STRUCT_PGM); }
363 
365  void setChanged(bool changed) { bitWrite(flags, MENUITEM_CHANGED, changed); }
367  bool isChanged() const { return bitRead(flags, MENUITEM_CHANGED); }
369  bool isSendRemoteNeeded(uint8_t remoteNo) const;
371  void setSendRemoteNeededAll();
373  void clearSendRemoteNeededAll();
375  void setSendRemoteNeeded(uint8_t remoteNo, bool needed);
376 
378  void setActive(bool active);
380  bool isActive() const { return bitRead(flags, MENUITEM_ACTIVE); }
381 
383  void setEditing(bool active);
385  bool isEditing() const { return bitRead(flags, MENUITEM_EDITING); }
386 
388  void setReadOnly(bool active) { bitWrite(flags, MENUITEM_READONLY, active); }
390  bool isReadOnly() const { return bitRead(flags, MENUITEM_READONLY); }
391 
393  void setLocalOnly(bool localOnly) { bitWrite(flags, MENUITEM_LOCAL_ONLY, localOnly); }
395  bool isLocalOnly() const { return bitRead(flags, MENUITEM_LOCAL_ONLY); }
396 
398  void setSecured(bool secured) { bitWrite(flags, MENUITEM_PIN_SECURED, secured); }
400  bool isSecured() const { return bitRead(flags, MENUITEM_PIN_SECURED); }
401 
403  void setVisible(bool visible) { bitWrite(flags, MENUITEM_PIN_VISIBLE, visible); }
405  bool isVisible() const { return bitRead(flags, MENUITEM_PIN_VISIBLE); }
406 
408  MenuItem* getNext() const { return next; }
410  void setNext(MenuItem* next) { this->next = next; }
411 
415  void changeOccurred(bool silent);
416 protected:
420  MenuItem(MenuType menuType, const AnyMenuInfo* menuInfo, MenuItem* next, bool infoProgMem);
421 
422 };
423 
428 class ValueMenuItem : public MenuItem {
429 protected:
430  uint16_t currentValue;
431 
433  ValueMenuItem(MenuType menuType, const AnyMenuInfo* info, uint16_t defaultVal, MenuItem* next, bool infoPgm)
434  : MenuItem(menuType, info, next, infoPgm) {
435  this->currentValue = defaultVal;
436  }
437 public:
439  void setCurrentValue(uint16_t val, bool silent = false);
440 
442  uint16_t getCurrentValue() const { return currentValue; }
443 };
444 
450 public:
451  WholeAndFraction() {
452  this->whole = this->fraction = this->negative = 0;
453  }
454 
455  WholeAndFraction(const WholeAndFraction& that) {
456  this->whole = that.whole;
457  this->fraction = that.fraction;
458  this->negative = that.negative;
459  }
460 
461  WholeAndFraction(uint16_t whole, uint16_t fract, bool negative) {
462  this->whole = whole;
463  this->fraction = fract;
464  this->negative = negative;
465  }
466  uint32_t whole: 15;
467  uint32_t fraction: 16;
468  uint32_t negative: 1;
469 };
470 
481 public:
489  AnalogMenuItem(const AnalogMenuInfo* info, uint16_t defaultVal, MenuItem* next = nullptr, bool infoInPgm = INFO_LOCATION_PGM)
490  : ValueMenuItem(MENUTYPE_INT_VALUE, (const AnyMenuInfo*)info, defaultVal, next, infoInPgm) {;}
491 
493  int getOffset() const;
495  uint16_t getDivisor() const;
497  int unitNameLength() const;
499  void copyUnitToBuffer(char* unitBuff, uint8_t size = 5) const;
500 
506  void copyValue(char* buffer, uint8_t bufferSize) const;
507 
508 
514  float getAsFloatingPointValue() const;
515 
520  void setFromFloatingPointValue(float value);
521 
527  WholeAndFraction getWholeAndFraction() const;
528 
534  void setFromWholeAndFraction(WholeAndFraction wf);
535 
539  uint8_t getDecimalPlacesForDivisor() const;
540 
544  uint16_t getActualDecimalDivisor() const;
545 };
546 
553 class EnumMenuItem : public ValueMenuItem {
554 public:
562  EnumMenuItem(const EnumMenuInfo *info, uint8_t defaultVal, MenuItem* next = nullptr, bool infoInPgm = INFO_LOCATION_PGM)
563  : ValueMenuItem(MENUTYPE_ENUM_VALUE, (const AnyMenuInfo*)info, defaultVal, next, infoInPgm) {;}
564 
570  void copyEnumStrToBuffer(char* buffer, int size, int idx) const;
571 
576  int getLengthOfEnumStr(int idx) const;
577 };
578 
585 public:
593  BooleanMenuItem(const BooleanMenuInfo* info, bool defaultVal, MenuItem* next = nullptr, bool infoInPgm = INFO_LOCATION_PGM)
594  : ValueMenuItem(MENUTYPE_BOOLEAN_VALUE, (const AnyMenuInfo*)info, defaultVal, next, infoInPgm) {;}
595 
599  BooleanNaming getBooleanNaming() const;
600 
602  bool getBoolean() const {return currentValue != 0;}
604  void setBoolean(bool b, bool silent = false) {setCurrentValue(b, silent);}
605 };
606 
612 class FloatMenuItem : public MenuItem {
613 private:
614  float currValue;
615 public:
622  FloatMenuItem(const FloatMenuInfo* info, MenuItem* next, bool infoInPgm = INFO_LOCATION_PGM)
623  : MenuItem(MENUTYPE_FLOAT_VALUE, (const AnyMenuInfo*)info, next, infoInPgm) { currValue = 0; }
624 
628  int getDecimalPlaces() const;
629 
633  void setFloatValue(float newVal, bool silent = false);
634 
638  float getFloatValue() const { return currValue; }
639 };
640 
641 
647 class ActionMenuItem : public MenuItem {
648 public:
655  ActionMenuItem(const AnyMenuInfo* info, MenuItem* next, bool infoInProgmem = INFO_LOCATION_PGM)
656  : MenuItem(MENUTYPE_ACTION_VALUE, info, next, infoInProgmem) {;}
657 };
658 
659 // forward reference
660 class RuntimeMenuItem;
661 
668 void copyMenuItemValue(const MenuItem* item, char* buffer, size_t bufferSize);
669 
677 void copyMenuItemNameAndValue(const MenuItem* item, char* buffer, size_t bufferSize, char additionalSep = ':');
678 
682 inline bool isMenuBasedOnValueItem(const MenuItem* item) {
683  if(item == nullptr) return false;
684  auto ty = uint8_t(item->getMenuType());
685  return ty == MENUTYPE_INT_VALUE || ty == MENUTYPE_ENUM_VALUE || ty == MENUTYPE_BOOLEAN_VALUE;
686 }
687 
691 inline bool isMenuRuntime(const MenuItem* t) {
692  return t && (uint8_t(t->getMenuType()) >= uint8_t(MENUTYPE_RUNTIME_VALUE));
693 }
694 
698 inline bool isMenuRuntimeMultiEdit(const MenuItem* t) {
699  return t && (uint8_t(t->getMenuType()) >= uint8_t(MENUTYPE_TEXT_VALUE));
700 }
701 
707 inline const RuntimeMenuItem* asRuntimeItem(const MenuItem* i) {
708  return reinterpret_cast<const RuntimeMenuItem*>(i);
709 }
710 
711 #endif
Definition: MenuItems.h:647
MenuCallbackFn callback
Definition: MenuItems.h:71
RenderFnMode
Definition: MenuItems.h:282
bool isEditing() const
Definition: MenuItems.h:385
uint16_t divisor
Definition: MenuItems.h:84
uint16_t id
Definition: MenuItems.h:102
Definition: MenuItems.h:324
Definition: MenuItems.h:288
Definition: MenuItems.h:199
Definition: MenuItems.h:139
MenuItem * getNext() const
Definition: MenuItems.h:408
uint16_t eepromAddr
Definition: MenuItems.h:145
bool isReadOnly() const
Definition: MenuItems.h:390
Definition: MenuItems.h:41
void setReadOnly(bool active)
Definition: MenuItems.h:388
Definition: MenuItems.h:428
Definition: MenuItems.h:98
Definition: MenuItems.h:241
bool isVisible() const
Definition: MenuItems.h:405
uint16_t id
Definition: MenuItems.h:143
bool isChanged() const
Definition: MenuItems.h:367
Definition: MenuItems.h:480
const RuntimeMenuItem * asRuntimeItem(const MenuItem *i)
Definition: MenuItems.h:707
Definition: MenuItems.h:253
uint16_t id
Definition: MenuItems.h:185
Definition: MenuItems.h:239
uint16_t eepromAddr
Definition: MenuItems.h:104
Definition: MenuItems.h:209
MenuCallbackFn callback
Definition: MenuItems.h:108
Definition: MenuItems.h:612
bool isMenuRuntimeMultiEdit(const MenuItem *t)
Definition: MenuItems.h:698
MenuType getMenuType() const
Definition: MenuItems.h:358
Definition: MenuItems.h:203
void setBoolean(bool b, bool silent=false)
Definition: MenuItems.h:604
Definition: MenuItems.h:276
Definition: MenuItems.h:215
void setChanged(bool changed)
Definition: MenuItems.h:365
uint16_t id
Definition: MenuItems.h:45
const char *const * menuItems
Definition: MenuItems.h:118
Definition: MenuItems.h:284
Flags
Definition: MenuItems.h:197
Definition: MenuItems.h:128
Definition: MenuItems.h:257
void(* MenuCallbackFn)(int id)
Definition: MenuItems.h:35
ValueMenuItem(MenuType menuType, const AnyMenuInfo *info, uint16_t defaultVal, MenuItem *next, bool infoPgm)
Definition: MenuItems.h:433
bool isLocalOnly() const
Definition: MenuItems.h:395
MenuType
Definition: MenuItems.h:237
Definition: MenuItems.h:255
Definition: MenuItems.h:126
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:124
float getFloatValue() const
Definition: MenuItems.h:638
Definition: MenuItems.h:181
MenuCallbackFn callback
Definition: MenuItems.h:51
Definition: MenuItems.h:286
Definition: MenuItems.h:553
Definition: MenuItems.h:219
uint16_t id
Definition: MenuItems.h:65
uint16_t eepromAddr
Definition: MenuItems.h:170
Definition: MenuItems.h:211
Definition: MenuItems.h:449
Definition: MenuItems.h:247
void setVisible(bool visible)
Definition: MenuItems.h:403
FloatMenuItem(const FloatMenuInfo *info, MenuItem *next, bool infoInPgm=INFO_LOCATION_PGM)
Definition: MenuItems.h:622
AnalogMenuItem(const AnalogMenuInfo *info, uint16_t defaultVal, MenuItem *next=nullptr, bool infoInPgm=INFO_LOCATION_PGM)
Definition: MenuItems.h:489
Definition: MenuItems.h:61
uint16_t numDecimalPlaces
Definition: MenuItems.h:189
Definition: MenuItems.h:217
MenuCallbackFn callback
Definition: MenuItems.h:191
Definition: MenuItems.h:290
ActionMenuItem(const AnyMenuInfo *info, MenuItem *next, bool infoInProgmem=INFO_LOCATION_PGM)
Definition: MenuItems.h:655
Definition: MenuItems.h:221
Definition: MenuItems.h:292
Definition: MenuItems.h:274
uint8_t copyNameToBuffer(char *sz, int size) const
Definition: MenuItems.h:341
void setNext(MenuItem *next)
Definition: MenuItems.h:410
uint16_t maxValue
Definition: MenuItems.h:49
Definition: MenuItems.h:245
int16_t offset
Definition: MenuItems.h:77
Definition: MenuItems.h:584
BooleanMenuItem(const BooleanMenuInfo *info, bool defaultVal, MenuItem *next=nullptr, bool infoInPgm=INFO_LOCATION_PGM)
Definition: MenuItems.h:593
uint16_t maxValue
Definition: MenuItems.h:106
Definition: MenuItems.h:225
uint16_t eepromAddr
Definition: MenuItems.h:47
EnumMenuItem(const EnumMenuInfo *info, uint8_t defaultVal, MenuItem *next=nullptr, bool infoInPgm=INFO_LOCATION_PGM)
Definition: MenuItems.h:562
Definition: MenuItems.h:261
Definition: MenuItems.h:243
bool isSecured() const
Definition: MenuItems.h:400
Definition: MenuItems.h:270
uint16_t maxValue
Definition: MenuItems.h:172
bool isMenuRuntime(const MenuItem *t)
Definition: MenuItems.h:691
Definition: MenuItems.h:268
Definition: MenuItems.h:207
Definition: RuntimeMenuItem.h:73
uint16_t eepromAddr
Definition: MenuItems.h:187
Definition: MenuItems.h:296
uint16_t maxValue
Definition: MenuItems.h:69
uint16_t maxValue
Definition: MenuItems.h:147
Definition: MenuItems.h:205
MenuCallbackFn callback
Definition: MenuItems.h:174
Definition: MenuItems.h:259
Definition: MenuItems.h:213
char name[NAME_SIZE_T]
Definition: MenuItems.h:43
Definition: MenuItems.h:251
Definition: MenuItems.h:263
Definition: MenuItems.h:223
Definition: MenuItems.h:164
Definition: MenuItems.h:249
uint16_t eepromAddr
Definition: MenuItems.h:67
BooleanNaming naming
Definition: MenuItems.h:155
MenuCallbackFn callback
Definition: MenuItems.h:149
void setLocalOnly(bool localOnly)
Definition: MenuItems.h:393
Definition: MenuItems.h:294
Definition: MenuItems.h:130
Definition: MenuItems.h:266
void setSecured(bool secured)
Definition: MenuItems.h:398
bool isActive() const
Definition: MenuItems.h:380
bool isMenuBasedOnValueItem(const MenuItem *item)
Definition: MenuItems.h:682
int(* RuntimeRenderingFn)(RuntimeMenuItem *item, uint8_t row, RenderFnMode mode, char *buffer, int bufferSize)
Definition: MenuItems.h:313
uint16_t getCurrentValue() const
Definition: MenuItems.h:442
uint16_t id
Definition: MenuItems.h:168
Definition: MenuItems.h:201
bool getBoolean() const
Definition: MenuItems.h:602
Definition: MenuItems.h:272