tcMenu
MenuItems.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 
14 #ifndef _MENUITEMS_h
15 #define _MENUITEMS_h
16 
17 #define INFO_LOCATION_PGM true
18 #define INFO_LOCATION_RAM false
19 
20 #include <PlatformDetermination.h>
21 #include <IoAbstraction.h>
22 
24 #define MAXIMUM_ID_ALLOWED 32000
25 
26 typedef uint16_t menuid_t;
27 
29 #define INVALID_MENU_ID 0xffff
30 
32 #ifndef NAME_SIZE_T
33 #define NAME_SIZE_T 20
34 #endif // NAME_SIZE_T
35 
37 #ifndef UNIT_SIZE_T
38 #define UNIT_SIZE_T 5
39 #endif // NAME_SIZE_T
40 
42 #define NO_CALLBACK NULL
43 
45 typedef void (*MenuCallbackFn)(int id);
46 
51 struct AnyMenuInfo {
53  char name[NAME_SIZE_T];
55  menuid_t id;
57  uint16_t eepromAddr;
59  uint16_t maxValue;
62 };
63 
73  char name[NAME_SIZE_T];
75  menuid_t id;
77  uint16_t eepromAddr;
79  uint16_t maxValue;
82 
87  int16_t offset;
94  uint16_t divisor;
98  char unitName[UNIT_SIZE_T];
99 };
100 
108 struct EnumMenuInfo {
110  char name[NAME_SIZE_T];
112  menuid_t id;
114  uint16_t eepromAddr;
116  uint16_t maxValue;
119 
128  const char * const *menuItems;
129 };
130 
134 enum BooleanNaming : uint8_t {
143 };
144 
153  char name[NAME_SIZE_T];
155  menuid_t id;
157  uint16_t eepromAddr;
159  uint16_t maxValue;
162 
168 };
169 
177 
184  char name[NAME_SIZE_T];
186  menuid_t id;
188  uint16_t eepromAddr;
193 };
194 
198 enum Flags : uint8_t {
213 
226 };
227 
228 #define MENUITEM_ALL_REMOTES 0xFC00
229 #define MENUITEM_ALL_CHANGE 0x0003
230 
238 enum MenuType : uint8_t {
265 
278 };
279 
283 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  const AnyMenuInfo *info = nullptr;
335  RuntimeRenderingFn renderFn = nullptr;
336  MenuType menuType;
337 public:
338 
344  uint8_t copyNameToBuffer(char* sz, int size) const { return copyNameToBuffer(sz, 0, size);}
345 
353  uint8_t copyNameToBuffer(char* sz, int offset, int size) const;
355  menuid_t getId() const;
357  uint16_t getMaximumValue() const;
359  uint16_t getEepromPosition() const;
361  MenuType getMenuType() const { return menuType; }
363  void triggerCallback() const;
364 
365  bool isInfoProgMem() const { return bitRead(flags, MENUITEM_INFO_STRUCT_PGM); }
366 
369  void setChanged(bool changed);
371  void setChanged(int num, bool changed) { bitWrite(flags, (num & 3), changed); }
373  bool isChanged(int num = 0) const { return bitRead(flags, (num & 3)); }
375  bool isSendRemoteNeeded(uint8_t remoteNo) const;
377  void setSendRemoteNeededAll();
381  void setSendRemoteNeeded(uint8_t remoteNo, bool needed);
382 
384  void setReadOnly(bool active) { bitWrite(flags, MENUITEM_READONLY, active); }
386  bool isReadOnly() const { return bitRead(flags, MENUITEM_READONLY); }
387 
389  void setLocalOnly(bool localOnly) { bitWrite(flags, MENUITEM_LOCAL_ONLY, localOnly); }
391  bool isLocalOnly() const { return bitRead(flags, MENUITEM_LOCAL_ONLY); }
392 
394  void setSecured(bool secured) { bitWrite(flags, MENUITEM_PIN_SECURED, secured); }
396  bool isSecured() const { return bitRead(flags, MENUITEM_PIN_SECURED); }
397 
399  void setVisible(bool visible) { bitWrite(flags, MENUITEM_PIN_VISIBLE, visible); }
401  bool isVisible() const { return bitRead(flags, MENUITEM_PIN_VISIBLE); }
402 
404  MenuItem* getNext() const { return next; }
406  void setNext(MenuItem* pNext) { this->next = pNext; }
407 
411  void changeOccurred(bool silent);
412 protected:
416  MenuItem(MenuType menuType, const AnyMenuInfo* menuInfo, MenuItem* next, bool infoProgMem);
417 
418 };
419 
424 class ValueMenuItem : public MenuItem {
425 protected:
426  uint16_t currentValue;
427 
429  ValueMenuItem(MenuType menuType, const AnyMenuInfo* info, uint16_t defaultVal, MenuItem* next, bool infoPgm)
430  : MenuItem(menuType, info, next, infoPgm) {
431  this->currentValue = defaultVal;
432  }
433 public:
435  void setCurrentValue(uint16_t val, bool silent = false);
436 
438  uint16_t getCurrentValue() const { return currentValue; }
439 };
440 
446 public:
447  WholeAndFraction() {
448  this->whole = this->fraction = this->negative = 0;
449  }
450 
451  WholeAndFraction(const WholeAndFraction& that) {
452  this->whole = that.whole;
453  this->fraction = that.fraction;
454  this->negative = that.negative;
455  }
456 
457  WholeAndFraction(uint16_t whole, uint16_t fract, bool negative) {
458  this->whole = whole;
459  this->fraction = fract;
460  this->negative = negative;
461  }
462  uint32_t whole: 15;
463  uint32_t fraction: 16;
464  uint32_t negative: 1;
465 };
466 
477 private:
478  int8_t step = 1;
479 public:
487  AnalogMenuItem(const AnalogMenuInfo* info, uint16_t defaultVal, MenuItem* next = nullptr, bool infoInPgm = INFO_LOCATION_PGM)
488  : ValueMenuItem(MENUTYPE_INT_VALUE, (const AnyMenuInfo*)info, defaultVal, next, infoInPgm) {;}
489 
491  int getOffset() const;
492 
494  int getStep() const { return step; }
495 
497  void setStep(int newStep) { step = newStep; }
498 
500  uint16_t getDivisor() const;
501 
503  int unitNameLength() const;
504 
506  void copyUnitToBuffer(char* unitBuff, uint8_t size = 5) const;
507 
513  void copyValue(char* buffer, uint8_t bufferSize) const;
514 
515 
521  float getAsFloatingPointValue() const;
522 
527  void setFromFloatingPointValue(float value);
528 
529  int getIntValueIncludingOffset() { return (int)currentValue + (int)getOffset(); }
530 
537 
544 
548  uint8_t getDecimalPlacesForDivisor() const;
549 
553  uint16_t getActualDecimalDivisor() const;
554 };
555 
566 class EnumMenuItem : public ValueMenuItem {
567 public:
575  EnumMenuItem(const EnumMenuInfo *info, uint8_t defaultVal, MenuItem* next = nullptr, bool infoInPgm = INFO_LOCATION_PGM)
576  : ValueMenuItem(MENUTYPE_ENUM_VALUE, (const AnyMenuInfo*)info, defaultVal, next, infoInPgm) {;}
577 
583  void copyEnumStrToBuffer(char* buffer, int size, int idx) const;
584 
589  int getLengthOfEnumStr(int idx) const;
590 };
591 
598 public:
606  BooleanMenuItem(const BooleanMenuInfo* info, bool defaultVal, MenuItem* next = nullptr, bool infoInPgm = INFO_LOCATION_PGM)
607  : ValueMenuItem(MENUTYPE_BOOLEAN_VALUE, (const AnyMenuInfo*)info, defaultVal, next, infoInPgm) {;}
608 
613 
615  bool getBoolean() const {return currentValue != 0;}
617  void setBoolean(bool b, bool silent = false) {setCurrentValue(b, silent);}
618 };
619 
625 class FloatMenuItem : public MenuItem {
626 private:
627  float currValue;
628 public:
636  FloatMenuItem(const FloatMenuInfo* info, MenuItem* next, bool infoInPgm = INFO_LOCATION_PGM)
637  : MenuItem(MENUTYPE_FLOAT_VALUE, (const AnyMenuInfo*)info, next, infoInPgm) { currValue = 0; }
638 
647  FloatMenuItem(const FloatMenuInfo* info, float current, MenuItem* next, bool infoInPgm = INFO_LOCATION_PGM)
648  : MenuItem(MENUTYPE_FLOAT_VALUE, (const AnyMenuInfo*)info, next, infoInPgm) { currValue = current; }
649 
653  int getDecimalPlaces() const;
654 
658  void setFloatValue(float newVal, bool silent = false);
659 
663  float getFloatValue() const { return currValue; }
664 };
665 
666 
672 class ActionMenuItem : public MenuItem {
673 public:
680  ActionMenuItem(const AnyMenuInfo* info, MenuItem* next, bool infoInProgmem = INFO_LOCATION_PGM)
681  : MenuItem(MENUTYPE_ACTION_VALUE, info, next, infoInProgmem) {;}
682 };
683 
684 // forward reference
685 class RuntimeMenuItem;
686 
693 void copyMenuItemValue(const MenuItem* item, char* buffer, size_t bufferSize, bool active = false);
694 
702 void copyMenuItemNameAndValue(const MenuItem* item, char* buffer, size_t bufferSize, char additionalSep = ':', bool active = false);
703 
708 void copyMenuItemValueDefault(const MenuItem* item, char* buffer, size_t bufferSize, const char* defValue, bool active = false);
709 
713 inline bool isMenuBasedOnValueItem(const MenuItem* item) {
714  if(item == nullptr) return false;
715  auto ty = uint8_t(item->getMenuType());
716  return ty == MENUTYPE_INT_VALUE || ty == MENUTYPE_ENUM_VALUE || ty == MENUTYPE_BOOLEAN_VALUE;
717 }
718 
722 inline bool isMenuRuntime(const MenuItem* t) {
723  return t && (uint8_t(t->getMenuType()) >= uint8_t(MENUTYPE_RUNTIME_VALUE));
724 }
725 
729 inline bool isMenuRuntimeMultiEdit(const MenuItem* t) {
730  return t && (uint8_t(t->getMenuType()) >= uint8_t(MENUTYPE_TEXT_VALUE));
731 }
732 
738 inline const RuntimeMenuItem* asRuntimeItem(const MenuItem* i) {
739  return reinterpret_cast<const RuntimeMenuItem*>(i);
740 }
741 
742 #endif
const char *const * menuItems
Definition: MenuItems.h:128
const RuntimeMenuItem * asRuntimeItem(const MenuItem *i)
Definition: MenuItems.h:738
MenuCallbackFn callback
Definition: MenuItems.h:118
uint16_t eepromAddr
Definition: MenuItems.h:77
bool isMenuRuntime(const MenuItem *t)
Definition: MenuItems.h:722
uint16_t maxValue
Definition: MenuItems.h:79
void copyMenuItemNameAndValue(const MenuItem *item, char *buffer, size_t bufferSize, char additionalSep=':', bool active=false)
Definition: MenuItems.cpp:320
uint16_t eepromAddr
Definition: MenuItems.h:57
char name[NAME_SIZE_T]
Definition: MenuItems.h:110
uint16_t eepromAddr
Definition: MenuItems.h:114
uint16_t eepromAddr
Definition: MenuItems.h:157
uint16_t numDecimalPlaces
Definition: MenuItems.h:190
menuid_t id
Definition: MenuItems.h:186
char unitName[UNIT_SIZE_T]
Definition: MenuItems.h:98
uint16_t eepromAddr
Definition: MenuItems.h:188
uint16_t maxValue
Definition: MenuItems.h:59
menuid_t id
Definition: MenuItems.h:112
AnyMenuInfo SubMenuInfo
Definition: MenuItems.h:176
MenuCallbackFn callback
Definition: MenuItems.h:161
MenuCallbackFn callback
Definition: MenuItems.h:81
RenderFnMode
Definition: MenuItems.h:283
@ RENDERFN_EEPROM_POS
Definition: MenuItems.h:289
@ RENDERFN_INVOKE
Definition: MenuItems.h:291
@ RENDERFN_NAME
Definition: MenuItems.h:287
@ RENDERFN_ACTIVATE
Definition: MenuItems.h:293
@ RENDERFN_GETRANGE
Definition: MenuItems.h:297
@ RENDERFN_GETPART
Definition: MenuItems.h:299
@ RENDERFN_VALUE
Definition: MenuItems.h:285
@ RENDERFN_SET_VALUE
Definition: MenuItems.h:295
@ RENDERFN_SET_TEXT_VALUE
Definition: MenuItems.h:301
MenuCallbackFn callback
Definition: MenuItems.h:192
uint16_t maxValue
Definition: MenuItems.h:159
menuid_t id
Definition: MenuItems.h:55
char name[NAME_SIZE_T]
Definition: MenuItems.h:53
void copyMenuItemValue(const MenuItem *item, char *buffer, size_t bufferSize, bool active=false)
Definition: MenuItems.cpp:332
menuid_t id
Definition: MenuItems.h:75
MenuCallbackFn callback
Definition: MenuItems.h:61
void copyMenuItemValueDefault(const MenuItem *item, char *buffer, size_t bufferSize, const char *defValue, bool active=false)
Definition: MenuItems.cpp:387
char name[NAME_SIZE_T]
Definition: MenuItems.h:153
menuid_t id
Definition: MenuItems.h:155
char name[NAME_SIZE_T]
Definition: MenuItems.h:73
bool isMenuRuntimeMultiEdit(const MenuItem *t)
Definition: MenuItems.h:729
int(* RuntimeRenderingFn)(RuntimeMenuItem *item, uint8_t row, RenderFnMode mode, char *buffer, int bufferSize)
Definition: MenuItems.h:318
char name[NAME_SIZE_T]
Definition: MenuItems.h:184
Flags
Definition: MenuItems.h:198
@ MENUITEM_REMOTE_SEND5
Definition: MenuItems.h:225
@ MENUITEM_REMOTE_SEND4
Definition: MenuItems.h:223
@ MENUITEM_INFO_STRUCT_PGM
Definition: MenuItems.h:212
@ MENUITEM_REMOTE_SEND2
Definition: MenuItems.h:219
@ MENUITEM_READONLY
Definition: MenuItems.h:204
@ MENUITEM_PIN_VISIBLE
Definition: MenuItems.h:210
@ MENUITEM_CHANGED_1
Definition: MenuItems.h:202
@ MENUITEM_LOCAL_ONLY
Definition: MenuItems.h:206
@ MENUITEM_REMOTE_SEND1
Definition: MenuItems.h:217
@ MENUITEM_PIN_SECURED
Definition: MenuItems.h:208
@ MENUITEM_CHANGED
Definition: MenuItems.h:200
@ MENUITEM_REMOTE_SEND3
Definition: MenuItems.h:221
@ MENUITEM_REMOTE_SEND0
Definition: MenuItems.h:215
uint16_t divisor
Definition: MenuItems.h:94
void(* MenuCallbackFn)(int id)
Definition: MenuItems.h:45
int16_t offset
Definition: MenuItems.h:87
bool isMenuBasedOnValueItem(const MenuItem *item)
Definition: MenuItems.h:713
BooleanNaming naming
Definition: MenuItems.h:167
BooleanNaming
Definition: MenuItems.h:134
@ NAMING_ON_OFF
Definition: MenuItems.h:138
@ NAMING_TRUE_FALSE
Definition: MenuItems.h:136
@ NAMING_CHECKBOX
Definition: MenuItems.h:142
@ NAMING_YES_NO
Definition: MenuItems.h:140
MenuType
Definition: MenuItems.h:238
@ MENUTYPE_DIALOG_BUTTON
Definition: MenuItems.h:264
@ MENUTYPE_COLOR_VALUE
Definition: MenuItems.h:277
@ MENUTYPE_IPADDRESS
Definition: MenuItems.h:269
@ MENUTYPE_DATE
Definition: MenuItems.h:273
@ MENUTYPE_INT_VALUE
Definition: MenuItems.h:240
@ MENUTYPE_ENUM_VALUE
Definition: MenuItems.h:242
@ MENUTYPE_SUB_VALUE
Definition: MenuItems.h:260
@ MENUTYPE_TIME
Definition: MenuItems.h:271
@ MENUTYPE_SCROLLER_VALUE
Definition: MenuItems.h:258
@ MENUTYPE_FLOAT_VALUE
Definition: MenuItems.h:246
@ MENUTYPE_BOOLEAN_VALUE
Definition: MenuItems.h:244
@ MENUTYPE_TEXT_VALUE
Definition: MenuItems.h:267
@ MENUTYPE_TITLE_ITEM
Definition: MenuItems.h:262
@ MENUTYPE_ACTION_VALUE
Definition: MenuItems.h:248
@ MENUTYPE_RUNTIME_LIST
Definition: MenuItems.h:252
@ MENUTYPE_BACK_VALUE
Definition: MenuItems.h:254
@ MENUTYPE_RUNTIME_VALUE
Definition: MenuItems.h:250
@ MENUTYPE_ACTIVATE_SUBMENU
Definition: MenuItems.h:256
@ MENUTYPE_LARGENUM_VALUE
Definition: MenuItems.h:275
uint16_t maxValue
Definition: MenuItems.h:116
Definition: MenuItems.h:71
Definition: MenuItems.h:51
Definition: MenuItems.h:151
Definition: MenuItems.h:108
Definition: MenuItems.h:182
Definition: MenuItems.h:672
ActionMenuItem(const AnyMenuInfo *info, MenuItem *next, bool infoInProgmem=INFO_LOCATION_PGM)
Definition: MenuItems.h:680
Definition: MenuItems.h:476
WholeAndFraction getWholeAndFraction() const
Definition: MenuItems.cpp:207
void setFromFloatingPointValue(float value)
Definition: MenuItems.cpp:243
AnalogMenuItem(const AnalogMenuInfo *info, uint16_t defaultVal, MenuItem *next=nullptr, bool infoInPgm=INFO_LOCATION_PGM)
Definition: MenuItems.h:487
void copyValue(char *buffer, uint8_t bufferSize) const
Definition: MenuItems.cpp:175
void setFromWholeAndFraction(WholeAndFraction wf)
Definition: MenuItems.cpp:226
float getAsFloatingPointValue() const
Definition: MenuItems.cpp:191
int getOffset() const
Definition: MenuItems.cpp:262
int unitNameLength() const
Definition: MenuItems.cpp:272
void setStep(int newStep)
Definition: MenuItems.h:497
uint16_t getActualDecimalDivisor() const
Definition: MenuItems.cpp:201
uint8_t getDecimalPlacesForDivisor() const
Definition: MenuItems.cpp:255
void copyUnitToBuffer(char *unitBuff, uint8_t size=5) const
Definition: MenuItems.cpp:277
int getStep() const
Definition: MenuItems.h:494
uint16_t getDivisor() const
Definition: MenuItems.cpp:267
Definition: MenuItems.h:597
bool getBoolean() const
Definition: MenuItems.h:615
BooleanMenuItem(const BooleanMenuInfo *info, bool defaultVal, MenuItem *next=nullptr, bool infoInPgm=INFO_LOCATION_PGM)
Definition: MenuItems.h:606
BooleanNaming getBooleanNaming() const
Definition: MenuItems.cpp:288
void setBoolean(bool b, bool silent=false)
Definition: MenuItems.h:617
Definition: MenuItems.h:566
int getLengthOfEnumStr(int idx) const
Definition: MenuItems.cpp:141
void copyEnumStrToBuffer(char *buffer, int size, int idx) const
Definition: MenuItems.cpp:129
EnumMenuItem(const EnumMenuInfo *info, uint8_t defaultVal, MenuItem *next=nullptr, bool infoInPgm=INFO_LOCATION_PGM)
Definition: MenuItems.h:575
Definition: MenuItems.h:625
void setFloatValue(float newVal, bool silent=false)
Definition: MenuItems.cpp:293
int getDecimalPlaces() const
Definition: MenuItems.cpp:300
FloatMenuItem(const FloatMenuInfo *info, MenuItem *next, bool infoInPgm=INFO_LOCATION_PGM)
Definition: MenuItems.h:636
float getFloatValue() const
Definition: MenuItems.h:663
FloatMenuItem(const FloatMenuInfo *info, float current, MenuItem *next, bool infoInPgm=INFO_LOCATION_PGM)
Definition: MenuItems.h:647
Definition: MenuItems.h:329
bool isReadOnly() const
Definition: MenuItems.h:386
void setChanged(bool changed)
Definition: MenuItems.cpp:112
bool isLocalOnly() const
Definition: MenuItems.h:391
void setLocalOnly(bool localOnly)
Definition: MenuItems.h:389
void changeOccurred(bool silent)
Definition: MenuItems.cpp:99
menuid_t getId() const
Definition: MenuItems.cpp:81
MenuItem(MenuType menuType, const AnyMenuInfo *menuInfo, MenuItem *next, bool infoProgMem)
Definition: MenuItems.cpp:12
void setReadOnly(bool active)
Definition: MenuItems.h:384
void setChanged(int num, bool changed)
Definition: MenuItems.h:371
const AnyMenuInfo * info
Definition: MenuItems.h:334
bool isSendRemoteNeeded(uint8_t remoteNo) const
Definition: MenuItems.cpp:22
uint8_t copyNameToBuffer(char *sz, int size) const
Definition: MenuItems.h:344
MenuType getMenuType() const
Definition: MenuItems.h:361
void setNext(MenuItem *pNext)
Definition: MenuItems.h:406
bool isVisible() const
Definition: MenuItems.h:401
void setSendRemoteNeeded(uint8_t remoteNo, bool needed)
Definition: MenuItems.cpp:26
bool isChanged(int num=0) const
Definition: MenuItems.h:373
uint16_t getEepromPosition() const
Definition: MenuItems.cpp:105
void setVisible(bool visible)
Definition: MenuItems.h:399
uint16_t getMaximumValue() const
Definition: MenuItems.cpp:90
void triggerCallback() const
Definition: MenuItems.cpp:41
void setSendRemoteNeededAll()
Definition: MenuItems.cpp:30
void setSecured(bool secured)
Definition: MenuItems.h:394
MenuItem * getNext() const
Definition: MenuItems.h:404
bool isSecured() const
Definition: MenuItems.h:396
void clearSendRemoteNeededAll()
Definition: MenuItems.cpp:37
Definition: RuntimeMenuItem.h:75
Definition: MenuItems.h:424
ValueMenuItem(MenuType menuType, const AnyMenuInfo *info, uint16_t defaultVal, MenuItem *next, bool infoPgm)
Definition: MenuItems.h:429
void setCurrentValue(uint16_t val, bool silent=false)
Definition: MenuItems.cpp:305
uint16_t getCurrentValue() const
Definition: MenuItems.h:438
Definition: MenuItems.h:445