UI_Lib  a1366e08a59cc549a65fa26081e6409aa12f26d5
This is a user interface library for graphical LCDs. It offers many different controls and indicators that can be nested depending on the element types.
Loading...
Searching...
No Matches
NumericIndicator.cpp
1/*
2 * NumericIndicator.cpp
3 */
4
6#include "Core/UI_Manager.h"
7#include <string.h>
8#include <stdio.h>
9#include <math.h>
10
11#include <stdlib.h>
12
13template <class T>
15{
16 _displayValue = value;
18
19 if (fabs(_displayValue) < pow(10, -_numFractionalDigits))
20 {
21 _displayValue = 0;
22 /* _unitPrefixPower is set above. */
23 }
24 else if (fabs(_displayValue) >= 1000)
25 {
26 while (fabs(_displayValue) >= 1000)
27 {
29 _displayValue /= 1000;
30 }
31 }
32 else if (fabs(_displayValue) < 1)
33 {
34 while (fabs(_displayValue) < 1)
35 {
37 _displayValue *= 1000;
38 }
39 }
40
41 switch (_unitPrefixPower)
42 {
43 case -12: _unitPrefix = "p"; break;
44 case -9: _unitPrefix = "n"; break;
45 case -6: _unitPrefix = "u"; break;
46 case -3: _unitPrefix = "m"; break;
47 case 0: _unitPrefix = ""; break;
48 case 3: _unitPrefix = "k"; break;
49 case 6: _unitPrefix = "M"; break;
50 case 9: _unitPrefix = "T"; break;
51 default: _unitPrefix = ""; break;
52 }
53}
54
55template <class T>
57{
58 uint8_t digits = 0;
59 //if (number < 0) digits = 1; // remove this line if '-' counts as a digit
60 while (fabs(number) >= 1)
61 {
62 number /= 10;
63 digits++;
64 }
65 return digits;
66}
67
68template <class T>
69NumericIndicator<T>::NumericIndicator(T* valuePointer, const char* baseUnit, T maxValue, unsigned char numFractionalDigits, uint16_t locX, uint16_t locY, uint8_t maxStringBufferLength) : UIElement(locX, locY, UI_INDICATOR)
70{
71 _stringDrawBuffer = new char[maxStringBufferLength]();
72 _valuePointer = valuePointer;
73 _baseUnit = baseUnit;
74 _unitPrefix = "";
75 _maxValue = maxValue;
76 _numFractionalDigits = numFractionalDigits;
78}
79
80template <class T>
82{
83 if (Visible)
84 {
86 {
87 _lastDrawnVisible = true;
88 // If the Draw() is called on a object of the NumericIndicator, the Type is UI_INDICATOR
89 // If the Draw() is called from an NumericControl object, the Type was set to UI_CONTROL there
90 if(Type == UI_INDICATOR)
91 {
92 UiManager.Gfx->fillRect(LocX, LocY, Width, Height, UiManager.ColorBackground);
93 }
94
97
98 //https://stackoverflow.com/questions/5932214/printf-string-variable-length-item
99 char formatStringBuffer[15];
100 sprintf(formatStringBuffer, "%%0%d.%df%s%s", _numDigits + (_numFractionalDigits > 0 ? 1 : 0), max(0, _numFractionalDigits + _unitPrefixPower), _unitPrefix, _baseUnit); // if _numFractionalDigits is 0, no decimal point is used (one character less); limit the number of factional characters to zero (no negative values)
101 sprintf(_stringDrawBuffer, formatStringBuffer, fabs(_displayValue));
102
103 uint16_t minus_width;
104 UiManager.Gfx->getTextBounds("-", 0, 0, nullptr, nullptr, &minus_width, nullptr);
105
106 UiManager.Gfx->setCursor(LocX + minus_width + 2 + UiManager.ElementMargin + UiManager.ElementPadding, LocY + Height - UiManager.ElementMargin - 2 * UiManager.ElementPadding - 1);
107 UiManager.Gfx->print(_stringDrawBuffer); // Draw value without minus sign
108 if (_displayValue < 0)
109 {
110 // Draw minus sign
111 UiManager.Gfx->setCursor(LocX + UiManager.ElementMargin + UiManager.ElementPadding, LocY + Height - UiManager.ElementMargin - 2 * UiManager.ElementPadding - 1);
112 UiManager.Gfx->print("-");
113 }
114 }
115 }
116 else if(!Visible && _lastDrawnVisible) // clear only when the Visible property changes from true to false
117 {
118 _lastDrawnVisible = false;
119 UiManager.Gfx->fillRect(LocX, LocY, Width, Height, UiManager.ColorBackground);
120 }
121}
122
123template <class T>
125{
126 Height = UiManager.FontHeight + 2 * UiManager.ElementPadding + 2 * UiManager.ElementMargin;
127
129 _unitPrefix = "m"; // make sure to set the _unitPrefix to a value != "" to take this width into account
130
131 //https://stackoverflow.com/questions/5932214/printf-string-variable-length-item
132 char formatStringBuffer[15];
133 sprintf(formatStringBuffer, "-%%0%d.%df%s%s", _numDigits + (_numFractionalDigits > 0 ? 1 : 0), max(0, _numFractionalDigits + _unitPrefixPower), _unitPrefix, _baseUnit); // if _numFractionalDigits is 0, no decimal point is used (one character less); limit the number of factional characters to zero (no negative values)
134 sprintf(_stringDrawBuffer, formatStringBuffer, 0.0f); // calculate the length of a string with all zeroes ("0" is a wide characters)
135
136 uint16_t minus_width;
137 UiManager.Gfx->getTextBounds("-", 0, 0, nullptr, nullptr, &minus_width, nullptr);
138
139 uint16_t string_width;
140 UiManager.Gfx->getTextBounds(_stringDrawBuffer, 0, 0, nullptr, nullptr, &string_width, nullptr);
141 Width = string_width + minus_width + 2 + 2 * UiManager.ElementPadding + 2 * UiManager.ElementMargin;
142}
Containing a class for a numeric indicator that is only showing a numeric variable value.
Containing a class that is used to handle the drawing and key handling of all UI_Elements.
UI_Manager UiManager
Access object for the singleton instance of the UI_Manager.
Definition UI_Manager.cpp:7
@ UI_INDICATOR
Indicator elements are only used to show information to the user.
Definition UIElementType.h:15
uint8_t numNonFractionalDigits(T number)
Calculate the number of non-fractional digits of the given number.
Definition NumericIndicator.cpp:56
virtual void RecalculateDimensions() override
Recalculate the Height and Width of the UIElement.
Definition NumericIndicator.cpp:124
T _maxValue
Maximum value that can be shown by this numeric indicator.
Definition NumericIndicator.h:37
char * _stringDrawBuffer
Buffer holding the string that is drawn to the screen.
Definition NumericIndicator.h:31
uint8_t _numFractionalDigits
Number of fractional digits that are shown by this indicator.
Definition NumericIndicator.h:38
const char * _baseUnit
Base unit that is appended to the calculated prefix.
Definition NumericIndicator.h:34
uint8_t _numDigits
Number of digits calculated from the maxValue (_numFractionalDigits + numNonFractionalDigits).
Definition NumericIndicator.h:39
virtual void Draw(bool redraw) override
Method used for drawing of the NumericIndicator.
Definition NumericIndicator.cpp:81
NumericIndicator(T *valuePointer, const char *baseUnit, T maxValue, unsigned char numFractionalDigits, uint16_t locX=0, uint16_t locY=0, uint8_t maxStringBufferLength=DEFAULT_NUMERIC_INDICATOR_STRING_LENGTH)
Constructor of the NumericIndicator.
Definition NumericIndicator.cpp:69
void calculateDisplayValue(float value)
Calculate the _displayValue and the _unitPrefix from the given value.
Definition NumericIndicator.cpp:14
float _displayValue
Value that is displayed by Draw().
Definition NumericIndicator.h:41
T * _valuePointer
Pointer to the numeric variable that is shown by this indicator.
Definition NumericIndicator.h:35
const char * _unitPrefix
Current display prefix character ("m", "k", "M")
Definition NumericIndicator.h:30
int8_t _unitPrefixPower
Current display prefix power (m = -3, k = 3, M = 6)
Definition NumericIndicator.h:42
T _lastValueDraw
Last drawn numeric value.
Definition NumericIndicator.h:36
bool _lastDrawnVisible
The Visible value that was last drawn.
Definition UIElement.h:26
UIElementType Type
Element type (control, indicator, container)
Definition UIElement.h:29
uint16_t Height
Drawing height of the UIElement.
Definition UIElement.h:33
uint16_t LocY
Y Location of the upper left corner of the UIElement.
Definition UIElement.h:31
bool Visible
The UIElement is only drawn if the visibility is set to true.
Definition UIElement.h:34
uint16_t Width
Drawing width of the UIElement.
Definition UIElement.h:32
UIElement(UIElementType type)
Constructor of the UIElement.
Definition UIElement.h:43
uint16_t LocX
X Location of the upper left corner of the UIElement.
Definition UIElement.h:30