tcMenu
EditableLargeNumberMenuItem.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 
6 #ifndef _EDITABLE_LARGE_NUMBER_MENU_ITEM_H_
7 #define _EDITABLE_LARGE_NUMBER_MENU_ITEM_H_
8 
9 #include "RuntimeMenuItem.h"
10 #include <IoLogging.h>
11 
18 #define LARGE_NUM_MAX_DIGITS 12
19 #define LARGE_NUM_ALLOC_SIZE (LARGE_NUM_MAX_DIGITS / 2)
20 
28 private:
29  uint8_t bcdRepresentation[LARGE_NUM_ALLOC_SIZE] = {};
30  bool negative = false;
31  uint8_t totalSize = 12;
32  uint8_t fractionDp = 0;
33 public:
37  LargeFixedNumber() = default;
38 
47  LargeFixedNumber(int totalDigits, int decimalPointIndex, uint32_t whole, uint32_t fraction, bool negative);
48 
49  LargeFixedNumber(const LargeFixedNumber& other) = default;
50  LargeFixedNumber& operator=(const LargeFixedNumber& other) = default;
51 
55  void clear();
56 
60  int decimalPointIndex() const { return fractionDp; }
61 
65  int getTotalDigits() const { return totalSize; }
66 
74  void setPrecision(uint8_t dp, uint8_t maxDigits = 12) {
75  fractionDp = dp;
76  totalSize = maxDigits;
77  clear();
78  }
79 
86  void setValue(uint32_t whole, uint32_t fraction, bool negative);
87 
93  void setFromFloat(float value);
94 
101  uint32_t fromBcdPacked(int start, int end);
102 
109  void convertToBcdPacked(uint32_t value, int start, int end);
110 
116  int getDigit(int digit);
117 
123  void setDigit(int digit, int val);
124 
132  float getAsFloat();
133 
137  bool isNegative() const { return negative; }
138 
143  void setNegative(bool neg) {
144  negative = neg;
145  }
146 
150  uint32_t getWhole() {
151  return fromBcdPacked(fractionDp, totalSize);
152  }
153 
157  uint32_t getFraction() {
158  return fromBcdPacked(0, fractionDp);
159  }
160 
165  uint8_t* getNumberBuffer() {
166  return bcdRepresentation;
167  }
168 };
169 
173 int largeNumItemRenderFn(RuntimeMenuItem* item, uint8_t row, RenderFnMode mode, char* buffer, int bufferSize);
174 
183 private:
184  LargeFixedNumber data;
185  bool negativeAllowed;
186 public:
187  EditableLargeNumberMenuItem(RuntimeRenderingFn renderFn, const LargeFixedNumber& initial, uint16_t id, bool allowNeg, MenuItem* next = nullptr)
188  : EditableMultiPartMenuItem(MENUTYPE_LARGENUM_VALUE, id, initial.getTotalDigits() + (allowNeg ? 1 : 0), renderFn, next) {
189  data = initial;
190  negativeAllowed = allowNeg;
191  }
192 
193  EditableLargeNumberMenuItem(RuntimeRenderingFn renderFn, uint16_t id, int maxDigits, int dps, bool allowNeg, MenuItem* next = nullptr)
194  : EditableMultiPartMenuItem(MENUTYPE_LARGENUM_VALUE, id, maxDigits + (allowNeg ? 1 : 0), renderFn, next) {
195  data.setPrecision(dps, maxDigits);
196  negativeAllowed = allowNeg;
197  }
198 
199  EditableLargeNumberMenuItem(RuntimeRenderingFn renderFn, uint16_t id, int maxDigits, int dps, MenuItem* next = nullptr)
200  : EditableMultiPartMenuItem(MENUTYPE_LARGENUM_VALUE, id, maxDigits + 1, renderFn, next) {
201  data.setPrecision(dps, maxDigits);
202  negativeAllowed = true;
203  }
204 
205  EditableLargeNumberMenuItem(const AnyMenuInfo* info, const LargeFixedNumber& initial, bool allowNeg, MenuItem* next = nullptr, bool isPgm = INFO_LOCATION_PGM)
206  : EditableMultiPartMenuItem(info, isPgm, MENUTYPE_LARGENUM_VALUE, initial.getTotalDigits() + (allowNeg ? 1 : 0), largeNumItemRenderFn, next) {
207  data = initial;
208  negativeAllowed = allowNeg;
209  }
210 
212  LargeFixedNumber* getLargeNumber() { return &data; }
213 
215  void setLargeNumberFromString(const char* largeNum);
216 
217  bool isNegativeAllowed() const { return negativeAllowed; }
218 };
219 
220 #endif //_EDITABLE_LARGE_NUMBER_MENU_ITEM_H_
int largeNumItemRenderFn(RuntimeMenuItem *item, uint8_t row, RenderFnMode mode, char *buffer, int bufferSize)
Definition: EditableLargeNumberMenuItem.cpp:119
RenderFnMode
Definition: MenuItems.h:283
int(* RuntimeRenderingFn)(RuntimeMenuItem *item, uint8_t row, RenderFnMode mode, char *buffer, int bufferSize)
Definition: MenuItems.h:318
@ MENUTYPE_LARGENUM_VALUE
Definition: MenuItems.h:275
Definition: MenuItems.h:51
Contains definitions of menu items that can be fully defined at runtime with no need for prog mem str...
Definition: EditableLargeNumberMenuItem.h:182
void setLargeNumberFromString(const char *largeNum)
Definition: EditableLargeNumberMenuItem.cpp:98
LargeFixedNumber * getLargeNumber()
Definition: EditableLargeNumberMenuItem.h:212
Definition: RuntimeMenuItem.h:230
Definition: EditableLargeNumberMenuItem.h:27
int decimalPointIndex() const
Definition: EditableLargeNumberMenuItem.h:60
uint32_t fromBcdPacked(int start, int end)
Definition: EditableLargeNumberMenuItem.cpp:22
void setFromFloat(float value)
Definition: EditableLargeNumberMenuItem.cpp:76
uint32_t getFraction()
Definition: EditableLargeNumberMenuItem.h:157
void setValue(uint32_t whole, uint32_t fraction, bool negative)
Definition: EditableLargeNumberMenuItem.cpp:15
void setDigit(int digit, int val)
Definition: EditableLargeNumberMenuItem.cpp:52
uint8_t * getNumberBuffer()
Definition: EditableLargeNumberMenuItem.h:165
float getAsFloat()
Definition: EditableLargeNumberMenuItem.cpp:66
void setNegative(bool neg)
Definition: EditableLargeNumberMenuItem.h:143
void clear()
Definition: EditableLargeNumberMenuItem.cpp:10
int getTotalDigits() const
Definition: EditableLargeNumberMenuItem.h:65
int getDigit(int digit)
Definition: EditableLargeNumberMenuItem.cpp:41
uint32_t getWhole()
Definition: EditableLargeNumberMenuItem.h:150
bool isNegative() const
Definition: EditableLargeNumberMenuItem.h:137
LargeFixedNumber()=default
void convertToBcdPacked(uint32_t value, int start, int end)
Definition: EditableLargeNumberMenuItem.cpp:32
void setPrecision(uint8_t dp, uint8_t maxDigits=12)
Definition: EditableLargeNumberMenuItem.h:74
Definition: MenuItems.h:329
const AnyMenuInfo * info
Definition: MenuItems.h:334
Definition: RuntimeMenuItem.h:75