-
Notifications
You must be signed in to change notification settings - Fork 9
/
Graphics.ino
358 lines (273 loc) · 8.18 KB
/
Graphics.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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// OLED screen constant
#define ROTATION_0 0
#define ROTATION_90 1
// display fonts
#define PORT_TEMP_FONT Helv32
#define PORT_HILO_FONT Unifont16
#define PORT_TEXT_FONT TPSS8
#define LAND_TEMP_FONT Helv32
#define LAND_HILO_FONT Unifont16
#define WEATHER_FONT Weather32
struct InfoString {
int x, y;
char buf[5];
};
struct BigInfoString {
int x, y;
char buf[64];
};
struct FormattedInfo {
InfoString curTemp, hi, lo, rh, pop, rain;
InfoString icon;
BigInfoString msg;
boolean scrollingRequired = false;
}
portraitInfo, landscapeInfo;
// app variables
Ticker scroller;
int messageLength = 0;
// ------------------------
void initDisplay() {
// ------------------------
display.begin(SSD1306_SWITCHCAPVCC);
display.clearDisplay();
// built in font
display.setTextColor(WHITE);
display.setTextWrap(false);
// custom fonts
setTextColor(WHITE);
// show splash screen
display.setRotation(ROTATION_90);
display.drawBitmap(0, 10, murga, MURGA_WIDTH, MURGA_HEIGHT, 1);
setFont(PORT_HILO_FONT);
const char* splashMsg1 = "Weather";
const char* splashMsg2 = "Station";
int x1 = (64 - getStringWidth(splashMsg1)) / 2;
int x2 = (64 - getStringWidth(splashMsg2)) / 2;
drawString(x1, 80, splashMsg1);
drawString(x2, 96, splashMsg2);
// render on screen
display.display();
}
// ------------------------
void draw() {
// ------------------------
// clear whatever is being drawn
display.clearDisplay();
// pick what to show
switch(displayFormat) {
case DISPLAY_CONSOLE: showConsole(); break;
case DISPLAY_LANDSCAPE: drawLandscape(); break;
case DISPLAY_PORTRAIT: drawPortrait(); break;
}
// send draw buffer to display
display.display();
}
// ------------------------
void showConsole() {
// ------------------------
display.setRotation(ROTATION_0);
byte lineHeight = 8;
int lineNum = 0;
for(int i = currentLogLine; i < MAX_LOG_LINES; i++) {
display.setCursor(0, lineNum++ * lineHeight);
display.print(logLines[i]);
}
for(int i = 0; i < currentLogLine; i++) {
display.setCursor(0, lineNum++ * lineHeight);
display.print(logLines[i]);
}
// if showing console, slow down scrolling
delay(300);
}
// ------------------------
void drawLandscape() {
// ------------------------
display.setRotation(ROTATION_0);
// main temperature
setFont(LAND_TEMP_FONT);
drawString(landscapeInfo.curTemp.x, landscapeInfo.curTemp.y, landscapeInfo.curTemp.buf);
setFont(WEATHER_FONT);
setCursorY(-10);
if(config.unit == CELSIUS)
drawChar(WI_CELSIUS);
else
drawChar(WI_FAHRENHEIT);
// 32x32 icon
setFont(WEATHER_FONT);
setCursor(landscapeInfo.icon.x, landscapeInfo.icon.y);
drawChar(wInfo.icon);
// hi lo error display
setFont(LAND_HILO_FONT);
// draw error icon
if(wInfo.error)
drawString(0, 0, "!");
drawString(70, 35, "Hi");
drawString(70, 50, "Lo");
drawString(landscapeInfo.hi.x, landscapeInfo.hi.y, landscapeInfo.hi.buf);
drawString(landscapeInfo.lo.x, landscapeInfo.lo.y, landscapeInfo.lo.buf);
if(screen1)
// toggle alive indication dot
display.drawPixel(127, 63, WHITE);
}
// ------------------------
void drawPortrait() {
// ------------------------
display.setRotation(ROTATION_90);
setFont(PORT_TEMP_FONT);
drawString(portraitInfo.curTemp.x, portraitInfo.curTemp.y, portraitInfo.curTemp.buf);
setFont(WEATHER_FONT);
setCursorY(-10);
drawChar(WI_DEGREES);
// partition line
display.drawFastHLine(0, 34, display.width(), WHITE);
// hi lo display
setFont(PORT_HILO_FONT);
if(screen1) {
drawString(0, 38, "Hi");
const char *Lo = "Lo";
int strW = getStringWidth(Lo);
drawString(display.width() - strW, 38, Lo);
drawString(portraitInfo.hi.x, portraitInfo.hi.y, portraitInfo.hi.buf);
drawString(portraitInfo.lo.x, portraitInfo.lo.y, portraitInfo.lo.buf);
}
else {
drawString(0, 38, "Rh");
if(config.scraperOWM) {
int strW = getStringWidth("Rn");
drawString(display.width() - strW, 38, "Rn");
drawString(portraitInfo.rain.x, portraitInfo.rain.y, portraitInfo.rain.buf);
}
else {
int strW = getStringWidth("PoP");
drawString(display.width() - strW, 38, "PoP");
drawString(portraitInfo.pop.x, portraitInfo.pop.y, portraitInfo.pop.buf);
}
drawString(portraitInfo.rh.x, portraitInfo.rh.y, portraitInfo.rh.buf);
}
// draw error icon
if(wInfo.error)
drawString(0, 74, "!");
// 32x32 icon
setFont(WEATHER_FONT);
setCursor(portraitInfo.icon.x, portraitInfo.icon.y);
drawChar(wInfo.icon);
// forecast text
setFont(PORT_TEXT_FONT);
drawString(portraitInfo.msg.x, portraitInfo.msg.y, portraitInfo.msg.buf);
}
// ------------------------
void scrollMessage() {
// ------------------------
static int waits = 0;
if((displayFormat == DISPLAY_PORTRAIT) && portraitInfo.scrollingRequired) {
// add pause at begining and end of message
if(waits++ < 10)
return;
portraitInfo.msg.x--;
if(portraitInfo.msg.x == (64 - messageLength - 1))
waits = 0;
if(portraitInfo.msg.x < (64 - messageLength - 1)) {
portraitInfo.msg.x = 0;
waits = 0;
}
draw();
}
}
// ------------------------
void formatInfo() {
// ------------------------
formatPortraitInfo();
formatLandscapeInfo();
}
// ------------------------
void formatPortraitInfo() {
// ------------------------
// format for portrait mode
setFont(PORT_TEMP_FONT);
sprintf(portraitInfo.curTemp.buf, "%i", wInfo.curTemp);
int strWidth = getStringWidth(portraitInfo.curTemp.buf);
setFont(WEATHER_FONT);
strWidth += getWidth(WI_DEGREES);
portraitInfo.curTemp.x = (64 - strWidth)/2;
portraitInfo.curTemp.y = 0;
setFont(PORT_HILO_FONT);
sprintf(portraitInfo.hi.buf, "%i", wInfo.hi);
portraitInfo.hi.x = 0;
portraitInfo.hi.y = 51;
sprintf(portraitInfo.lo.buf, "%i", wInfo.lo);
strWidth = getStringWidth(portraitInfo.lo.buf);
portraitInfo.lo.x = 64 - strWidth;
portraitInfo.lo.y = 51;
sprintf(portraitInfo.rh.buf, "%i%%", wInfo.rh);
portraitInfo.rh.x = 0;
portraitInfo.rh.y = 51;
sprintf(portraitInfo.pop.buf, "%i%%", wInfo.pop);
strWidth = getStringWidth(portraitInfo.pop.buf);
portraitInfo.pop.x = 64 - strWidth;
portraitInfo.pop.y = 51;
sprintf(portraitInfo.rain.buf, "%i", wInfo.rain);
strWidth = getStringWidth(portraitInfo.rain.buf);
portraitInfo.rain.x = 64 - strWidth;
portraitInfo.rain.y = 51;
setFont(WEATHER_FONT);
int charWidth = getWidth(wInfo.icon);
int charHeight = getHeight(wInfo.icon);
int fromTop = getOffsetTop(wInfo.icon);
// x goes from 0 to 64. Center the icon
portraitInfo.icon.x = (64 - charWidth) / 2;
// y ranges from 65 <-> (128 - 16). Center vertically
portraitInfo.icon.y = 65 + (48 - charHeight)/2 - fromTop;
setFont(PORT_TEXT_FONT);
sprintf(portraitInfo.msg.buf, "%s", wInfo.msg);
strWidth = getStringWidth(portraitInfo.msg.buf);
if(strWidth > 70) {
portraitInfo.msg.x = 0;
// setup async tasks
messageLength = strWidth;
portraitInfo.scrollingRequired = true;
scroller.attach_ms(100, scrollMessage);
}
else {
portraitInfo.scrollingRequired = false;
portraitInfo.msg.x = (64 - strWidth)/2;
// detach scroller
scroller.detach();
}
portraitInfo.msg.y = 128 - 16;
}
// ------------------------
void formatLandscapeInfo() {
// ------------------------
// format for landscape mode
sprintf(landscapeInfo.curTemp.buf, "%i", wInfo.curTemp, 0xb0);
landscapeInfo.curTemp.x = 60;
landscapeInfo.curTemp.y = 0;
sprintf(landscapeInfo.hi.buf, "%i", wInfo.hi);
landscapeInfo.hi.x = 95;
landscapeInfo.hi.y = 35;
sprintf(landscapeInfo.lo.buf, "%i", wInfo.lo);
landscapeInfo.lo.x = 95;
landscapeInfo.lo.y = 50;
setFont(WEATHER_FONT);
int charWidth = getWidth(wInfo.icon);
int charHeight = getHeight(wInfo.icon);
int fromTop = getOffsetTop(wInfo.icon);
// x goes from 0 to 60. Center the icon
landscapeInfo.icon.x = (60 - charWidth) / 2;
// y ranges from 0 <-> 64. Center vertically
landscapeInfo.icon.y = ((64 - charHeight)/2 - fromTop) + 5;
// unused for now
sprintf(landscapeInfo.rh.buf, "%i%%", wInfo.rh);
landscapeInfo.rh.x = 0;
landscapeInfo.rh.y = 0;
sprintf(landscapeInfo.pop.buf, "%i%%", wInfo.pop);
landscapeInfo.pop.x = 0;
landscapeInfo.pop.y = 0;
sprintf(landscapeInfo.rain.buf, "%i", wInfo.rain);
landscapeInfo.rain.x = 0;
landscapeInfo.rain.y = 0;
sprintf(landscapeInfo.msg.buf, "%s", wInfo.msg);
landscapeInfo.msg.x = 0;
landscapeInfo.msg.y = 0;
}