tcMenu
EditableLargeNumberMenuItem.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 _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;
31  uint8_t totalSize;
32  uint8_t fractionDp;
33 public:
38  totalSize = 12;
39  setPrecision(4);
40  }
41 
45  void clear();
46 
50  int decimalPointIndex() const { return fractionDp; }
51 
59  void setPrecision(uint8_t dp, uint8_t maxDigits = 12) {
60  fractionDp = dp;
61  totalSize = maxDigits;
62  clear();
63  }
64 
71  void setValue(uint32_t whole, uint32_t fraction, bool negative);
72 
78  void setFromFloat(float value);
79 
86  uint32_t fromBcdPacked(int start, int end);
87 
94  void convertToBcdPacked(uint32_t value, int start, int end);
95 
101  int getDigit(int digit);
102 
108  void setDigit(int digit, int val);
109 
117  float getAsFloat();
118 
122  bool isNegative() const { return negative; }
123 
128  void setNegative(bool neg) {
129  negative = neg;
130  }
131 
135  uint32_t getWhole() {
136  return fromBcdPacked(fractionDp, totalSize);
137  }
138 
142  uint32_t getFraction() {
143  return fromBcdPacked(0, fractionDp);
144  }
145 
150  uint8_t* getNumberBuffer() {
151  return bcdRepresentation;
152  }
153 };
154 
158 int largeNumItemRenderFn(RuntimeMenuItem* item, uint8_t row, RenderFnMode mode, char* buffer, int bufferSize);
159 
167 private:
168  LargeFixedNumber data;
169  bool negativeAllowed;
170 public:
171  EditableLargeNumberMenuItem(RuntimeRenderingFn renderFn, uint16_t id, int maxDigits, int dps, bool allowNeg, MenuItem* next = nullptr)
172  : EditableMultiPartMenuItem(MENUTYPE_LARGENUM_VALUE, id, maxDigits + (allowNeg ? 1 : 0), renderFn, next) {
173  data.setPrecision(dps, maxDigits);
174  negativeAllowed = allowNeg;
175  }
176 
177  EditableLargeNumberMenuItem(RuntimeRenderingFn renderFn, uint16_t id, int maxDigits, int dps, MenuItem* next = nullptr)
178  : EditableMultiPartMenuItem(MENUTYPE_LARGENUM_VALUE, id, maxDigits + 1, renderFn, next) {
179  data.setPrecision(dps, maxDigits);
180  negativeAllowed = true;
181  }
182 
184  LargeFixedNumber* getLargeNumber() { return &data; }
185 
187  void setLargeNumberFromString(const char* largeNum);
188 
189  bool isNegativeAllowed() { return negativeAllowed; }
190 };
191 
192 #endif //_EDITABLE_LARGE_NUMBER_MENU_ITEM_H_
void setDigit(int digit, int val)
Definition: EditableLargeNumberMenuItem.cpp:51
RenderFnMode
Definition: MenuItems.h:287
uint32_t getWhole()
Definition: EditableLargeNumberMenuItem.h:135
Definition: MenuItems.h:329
uint32_t getFraction()
Definition: EditableLargeNumberMenuItem.h:142
void setPrecision(uint8_t dp, uint8_t maxDigits=12)
Definition: EditableLargeNumberMenuItem.h:59
uint8_t * getNumberBuffer()
Definition: EditableLargeNumberMenuItem.h:150
void setNegative(bool neg)
Definition: EditableLargeNumberMenuItem.h:128
void clear()
Definition: EditableLargeNumberMenuItem.cpp:9
float getAsFloat()
Definition: EditableLargeNumberMenuItem.cpp:65
void setFromFloat(float value)
Definition: EditableLargeNumberMenuItem.cpp:75
LargeFixedNumber * getLargeNumber()
Definition: EditableLargeNumberMenuItem.h:184
int largeNumItemRenderFn(RuntimeMenuItem *item, uint8_t row, RenderFnMode mode, char *buffer, int bufferSize)
Definition: EditableLargeNumberMenuItem.cpp:116
Definition: MenuItems.h:279
Definition: RuntimeMenuItem.h:201
Definition: RuntimeMenuItem.h:73
LargeFixedNumber()
Definition: EditableLargeNumberMenuItem.h:37
Definition: EditableLargeNumberMenuItem.h:27
int decimalPointIndex() const
Definition: EditableLargeNumberMenuItem.h:50
void convertToBcdPacked(uint32_t value, int start, int end)
Definition: EditableLargeNumberMenuItem.cpp:31
void setValue(uint32_t whole, uint32_t fraction, bool negative)
Definition: EditableLargeNumberMenuItem.cpp:14
int getDigit(int digit)
Definition: EditableLargeNumberMenuItem.cpp:40
int(* RuntimeRenderingFn)(RuntimeMenuItem *item, uint8_t row, RenderFnMode mode, char *buffer, int bufferSize)
Definition: MenuItems.h:318
Definition: EditableLargeNumberMenuItem.h:166
bool isNegative() const
Definition: EditableLargeNumberMenuItem.h:122
uint32_t fromBcdPacked(int start, int end)
Definition: EditableLargeNumberMenuItem.cpp:21