-
Notifications
You must be signed in to change notification settings - Fork 0
/
oled_controller.c
607 lines (517 loc) · 13.5 KB
/
oled_controller.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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#include "sdk_config.h"
#include "helper.h"
#include "stdio.h"
#include "assert.h"
#include "nrf_delay.h"
#include "nrf_gpio.h"
#include "math.h"
#include "nrfx_spim.h"
#include "oled_controller.h"
#include "u8g2.h"
#include "u8x8.h"
#include "sdk_common.h"
#if NRF_MODULE_ENABLED(U8G2_OLED_CONTROLLER)
#define NRF_LOG_MODULE_NAME oled
#if OLED_CONFIG_LOG_ENABLED
#define NRF_LOG_LEVEL OLED_CONFIG_LOG_LEVEL
#define NRF_LOG_INFO_COLOR OLED_CONFIG_INFO_COLOR
#define NRF_LOG_DEBUG_COLOR OLED_CONFIG_DEBUG_COLOR
#else // OLED_CONFIG_LOG_ENABLED
#define NRF_LOG_LEVEL 3
#endif // OLED_CONFIG_LOG_ENABLED
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
NRF_LOG_MODULE_REGISTER();
bool detected_device = false;
#define SPI_INSTANCE 1
static const nrfx_spim_t m_spi = NRFX_SPIM_INSTANCE(SPI_INSTANCE);
APP_TIMER_DEF(m_oled_timer_id);
static volatile bool m_xfer_done = false;
static uint8_t m_sample;
static bool readsomething = false;
static u8g2_t u8g2;
oled_data_t oled_data;
volatile bool doUpdateOLED;
void updateOLEDDisplay()
{
if (doUpdateOLED)
{
oled_redraw_handler(NULL);
}
doUpdateOLED = false;
}
static void oled_timer_handler(void * p_context)
{
if (!doUpdateOLED)
{
doUpdateOLED = true;
}
}
static void oled_redraw_handler(void * p_context)
{
if (!detected_device)
{
return;
}
oled_data.tick =! oled_data.tick;
u8g2_ClearBuffer(&u8g2);
u8g2_SetFont(&u8g2, u8g2_font_ncenB08_tr);
oled_printKmh(oled_data.kmh);
oled_printCadence(oled_data.cadence);
oled_printPower(oled_data.power);
// oled_printGear(oled_data.gear);
oled_printGearRatio(oled_data.gear_ratio);
oled_printRawResistance(oled_data.raw_resistance);
// oled_printSimResistance(oled_data.sim_resistance);
oled_printTrainerResistance(oled_data.trainer_resistance);
oled_printSterzoDeactivatedIndicator();
oled_printFTMSActivatedIndicator();
if (oled_data.gear_down_counter>0)
{
oled_printGearIndicator(GEAR_DOWN_INDICATOR);
oled_data.gear_down_counter--;
}
else if (oled_data.gear_up_counter>0)
{
oled_printGearIndicator(GEAR_UP_INDICATOR);
oled_data.gear_up_counter--;
}
if (oled_data.trigger_down_counter>0)
{
oled_printTriggerDownIndicator();
oled_data.trigger_down_counter--;
}
else if (oled_data.trigger_up_counter>0)
{
oled_printTriggerUpIndicator();
oled_data.trigger_up_counter--;
}
if (oled_data.mode == SIM)
{
oled_printGrade(oled_data.grade);
oled_printWindResistance(oled_data.wind_resistance);
oled_printGravitationalResistance(oled_data.gravitational_resistance);
oled_printRollingResistance(oled_data.rolling_resistance);
if (oled_data.wind_change_counter > 0)
{
if (oled_data.tick)
{
oled_printWindChangeIndicator();
}
oled_data.wind_change_counter--;
}
if (oled_data.rolling_change_counter > 0)
{
if (oled_data.tick)
{
oled_printRollingChangeIndicator();
}
oled_data.rolling_change_counter--;
}
if (oled_data.gravitational_change_counter > 0)
{
if (oled_data.tick)
{
oled_printGravitationalChangeIndicator();
}
oled_data.gravitational_change_counter--;
}
oled_printmode(SIM);
}
else if (oled_data.mode == ERG)
{
oled_printmode(ERG);
}
if (oled_data.tick)
{
oled_printTick();
}
u8g2_SendBuffer(&u8g2);
}
static void oled_printmode(sim_mode_t mode)
{
if (!detected_device)
{
return;
}
switch (mode)
{
case SIM:
oled_printStringAt(SIXTH_ROW, 70, "SIM", false, false);
break;
case ERG:
oled_printStringAt(SIXTH_ROW, 70, "ERG", false, false);
break;
default:
break;
}
}
static void oled_printTick()
{
if (!detected_device)
{
return;
}
oled_printStringAt(THIRD_ROW, 60, ".", false, false);
}
static void oled_printGrade(double grade)
{
if (!detected_device)
{
return;
}
char buf[16];
sprintf(buf, "Gr: %.2f%%", grade);
oled_printStringAt(FOURTH_ROW, 70, buf, false, false);
}
void oled_printGearIndicator(gear_indicator_t indicator)
{
if (!detected_device)
{
return;
}
switch (indicator)
{
case GEAR_UP_INDICATOR:
oled_printStringAt(SIXTH_ROW, 110, "U", false, false);
break;
case GEAR_DOWN_INDICATOR:
oled_printStringAt(SIXTH_ROW, 110, "D", false, false);
break;
default:
break;
}
}
static void oled_printTriggerUpIndicator()
{
if (!detected_device)
{
return;
}
oled_printStringAt(THIRD_ROW, 105, "U", false, false);
}
static void oled_printTriggerDownIndicator()
{
if (!detected_device)
{
return;
}
oled_printStringAt(THIRD_ROW, 115, "D", false, false);
}
static void oled_printWindChangeIndicator()
{
if (!detected_device)
{
return;
}
oled_printStringAt(FOURTH_ROW, 60, ".", false, false);
}
static void oled_printFTMSActivatedIndicator()
{
if (!detected_device)
{
return;
}
if (oled_data.ftms_active)
{
oled_printStringAt(FIFTH_ROW, 70, "FTMS", false, false);
}
else
{
oled_printStringAt(FIFTH_ROW, 70, "F-EC", false, false);
}
}
static void oled_printSterzoDeactivatedIndicator()
{
if (!detected_device)
{
return;
}
if (!oled_data.sterzo_active)
{
oled_printStringAt(FIFTH_ROW, 75, "x", false, false);
}
}
static void oled_printGravitationalChangeIndicator()
{
if (!detected_device)
{
return;
}
oled_printStringAt(FIFTH_ROW, 60, ".", false, false);
}
static void oled_printRollingChangeIndicator()
{
if (!detected_device)
{
return;
}
oled_printStringAt(SIXTH_ROW, 60, ".", false, false);
}
static void oled_printRawResistance(int16_t raw_resistance)
{
if (!detected_device)
{
return;
}
char buf[16];
sprintf(buf, "RR: %d", raw_resistance);
oled_printStringAt(THIRD_ROW, 10, buf, false, false);
}
static void oled_printWindResistance(int16_t wind_resistance)
{
if (!detected_device)
{
return;
}
char buf[16];
sprintf(buf, "w: %d", wind_resistance);
oled_printStringAt(FOURTH_ROW, 10, buf, false, false);
}
static void oled_printGravitationalResistance(int16_t gravitational_resistance)
{
if (!detected_device)
{
return;
}
char buf[16];
sprintf(buf, "g: %d", gravitational_resistance);
oled_printStringAt(FIFTH_ROW, 10, buf, false, false);
}
static void oled_printRollingResistance(int16_t rolling_resistance)
{
if (!detected_device)
{
return;
}
char buf[16];
sprintf(buf, "r: %d", rolling_resistance);
oled_printStringAt(SIXTH_ROW, 10, buf, false, false);
}
static void oled_printSimResistance(int16_t sim_resistance)
{
if (!detected_device)
{
return;
}
char buf[16];
sprintf(buf, "SR: %d", sim_resistance);
oled_printStringAt(THIRD_ROW, 50, buf, false, false);
}
static void oled_printTrainerResistance(uint8_t sim_resistance)
{
if (!detected_device)
{
return;
}
char buf[16];
sprintf(buf, "TR: %u", sim_resistance);
oled_printStringAt(THIRD_ROW, 70, buf, false, false);
}
static void oled_printKmh(double kmh)
{
if (!detected_device)
{
return;
}
char buf[16];
sprintf(buf, "Km/h: %.0f", kmh);
oled_printStringAt(SECOND_ROW, 70, buf, false, false);
}
static void oled_printCadence(double rpm)
{
if (!detected_device)
{
return;
}
char buf[16];
sprintf(buf, "RPM: %.0f", rpm);
oled_printStringAt(FIRST_ROW, 70, buf, false, false);
}
static void oled_printPower(uint16_t power)
{
if (!detected_device)
{
return;
}
char buf[16];
sprintf(buf, "Pow: %u", power);
oled_printStringAt(SECOND_ROW, 10, buf, false, false);
}
static void oled_printGearRatio(double gear_ratio)
{
if (!detected_device)
{
return;
}
char buf[16];
//sprintf(buf, "Gear: %d", gear_offset);
sprintf(buf, "Gear: %.2f", gear_ratio);
oled_printStringAt(FIRST_ROW, 10, buf, false, false);
}
static void oled_PrintGear(int8_t gear)
{
if (!detected_device)
{
return;
}
char buf[16];
//sprintf(buf, "Gear: %d", gear_offset);
sprintf(buf, "Gear: %d", gear);
oled_printStringAt(FIRST_ROW, 10, buf, false, false);
}
void oled_printStringAt(display_row_t x, uint8_t y, char *str, bool cls, bool printImmediately)
{
if (!detected_device)
{
return;
}
if (cls)
{
u8g2_ClearBuffer(&u8g2);
}
u8g2_SetFont(&u8g2, u8g2_font_ncenB08_tr);
u8g2_DrawStr(&u8g2, y, x, str);
if (printImmediately)
{
u8g2_SendBuffer(&u8g2);
}
}
void oled_initialize()
{
ret_code_t err_code;
uint8_t address;
spi_init();
u8g2_Setup_ssd1327_ws_128x128_f(&u8g2, U8G2_R0, u8x8_HW_com_spi_nrf52832, u8g2_nrf_gpio_and_delay_spi_cb);
u8g2_InitDisplay(&u8g2);
u8g2_SetPowerSave(&u8g2,0);
err_code = app_timer_create(&m_oled_timer_id,
APP_TIMER_MODE_REPEATED,
oled_timer_handler);//oled_redraw_handler);
APP_ERROR_CHECK(err_code);
}
void startOLEDUpdates()
{
if (!detected_device)
{
return;
}
oled_data.mode = NONE;
oled_data.tick = true;
NRF_LOG_INFO("Start OLED redraw()");
ret_code_t err_code = app_timer_start(m_oled_timer_id, APP_TIMER_TICKS(REDRAW_OLED), NULL);
APP_ERROR_CHECK(err_code);
NRF_LOG_INFO("Started OLED redraw()");
}
void stopOLEDUpdates()
{
if (!detected_device)
{
return;
}
ret_code_t err_code = app_timer_stop(m_oled_timer_id);
APP_ERROR_CHECK(err_code);
NRF_LOG_INFO("Stop OLED redraw()");
}
static uint8_t u8x8_HW_com_spi_nrf52832(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
uint8_t *data;
bool res = false;
ret_code_t err_code;
static uint8_t buffer[64];
static uint8_t buf_idx = 1;
switch(msg)
{
case U8X8_MSG_BYTE_SEND:
{
buf_idx = 0;
data = (uint8_t *)arg_ptr;
while( arg_int > 0 )
{
buffer[buf_idx++] = *data;
data++;
arg_int--;
}
// m_xfer_done = false;
nrfx_spim_xfer_desc_t spim_xfer_desc = NRFX_SPIM_XFER_TX(&buffer, buf_idx);
err_code = nrfx_spim_xfer(&m_spi, &spim_xfer_desc,0);
APP_ERROR_CHECK(err_code);
/*while (!m_xfer_done)
{
__WFE();
}
*/
break;
}
case U8X8_MSG_BYTE_SET_DC:
{
u8x8_gpio_SetDC(u8x8, arg_int);
break;
}
case U8X8_MSG_BYTE_START_TRANSFER:
{
buf_idx = 0;
break;
}
case U8X8_MSG_BYTE_END_TRANSFER:
{
break;
}
default:
return 0;
}
return 1;
}
/* // Only used in asynchronous mode
void spi_handler(nrfx_spim_evt_t const * p_event, void * p_context)
{
switch(p_event->type)
{
case NRFX_SPIM_EVENT_DONE:
m_xfer_done = true;
break;
}
m_xfer_done = true;
}
*/
void spi_init (void)
{
ret_code_t err_code;
nrfx_spim_config_t spi_oled_config = NRFX_SPIM_DEFAULT_CONFIG;
spi_oled_config.sck_pin = OLED_SPI_PIN_CLK;
spi_oled_config.mosi_pin = OLED_SPI_PIN_DI;
spi_oled_config.ss_pin = OLED_SPI_PIN_CS;
spi_oled_config.frequency = NRF_SPIM_FREQ_4M; // The SSD1326 can go up to 10 MHz clock
spi_oled_config.mode = NRF_SPIM_MODE_0;
spi_oled_config.ss_active_high = false;
err_code = nrfx_spim_init(&m_spi, &spi_oled_config, NULL, NULL);
//err_code = nrfx_spim_init(&m_spi, &spi_oled_config, spi_handler, NULL); // Asynchronous mode
APP_ERROR_CHECK(err_code);
// Enable the out-of-band GPIOs
nrf_gpio_cfg_output(OLED_SPI_PIN_DC);
nrf_gpio_cfg_output(OLED_SPI_PIN_RESET);
detected_device = true;
}
uint8_t u8g2_nrf_gpio_and_delay_spi_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
switch(msg)
{
case U8X8_MSG_GPIO_DC:
nrf_gpio_pin_write(OLED_SPI_PIN_DC, arg_int);
break;
case U8X8_MSG_GPIO_RESET:
nrf_gpio_pin_write(OLED_SPI_PIN_RESET, arg_int);
break;
case U8X8_MSG_DELAY_MILLI:
nrf_delay_ms(arg_int);
break;
case U8X8_MSG_DELAY_10MICRO:
nrf_delay_us(10*arg_int);
break;
default:
u8x8_SetGPIOResult(u8x8, 1); // default return value
break;
}
return 1;
}
#endif // U8G2_OLED_CONTROLLER