-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbarGraph.ino
100 lines (92 loc) · 1.98 KB
/
barGraph.ino
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
byte _level0[8] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
// -- character with one bar
byte _level1[8] = {
B10000,
B10000,
B10000,
B10000,
B10000,
B10000,
B10000,
B10000
};
// -- character with two bars
byte _level2[8] = {
B11000,
B11000,
B11000,
B11000,
B11000,
B11000,
B11000,
B11000
};
// -- character with three bars
byte _level3[8] = {
B11100,
B11100,
B11100,
B11100,
B11100,
B11100,
B11100,
B11100
};
// -- character with four bars
byte _level4[8] = {
B11110,
B11110,
B11110,
B11110,
B11110,
B11110,
B11110,
B11110
};
void initBarGraph() {
// -- creating characters
lcd.createChar(0, _level0);
lcd.createChar(1, _level1);
lcd.createChar(2, _level2);
lcd.createChar(3, _level3);
lcd.createChar(4, _level4);
}
int _prevValue = 0;
byte _lastFullChars = 0;
void drawBarGraph(byte _numCols, byte _startX, byte _startY, int value, int maxValue) {
// -- calculate full (filled) character count
byte fullChars = (long)value * _numCols / maxValue;
// -- calculate partial character bar count
byte mod = ((long)value * _numCols * 5 / maxValue) % 5;
// -- if value does not change, do not draw anything
int normalizedValue = (int)fullChars * 5 + mod;
if(_prevValue != normalizedValue) {
// -- do not clear the display to eliminate flickers
lcd.setCursor(_startX, _startY);
// -- write filled characters
for(byte i=0; i<fullChars; i++) {
lcd.write(0);
}
// -- write the partial character
if(mod > 0) {
lcd.write(mod); // -- index the right partial character
++fullChars;
}
// -- clear characters left over the previous draw
for(byte i=fullChars;i<_lastFullChars;i++) {
lcd.write(' ');
}
// -- save cache
_lastFullChars = fullChars;
_prevValue = normalizedValue;
}
}