-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathui.cpp
2910 lines (2565 loc) · 88.1 KB
/
ui.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
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <string.h>
#include <float.h>
#include <math.h>
#include "pico/multicore.h"
#include "ui.h"
#include "fft_filter.h"
#include <hardware/flash.h>
#include "pico/util/queue.h"
#include "fonts.h"
#include <algorithm>
#define WATERFALL_WIDTH (128)
#define WATERFALL_MAX_VALUE (64)
////////////////////////////////////////////////////////////////////////////////
// Encoder
////////////////////////////////////////////////////////////////////////////////
void ui::setup_encoder()
{
gpio_set_function(PIN_AB, GPIO_FUNC_PIO1);
gpio_set_function(PIN_AB+1, GPIO_FUNC_PIO1);
uint offset = pio_add_program(pio, &quadrature_encoder_program);
quadrature_encoder_program_init(pio, sm, offset, PIN_AB, 1000);
if ((settings[idx_hw_setup] >> flag_encoder_res) & 1) {
new_position = -((quadrature_encoder_get_count(pio, sm) + 1)/2);
} else {
new_position = -((quadrature_encoder_get_count(pio, sm) + 2)/4);
}
old_position = new_position;
}
int32_t ui::get_encoder_change(void)
{
if ((settings[idx_hw_setup] >> flag_encoder_res) & 1) {
new_position = -((quadrature_encoder_get_count(pio, sm) + 1)/2);
} else {
new_position = -((quadrature_encoder_get_count(pio, sm) + 2)/4);
}
int32_t delta = new_position - old_position;
old_position = new_position;
if((settings[idx_hw_setup] >> flag_reverse_encoder) & 1)
{
return -delta;
} else {
return delta;
}
}
int32_t ui::encoder_control(int32_t *value, int32_t min, int32_t max)
{
int32_t position_change = get_encoder_change();
*value += position_change;
if(*value > max) *value = min;
if(*value < min) *value = max;
return position_change;
}
////////////////////////////////////////////////////////////////////////////////
// Display
////////////////////////////////////////////////////////////////////////////////
void ui::setup_display() {
disp.external_vcc=false;
ssd1306_init(&disp, 128, 64, 0x3C, i2c1);
}
void ui::display_clear(bool colour)
{
cursor_x = 0;
cursor_y = 0;
ssd1306_clear(&disp, colour);
}
void ui::display_clear_str(uint32_t scale, bool colour)
{
ssd1306_fill_rectangle(&disp, 0, cursor_y, 128, 9*scale, colour);
}
void ui::display_linen(uint8_t line)
{
cursor_y = 9*(line-1);
cursor_x = 0;
}
void ui::display_set_xy(int16_t x, int16_t y)
{
cursor_x = x;
cursor_y = y;
}
void ui::display_add_xy(int16_t x, int16_t y)
{
cursor_x += x;
cursor_y += y;
}
uint16_t ui::display_get_x() { return cursor_x; }
uint16_t ui::display_get_y() { return cursor_y; }
void ui::display_draw_separator(uint16_t y, uint32_t scale, bool colour){
// always draw top line
ssd1306_draw_line(&disp, 0, y, 127, y, colour);
// for 2px, just draw another below
if (scale == 2) {
ssd1306_draw_line(&disp, 0, y+1, 127, y+1, colour);
}
// for 3px draw top and bottom, middle blank
if (scale == 3) {
ssd1306_draw_line(&disp, 0, y+2, 127, y+2, colour);
}
}
void ui::display_print_char(char x, uint32_t scale, uint32_t style)
{
if ( !(style&style_nowrap) && (cursor_x > 128 - 6*(signed)scale)) {
cursor_x = 0;
cursor_y += 9*scale;
}
int colour = 1;
if (style & style_reverse) colour=0;
if (style & style_xor) colour=2;
if (scale & 0x01) { // odd numbers use 8x6 chars
ssd1306_draw_char_with_font(&disp, cursor_x, cursor_y, scale, font_8x5, x, colour);
} else { // even, use 16x12
ssd1306_draw_char_with_font(&disp, cursor_x, cursor_y, scale/2, font_16x12, x, colour);
}
cursor_x += (6*scale);
}
/* return index of 1st match. -1 if not found */
int ui::strchr_idx(const char str[], uint8_t c) {
for (unsigned int i=0; i<strlen(str);i++){
if (str[i] == c) return i;
}
return -1;
}
void ui::display_print_str(const char str[], uint32_t scale, uint32_t style)
{
int16_t box_x1 = INT16_MAX;
int16_t box_y1 = INT16_MAX;
int16_t box_x2 = INT16_MIN;
int16_t box_y2 = INT16_MIN;
bool colour = !(style&style_reverse);
int next_ln;
unsigned int length;
// find the index of the next \n
next_ln = strchr_idx( str, '\n');
// if found, compute length of string, if not, length to end of str
length = (next_ln<0) ? strlen(str) : (unsigned)next_ln;
if (style & style_centered) {
cursor_x = (128- 6*scale*length)/2;
}
if (style & style_right) {
cursor_x = (128 - 6*scale*length);
}
for (size_t i=0; i<strlen(str); i++) {
if (str[i] == '\a') {
colour = !colour;
continue;
}
if (str[i] == '\n') {
next_ln = strchr_idx( &str[i+1], '\n');
length = (next_ln<0) ? strlen(str)-(i+1) : (unsigned)next_ln-(i+1);
if (style & style_centered) {
cursor_x = (128- 6*scale*length)/2;
} else if (style & style_right) {
cursor_x = (128- 6*scale*length);
} else {
cursor_x = 0;
}
cursor_y += 9*scale;
continue;
}
if ( !(style&style_nowrap) && (cursor_x > 128 - 6*(signed)scale)) {
cursor_x = 0;
cursor_y += 9*scale;
}
if (scale & 0x01) { // odd numbers use 8x6 chars
ssd1306_draw_char_with_font(&disp, cursor_x, cursor_y, scale, font_8x5, str[i], colour);
} else { // even, use 16x12
ssd1306_draw_char_with_font(&disp, cursor_x, cursor_y, scale/2, font_16x12, str[i], colour);
}
if (style&style_bordered) {
if (cursor_x < box_x1) box_x1=cursor_x;
if (cursor_y < box_y1) box_y1=cursor_y;
if ((signed)(cursor_x + 5*scale) > box_x2) box_x2 = (cursor_x + 5*scale);
if ((signed)(cursor_y + 8*scale) > box_y2) box_y2 = (cursor_y + 8*scale);
}
cursor_x += 6*scale;
}
if (style&style_bordered) {
// text, black, white, black
ssd1306_draw_rectangle(&disp, box_x1-1, box_y1-1, box_x2-box_x1+1, box_y2-box_y1+1, 1-colour);
ssd1306_draw_rectangle(&disp, box_x1-2, box_y1-2, box_x2-box_x1+3, box_y2-box_y1+3, colour);
ssd1306_draw_rectangle(&disp, box_x1-3, box_y1-3, box_x2-box_x1+5, box_y2-box_y1+5, 1-colour);
}
}
void ui::display_print_num(const char format[], int16_t num, uint32_t scale, uint32_t style)
{
char buff[16];
snprintf(buff, 16, format, num);
display_print_str(buff, scale, style);
}
void ui::display_print_freq(char separator, uint32_t frequency, uint32_t scale, uint32_t style)
{
char buff[16];
const int32_t MHz = frequency / 1000000;
frequency %= 1000000;
const int32_t kHz = frequency / 1000;
frequency %= 1000;
const int32_t Hz = frequency;
snprintf(buff, 16, "%2ld%c%03ld%c%03ld", MHz, separator, kHz, separator, Hz);
display_print_str(buff, scale, style);
}
void ui::display_draw_icon(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint16_t pixels[])
{
for (uint8_t yy = 0; yy < h; ++yy)
{
for (uint8_t xx = 0; xx < w; ++xx)
{
ssd1306_draw_pixel(&disp, x+h-xx-1, y+yy, (pixels[yy] >> xx) & 1);
}
}
}
void ui::display_draw_volume(uint8_t v, uint8_t x)
{
const uint16_t mute_icon[15] = {0x0, 0xa, 0x1a, 0x38, 0x78, 0x6f8, 0x6f8, 0x6f8, 0x7f8, 0x6f8, 0x478, 0x838, 0x1018, 0x8, 0x0};
const uint8_t scaled_volume = (v+5)*12/9;
if (v == 0)
{
display_draw_icon(x, 0, 16, 15, mute_icon);
}
else
{
for (uint8_t i = 0; i < 16; i++)
{
uint8_t h = i * 12/16;
if(scaled_volume >= i) u8g2_DrawVLine(&u8g2, x + i, 13-h, h);
}
}
}
void ui::display_draw_battery(float v, uint8_t x)
{
u8g2_DrawVLine(&u8g2, x+0 , 2, 12);
u8g2_DrawVLine(&u8g2, x+14, 2, 12);
u8g2_DrawVLine(&u8g2, x+15, 6, 4);
u8g2_DrawHLine(&u8g2, x+0, 2, 14);
u8g2_DrawHLine(&u8g2, x+0, 13, 14);
const bool vbus_present = gpio_get(24);
if(vbus_present)
{
const uint16_t power_icon[8] = {0x10, 0x18, 0x11C, 0xDC, 0xF6, 0x72, 0x31, 0x10};
display_draw_icon(x+4, 4, 9, 8, power_icon);
}
else
{
const float v_min = 1.8f;
const float v_max = 5.5f;
const uint8_t pixels = 11.0f*(v-v_min)/(v_max-v_min);
u8g2_DrawBox(&u8g2, x+2, 4, pixels, 8);
}
}
void ui::display_show()
{
// Enable below to output display contents to uart
#if 0
const uint16_t w = u8g2_GetDisplayWidth(&u8g2);
const uint16_t h = u8g2_GetDisplayHeight(&u8g2);
const uint16_t p = u8g2_GetBufferTileHeight(&u8g2);
for (size_t j = 0; j < h / p; j++)
{
for (size_t i = 0; i < w; i++)
{
printf("%02x,", *(u8g2.tile_buf_ptr + i + j * (w)));
}
printf("\n");
}
printf("\n");
#endif
u8g2_SendBuffer(&u8g2);
}
////////////////////////////////////////////////////////////////////////////////
// Home page status display (original)
////////////////////////////////////////////////////////////////////////////////
void ui::renderpage_original(rx_status & status, rx & receiver)
{
receiver.access(false);
const float power_dBm = status.signal_strength_dBm;
const float battery_voltage = 3.0f * 3.3f * (status.battery/65535.0f);
receiver.release();
const uint8_t buffer_size = 21;
char buff [buffer_size];
display_clear();
//frequency
uint32_t remainder, MHz, kHz, Hz;
MHz = (uint32_t)settings[idx_frequency]/1000000u;
remainder = (uint32_t)settings[idx_frequency]%1000000u;
kHz = remainder/1000u;
remainder = remainder%1000u;
Hz = remainder;
u8g2_SetFont(&u8g2, font_seg_big);
snprintf(buff, buffer_size, "%2lu", MHz);
u8g2_DrawStr(&u8g2, 0, 42, buff);
snprintf(buff, buffer_size, "%03lu", kHz);
u8g2_DrawStr(&u8g2, 39, 42, buff);
u8g2_DrawBox(&u8g2, 35, 39, 3, 3);
u8g2_SetFont(&u8g2, font_seg_mid);
snprintf(buff, buffer_size, "%03lu", Hz);
u8g2_DrawStr(&u8g2, 94, 31, buff);
u8g2_DrawBox(&u8g2, 90, 29, 3, 3);
//mode
const uint8_t text_height = 14u;
u8g2_SetFont(&u8g2, u8g2_font_9x15_tf);
u8g2_DrawStr(&u8g2, 0, text_height, modes[settings[idx_mode]]);
uint16_t x = u8g2_GetStrWidth(&u8g2, modes[0]) + 2;
//volume
display_draw_volume(settings[idx_volume], x);
x += 18;
//battery
display_draw_battery(battery_voltage, x);
//power
snprintf(buff, buffer_size, "% 4ddBm", (int)power_dBm);
uint16_t w = u8g2_GetStrWidth(&u8g2, buff);
u8g2_DrawStr(&u8g2, 127-w, text_height, buff);
//step size
u8g2_SetFont(&u8g2, u8g2_font_7x14_tf);
w = u8g2_GetStrWidth(&u8g2, steps[settings[idx_step]]);
u8g2_DrawStr(&u8g2, 127 - w, 42, steps[settings[idx_step]]);
int8_t power_s = dBm_to_S(power_dBm);
const uint16_t seg_w = 8;
const uint16_t seg_h = 12;
const uint16_t seg_y = 47;
const uint16_t seg_x = 3;
u8g2_SetDrawColor(&u8g2, 1);
u8g2_DrawRFrame(&u8g2, seg_x, seg_y, (seg_w+1)*13+4, seg_h+5, 2);
for (int8_t i = 0; i < 13; i++)
{
u8g2_SetDrawColor(&u8g2, 0);
u8g2_DrawRBox(&u8g2, i * (seg_w + 1) - 1 + seg_x + 2, seg_y+2, seg_w + 2, seg_h + 2, 2);
u8g2_SetDrawColor(&u8g2, 1);
if (i < power_s)
{
u8g2_DrawRBox(&u8g2, i * (seg_w + 1) + seg_x + 2, seg_y+2, seg_w, seg_h, 2);
}
}
u8g2_DrawVLine(&u8g2, settings[idx_squelch] * (seg_w + 1) + seg_x + 2, seg_y, seg_h+4);
const char smeter[13][6] = {"S0", "S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "+10", "+20", "+30"};
u8g2_SetFont(&u8g2, u8g2_font_9x15_tf);
w = u8g2_GetStrWidth(&u8g2, smeter[power_s]);
u8g2_SetDrawColor(&u8g2, 0);
u8g2_DrawRBox(&u8g2, (128-(w+4))/2, 48, w+4, 14, 2);
u8g2_SetDrawColor(&u8g2, 1);
u8g2_DrawStr(&u8g2, (128-w)/2, 60, smeter[power_s]);
display_show();
}
////////////////////////////////////////////////////////////////////////////////
// Home page status display with bigger spectrum view
////////////////////////////////////////////////////////////////////////////////
void ui::renderpage_bigspectrum(rx_status & status, rx & receiver)
{
display_clear();
draw_slim_status(0, status, receiver);
draw_h_tick_marks(8);
draw_spectrum(13, 63);
display_show();
}
////////////////////////////////////////////////////////////////////////////////
// Home page status display with combined view
////////////////////////////////////////////////////////////////////////////////
void ui::renderpage_combinedspectrum(bool view_changed, rx_status & status, rx & receiver)
{
if (view_changed) display_clear();
ssd1306_fill_rectangle(&disp, 0, 0, 128, 48, 0);
draw_waterfall(48);
draw_slim_status(0, status, receiver);
draw_h_tick_marks(8);
draw_spectrum(13, 47);
display_show();
}
////////////////////////////////////////////////////////////////////////////////
// Home page status display with big waterfall
////////////////////////////////////////////////////////////////////////////////
void ui::renderpage_waterfall(bool view_changed, rx_status & status, rx & receiver)
{
if (view_changed) display_clear();
ssd1306_fill_rectangle(&disp, 0, 0, 128, 13, 0);
draw_waterfall(13);
draw_h_tick_marks(8);
draw_slim_status(0, status, receiver);
display_show();
}
////////////////////////////////////////////////////////////////////////////////
// Home page status display with big simple text
////////////////////////////////////////////////////////////////////////////////
void ui::renderpage_status(rx_status & status, rx & receiver)
{
receiver.access(false);
const float battery_voltage = 3.0f * 3.3f * (status.battery/65535.0f);
const float temp_voltage = 3.3f * (status.temp/65535.0f);
const float temp = 27.0f - (temp_voltage - 0.706f)/0.001721f;
const float block_time = (float)adc_block_size/(float)adc_sample_rate;
const float busy_time = ((float)status.busy_time*1e-6f);
const uint8_t usb_buf_level = status.usb_buf_level;
receiver.release();
display_clear();
draw_slim_status(0, status, receiver);
u8g2_SetDrawColor(&u8g2, 1);
u8g2_SetFont(&u8g2, u8g2_font_6x10_tf);
u8g2_DrawHLine(&u8g2, 0, 8, 128);
const uint8_t buffer_size = 21;
char buff [buffer_size];
//battery
uint16_t y = 8; //draw from left
y += 10;
snprintf(buff, buffer_size, "Battery : %2.1fV", battery_voltage);
u8g2_DrawStr(&u8g2, 0, y, buff);
//temp
y += 10;
snprintf(buff, buffer_size, "CPU Temp: %2.0f%cC", temp, '\xb0');
u8g2_DrawStr(&u8g2, 0, y, buff);
//cpu load
y += 10;
snprintf(buff, buffer_size, "CPU Load: %3.0f%%", (100.0f * busy_time) / block_time);
u8g2_DrawStr(&u8g2, 0, y, buff);
//usb buffer
y += 10;
snprintf(buff, buffer_size, "USB Buff: %3d%%", usb_buf_level);
u8g2_DrawStr(&u8g2, 0, y, buff);
display_show();
}
void ui::renderpage_fun(bool view_updated, rx_status & status, rx & receiver)
{
static int degrees = 0;
static int xm, ym;
if (degrees == 0) {
xm = rand()%10+1;
ym = rand()%10;
}
display_clear();
ssd1306_bmp_show_image(&disp, crystal, sizeof(crystal));
ssd1306_scroll_screen(&disp, 40*cos(xm*M_PI*degrees/180), 20*sin(ym*M_PI*degrees/180));
if ((degrees+=3) >=360) degrees = 0;
display_show();
}
// Draw a slim 8 pixel status line
void ui::draw_slim_status(uint16_t y, rx_status & status, rx & receiver)
{
receiver.access(false);
const float power_dBm = status.signal_strength_dBm;
receiver.release();
display_set_xy(0,y);
display_print_freq(',', settings[idx_frequency],1);
display_add_xy(4,0);
//mode
display_print_str(modes[settings[idx_mode]],1);
//signal strength dBm
display_print_num("% 4ddBm", (int)power_dBm, 1, style_right);
}
// draw vertical signal strength
void ui::draw_vertical_dBm(uint16_t x, float power_dBm, float squelch) {
int bar_len = dBm_to_63px(power_dBm);
int sq = dBm_to_63px(squelch);
ssd1306_fill_rectangle(&disp, x, 0, 3, 63, 0);
ssd1306_fill_rectangle(&disp, x, 63 - bar_len, 3, bar_len + 1, 1);
ssd1306_draw_line(&disp, x, 63-sq, x+3, 63-sq, 2);
}
int ui::dBm_to_S(float power_dBm) {
int power_s = floorf((power_dBm-S0)/6.0f);
if(power_dBm >= S9) power_s = floorf((power_dBm-S9)/10.0f)+9;
if(power_s < 0) power_s = 0;
if(power_s > 12) power_s = 12;
return (power_s);
}
float ui::S_to_dBm(int S) {
float dBm = 0;
if (S<=9) {
dBm = S0 + 6.0f * S;
} else {
dBm = S9_10 + (S-10) * 10.f;
}
return (dBm);
}
int32_t ui::dBm_to_63px(float power_dBm) {
int32_t power = floorf((power_dBm-S0));
power = power * 63 / (S9_10 + 20 - S0);
if (power<0) power=0;
if (power>63) power=63;
return (power);
}
void ui::draw_h_tick_marks(uint16_t startY)
{
// tick marks at startY
ssd1306_draw_line(&disp, 0, startY + 2, 127, startY + 2, 1);
ssd1306_draw_line(&disp, 0, startY, 0, startY + 4, 1);
ssd1306_draw_line(&disp, 64, startY, 64, startY + 4, 1);
ssd1306_draw_line(&disp, 127, startY, 127, startY + 4, 1);
ssd1306_draw_line(&disp, 32, startY + 1, 32, startY + 3, 1);
ssd1306_draw_line(&disp, 96, startY + 1, 96, startY + 3, 1);
}
////////////////////////////////////////////////////////////////////////////////
// draw a classic analog meter movement.
////////////////////////////////////////////////////////////////////////////////
// height positive : a circle sector from the top down up to and including the full circle
// height positive : draw a linear movement meter like all the cheap CBs in the 80s
void ui::draw_analogmeter( uint16_t startx, uint16_t starty,
uint16_t width, int16_t height,
float needle_pct, int numticks,
const char* legend, const char labels[][5]
) {
#define TICK_LEN 3
// I hope you like high school trig and geometry...
int segment_h = height; // pixels high
int segment_w2 = width/2; // pixels wide
// compute the radius
float radius;
float halfdeg_range;
float deg_min, deg_max, deg_range;
// pointless and crashed with DIV0
if (height == 0) return;
if (height > 0) { // positive height, angular meter
if (height <= segment_w2) {
radius = (pow(segment_w2, 2) / segment_h + segment_h) / 2;
halfdeg_range = asinf(segment_w2/radius)*180.0 / M_PI;
deg_min = (90-halfdeg_range);
deg_max = (90+halfdeg_range);
deg_range = (deg_max-deg_min);
} else { // (height > segment_w2)
radius = segment_w2;
halfdeg_range = acosf((segment_h-radius)/radius)*180.0 / M_PI;
deg_min = (-90 +halfdeg_range);
deg_max = (270 -halfdeg_range);
deg_range = (deg_max-deg_min);
}
// draw arc
for (int degrees=deg_min; degrees<=deg_max; degrees++) {
ssd1306_draw_pixel(&disp,
(startx+width/2) + radius*cos(M_PI*degrees/180),
(starty + radius) - radius*sin(M_PI*degrees/180),
1);
ssd1306_draw_pixel(&disp,
(startx+width/2) + (1+radius)*cos(M_PI*degrees/180),
(starty + radius) - (1+radius)*sin(M_PI*degrees/180),
1);
}
// tick marks
if (numticks) {
int i=0;
for (float degrees=deg_max; degrees>=deg_min; degrees-=(float)(deg_range/(numticks-1))) {
for (int8_t l = -TICK_LEN; l <= +TICK_LEN; l++){
ssd1306_draw_pixel(&disp,
(startx+width/2) + (radius+l)*cos(M_PI*degrees/180),
(starty + radius) - (radius+l)*sin(M_PI*degrees/180),
1);
}
// tick labels
if ( (labels) && (strlen(labels[i])) ) {
display_set_xy(
(startx+width/2) + (radius+6)*cos(M_PI*degrees/180) - 2*strlen(labels[i]),
(starty + radius) - (radius+6)*sin(M_PI*degrees/180) - 8
);
display_print_str(labels[i]);
}
i++;
}
}
// draw legend
if (strlen(legend)) {
if (height == width) { // a circle
display_set_xy(startx + width/2 - 12*strlen(legend)/2, starty+segment_h/2-8);
} else {
display_set_xy(startx + width/2 - 12*strlen(legend)/2, starty+segment_h-8);
}
display_print_str(legend, 2);
}
// draw the needle
float degrees = needle_pct * deg_range/100.0;
degrees = deg_max - degrees;
if (degrees < deg_min) degrees = deg_min;
if (degrees > deg_max) degrees = deg_max;
// can skip invisible part of needle => radius-50
// draw_line is crap at angled lines so plot pixels
int startr=0;
if (starty+radius > 64){
startr = starty+radius-64; // 64 is display height
}
for (int r=startr; r<radius; r++) {
ssd1306_draw_pixel(&disp,
(startx+width/2) + r*cos(M_PI*degrees/180),
(starty + radius) - r*sin(M_PI*degrees/180),
1);
}
}
else
{ // draw a CB style rectangular needle movement
height *= -1;
// draw straight arc
ssd1306_draw_line(&disp, startx, starty+height/2-1, startx+width, starty+height/2-1, 1);
ssd1306_draw_line(&disp, startx, starty+height/2, startx+width, starty+height/2, 1);
// tick marks
if (numticks) {
for (int i=0; i < numticks; i++) {
int x = startx + i*width/(numticks-1);
ssd1306_draw_line(&disp, x, starty+(height/2)-TICK_LEN-1, x, starty+(height/2)+TICK_LEN, 1);
// tick labels
if ( (labels) && strlen(labels[i]) ) {
display_set_xy( x - (3*strlen(labels[i])-1), starty+(height/2)-TICK_LEN-10);
display_print_str(labels[i]);
}
}
}
// draw the needle
int x = startx + width*needle_pct/100;
ssd1306_draw_line(&disp, x, starty, x, starty+height, 1);
// draw legend
if (strlen(legend)) {
display_set_xy(startx + width/2 - 6*strlen(legend)/2, starty+(height/2)+TICK_LEN+3);
display_print_str(legend, 1);
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Home page status display - S meter
////////////////////////////////////////////////////////////////////////////////
void ui::renderpage_smeter(bool view_changed, rx_status & status, rx & receiver)
{
#define NUM_DBM 3
static int dBm_ptr = 0;
static float dBm_avg[NUM_DBM] = {-FLT_MAX, -FLT_MAX, -FLT_MAX};
receiver.access(false);
const float power_dBm = status.signal_strength_dBm;
receiver.release();
dBm_avg[dBm_ptr++] = power_dBm;
if (dBm_ptr >= NUM_DBM) dBm_ptr = 0;
float avg_power_dBm = 0.0;
for (uint8_t i=0; i<NUM_DBM; i++) {
avg_power_dBm += dBm_avg[i];
}
avg_power_dBm /= NUM_DBM;
display_clear();
draw_slim_status(0, status, receiver);
// -127dBm is needle to left
// 100 percent needle swing
// 84 dB of swing range
uint16_t percent = (avg_power_dBm+127) * 100/84;
const char labels[13][5] = {
"", "1", "", "3",
"", "5", "", "7",
"", "9", "", "+12",
""
};
// angular meter movement
draw_analogmeter( 9, 33, 110, 15, percent, 13, "S", labels );
ssd1306_draw_rectangle(&disp, 0,9,127,54,1);
display_show();
}
////////////////////////////////////////////////////////////////////////////////
// Paints the spectrum from startY to bottom of screen
////////////////////////////////////////////////////////////////////////////////
void ui::draw_spectrum(uint16_t startY, uint16_t endY)
{
//plot
const uint8_t spectrum_zoom = (settings[idx_bandwidth_spectrum] & mask_spectrum) >> flag_spectrum;
const uint8_t smoothing_factor = 1;
const uint8_t max_height = (endY-startY-2);
const uint8_t scale = 256/max_height;
int16_t y=0, smoothed_y=0;
for(uint16_t x=0; x<128; x++)
{
if(spectrum_zoom == 1) y = spectrum[x*2]/scale;
else if(spectrum_zoom == 2) y = spectrum[64+x]/scale;
else if(spectrum_zoom == 3) y = spectrum[96+(x>>1)]/scale;
else if(spectrum_zoom == 4) y = spectrum[112+(x>>2)]/scale;
smoothed_y = (smoothed_y - (smoothed_y>>smoothing_factor)) + (y>>smoothing_factor);
ssd1306_draw_line(&disp, x, endY-smoothed_y, x, endY, 1);
}
for (int16_t y = 0; y < max_height; ++y)
{
if (y == ((uint16_t)4*dB10/scale))
{
for (uint8_t x = 0; x < 128; x += 4)
{
ssd1306_draw_line(&disp, x, endY - y, x + 1, endY - y, 2);
}
}
}
}
void ui::draw_waterfall(uint16_t starty)
{
static int8_t tmp_line[WATERFALL_WIDTH];
static int8_t curr_line[WATERFALL_WIDTH];
// Move waterfall down to make room for the new line
ssd1306_scroll_screen(&disp, 0, 1);
int16_t err = 0;
for(uint16_t x=0; x<WATERFALL_WIDTH; x++)
{
int16_t y = spectrum[2*x]>>2;//scale from 8 to 6 bits
curr_line[x] = y + tmp_line[x];
tmp_line[x] = 0;
}
for(uint16_t x=0; x<WATERFALL_WIDTH; x++)
{
// Simple Floyd-Steinberg dithering
if(curr_line[x] > 32)
{
ssd1306_draw_pixel(&disp, x, starty, 1);
err = curr_line[x] - 64;
} else {
ssd1306_draw_pixel(&disp, x, starty, 0);
err = curr_line[x] - 0;
}
if(x < (WATERFALL_WIDTH - 1))
{
curr_line[x + 1] += 7 * err / 16;
tmp_line[x + 1] += err / 16;
}
tmp_line[x] += 5 * err / 16;
if(x > 0)
{
tmp_line[x - 1] += 3 * err / 16;
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Generic Menu Options
////////////////////////////////////////////////////////////////////////////////
void ui::print_enum_option(const char options[], uint8_t option){
const uint8_t MAX_OPTS=32;
char *splits[MAX_OPTS];
int num_splits;
char *new_options;
new_options = (char*)malloc(strlen(options)+1);
strcpy (new_options, options);
splits[0] = strtok(new_options, "#");
for ( num_splits = 1; num_splits < MAX_OPTS; num_splits++) {
splits[num_splits] = strtok(NULL, "#");
if (!splits[num_splits]) break;
}
if ( (num_splits==2) && (strlen(splits[0])+strlen(splits[1]))*12 < 128) {
display_print_str(splits[0],2, (option==0) ? style_reverse : 0);
display_print_str(" ");
display_print_str(splits[1],2, style_right|((option==1) ? style_reverse : 0));
} else {
// default to 1st option if invalid
if (option > num_splits) option = 0;
display_print_str(splits[option], 2, style_centered);
}
}
bool ui::bit_entry(const char title[], const char options[], uint8_t bit_position, uint32_t *value, bool &ok)
{
uint32_t bit = (*value >> bit_position) & 1;
bool changed = false;
bool done = enumerate_entry(title, options, &bit, ok, changed);
if(bit)
{
*value |= (1 << bit_position);
} else {
*value &= ~(1 << bit_position);
}
return done;
}
//choose from an enumerate list of settings
bool ui::menu_entry(const char title[], const char options[], uint32_t *value, bool &ok)
{
enum e_state{idle, active};
static e_state state = idle;
static int32_t select = 0;
bool draw_display = false;
if(state == idle)
{
draw_display = true;
select = *value;
state = active;
}
else if(state == active)
{
uint32_t max = 0;
for (size_t i=0; i<strlen(options); i++) {
if (options[i] == '#') max++;
}
// workaround for accidental last # omissions
if (options[strlen(options)-1] != '#') max++;
if (max > 0) max--;
draw_display = encoder_control(&select, 0, max)!=0;
//select menu item
if(menu_button.is_pressed() || encoder_button.is_pressed()){
*value = select;
ok = true;
state = idle;
return true;
}
//cancel
if(back_button.is_pressed()){
ok = false;
state = idle;
return true;
}
}
if(draw_display)
{
display_clear();
display_print_str(title, 2, style_centered);
display_draw_separator(18,3);
display_linen(4);
print_enum_option(options, select);
display_show();
}
return false;
}
//choose from an enumerate list of settings
bool ui::enumerate_entry(const char title[], const char options[], uint32_t *value, bool &ok, bool &changed)
{
enum e_state{idle, active};
static e_state state = idle;
static uint32_t original_value = 0;
bool draw_display = false;
if(state == idle)
{
draw_display = true;
original_value = *value;
state = active;
}
else if(state == active)
{
uint32_t max = 0;
for (size_t i=0; i<strlen(options); i++) {
if (options[i] == '#') max++;
}
// workaround for accidental last # oissions
if (options[strlen(options)-1] != '#') max++;
if (max > 0) max--;
draw_display = changed = encoder_control((int32_t*)value, 0, max)!=0;
//select menu item
if(menu_button.is_pressed() || encoder_button.is_pressed()){
ok = true;
state = idle;
return true;
}
//cancel
if(back_button.is_pressed()){
*value = original_value;
changed = true;
ok = false;
state = idle;
return true;
}
}
if(draw_display)
{
display_clear();
display_print_str(title, 2, style_centered);
display_draw_separator(40,1);
display_linen(6);
print_enum_option(options, *value);
display_show();
}
return false;
}
//select a number in a range
bool ui::number_entry(const char title[], const char format[], int16_t min, int16_t max, int16_t multiple, int32_t *value, bool &ok, bool &changed)
{
enum e_state{idle, active};
static e_state state = idle;
static int32_t old_value = 0;
bool draw_display = false;
if(state == idle)
{
draw_display = true;
old_value=*value;
state = active;
}
else if(state == active)
{
draw_display = changed = encoder_control((int32_t*)value, min, max)!=0;
//select menu item
if(menu_button.is_pressed() || encoder_button.is_pressed()){
ok = true;
state = idle;
return true;
}
//cancel
if(back_button.is_pressed()){