-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay.cpp
More file actions
257 lines (204 loc) · 6.16 KB
/
Copy pathdisplay.cpp
File metadata and controls
257 lines (204 loc) · 6.16 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include "display.h"
#include "digits.h"
#include "imagedata.h"
#include "epdpaint.h"
#include "arrow.h"
#include "bars.h"
constexpr auto FONT_WIDTH = 32;
constexpr auto FONT_HEIGHT = 53;
constexpr auto FONT_SIZE = FONT_WIDTH * FONT_HEIGHT / 8;
constexpr auto DISPLAY_WIDTH = 152;
constexpr auto DISPLAY_HEIGHT = DISPLAY_WIDTH;
constexpr auto COLORED = 0;
constexpr auto UNCOLORED = 1;
Display::Display()
{
}
void Display::update(uint16_t voc, char trend, char level, float volt, float percent)
{
isHistory = false;
init();
epd.ClearFrame();
x = (DISPLAY_WIDTH - 4 * FONT_WIDTH) / 2;
y = (DISPLAY_HEIGHT - FONT_HEIGHT) / 2;
drawVoc(voc);
drawTrend(trend);
drawPpm();
drawLevel(level);
drawBattery(volt, percent);
epd.DisplayFrame();
off();
}
void Display::history(HistoryBuffer history)
{
if (isHistory) {
return;
}
isHistory = true;
init();
epd.ClearFrame();
x = 0;
y = 16;
auto info = history.info();
auto min = info.min;
auto max = info.max;
auto diff = max - min;
uint8_t res = diff / (DISPLAY_HEIGHT - 32) + 1;
//unsigned char image;
//Paint paint(&image, 8, 1);
//for (; x < 152; x += 8) {
// for (y = 16; y <= DISPLAY_HEIGHT - 16; ++y) {
// paint.Clear(UNCOLORED);
// for (uint8_t j = x; j < x + 8 && j < 150; ++j) {
// uint16_t value = history.get(j);
// uint8_t height = DISPLAY_HEIGHT - 16 - (value - min) / res;
// if (height == y) {
// paint.DrawPixel((j - x), 0, COLORED);
// if (j - x == 7) {
// paint.DrawPixel((j - x) - 1, 0, COLORED);
// }
// else {
// paint.DrawPixel((j - x) + 1, 0, COLORED);
// }
// }
// }
// epd.SetPartialWindow(paint.GetImage(), x, y, 8, 1);
// }
//}
unsigned char image[DISPLAY_HEIGHT - 32]{};
Paint paint(image, 8, DISPLAY_HEIGHT - 32);
paint.Clear(UNCOLORED);
for (uint8_t i = 0; i < history.size(); ++i) {
uint16_t value = history.get(i);
if (value != 0) {
paint.DrawPixel((2 + i) % 8, DISPLAY_HEIGHT - 32 - (value - min) / res, COLORED);
paint.DrawPixel((2 + i) % 8, DISPLAY_HEIGHT - 32 - (value - min) / res - 1, COLORED);
}
if (i % 8 == 5) {
epd.SetPartialWindow(paint.GetImage(), x + i, y, paint.GetWidth(), paint.GetHeight());
paint.Clear(UNCOLORED);
}
}
char str[5];
sprintf(str, "%d", min);
unsigned char image2[400]{};
Paint paint2(image2, 48, 16);
paint2.Clear(UNCOLORED);
paint2.DrawStringAt(0, 0, str, &Font16, COLORED);
epd.SetPartialWindow(paint2.GetImage(), 2, DISPLAY_HEIGHT - 16, paint2.GetWidth(), paint2.GetHeight());
paint2.Clear(UNCOLORED);
sprintf(str, "%d", max);
paint2.DrawStringAt(0, 0, str, &Font16, COLORED);
epd.SetPartialWindow(paint2.GetImage(), 2, 2, paint2.GetWidth(), paint2.GetHeight());
paint2.SetWidth(8);
paint2.Clear(UNCOLORED);
for (uint8_t i = 0; i < 15; ++i) {
paint2.DrawPixel(0, i + 1, COLORED);
}
for (uint8_t i = 0; i < 5; ++i) {
epd.SetPartialWindow(paint2.GetImage(), DISPLAY_WIDTH - 24 * i, DISPLAY_HEIGHT - 16, 8, 16);
}
epd.DisplayFrame();
off();
}
void Display::init()
{
pinMode(13, OUTPUT);
Serial.println("e-Paper initializing");
if (epd.Init() != 0) {
Serial.println("e-Paper init failed");
return;
}
Serial.println("e-Paper init go");
epd.SendCommand(VCOM_AND_DATA_INTERVAL_SETTING);
epd.SendData(0x77);
}
void Display::off()
{
epd.Sleep();
pinMode(13, INPUT);
}
void Display::drawVoc(uint16_t voc)
{
unsigned char image[FONT_SIZE]{};
char num[4]{};
for (uint8_t i = 0; i < 4; ++i) {
num[3 - i] = voc % 10;
voc /= 10;
}
uint8_t i = 0;
while (num[i] == 0) {
++i;
x += FONT_WIDTH;
}
for (; i < 4; ++i) {
char c = num[i];
for (uint8_t j = 0; j < FONT_SIZE; ++j) {
image[j] = pgm_read_byte(digits[c] + j);
}
epd.SetPartialWindow(image, x, y, FONT_WIDTH, FONT_HEIGHT);
x += FONT_WIDTH;
}
}
void Display::drawTrend(char trend)
{
if (trend == 0) {
return;
}
uint8_t image[32]{};
y += FONT_HEIGHT / 2;
if (trend > 0) {
y -= 16;
for (uint8_t j = 0; j < 32; ++j) {
image[j] = pgm_read_byte(arrows[0] + j);
}
}
else if (trend < 0) {
for (uint8_t j = 0; j < 32; ++j) {
image[j] = pgm_read_byte(arrows[1] + j);
}
}
epd.SetPartialWindow(image, x, y, 16, 16);
}
void Display::drawPpm()
{
x = 80;
y = 105;
uint8_t image[600]{};
Paint paint(image, 152, 30);
paint.Clear(UNCOLORED);
paint.DrawStringAt(0, 0, "CO ppm", &Font16, COLORED);
paint.DrawStringAt(22, 8, "2", &Font12, COLORED);
epd.SetPartialWindow(paint.GetImage(), x, y, paint.GetWidth(), paint.GetHeight());
}
void Display::drawLevel(char level)
{
x = 0;
y = DISPLAY_HEIGHT - 16;
uint8_t image[304]{};
for (uint16_t j = 0; j < 304; ++j) {
image[j] = pgm_read_byte(bars[level] + j);
}
epd.SetPartialWindow(image, x, y, 152, 16);
}
void Display::drawBattery(float volt, float percent)
{
x = 0;
y = 2;
unsigned char image[400]{};
Paint paint(image, 152, 16);
paint.Clear(UNCOLORED);
paint.DrawRectangle(2, 0, 35, 15, COLORED);
paint.DrawFilledRectangle(3, 1, (35-2)*percent, 14, COLORED);
paint.DrawFilledRectangle(35, 3, 38, 11, COLORED);
char p_string[4];
sprintf(p_string, "%d%%", (uint8_t)(percent * 100));
paint.DrawStringAt(42, 2, p_string, &Font16, COLORED);
char v_string[5];
sprintf(v_string, "%dV", (uint16_t)(volt * 100));
Serial.println(v_string);
paint.DrawStringAt(152 - 4 * 11, 2, v_string, &Font16, COLORED);
paint.DrawPixel(152 - 4 * 11 + 10, 12, COLORED);
paint.DrawPixel(152 - 4 * 11 + 11, 12, COLORED);
epd.SetPartialWindow(paint.GetImage(), x, y, paint.GetWidth(), paint.GetHeight());
}