-
Notifications
You must be signed in to change notification settings - Fork 128
/
ui.c
3230 lines (2988 loc) · 96.2 KB
/
ui.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
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
/*
* Copyright (c) 2019-2020, Dmitry (DiSlord) [email protected]
* Based on TAKAHASHI Tomohiro (TTRFTECH) [email protected]
* All rights reserved.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* The software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "ch.h"
#include "hal.h"
#include "chprintf.h"
#include "nanovna.h"
#include "si5351.h"
#define NO_EVENT 0
#define EVT_BUTTON_SINGLE_CLICK 0x01
#define EVT_BUTTON_DOUBLE_CLICK 0x02
#define EVT_BUTTON_DOWN_LONG 0x04
#define EVT_UP 0x10
#define EVT_DOWN 0x20
#define EVT_REPEAT 0x40
#define BUTTON_DOWN_LONG_TICKS MS2ST(500) // 500ms
#define BUTTON_DOUBLE_TICKS MS2ST(250) // 250ms
#define BUTTON_REPEAT_TICKS MS2ST( 30) // 30ms
#define BUTTON_DEBOUNCE_TICKS MS2ST( 20) // 20ms
/* lever switch assignment */
#define BIT_UP1 3
#define BIT_PUSH 2
#define BIT_DOWN1 1
#define READ_PORT() palReadPort(GPIOA)
#define BUTTON_MASK 0b1111
static uint16_t last_button = 0b0000;
static systime_t last_button_down_ticks;
static systime_t last_button_repeat_ticks;
uint8_t operation_requested = OP_NONE;
static uint16_t menu_button_height = MENU_BUTTON_HEIGHT(MENU_BUTTON_MIN);
enum {
UI_NORMAL, UI_MENU, UI_NUMERIC, UI_KEYPAD
};
// Keypad structures
// Enum for keypads_list
enum {
KM_START = 0, KM_STOP, KM_CENTER, KM_SPAN, KM_CW, KM_VAR,
KM_SCALE, KM_REFPOS, KM_EDELAY, KM_VELOCITY_FACTOR, KM_SCALEDELAY,
KM_XTAL, KM_THRESHOLD, KM_VBAT,
#ifdef __S21_MEASURE__
KM_MEASURE_R,
#endif
#ifdef __VNA_Z_RENORMALIZATION__
KM_Z_PORT,
#endif
#ifdef __USE_RTC__
KM_RTC_DATE,
KM_RTC_TIME,
#endif
KM_NONE
};
typedef struct {
uint8_t x:4;
uint8_t y:4;
uint8_t c;
} keypads_t;
typedef struct {
const keypads_t *keypad_type;
const char *name;
} keypads_list;
// Max keyboard input length
#define NUMINPUT_LEN 12
static uint8_t ui_mode = UI_NORMAL;
static const keypads_t *keypads;
static uint8_t keypad_mode;
static char kp_buf[NUMINPUT_LEN+2];
static int8_t kp_index = 0;
static uint8_t menu_current_level = 0;
static int8_t selection = -1;
// UI menu structure
// Type of menu item:
#define MT_NONE 0x00
#define MT_SUBMENU 0x01
#define MT_CALLBACK 0x02
#define MT_ADV_CALLBACK 0x03
// Set for custom label
#define MT_CUSTOM_LABEL 0
// Button definition (used in MT_ADV_CALLBACK for custom)
#define BUTTON_ICON_NONE -1
#define BUTTON_ICON_NOCHECK 0
#define BUTTON_ICON_CHECK 1
#define BUTTON_ICON_GROUP 2
#define BUTTON_ICON_GROUP_CHECKED 3
#define BUTTON_ICON_CHECK_AUTO 4
#define BUTTON_ICON_CHECK_MANUAL 5
#define BUTTON_BORDER_NONE 0x00
#define BUTTON_BORDER_WIDTH_MASK 0x0F
// Define mask for draw border (if 1 use light color, if 0 dark)
#define BUTTON_BORDER_TYPE_MASK 0xF0
#define BUTTON_BORDER_TOP 0x10
#define BUTTON_BORDER_BOTTOM 0x20
#define BUTTON_BORDER_LEFT 0x40
#define BUTTON_BORDER_RIGHT 0x80
#define BUTTON_BORDER_FLAT 0x00
#define BUTTON_BORDER_RISE (BUTTON_BORDER_TOP|BUTTON_BORDER_RIGHT)
#define BUTTON_BORDER_FALLING (BUTTON_BORDER_BOTTOM|BUTTON_BORDER_LEFT)
typedef struct {
uint16_t bg;
uint16_t fg;
uint8_t border;
int8_t icon;
union {
int32_t i;
uint32_t u;
float f;
const char *text;
} p1; // void data for label printf
char label[32];
} button_t;
// Call back functions for MT_CALLBACK type
typedef void (*menuaction_cb_t)(uint16_t data);
#define UI_FUNCTION_CALLBACK(ui_function_name) void ui_function_name(uint16_t data)
typedef void (*menuaction_acb_t)(uint16_t data, button_t *b);
#define UI_FUNCTION_ADV_CALLBACK(ui_function_name) void ui_function_name(uint16_t data, button_t *b)
// Set structure align as WORD (save flash memory)
#pragma pack(push, 2)
typedef struct {
uint8_t type;
uint8_t data;
char *label;
const void *reference;
} menuitem_t;
#pragma pack(pop)
// Touch screen
#define EVT_TOUCH_NONE 0
#define EVT_TOUCH_DOWN 1
#define EVT_TOUCH_PRESSED 2
#define EVT_TOUCH_RELEASED 3
#define TOUCH_INTERRUPT_ENABLED 1
static uint8_t touch_status_flag = 0;
static uint8_t last_touch_status = EVT_TOUCH_NONE;
static int16_t last_touch_x;
static int16_t last_touch_y;
#define KP_CONTINUE 0
#define KP_DONE 1
#define KP_CANCEL 2
static void ui_mode_normal(void);
static void ui_mode_menu(void);
static void draw_menu(uint32_t mask);
static void ui_mode_keypad(int _keypad_mode);
static void touch_position(int *x, int *y);
static void menu_move_back(bool leave_ui);
static void menu_push_submenu(const menuitem_t *submenu);
void drawMessageBox(char *header, char *text, uint32_t delay);
#ifdef UI_USE_NUMERIC_INPUT
static void ui_mode_numeric(int _keypad_mode);
#endif
static uint16_t btn_check(void)
{
systime_t ticks;
// Debounce input
while(TRUE){
ticks = chVTGetSystemTimeX();
if(ticks - last_button_down_ticks > BUTTON_DEBOUNCE_TICKS)
break;
chThdSleepMilliseconds(2);
}
uint16_t status = 0;
uint16_t cur_button = READ_PORT() & BUTTON_MASK;
// Detect only changed and pressed buttons
uint16_t button_set = (last_button ^ cur_button) & cur_button;
last_button_down_ticks = ticks;
last_button = cur_button;
if (button_set & (1<<BIT_PUSH))
status |= EVT_BUTTON_SINGLE_CLICK;
if (button_set & (1<<BIT_UP1))
status |= EVT_UP;
if (button_set & (1<<BIT_DOWN1))
status |= EVT_DOWN;
return status;
}
static uint16_t btn_wait_release(void)
{
while (TRUE) {
systime_t ticks = chVTGetSystemTimeX();
systime_t dt = ticks - last_button_down_ticks;
// Debounce input
// if (dt < BUTTON_DEBOUNCE_TICKS){
// chThdSleepMilliseconds(10);
// continue;
// }
chThdSleepMilliseconds(10);
uint16_t cur_button = READ_PORT() & BUTTON_MASK;
uint16_t changed = last_button ^ cur_button;
if (dt >= BUTTON_DOWN_LONG_TICKS && (cur_button & (1<<BIT_PUSH)))
return EVT_BUTTON_DOWN_LONG;
if (changed & (1<<BIT_PUSH)) // release
return EVT_BUTTON_SINGLE_CLICK;
if (changed) {
// finished
last_button = cur_button;
last_button_down_ticks = ticks;
return 0;
}
if (dt > BUTTON_DOWN_LONG_TICKS &&
ticks > last_button_repeat_ticks) {
uint16_t status = 0;
if (cur_button & (1<<BIT_DOWN1))
status |= EVT_DOWN | EVT_REPEAT;
if (cur_button & (1<<BIT_UP1))
status |= EVT_UP | EVT_REPEAT;
last_button_repeat_ticks = ticks + BUTTON_REPEAT_TICKS;
return status;
}
}
}
#if 0
static void btn_wait(void){
while (READ_PORT() & BUTTON_MASK) chThdSleepMilliseconds(10);
}
#endif
#if 0
static void bubbleSort(uint16_t *v, int n) {
bool swapped = true;
int i = 0, j;
while (i < n - 1 && swapped) { // keep going while we swap in the unordered part
swapped = false;
for (j = n - 1; j > i; j--) { // unordered part
if (v[j] < v[j - 1]) {
SWAP(uint16_t, v[j], v[j - 1]);
swapped = true;
}
}
i++;
}
}
#endif
#define SOFTWARE_TOUCH
//*******************************************************************************
// Software Touch module
//*******************************************************************************
#ifdef SOFTWARE_TOUCH
// ADC read count for measure X and Y (2^N count)
#define TOUCH_X_N 3
#define TOUCH_Y_N 3
static int
touch_measure_y(void)
{
// drive low to high on X line (At this state after touch_prepare_sense)
// palSetPadMode(GPIOB, GPIOB_XN, PAL_MODE_OUTPUT_PUSHPULL); //
// palSetPadMode(GPIOA, GPIOA_XP, PAL_MODE_OUTPUT_PUSHPULL); //
// drive low to high on X line (coordinates from top to bottom)
palClearPad(GPIOB, GPIOB_XN);
// palSetPad(GPIOA, GPIOA_XP);
// open Y line (At this state after touch_prepare_sense)
// palSetPadMode(GPIOB, GPIOB_YN, PAL_MODE_INPUT); // Hi-z mode
palSetPadMode(GPIOA, GPIOA_YP, PAL_MODE_INPUT_ANALOG); // <- ADC_TOUCH_Y channel
// chThdSleepMilliseconds(20);
uint32_t v = 0, cnt = 1<<TOUCH_Y_N;
do{v+=adc_single_read(ADC_TOUCH_Y);}while(--cnt);
return v>>TOUCH_Y_N;
}
static int
touch_measure_x(void)
{
// drive high to low on Y line (coordinates from left to right)
palSetPad(GPIOB, GPIOB_YN);
palClearPad(GPIOA, GPIOA_YP);
// Set Y line as output
palSetPadMode(GPIOB, GPIOB_YN, PAL_MODE_OUTPUT_PUSHPULL);
palSetPadMode(GPIOA, GPIOA_YP, PAL_MODE_OUTPUT_PUSHPULL);
// Set X line as input
palSetPadMode(GPIOB, GPIOB_XN, PAL_MODE_INPUT); // Hi-z mode
palSetPadMode(GPIOA, GPIOA_XP, PAL_MODE_INPUT_ANALOG); // <- ADC_TOUCH_X channel
uint32_t v = 0, cnt = 1<<TOUCH_X_N;
do{v+=adc_single_read(ADC_TOUCH_X);}while(--cnt);
return v>>TOUCH_X_N;
}
// Manually measure touch event
static inline int
touch_status(void)
{
return adc_single_read(ADC_TOUCH_Y) > TOUCH_THRESHOLD;
}
static void
touch_prepare_sense(void)
{
// Set Y line as input
palSetPadMode(GPIOB, GPIOB_YN, PAL_MODE_INPUT); // Hi-z mode
palSetPadMode(GPIOA, GPIOA_YP, PAL_MODE_INPUT_PULLDOWN); // Use pull
// drive high on X line (for touch sense on Y)
palSetPad(GPIOB, GPIOB_XN);
palSetPad(GPIOA, GPIOA_XP);
// force high X line
palSetPadMode(GPIOB, GPIOB_XN, PAL_MODE_OUTPUT_PUSHPULL);
palSetPadMode(GPIOA, GPIOA_XP, PAL_MODE_OUTPUT_PUSHPULL);
// chThdSleepMilliseconds(10); // Wait 10ms for denounce touch
}
#ifdef __REMOTE_DESKTOP__
static uint8_t touch_remote = REMOTE_NONE;
void remote_touch_set(uint16_t state, int16_t x, int16_t y) {
touch_remote = state;
if (x!=-1) last_touch_x = x;
if (y!=-1) last_touch_y = y;
handle_touch_interrupt();
}
#endif
static void
touch_start_watchdog(void)
{
if (touch_status_flag&TOUCH_INTERRUPT_ENABLED) return;
touch_status_flag^=TOUCH_INTERRUPT_ENABLED;
adc_start_analog_watchdog();
#ifdef __REMOTE_DESKTOP__
touch_remote = REMOTE_NONE;
#endif
}
static void
touch_stop_watchdog(void)
{
if (!(touch_status_flag&TOUCH_INTERRUPT_ENABLED)) return;
touch_status_flag^=TOUCH_INTERRUPT_ENABLED;
adc_stop_analog_watchdog();
}
// Touch panel timer check (check press frequency 20Hz)
static const GPTConfig gpt3cfg = {
20, // 200Hz timer clock. 200/10 = 20Hz touch check
NULL, // Timer callback.
0x0020, // CR2:MMS=02 to output TRGO
0
};
//
// Touch init function init timer 3 trigger adc for check touch interrupt, and run measure
//
static void touch_init(void){
// Prepare pin for measure touch event
touch_prepare_sense();
// Start touch interrupt, used timer_3 ADC check threshold:
gptStart(&GPTD3, &gpt3cfg); // Init timer 3
gptStartContinuous(&GPTD3, 10); // Start timer 3 vs timer 10 interval
touch_start_watchdog(); // Start ADC watchdog (measure by timer 3 interval and trigger interrupt if touch pressed)
}
// Main software touch function, should:
// set last_touch_x and last_touch_x
// return touch status
static int
touch_check(void)
{
touch_stop_watchdog();
int stat = touch_status();
if (stat) {
int y = touch_measure_y();
int x = touch_measure_x();
touch_prepare_sense();
if (touch_status())
{
last_touch_x = x;
last_touch_y = y;
}
#ifdef __REMOTE_DESKTOP__
touch_remote = REMOTE_NONE;
} else {
stat = touch_remote == REMOTE_PRESS;
#endif
}
if (stat != last_touch_status) {
last_touch_status = stat;
return stat ? EVT_TOUCH_PRESSED : EVT_TOUCH_RELEASED;
}
return stat ? EVT_TOUCH_DOWN : EVT_TOUCH_NONE;
}
//*******************************************************************************
// End Software Touch module
//*******************************************************************************
#endif // end SOFTWARE_TOUCH
static inline void
touch_wait_release(void)
{
while (touch_check() != EVT_TOUCH_RELEASED)
;
}
static inline void
touch_wait_pressed(void)
{
while (touch_check() != EVT_TOUCH_PRESSED)
;
}
#define CALIBRATION_OFFSET 16
#define TOUCH_MARK_W 9
#define TOUCH_MARK_H 9
#define TOUCH_MARK_X 4
#define TOUCH_MARK_Y 4
static const uint8_t touch_bitmap[]={
_BMP16(0b0000100000000000),
_BMP16(0b0100100100000000),
_BMP16(0b0010101000000000),
_BMP16(0b0000100000000000),
_BMP16(0b1111011110000000),
_BMP16(0b0000100000000000),
_BMP16(0b0010101000000000),
_BMP16(0b0100100100000000),
_BMP16(0b0000100000000000),
};
static void getTouchPoint(uint16_t x, uint16_t y, const char *name, int16_t *data) {
// Clear screen and ask for press
lcd_set_foreground(LCD_FG_COLOR);
lcd_set_background(LCD_BG_COLOR);
lcd_clear_screen();
lcd_blitBitmap(x, y, TOUCH_MARK_W, TOUCH_MARK_H, touch_bitmap);
lcd_printf((LCD_WIDTH-18*FONT_WIDTH)/2, (LCD_HEIGHT-FONT_GET_HEIGHT)/2, "TOUCH %s *", name);
// Wait release, and fill data
touch_wait_release();
data[0] = last_touch_x;
data[1] = last_touch_y;
}
void
touch_cal_exec(void)
{
const uint16_t x1 = CALIBRATION_OFFSET - TOUCH_MARK_X;
const uint16_t y1 = CALIBRATION_OFFSET - TOUCH_MARK_Y;
const uint16_t x2 = LCD_WIDTH - 1 - CALIBRATION_OFFSET - TOUCH_MARK_X;
const uint16_t y2 = LCD_HEIGHT - 1 - CALIBRATION_OFFSET - TOUCH_MARK_Y;
getTouchPoint(x1, y1, "UPPER LEFT", &config._touch_cal[0]);
getTouchPoint(x2, y2, "LOWER RIGHT", &config._touch_cal[2]);
}
void
touch_draw_test(void)
{
int x0, y0;
int x1, y1;
lcd_set_foreground(LCD_FG_COLOR);
lcd_set_background(LCD_BG_COLOR);
lcd_clear_screen();
lcd_drawstring(OFFSETX, LCD_HEIGHT - FONT_GET_HEIGHT, "TOUCH TEST: DRAG PANEL, PRESS BUTTON TO FINISH");
while (1) {
if (btn_check() & EVT_BUTTON_SINGLE_CLICK) break;
if (touch_check() == EVT_TOUCH_PRESSED){
touch_position(&x0, &y0);
do {
chThdSleepMilliseconds(50);
touch_position(&x1, &y1);
lcd_line(x0, y0, x1, y1);
x0 = x1;
y0 = y1;
} while (touch_check() != EVT_TOUCH_RELEASED);
}
}
}
static void
touch_position(int *x, int *y)
{
#ifdef __REMOTE_DESKTOP__
if (touch_remote != REMOTE_NONE) {
*x = last_touch_x;
*y = last_touch_y;
return;
}
#endif
int tx = ((LCD_WIDTH-1-CALIBRATION_OFFSET)*(last_touch_x - config._touch_cal[0]) + CALIBRATION_OFFSET * (config._touch_cal[2] - last_touch_x)) / (config._touch_cal[2] - config._touch_cal[0]);
if (tx<0) tx = 0; else if (tx>=LCD_WIDTH ) tx = LCD_WIDTH -1;
int ty = ((LCD_HEIGHT-1-CALIBRATION_OFFSET)*(last_touch_y - config._touch_cal[1]) + CALIBRATION_OFFSET * (config._touch_cal[3] - last_touch_y)) / (config._touch_cal[3] - config._touch_cal[1]);
if (ty<0) ty = 0; else if (ty>=LCD_HEIGHT) ty = LCD_HEIGHT-1;
*x = tx;
*y = ty;
}
static void
show_version(void)
{
int x = 5, y = 5, i = 1;
int str_height = FONT_STR_HEIGHT + 2;
lcd_set_foreground(LCD_TRACE_2_COLOR);
lcd_set_background(LCD_BG_COLOR);
lcd_clear_screen();
uint16_t shift = 0b00010010000;
lcd_drawstring_size(BOARD_NAME, x , y, 3);
lcd_set_foreground(LCD_FG_COLOR);
y+=FONT_GET_HEIGHT*3+3-5;
while (info_about[i]) {
do {shift>>=1; y+=5;} while (shift&1);
lcd_drawstring(x, y+=str_height-5, info_about[i++]);
}
lcd_printf(x, y+= str_height, "TCXO = %q" S_Hz, config._xtal_freq);
y+=str_height*2;
// Update battery and time
uint16_t cnt = 0;
while (true) {
if (touch_check() == EVT_TOUCH_PRESSED)
break;
if (btn_check() & EVT_BUTTON_SINGLE_CLICK)
break;
chThdSleepMilliseconds(40);
if ((cnt++)&0x07) continue; // Not update time so fast
#ifdef __USE_RTC__
uint32_t tr = rtc_get_tr_bin(); // TR read first
uint32_t dr = rtc_get_dr_bin(); // DR read second
lcd_printf(x, y, "Time: 20%02d/%02d/%02d %02d:%02d:%02d" " (LS%c)",
RTC_DR_YEAR(dr),
RTC_DR_MONTH(dr),
RTC_DR_DAY(dr),
RTC_TR_HOUR(dr),
RTC_TR_MIN(dr),
RTC_TR_SEC(dr),
(RCC->BDCR & STM32_RTCSEL_MASK) == STM32_RTCSEL_LSE ? 'E' : 'I');
#endif
#if 1
uint32_t vbat=adc_vbat_read();
lcd_printf(x, y + str_height, "Batt: %d.%03d" S_VOLT, vbat/1000, vbat%1000);
#endif
}
}
#ifdef __DFU_SOFTWARE_MODE__
void
enter_dfu(void)
{
touch_stop_watchdog();
int x = 5, y = 20;
lcd_set_foreground(LCD_FG_COLOR);
lcd_set_background(LCD_BG_COLOR);
// leave a last message
lcd_clear_screen();
lcd_drawstring(x, y, "DFU: Device Firmware Update Mode\n"
"To exit DFU mode, please reset device yourself.");
// see __early_init in ./NANOVNA_STM32_F072/board.c
*((unsigned long *)BOOT_FROM_SYTEM_MEMORY_MAGIC_ADDRESS) = BOOT_FROM_SYTEM_MEMORY_MAGIC;
NVIC_SystemReset();
}
#endif
static bool
select_lever_mode(int mode)
{
if (lever_mode == mode) return false;
lever_mode = mode;
request_to_redraw(REDRAW_BACKUP | REDRAW_FREQUENCY | REDRAW_MARKER);
return true;
}
static UI_FUNCTION_ADV_CALLBACK(menu_calop_acb)
{
static const struct {uint8_t mask, next;} c_list[5]={
[CAL_LOAD] = {CALSTAT_LOAD, 3},
[CAL_OPEN] = {CALSTAT_OPEN, 1},
[CAL_SHORT]= {CALSTAT_SHORT, 2},
[CAL_THRU] = {CALSTAT_THRU, 5},//Calibration screen does not show electrical delay
[CAL_ISOLN]= {CALSTAT_ISOLN, 4},
};
if (b){
if (cal_status & c_list[data].mask) b->icon = BUTTON_ICON_CHECK;
return;
}
// TODO: Hack! reset button state
last_button = 0;
cal_collect(data);
selection = c_list[data].next;
}
extern const menuitem_t menu_save[];
static UI_FUNCTION_CALLBACK(menu_caldone_cb)
{
cal_done();
menu_move_back(false);
if (data == 0)
menu_push_submenu(menu_save);
}
static UI_FUNCTION_CALLBACK(menu_cal_reset_cb)
{
(void)data;
// RESET
cal_status = 0;
lastsaveid = NO_SAVE_SLOT;
set_power(SI5351_CLK_DRIVE_STRENGTH_AUTO);
}
static UI_FUNCTION_ADV_CALLBACK(menu_cal_range_acb){
(void)data;
bool calibrated = cal_status & (CALSTAT_ES|CALSTAT_ER|CALSTAT_ET|CALSTAT_ED|CALSTAT_EX|CALSTAT_OPEN|CALSTAT_SHORT|CALSTAT_THRU);
if (b){
if (calibrated){
b->bg = (cal_status&CALSTAT_INTERPOLATED) ? LCD_INTERP_CAL_COLOR : LCD_MENU_COLOR;
plot_printf(b->label, sizeof(b->label), "CAL: %dp\n %.6F" S_Hz "\n %.6F" S_Hz, cal_sweep_points, (float)cal_frequency0, (float)cal_frequency1);
}
else
plot_printf(b->label, sizeof(b->label), "RESET\nCAL RANGE");
return;
}
// Reset range to calibration
if (calibrated && (cal_status&CALSTAT_INTERPOLATED)){
reset_sweep_frequency();
set_power(cal_power);
}
}
static UI_FUNCTION_ADV_CALLBACK(menu_cal_apply_acb)
{
(void)data;
if (b){
b->icon = (cal_status&CALSTAT_APPLY) ? BUTTON_ICON_CHECK : BUTTON_ICON_NOCHECK;
return;
}
// toggle applying correction
cal_status ^= CALSTAT_APPLY;
request_to_redraw(REDRAW_CAL_STATUS);
}
static UI_FUNCTION_ADV_CALLBACK(menu_recall_acb)
{
if (b){
const properties_t *p = get_properties(data);
if (p)
plot_printf(b->label, sizeof(b->label), "%.6F" S_Hz "\n%.6F" S_Hz, (float)p->_frequency0, (float)p->_frequency1, data);
else
plot_printf(b->label, sizeof(b->label), "Empty %d", data);
if (lastsaveid == data) b->icon = BUTTON_ICON_CHECK;
return;
}
load_properties(data);
}
#define MENU_CONFIG_TOUCH_CAL 0
#define MENU_CONFIG_TOUCH_TEST 1
#define MENU_CONFIG_VERSION 2
#define MENU_CONFIG_RESET 3
#define MENU_CONFIG_LOAD 4
static UI_FUNCTION_CALLBACK(menu_config_cb)
{
switch (data) {
case MENU_CONFIG_TOUCH_CAL:
touch_cal_exec();
break;
case MENU_CONFIG_TOUCH_TEST:
touch_draw_test();
break;
case MENU_CONFIG_VERSION:
show_version();
break;
case MENU_CONFIG_RESET:
clear_all_config_prop_data();
NVIC_SystemReset();
break;
#ifdef __SD_CARD_LOAD__
case MENU_CONFIG_LOAD:
if (!sd_card_load_config())
drawMessageBox("Error", "No config.ini", 2000);
break;
#endif
}
ui_mode_normal();
request_to_redraw(REDRAW_CLRSCR | REDRAW_AREA | REDRAW_BATTERY | REDRAW_CAL_STATUS | REDRAW_FREQUENCY);
}
static UI_FUNCTION_CALLBACK(menu_config_save_cb)
{
(void)data;
config_save();
menu_move_back(true);
}
#ifdef __DFU_SOFTWARE_MODE__
static UI_FUNCTION_CALLBACK(menu_dfu_cb)
{
(void)data;
enter_dfu();
}
#endif
static UI_FUNCTION_ADV_CALLBACK(menu_save_acb)
{
if (b){
b->p1.u = data;
return;
}
if (caldata_save(data) == 0) {
menu_move_back(true);
request_to_redraw(REDRAW_CAL_STATUS);
}
}
static UI_FUNCTION_ADV_CALLBACK(menu_trace_acb)
{
if (b){
if (trace[data].enabled){
b->bg = LCD_TRACE_1_COLOR + data;
if (data == selection) b->bg = LCD_MENU_ACTIVE_COLOR;
if (current_trace == data)
b->icon = BUTTON_ICON_CHECK;
}
b->p1.u = data;
return;
}
if (trace[data].enabled) {
if (data == current_trace) {
trace[data].enabled = FALSE; // disable if active trace is selected
current_trace = TRACE_INVALID; // invalidate current
for (int i = 0; i < TRACES_MAX; i++) // set first enabled as current trace
if (trace[i].enabled) {current_trace = i; break;}
} else {
// make active selected trace
current_trace = data;
}
} else {
trace[data].enabled = TRUE;
current_trace = data;
}
request_to_redraw(REDRAW_AREA);
}
extern const menuitem_t menu_marker_smith[];
static UI_FUNCTION_ADV_CALLBACK(menu_marker_smith_acb)
{
if (b){
b->icon = marker_smith_format == data ? BUTTON_ICON_GROUP_CHECKED : BUTTON_ICON_GROUP;
b->p1.text = get_smith_format_names(data);
return;
}
marker_smith_format = data;
request_to_redraw(REDRAW_AREA | REDRAW_MARKER);
}
static UI_FUNCTION_ADV_CALLBACK(menu_format_acb)
{
if (current_trace == TRACE_INVALID) return;
if (b){
if (trace[current_trace].type == data)
b->icon = BUTTON_ICON_CHECK;
const char *name = get_trace_typename(data);
if (data == TRC_SMITH)
plot_printf(b->label, sizeof(b->label), "%s\n" R_LINK_COLOR " %s", name, get_smith_format_names(marker_smith_format));
else
b->p1.text = name;
return;
}
if (trace[current_trace].type == data && data == TRC_SMITH)
menu_push_submenu(menu_marker_smith);
else
set_trace_type(current_trace, data);
// ui_mode_normal();
}
#if 0
static UI_FUNCTION_ADV_CALLBACK(menu_channel_acb)
{
if (current_trace == TRACE_INVALID) return;
if (b){
if (trace[current_trace].channel == data)
b->icon = BUTTON_ICON_CHECK;
return;
}
set_trace_channel(current_trace, data);
menu_move_back(true);
}
#endif
static UI_FUNCTION_ADV_CALLBACK(menu_channel_acb)
{
(void)data;
if (current_trace == TRACE_INVALID) {if (b) b->p1.text = ""; return;}
int ch = trace[current_trace].channel;
if (b){
b->p1.text = ch == 0 ? "S11 (REFL)" : "S21 (THRU)";
return;
}
set_trace_channel(current_trace, ch^1);
}
static UI_FUNCTION_ADV_CALLBACK(menu_transform_window_acb)
{
char *text = "";
switch(props_mode & TD_WINDOW){
case TD_WINDOW_MINIMUM: text = "MINIMUM"; data = TD_WINDOW_NORMAL; break;
case TD_WINDOW_NORMAL: text = "NORMAL"; data = TD_WINDOW_MAXIMUM; break;
case TD_WINDOW_MAXIMUM: text = "MAXIMUM"; data = TD_WINDOW_MINIMUM; break;
}
if(b){
b->p1.text = text;
return;
}
props_mode = (props_mode & ~TD_WINDOW) | data;
}
static UI_FUNCTION_ADV_CALLBACK(menu_transform_acb)
{
(void)data;
if(b){
if (props_mode & DOMAIN_TIME) b->icon = BUTTON_ICON_CHECK;
b->p1.text = (props_mode&DOMAIN_TIME) ? "ON" : "OFF";
return;
}
props_mode ^= DOMAIN_TIME;
select_lever_mode(LM_MARKER);
}
static UI_FUNCTION_ADV_CALLBACK(menu_transform_filter_acb)
{
if(b){
b->icon = (props_mode & TD_FUNC) == data ? BUTTON_ICON_GROUP_CHECKED : BUTTON_ICON_GROUP;
return;
}
props_mode = (props_mode & ~TD_FUNC) | data;
// ui_mode_normal();
}
const menuitem_t menu_bandwidth[];
static UI_FUNCTION_ADV_CALLBACK(menu_bandwidth_sel_acb)
{
(void)data;
if (b){
b->p1.u = get_bandwidth_frequency(config._bandwidth);
return;
}
menu_push_submenu(menu_bandwidth);
}
static UI_FUNCTION_ADV_CALLBACK(menu_bandwidth_acb)
{
if (b){
b->icon = config._bandwidth == data ? BUTTON_ICON_GROUP_CHECKED : BUTTON_ICON_GROUP;
b->p1.u = get_bandwidth_frequency(data);
return;
}
set_bandwidth(data);
}
#ifdef __USE_SMOOTH__
static UI_FUNCTION_ADV_CALLBACK(menu_smooth_func_acb)
{
(void)data;
if (b){
b->p1.text = (VNA_mode & VNA_SMOOTH_FUNCTION) ? "Arith" : "Geom";
return;
}
VNA_mode^= VNA_SMOOTH_FUNCTION;
}
static UI_FUNCTION_ADV_CALLBACK(menu_smooth_acb)
{
if (b){
b->icon = get_smooth_factor() == data ? BUTTON_ICON_GROUP_CHECKED : BUTTON_ICON_GROUP;
b->p1.u = data;
return;
}
set_smooth_factor(data);
}
#endif
#ifdef __USE_BACKUP__
static UI_FUNCTION_ADV_CALLBACK(menu_backup_acb)
{
(void)data;
if (b){
b->icon = (VNA_mode & VNA_MODE_BACKUP) ? BUTTON_ICON_CHECK : BUTTON_ICON_NOCHECK;
return;
}
VNA_mode^= VNA_MODE_BACKUP;
request_to_redraw(REDRAW_BACKUP);
}
#endif
const menuitem_t menu_sweep_points[];
static UI_FUNCTION_ADV_CALLBACK(menu_points_sel_acb)
{
(void)data;
if (b){
b->p1.u = sweep_points;
return;
}
menu_push_submenu(menu_sweep_points);
}
static const uint16_t point_counts_set[POINTS_SET_COUNT] = POINTS_SET;
static UI_FUNCTION_ADV_CALLBACK(menu_points_acb)
{
uint16_t p_count = point_counts_set[data];
if (b){
b->icon = sweep_points == p_count ? BUTTON_ICON_GROUP_CHECKED : BUTTON_ICON_GROUP;
b->p1.u = p_count;
return;
}
set_sweep_points(p_count);
}
const menuitem_t menu_power[];
static UI_FUNCTION_ADV_CALLBACK(menu_power_sel_acb)
{
(void)data;
if (b){
if (current_props._power == SI5351_CLK_DRIVE_STRENGTH_AUTO)
plot_printf(b->label, sizeof(b->label), "POWER AUTO");
else
plot_printf(b->label, sizeof(b->label), "POWER" R_LINK_COLOR " %um" S_AMPER, 2+current_props._power*2);
return;
}
menu_push_submenu(menu_power);
}
static UI_FUNCTION_ADV_CALLBACK(menu_power_acb)
{
if (b){
b->icon = current_props._power == data ? BUTTON_ICON_GROUP_CHECKED : BUTTON_ICON_GROUP;
b->p1.u = 2+data*2;
return;
}
set_power(data);
}
static UI_FUNCTION_ADV_CALLBACK(menu_keyboard_acb)
{
if (data == KM_SCALE && current_trace != TRACE_INVALID) {
if ((1<<trace[current_trace].type) & ((1<<TRC_DELAY)|(1<<TRC_sC)|(1<<TRC_sL)|(1<<TRC_pC)|(1<<TRC_pL)))
data = KM_SCALEDELAY;
}
if (b){
switch(data){
// case KM_SCALE: b->p1.f = current_trace != TRACE_INVALID ? get_trace_scale(current_trace) : 0; break;
case KM_VELOCITY_FACTOR: b->p1.u = velocity_factor; break;
case KM_VAR: plot_printf(b->label, sizeof(b->label), var_freq ? "JOG STEP\n" R_LINK_COLOR " %.3q" S_Hz : "JOG STEP\n AUTO", var_freq); break;
case KM_XTAL: b->p1.u = config._xtal_freq; break;
case KM_THRESHOLD: b->p1.u = config._harmonic_freq_threshold; break;
case KM_VBAT: b->p1.u = config._vbat_offset; break;
case KM_EDELAY: b->p1.f = electrical_delay * 1E-12; break;
#ifdef __S21_MEASURE__
case KM_MEASURE_R: b->p1.f = config._measure_r; break;
#endif
#ifdef __VNA_Z_RENORMALIZATION__
case KM_Z_PORT: b->p1.f = current_props._portz; break;
#endif
}
return;
}