-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDLFloatMenuItem.cpp
119 lines (89 loc) · 2.47 KB
/
DLFloatMenuItem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "DLMenu.h"
///
/// FloatMenuItem
///
DLFloatMenuItem::DLFloatMenuItem(LiquidCrystal *lcd, const char *label, int address, int d) : DLMenuItem(lcd, label, address){
// prepare stuff
n = new DLNumber(address);
n->getUintValue();
digits = d;
sections = digits+1; // all digits plus exponent
numerals = new char[digits+1]; // to store 'exploded' digits from a float
explode();
}
void DLFloatMenuItem::explode(void){
float f = n->getFloatValue();
// exponent first
exponent = (int)floor(log10(fabs(f)));
if(exponent > 9 || exponent < -9) exponent = 0;
// convert the float to d.ddd...
f = f / (float)pow(10, exponent);
for(int i = 0; i < digits; i++){
numerals[i] = (int)floor(f);
f = f - floor(f);
f = f*10.;
}
}
void DLFloatMenuItem::printNumber(void){
lcd->setCursor(0, 1);
// first character: plus or minus sign
if(numerals[0] >= 0) lcd->print("+");
else lcd->print("-");
lcd->print(abs(numerals[0]));
lcd->print(".");
for(int i = 1; i < digits; i++){
lcd->print((int)numerals[i]);
}
lcd->print("e");
if(exponent >= 0) lcd->print("+");
else lcd->print("-");
lcd->print(abs((int)exponent));
}
void DLFloatMenuItem::placeCursor(void){
int pos;
// set cursor on the right spot:
// 0123456789ABCDEF < lcd column
// 0 1234 5 < section
// +1.3568e+2
if(s == 0) pos = 1;
else if(s == sections-1) pos = s + 4;
else pos = s + 2;
lcd->setCursor(pos, 1);
lcd->cursor();
}
void DLFloatMenuItem::show(bool endFirst){
// set the currently displayed section
// print the label
lcd->clear();
progmem_to_lcd(lcd, 0, label);
// print the number
printNumber();
// set cursor position
if(endFirst) s = sections-1;
else s = 0;
placeCursor();
}
void DLFloatMenuItem::add(int i){
if(s == 0) numerals[s] = constrain(numerals[s] + i, -9, 9);
else if(s > 0 && s < sections-1) numerals[s] = constrain(numerals[s] + i, 0, 9);
else exponent = constrain(exponent + i, -9, 9);
printNumber();
placeCursor();
}
void DLFloatMenuItem::increase(void){ add(1); };
void DLFloatMenuItem::decrease(void){ add(-1); };
void DLFloatMenuItem::hide(void){
// assemble a float from numerals and exponent and save the Number
float f = 0;
int i;
for(i = 0; i < digits; i++){
f += (float)numerals[i] * powf(10, -i);
}
f = f * powf(10, exponent);
if(n->getFloatValue() != f) n->setValue(f);
lcd->clear();
}
void DLFloatMenuItem::setValue(float value){
n->setValue(value);
explode();
}