-
Notifications
You must be signed in to change notification settings - Fork 0
/
oled.cpp
529 lines (495 loc) · 13.1 KB
/
oled.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
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
/**
* @file RAK1921_oled.cpp
* @author Bernd Giesecke ([email protected])
* @brief Initialization and usage of RAK1921 OLED
* @version 0.1
* @date 2022-02-18
*
* @copyright Copyright (c) 2022
*
*/
#include "app.h"
#include <nRF_SSD1306Wire.h>
void oled_show(void);
/** Width of the display in pixel */
#define OLED_WIDTH 128
/** Height of the display in pixel */
#define OLED_HEIGHT 64
/** Height of the status bar in pixel */
#define STATUS_BAR_HEIGHT 11
/** Height of a single line */
#define LINE_HEIGHT 10
/** Number of message lines */
#define NUM_OF_LINES (OLED_HEIGHT - STATUS_BAR_HEIGHT) / LINE_HEIGHT
/** Line buffer for messages */
char disp_buffer[NUM_OF_LINES + 1][32] = {0};
/** Current line used */
uint8_t current_line = 0;
/** Display class using Wire */
SSD1306Wire display(0x3c, PIN_WIRE_SDA, PIN_WIRE_SCL, GEOMETRY_128_64, &Wire);
/** Flag if display is on or off */
volatile bool display_power = true;
// Forward declarations for UI
extern uint8_t ui_last_dr;
extern uint8_t ui_max_dr;
extern uint8_t ui_min_dr;
extern uint8_t ui_last_band;
extern uint8_t ui_last_adr;
extern uint8_t ui_last_tx;
extern uint8_t ui_max_tx;
extern uint8_t ui_min_tx;
extern uint32_t ui_p2p_freq;
extern uint8_t ui_p2p_sf;
extern uint8_t ui_p2p_bw;
extern uint8_t ui_p2p_cr;
extern uint8_t ui_p2p_tx;
/**
* @brief Initialize the display
*
* @return true always
* @return false never
*/
bool init_oled(void)
{
Wire.begin();
delay(500); // Give display reset some time
display.setI2cAutoInit(true);
display.init();
display.displayOff();
display.clear();
display.displayOn();
display.setBrightness(200);
#ifdef _RAK19026_
display.flipScreenVertically();
#endif
display.setContrast(100, 241, 64);
display.setFont(ArialMT_Plain_10);
display.display();
return true;
}
/**
* @brief Write the top line of the display
*/
void oled_write_header(char *header_line, bool show_error)
{
uint16_t len = 0;
char oled_line[64];
display.setFont(ArialMT_Plain_10);
// clear the status bar
display.setColor(BLACK);
display.fillRect(0, 0, OLED_WIDTH, STATUS_BAR_HEIGHT);
display.setColor(WHITE);
display.setTextAlignment(TEXT_ALIGN_LEFT);
if (sd_card_error && has_sd && show_error)
{
MYLOG("DISP", "SD card error! %d %d", sd_card_error, has_sd);
display.drawString(0, 0, (char *)"SD CARD ERROR");
}
else
{
if (g_custom_parameters.location_on)
{
sprintf(oled_line, "%s %s", has_gnss_location ? "O" : "X", header_line);
display.drawString(0, 0, oled_line);
}
else
{
display.drawString(0, 0, header_line);
}
}
#ifdef _VARIANT_RAK4630_
uint8_t usbStatus = NRF_POWER->USBREGSTATUS;
switch (usbStatus)
{
case 0:
{
float bat = api.system.bat.get();
bat = 0.0;
for (int idx = 0; idx < 10; idx++)
{
bat += api.system.bat.get();
}
bat = bat / 10.0;
len = sprintf(oled_line, "%.2fV", bat);
display.drawString(127 - (display.getStringWidth(oled_line, len)), 0, oled_line);
break;
}
case 3: // VBUS voltage above valid threshold and USBREG output settling time elapsed (same information as USBPWRRDY event)
{
len = sprintf(oled_line, "%s", "USB");
display.drawString(127 - (display.getStringWidth(oled_line, len)), 0, oled_line);
break;
}
default:
break;
}
#else
float bat = api.system.bat.get();
bat = 0.0;
for (int idx = 0; idx < 10; idx++)
{
bat += api.system.bat.get();
}
bat = bat / 10.0;
len = sprintf(oled_line, "%.2fV", bat);
display.drawString(127 - (display.getStringWidth(oled_line, len)), 0, oled_line);
#endif
// draw divider line
display.drawLine(0, 11, 127, 11);
display.display();
}
/**
* @brief Add a line to the display buffer
*
* @param line Pointer to char array with the new line
*/
void oled_add_line(char *line)
{
if (current_line == NUM_OF_LINES)
{
// Display is full, shift text one line up
for (int idx = 0; idx < NUM_OF_LINES; idx++)
{
memcpy(disp_buffer[idx], disp_buffer[idx + 1], 32);
}
current_line--;
}
snprintf(disp_buffer[current_line], 32, "%s", line);
if (current_line != NUM_OF_LINES)
{
current_line++;
}
oled_show();
}
/**
* @brief Update display messages
*
*/
void oled_show(void)
{
display.setColor(BLACK);
display.fillRect(0, STATUS_BAR_HEIGHT + 1, OLED_WIDTH, OLED_HEIGHT);
display.setFont(ArialMT_Plain_10);
display.setColor(WHITE);
display.setTextAlignment(TEXT_ALIGN_LEFT);
for (int line = 0; line < current_line; line++)
{
display.drawString(0, (line * LINE_HEIGHT) + STATUS_BAR_HEIGHT + 1, disp_buffer[line]);
}
display.display();
}
/**
* @brief Clear the display
*
*/
void oled_clear(void)
{
display.setColor(BLACK);
display.fillRect(0, STATUS_BAR_HEIGHT + 1, OLED_WIDTH, OLED_HEIGHT);
display.setColor(WHITE);
current_line = 0;
}
/**
* @brief Write a line at given position
*
* @param line line number
* @param y_pos x position to start
* @param text String text
*/
void oled_write_line(int16_t line, int16_t y_pos, String text)
{
display.drawString(y_pos, (line * LINE_HEIGHT) + STATUS_BAR_HEIGHT + 1, text);
}
/**
* @brief Display the buffer
*
*/
void oled_display(void)
{
display.display();
}
/**
* @brief Timer callback for display saver
*
*/
void oled_saver(void *)
{
oled_power(false);
}
/**
* @brief Switch display on/off
*
* @param on_off true == Display on, false == Display off
*/
void oled_power(bool on_off)
{
if (on_off)
{
display.displayOn();
display_power = true;
// Restart display saver timer if enabled
if (g_custom_parameters.display_saver)
{
api.system.timer.start(RAK_TIMER_2, 60000, NULL);
}
}
else
{
display.displayOff();
display_power = false;
}
}
/**
* @brief Settings display handler
*
* @param menu which menu content to show
* @param menu_len number of menu items
* @param sel_menu selected menu
* @param sel_item selected item
* @param display_saver status display saver
* @param location_on status location module power saver
*/
void display_show_menu(char *menu[], uint8_t menu_len, uint8_t sel_menu, uint8_t sel_item, bool display_saver, bool location_on)
{
// char line[64];
oled_clear();
uint16_t y_pos = 0;
uint16_t line_pos = 0;
// Handle info menu
if (sel_menu == T_INFO_MENU)
{
oled_write_line(0, 0, (char *)"(1) Back");
switch (g_last_settings.test_mode)
{
case MODE_LINKCHECK:
oled_write_line(1, 0, (char *)"LinkCheck Mode");
break;
case MODE_P2P:
oled_write_line(1, 0, (char *)"P2P Mode");
break;
case MODE_FIELDTESTER:
oled_write_line(1, 0, (char *)"FieldTester mode");
break;
case MODE_FIELDTESTER_V2:
oled_write_line(1, 0, (char *)"FieldTester V2 mode");
break;
}
sprintf(line_str, "Sent interval %ds", g_last_settings.send_interval / 1000);
oled_write_line(2, 0, line_str);
sprintf(line_str, "Location %s", location_on ? "on" : "off");
oled_write_line(3, 0, line_str);
sprintf(line_str, "Display saver %s", display_saver ? "on" : "off");
oled_write_line(4, 0, line_str);
}
// Handle send interval menu
else if (sel_menu == S_SEND_INT)
{
oled_write_line(0, 0, (char *)"(1) Back");
oled_write_line(1, 0, (char *)"(2) 10 seconds more");
oled_write_line(2, 0, (char *)"(3) 10 seconds less");
sprintf(line_str, " ==> %ld s", g_last_settings.send_interval / 1000);
oled_write_line(4, 0, line_str);
}
// Handle LoRaWAN band selection
else if (sel_menu == S_LPW_BAND)
{
oled_write_line(0, 0, (char *)"(1) Back");
oled_write_line(1, 0, (char *)"(2) Next");
oled_write_line(2, 0, (char *)"(3) Prev");
uint8_t prev_item = sel_item == 0 ? 12 : sel_item - 1;
uint8_t next_item = sel_item == 12 ? 0 : sel_item + 1;
sprintf(line_str, "%s", g_regions_list[prev_item]);
oled_write_line(2, 64, line_str);
sprintf(line_str, "==> %s", g_regions_list[sel_item]);
oled_write_line(3, 64, line_str);
sprintf(line_str, "%s", g_regions_list[next_item]);
oled_write_line(4, 64, line_str);
}
// Handle LoRaWAN ADR selection
else if (sel_menu == S_LPW_ADR)
{
oled_write_line(0, 0, (char *)"(1) Back");
if (sel_item == 10)
{
sprintf(line_str, "(2) ADR OFF");
}
else
{
sprintf(line_str, "(2) ADR ON");
}
oled_write_line(1, 0, line_str);
}
// Handle LoRaWAN DR selection
else if (sel_menu == S_LPW_DR)
{
oled_write_line(0, 0, (char *)"(1) Back");
oled_write_line(1, 0, (char *)"(2) Next");
oled_write_line(2, 0, (char *)"(3) Prev");
uint8_t prev_item = (ui_last_dr == ui_min_dr ? ui_max_dr : ui_last_dr - 1);
uint8_t next_item = (ui_last_dr == ui_max_dr ? ui_min_dr : ui_last_dr + 1);
sprintf(line_str, "DR%d", prev_item);
oled_write_line(2, 64, line_str);
sprintf(line_str, "==> DR%d", ui_last_dr);
oled_write_line(3, 64, line_str);
sprintf(line_str, "DR%d", next_item);
oled_write_line(4, 64, line_str);
}
// Handle LoRaWAN TX selection
else if (sel_menu == S_LPW_TX)
{
oled_write_line(0, 0, (char *)"(1) Back");
oled_write_line(1, 0, (char *)"(2) Next");
oled_write_line(2, 0, (char *)"(3) Prev");
uint8_t prev_item = (ui_last_tx == ui_min_tx ? ui_max_tx : ui_last_tx - 1);
uint8_t next_item = (ui_last_tx == ui_max_tx ? ui_min_tx : ui_last_tx + 1);
sprintf(line_str, "TXP %d", prev_item);
oled_write_line(2, 64, line_str);
sprintf(line_str, "==> TXP %d", ui_last_tx);
oled_write_line(3, 64, line_str);
sprintf(line_str, "TXP %d", next_item);
oled_write_line(4, 64, line_str);
}
// Handle P2P frequency menu
else if (sel_menu == S_P2P_FREQ)
{
oled_write_line(0, 0, (char *)"(1) Back");
oled_write_line(1, 0, (char *)"(2) 0.1MHz up");
oled_write_line(2, 0, (char *)"(3) 0.1MHz down");
sprintf(line_str, " ==> %.3f MHz", (long)ui_p2p_freq / 1000000.0);
oled_write_line(4, 0, line_str);
}
// Handle P2P SF menu
else if (sel_menu == S_P2P_SF)
{
oled_write_line(0, 0, (char *)"(1) Back");
oled_write_line(1, 0, (char *)"(2) Next");
oled_write_line(2, 0, (char *)"(3) Prev");
uint8_t prev_item = (sel_item == 6 ? 12 : sel_item - 1);
uint8_t next_item = (sel_item == 12 ? 6 : sel_item + 1);
sprintf(line_str, "SF %d", prev_item);
oled_write_line(2, 64, line_str);
sprintf(line_str, "==> SF %d", sel_item);
oled_write_line(3, 64, line_str);
sprintf(line_str, "SF %d", next_item);
oled_write_line(4, 64, line_str);
}
// Handle P2P BW menu
else if (sel_menu == S_P2P_BW)
{
oled_write_line(0, 0, (char *)"(1) Back");
oled_write_line(1, 0, (char *)"(2) Next");
oled_write_line(2, 0, (char *)"(3) Prev");
uint8_t prev_item = (sel_item == 0 ? 9 : sel_item - 1);
uint8_t next_item = (sel_item == 9 ? 0 : sel_item + 1);
sprintf(line_str, "BW %skHz", p_bw_menu[prev_item]);
oled_write_line(2, 44, line_str);
sprintf(line_str, "==> BW %skHz", p_bw_menu[sel_item]);
oled_write_line(3, 44, line_str);
sprintf(line_str, "BW %skHz", p_bw_menu[next_item]);
oled_write_line(4, 44, line_str);
}
// Handle P2P SF menu
else if (sel_menu == S_P2P_CR)
{
oled_write_line(0, 0, (char *)"(1) Back");
oled_write_line(1, 0, (char *)"(2) Next");
oled_write_line(2, 0, (char *)"(3) Prev");
uint8_t prev_item = (sel_item == 0 ? 3 : sel_item - 1);
uint8_t next_item = (sel_item == 3 ? 0 : sel_item + 1);
sprintf(line_str, "CR 4/%d", prev_item + 5);
oled_write_line(2, 64, line_str);
sprintf(line_str, "==> CR 4/%d", sel_item + 5);
oled_write_line(3, 64, line_str);
sprintf(line_str, "CR 4/%d", next_item + 5);
oled_write_line(4, 64, line_str);
}
// Handle P2P TX selection
else if (sel_menu == S_P2P_TX)
{
oled_write_line(0, 0, (char *)"(1) Back");
oled_write_line(1, 0, (char *)"(2) Next");
oled_write_line(2, 0, (char *)"(3) Prev");
uint8_t prev_item = (sel_item == 5 ? 22 : sel_item - 1);
uint8_t next_item = (sel_item == 22 ? 5 : sel_item + 1);
sprintf(line_str, "TXP %d", prev_item);
oled_write_line(2, 64, line_str);
sprintf(line_str, "==> TXP %d", sel_item);
oled_write_line(3, 64, line_str);
sprintf(line_str, "TXP %d", next_item);
oled_write_line(4, 64, line_str);
}
// Handle "standard" menu
else
{
for (int idx = 0; idx < menu_len; idx++)
{
if (idx + 1 == sel_item)
{
sprintf(line_str, "==> %s", menu[idx]);
}
else
{
// Handle settings menu special items
if ((sel_menu == T_SETT_MENU) && (idx == 2))
{
if (location_on)
{
sprintf(line_str, "(%d) %s on", idx + 1, menu[idx]);
}
else
{
sprintf(line_str, "(%d) %s off", idx + 1, menu[idx]);
}
}
else if ((sel_menu == T_SETT_MENU) && (idx == 3))
{
if (display_saver)
{
sprintf(line_str, "(%d) %s on", idx + 1, menu[idx]);
}
else
{
sprintf(line_str, "(%d) %s off", idx + 1, menu[idx]);
}
}
else
{
sprintf(line_str, "(%d) %s", idx + 1, menu[idx]);
}
}
if (idx > 4)
{
y_pos = 64;
line_pos = idx - 5;
}
else
{
y_pos = 0;
line_pos = idx;
}
oled_write_line(line_pos, y_pos, line_str);
}
}
oled_display();
}
void prepare_oled_header(void)
{
/** Update header and battery value */
if (has_oled && !g_settings_ui)
{
oled_clear();
if (g_custom_parameters.test_mode == MODE_FIELDTESTER)
{
sprintf(line_str, "RAK FieldTester");
}
else if (g_custom_parameters.test_mode == MODE_FIELDTESTER_V2)
{
sprintf(line_str, "RAK FieldTest V2");
}
else
{
sprintf(line_str, "RAK Signal Meter");
}
oled_write_header(line_str);
}
}