-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.c
538 lines (465 loc) · 15 KB
/
graphics.c
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/**************************************************************************//**
* @brief Draws the graphics on the display
* @version 3.20.5
******************************************************************************
* @section License
* <b>(C) Copyright 2014 Silicon Labs, http://www.silabs.com</b>
*******************************************************************************
*
* This file is licensed under the Silabs License Agreement. See the file
* "Silabs_License_Agreement.txt" for details. Before using this software for
* any purpose, you must agree to the terms of that agreement.
*
******************************************************************************/
#include "graphics.h"
#include "em_types.h"
#include "glib.h"
#include "dmd.h"
#include "display.h"
#include "textdisplay.h"
#include "retargettextdisplay.h"
#include "background.h"
#include <string.h>
#include <stdio.h>
static GLIB_Context_t glibContext; /* Global glib context */
static void GRAPHICS_DrawTemperatureC(int xoffset, int yoffset, int32_t tempData);
static void GRAPHICS_DrawTemperatureF(int xoffset, int yoffset, int32_t tempData);
static void GRAPHICS_DrawHumidity(int xoffset, uint32_t rhData);
static void GRAPHICS_DrawUV(int xoffset, uint32_t uvData);
static void findCoord (int rh, int *x, int *height);
static void GRAPHICS_CreateString(char *string, int32_t value);
/* Humidity sine lookup table for coordinates (1/4 sine wave, 50 points) */
static const int8_t sinLUT [] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22
, 23, 23, 24, 25, 26, 26, 27, 28, 28, 29, 30, 30, 31, 31, 32, 32, 32, 33, 33, 33
, 34, 34, 34, 34, 34, 34, 34};
/**************************************************************************//**
* @brief Initializes the graphics stack.
* @note This function will /hang/ if errors occur (usually
* caused by faulty displays.
*****************************************************************************/
void GRAPHICS_Init(void)
{
EMSTATUS status;
/* Initialize the display module. */
status = DISPLAY_Init();
if (DISPLAY_EMSTATUS_OK != status)
while (1)
;
/* Initialize the DMD module for the DISPLAY device driver. */
status = DMD_init(0);
if (DMD_OK != status)
while (1)
;
status = GLIB_contextInit(&glibContext);
if (GLIB_OK != status)
while (1)
;
glibContext.backgroundColor = Black;
glibContext.foregroundColor = White;
}
/**************************************************************************//**
* @brief Clears the screen
*****************************************************************************/
void GRAPHICS_Clear()
{
GLIB_clear(&glibContext);
}
/**************************************************************************//**
* @brief Draws a single background image
*****************************************************************************/
static void GRAPHICS_DrawBackgroundSingle()
{
EMSTATUS status = DMD_OK;
int y;
int x;
int img;
for (y = 0; y < 128; y++)
{
/* Write bitmap to display */
img = y * (BACKGROUND_SINGLE_XSIZE / 8);
status = DMD_writeData(0, y,
backgroundBee + img,
128);
// To write the image in reverse (avoids the LSB/MSB first issue)
// for (x = 0; x < 16; x++)
// {
// status |= DMD_writeData(120-8*x, y,
// backgroundBee + img + x,
// 8);
// }
if (status != DMD_OK)
{
while (1)
;
}
}
}
/**************************************************************************//**
* @brief Draws the background image
* @param xoffset
* The background image is wider than the display. This parameter
* selects which part to show. The offset is given in multiples of 8.
* If you increase xoffset by 1, the background will be shifted by 8
* pixels
*****************************************************************************/
static void GRAPHICS_DrawBackground(int xoffset)
{
EMSTATUS status;
int y;
int img;
int size;
if (xoffset < 0)
{
size = -xoffset*8;
}
else
{
size = 128;
}
for (y = 0; y < 128; y++)
{
/* Write bitmap to display */
img = xoffset + y * (BACKGROUND_XSIZE / 8);
if (xoffset < 0)
img = (48+xoffset) + y * (BACKGROUND_XSIZE / 8);
status = DMD_writeData(0, y,
background + img,
size);
img = y * (BACKGROUND_XSIZE / 8);
if (size != 128)
status |= DMD_writeData(size, y,
background + img,
128 - size);
if (status != DMD_OK)
{
while (1)
;
}
}
}
/**************************************************************************//**
* @brief This function draws the background image
* @param xoffset
* The background image is wider than the display. This parameter
* selects which part to show. The offset is given in multiples of 8.
* If you increase xoffset by 1, the bacground will be shifted by 8
* pixels
*****************************************************************************/
void GRAPHICS_ShowStatus(bool si114x_status, bool si7013_status, bool removeObject, bool lowBat)
{
GLIB_clear(&glibContext);
if (removeObject)
{
GLIB_drawString(&glibContext, "Please remove ", 15, 5, 40, 0);
GLIB_drawString(&glibContext, "object in front", 15, 5, 50, 0);
GLIB_drawString(&glibContext, "of sensor board", 15, 5, 60, 0);
}
else
{
GLIB_drawString(&glibContext, "To start demo ", 14, 5, 20, 0);
GLIB_drawString(&glibContext, "press PB1 or", 14, 5, 30, 0);
GLIB_drawString(&glibContext, "hover hand over", 15, 5, 40, 0);
GLIB_drawString(&glibContext, "sensor board.", 13, 5, 50, 0);
}
if (lowBat)
{
GLIB_drawString(&glibContext, "Low Battery!", 16, 5, 115, 0);
}
else if ((!si114x_status) || (!si7013_status))
{
GLIB_drawString(&glibContext, "SensorEXP fail!", 16, 5, 115, 0);
}
DMD_updateDisplay();
}
/**************************************************************************//**
* @brief This function draws a filled and an open circle depending on where your hand is.
* @param size
* This parameter draws a filled cirle representing the distance to your hand.
* @param cirle
* This parameter draws an open cirle representing the threshold for a click.
* @param conf
* This parameter draws a number in the left hand corner. Used for configuration.
*****************************************************************************/
void GRAPHICS_DrawProx(int size, int circle, int conf)
{
// Draw background
GRAPHICS_DrawBackgroundSingle();
// Draw flappyConf
int lsb = conf % 10;
int msb = (conf - lsb)/10;
GLIB_drawChar(&glibContext, (char) (msb + 0x30) , 2, 2, false);
GLIB_drawChar(&glibContext, (char) (lsb + 0x30) , 10, 2, false);
// Draw distance circle
GLIB_drawCircleFilled(&glibContext, 128, 0, size);
// Draw threshold circle
if (circle)
GLIB_drawCircle(&glibContext, 128, 0, circle);
// Update display
DMD_updateDisplay();
}
/**************************************************************************//**
* @brief This function draws the actual UI
* @param xoffset
* This parameter selects which part of the UI to show.
* @param tempData
* Temperature data (given in Celsius) multiplied by 1000
* @param rhData
* Relative humidity (in percent), multiplied by 1000.
* @param uvData
* UV Index
* @param degF
* Set to 0 to display temperature in Celsius, otherwise Fahrenheit.
*****************************************************************************/
void GRAPHICS_Draw(int xoffset, int32_t tempData, uint32_t rhData,
uint32_t uvData, int yoffset, bool lowBat)
{
/* Draw background */
GRAPHICS_DrawBackground(xoffset);
if ( lowBat )
GLIB_drawString(&glibContext, "Low Battery!", 16, 28, 3, 0);
/* Draw temperature */
/* offset < 0 indicates the display needs to wrap around */
/* yoffset 0 = degC, yoffset 16 = degF */
GRAPHICS_DrawTemperatureF(xoffset, yoffset, tempData);
GRAPHICS_DrawTemperatureC(xoffset, yoffset, tempData);
GRAPHICS_DrawHumidity(xoffset, rhData);
GRAPHICS_DrawUV(xoffset, uvData);
DMD_updateDisplay();
}
/**************************************************************************//**
* @brief Helper function for drawing the temperature in Fahrenheit
* @param xoffset
* This parameter selects which part of the UI to show.
* @param yoffset
* This parameter selects which part of the UI to show.
* @param tempData
* Temperature data (given in Celsius) multiplied by 1000
*****************************************************************************/
static void GRAPHICS_DrawTemperatureF(int xoffset, int yoffset, int32_t tempData)
{
char string[10];
int dx;
int dy;
int height;
if (yoffset > 0)
dy = (yoffset * 8) - 128;
else
dy = (yoffset * 8) + 128;
dx = xoffset * 8;
tempData = ((tempData * 9) / 5) + 32000;
/* Text representation */
GRAPHICS_CreateString(string, tempData);
GLIB_drawString(&glibContext, string, strlen(string), 38 - dx, 40 - dy, 0);
/* Line in thermometer */
tempData /= 1000;
if (tempData < 16)
{
height = 91;
}
else if (tempData > 104)
{
height = 14;
}
else
{
height = 91 - (((tempData - 16) * 223) >> 8); /* *0.87 -> 223/256 */
}
if (yoffset != 16)
height = 91;
GLIB_drawLineV(&glibContext, 96 - dx, 91, height);
GLIB_drawLineV(&glibContext, 97 - dx, 91, height);
GLIB_drawLineV(&glibContext, 98 - dx, 91, height);
/* Marks on the thermometer */
GLIB_drawString(&glibContext, "F", 1, 112 - dx, 42 - dy, 0);
GLIB_drawString(&glibContext, "20", 2, 72 - dx, 85 - dy, 0);
GLIB_drawString(&glibContext, "100", 3, 64 - dx, 15 - dy, 0);
}
/**************************************************************************//**
* @brief Helper function for drawing the temperature in Celsius.
* @param xoffset
* This parameter selects which part of the UI to show.
* @param yoffset
* This parameter selects which part of the UI to show.
* @param tempData
* Temperature data (given in Celsius) multiplied by 1000
*****************************************************************************/
static void GRAPHICS_DrawTemperatureC(int xoffset, int yoffset, int32_t tempData)
{
char string[10];
int dx;
int dy;
int height;
dy = yoffset * 8;
dx = xoffset * 8;
/* Text in display */
GRAPHICS_CreateString(string, tempData);
GLIB_drawString(&glibContext, string, strlen(string), 38 - dx, 40 - dy, 0);
tempData /= 1000;
if (tempData <= 0)
{
height = 90;
}
else if (tempData > 41)
{
height = 14;
}
else
{
height = 91 - ((tempData * 238) >> 7); /* *1.86 -> 238/128 */
}
if (yoffset != 0)
height = 91;
/* Line in thermometer */
GLIB_drawLineV(&glibContext, 96 - dx, 91, height);
GLIB_drawLineV(&glibContext, 97 - dx, 91, height);
GLIB_drawLineV(&glibContext, 98 - dx, 91, height);
/* Marks on the thermometer */
GLIB_drawString(&glibContext, "0", 1, 80 - dx, 85 - dy, 0);
GLIB_drawString(&glibContext, "40", 2, 72 - dx, 15 - dy, 0);
GLIB_drawString(&glibContext, "C", 1, 112 - dx, 42 - dy, 0);
}
/**************************************************************************//**
* @brief Helper function for drawing the humidity part of the UI.
* @param xoffset
* This parameter selects which part of the UI to show.
* @param rhData
* Relative humidity (in percent), multiplied by 1000.
*****************************************************************************/
static void GRAPHICS_DrawHumidity(int xoffset, uint32_t rhData)
{
char string[10];
int height, x;
int dx;
dx = xoffset * 8;
GRAPHICS_CreateString(string, rhData);
GLIB_drawString(&glibContext, string, strlen(string), 181 - dx, 40, 0);
rhData /= 1000;
findCoord (rhData,&x,&height);
if (xoffset == 16)
GLIB_drawLine(&glibContext, 200 - dx, 105, 200 + x - dx, 105 - height);
}
/**************************************************************************//**
* @brief Helper function for drawing the UV part of the GUI.
* @param xoffset
* This parameter selects which part of the UI to show.
* @param uvData
* UV Index
*****************************************************************************/
static void GRAPHICS_DrawUV(int xoffset, uint32_t uvData)
{
char string[3];
int dx;
if (xoffset > 0)
dx = xoffset * 8;
else
dx = (xoffset+48) * 8;
/* Draw UV data */
if (uvData > 10)
{
string[0] = '1';
string[1] = '1';
string[2] = '+';
}
else
{
string[2] = ' ';
if (uvData == 10)
{
string[0] = '1';
string[1] = '0';
}
else
{
string[0] = ' ';
string[1] = '0' + uvData;
}
}
GLIB_drawString(&glibContext, string, 3, 316 - dx, 55, 0);
/* Print 5 point scale string*/
switch (uvData)
{
case 0:
case 1:
case 2:
GLIB_drawString(&glibContext, " Low", 6, 291 - dx, 96, 0);
break;
case 3:
case 4:
case 5:
GLIB_drawString(&glibContext, "Moderate", 8, 291 - dx, 96, 0);
break;
case 6:
case 7:
GLIB_drawString(&glibContext, " High", 6, 291 - dx, 96, 0);
break;
case 8:
case 9:
case 10:
GLIB_drawString(&glibContext, "Very High", 9, 291 - dx, 96, 0);
break;
default:
GLIB_drawString(&glibContext, "Extreme", 7, 291 - dx, 96, 0);
}
}
/**************************************************************************//**
* @brief Helper function for finding the positions to draw the line in the
* humidity gauge. (The other position to draw from is fixed.)
* @param[in] rh
* Relative humidity
* @param[out] x
* X coordintate to draw to.
* @param[out] height
* The y coordinate to draw to.
*****************************************************************************/
static void findCoord (int rh, int *x, int *height)
{
int index;
if (rh <= 50)
{
index = rh;
}
else
{
index = (100 - rh);
}
*height = sinLUT[index];
if (rh < 50)
{
index = 50 - rh;
*x = (-sinLUT[index]) ;
}
else
{
index = rh-50;
*x = (sinLUT[index]) ;
}
}
/**************************************************************************//**
* @brief Helper function for printing numbers. Consumes less space than
* snprintf. Limited to two digits and one decimal.
* @param string
* The string to print to
* @param value
* The value to print
*****************************************************************************/
static void GRAPHICS_CreateString(char *string, int32_t value)
{
if (value < 0)
{
value = -value;
string[0] = '-';
}
else
{
string[0] = ' ';
}
string[5] = 0;
string[4] = '0' + (value % 1000) / 100;
string[3] = '.';
string[2] = '0' + (value % 10000) / 1000;
string[1] = '0' + (value % 100000) / 10000;
if (string[1] == '0')
{
string[1] = ' ';
}
}