-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDLIntMenuItem.cpp
94 lines (75 loc) · 1.77 KB
/
DLIntMenuItem.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
#include "DLMenu.h"
///
/// FloatMenuItem
///
DLIntMenuItem::DLIntMenuItem(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;
numerals = new char[digits];
explode();
}
void DLIntMenuItem::explode(void){
unsigned long r = n->getUintValue();
int i, m;
// if stored number is bigger than number of digits, drop everything
m = 1;
for(i = 0; i < digits; i++){
m *= 10;
}
if (i >= m){
n->setValue((unsigned long)0);
r = 0;
}
for (i = 0; i < digits; i++){
numerals[digits - i - 1] = r % 10;
r = r / 10;
}
}
void DLIntMenuItem::printNumber(void){
lcd->setCursor(0, 1);
for (int i = 0; i < digits; i++){
lcd->print((int)numerals[i]);
}
}
void DLIntMenuItem::add(int i)
{
numerals[s] = constrain(numerals[s] + i, 0, 9);
printNumber();
placeCursor();
}
void DLIntMenuItem::increase(void) { add(1); };
void DLIntMenuItem::decrease(void) { add(-1); };
void DLIntMenuItem::show(bool endFirst)
{
// print the label
lcd->clear();
progmem_to_lcd(lcd, 0, label);
// set cursor position
if (endFirst)
s = sections - 1;
else
s = 0;
printNumber();
placeCursor();
}
void DLIntMenuItem::hide(void)
{
// assemble an integer from numerals and save
unsigned long v = 0;
unsigned long m = 1; // 'magnitude'
for (int i = 0; i < digits; i++)
{
v += numerals[digits - i - 1] * m;
m *= 10;
}
if (n->getUintValue() != v)
n->setValue(v);
lcd->clear();
}
void DLIntMenuItem::setValue(unsigned long value){
n->setValue(value);
explode();
}