-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDisplay.cpp
312 lines (259 loc) · 6.8 KB
/
Display.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include "Display.h"
#include "XBM.h"
#include "DisplayPageF.h"
#include "DisplayPageT.h"
#include "DisplayPageH.h"
#include "DisplayPageP.h"
#include "DisplayPageS.h"
Display::Display() {
m_isConnected = false;
m_lastHeaderUpdate = 0;
m_lastPageUpdate = 0;
m_wifiFlag = false;
m_fhemFlag = false;
m_addonFlag = false;
m_mode = "";
m_currentPage = -1;
m_offTime = 0;
}
bool Display::IsConnected() {
return m_isConnected;
}
bool Display::Begin(StateManager *stateManager, int startMode, OLED::Controllers controller) {
m_stateManager = stateManager;
m_interval = 3;
m_isConnected = m_display.Begin(0x3C, OLED::Orientations::UpsideDown, controller);
if(m_isConnected) {
m_display.Clear();
if (startMode == -1) {
m_display.On();
m_offTime = -1;
}
else {
m_offTime = millis() + startMode * 1000;
}
m_display.SetFont(Roboto_Light_15);
m_display.Print(DisplayArea_Line2, "LGW V" + m_stateManager->GetVersion(), OLED::Alignments::Center);
m_display.Refresh();
m_display.SetFont(Roboto_Light_13);
}
return m_isConnected;
}
void Display::SetWifiFlag(bool flag) {
m_wifiFlag = flag;
}
void Display::SetFhemFlag(bool flag) {
m_fhemFlag = flag;
}
void Display::SetAddonFlag(bool flag) {
m_addonFlag = flag;
}
void Display::SetLED(bool show) {
Clear(DisplayArea_LED);
if (show) {
m_display.DrawRect(DisplayArea_LED, true, OLED::Colors::White);
}
m_display.Refresh();
}
void Display::ShowProgress(unsigned int maxValue, String message) {
m_maxProgress = maxValue;
m_currentProgress = 0;
Print(message, DisplayArea_Line1, OLED::Alignments::Center);
m_display.DrawRect(DisplayArea_ProgressBar, true, OLED::Colors::Black);
m_display.DrawRect(DisplayArea_ProgressBar, false);
Print("0%", DisplayArea_Line3, OLED::Alignments::Center);
m_display.Refresh();
}
void Display::MoveProgress(unsigned long offset) {
if (offset == 0) {
m_currentProgress++;
}
else {
m_currentProgress += offset;
}
byte percent = (float)((float)m_currentProgress / (float)m_maxProgress) * 100;
byte width = percent * 1.24;
m_display.DrawRect(2, DisplayArea_ProgressBar[1] + 2, width, DisplayArea_ProgressBar[3] -3, true);
Print(String(percent) + "%", DisplayArea_Line3, OLED::Alignments::Center);
m_display.Refresh();
}
void Display::HideProgress() {
Clear(DisplayArea_Pages);
m_display.Refresh();
}
void Display::PushContent() {
m_display.PushContent();
}
void Display::PopContent() {
m_display.PopContent();
}
void Display::Refresh() {
m_display.Refresh();
}
bool Display::IsOn() {
return m_display.IsOn();
}
void Display::Print(String text, const int area[4], OLED::Alignments alignment, bool largeFont) {
m_display.DrawRect(area, true, OLED::Colors::Black);
if (largeFont) {
m_display.SetFont(Roboto_Light_20);
}
else {
m_display.SetFont(Roboto_Light_13);
}
m_display.Print(area, text, alignment);
m_display.SetFont(Roboto_Light_13);
}
void Display::DrawXBM(const int area[4], const char *xbm) {
m_display.DrawXBM(area[0], area[1], area[2], area[3], xbm);
}
void Display::Command(String command) {
String lowerCommand = command;
lowerCommand.toLowerCase();
if (lowerCommand == "off") {
m_display.Off();
}
else {
m_display.On();
}
int pos = lowerCommand.indexOf('=');
if (pos != -1) {
String value = command.substring(pos +1);
lowerCommand = lowerCommand.substring(0, pos);
if (lowerCommand == "mode") {
m_mode = value;
m_currentPage = -1;
m_lastPageUpdate = 0;
}
else if (lowerCommand == "interval") {
m_interval = value.toInt();
}
else if (lowerCommand == "show") {
Values.FhemText1 = "";
Values.FhemText2 = "";
Values.FhemText3 = "";
Values.FhemIcon = "";
value += ",";
String part = "";
byte idx = 0;
for (byte b = 0; b < value.length(); b++) {
char c = value[b];
if (c != ',') {
part += c;
}
else {
switch (idx) {
case 0:
Values.FhemText1 = part;
break;
case 1:
Values.FhemText2 = part;
break;
case 2:
Values.FhemText3 = part;
break;
case 3:
Values.FhemIcon = part;
break;
default:
break;
}
idx++;
part = "";
}
}
m_mode = "f";
m_currentPage = -1;
m_lastPageUpdate = 0;
}
}
}
void Display::UpdatePages() {
char nextPage = '-';
if (m_mode.length() > 0) {
if (m_currentPage == -1) {
m_currentPage = 0;
nextPage = m_mode[m_currentPage];
}
else {
m_currentPage++;
if (m_currentPage > m_mode.length() -1) {
m_currentPage = 0;
}
nextPage = m_mode[m_currentPage];
}
}
if (nextPage == 'f') {
DrawPageF(this);
}
else if (nextPage == 't') {
DrawPageT(this);
}
else if (nextPage == 'h') {
DrawPageH(this);
}
else if (nextPage == 's') {
DrawPageS(this);
}
else if (nextPage == 'p') {
DrawPageP(this);
}
}
void Display::Handle(WSBase::Frame frame, unsigned int framesPerMinute, String version, int32_t rssi, bool wifiFlag) {
m_wifiFlag = wifiFlag;
if (IsConnected()) {
if (m_offTime >= 0 && millis() > m_offTime) {
m_display.Off();
m_offTime = -1;
}
if (millis() < m_lastHeaderUpdate) {
m_lastHeaderUpdate = 0;
}
if (millis() > m_lastHeaderUpdate + 1000) {
Clear(DisplayArea_RSSI);
String text;
text += rssi;
text += " dBm";
m_display.Print(DisplayArea_RSSI, text, OLED::Alignments::Right);
if (m_wifiFlag) {
m_display.DrawXBM(DisplayArea_WifiFlag, xbm_wifi_bits);
}
else {
Clear(DisplayArea_WifiFlag);
}
if (m_fhemFlag) {
m_display.DrawXBM(DisplayArea_FhemFlag, xbm_fhem_bits);
}
else {
Clear(DisplayArea_FhemFlag);
}
if (m_addonFlag) {
m_display.DrawXBM(DisplayArea_AddonFlag, xbm_cpu_bits);
}
else {
Clear(DisplayArea_AddonFlag);
}
m_display.Refresh();
m_stateManager->SetDisplayStatus(IsOn() ? "on" : "off");
m_lastHeaderUpdate = millis();
}
if (millis() < m_lastPageUpdate) {
m_lastPageUpdate = 0;
}
if (millis() > m_lastPageUpdate + m_interval * 1000) {
Values.Temperature = frame.Temperature;
Values.Humidity = frame.Humidity;
Values.Pressure = frame.Pressure;
Values.FramesPerMinute = framesPerMinute;
Values.Version = version;
UpdatePages();
m_lastPageUpdate = millis();
}
}
}
void Display::Clear() {
m_display.Clear();
}
void Display::Clear(const int area[4]) {
m_display.DrawRect(area[0], area[1], area[2], area[3], true, OLED::Colors::Black);
}