-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
ui.c
3756 lines (3459 loc) · 131 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-2023, 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 "nanovna.h"
#include "hal.h"
#include "chprintf.h"
#include <string.h>
#include "si5351.h"
// Use size optimization (UI not need fast speed, better have smallest size)
#pragma GCC optimize ("Os")
// Hardware UI buttons/touch data / functions
#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 BUTTON_DOWN (1<<GPIOA_LEVER1)
#define BUTTON_PUSH (1<<GPIOA_PUSH)
#define BUTTON_UP (1<<GPIOA_LEVER2)
#define READ_BUTTONS() (palReadPort(GPIOA) & (BUTTON_DOWN | BUTTON_PUSH | BUTTON_UP))
static uint16_t last_button = 0b0000;
static systime_t last_button_down_ticks;
static systime_t last_button_repeat_ticks;
// 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;
uint8_t operation_requested = OP_NONE;
//==============================================
static uint16_t menu_button_height = MENU_BUTTON_HEIGHT(MENU_BUTTON_MIN);
enum {
UI_NORMAL, UI_MENU, UI_KEYPAD,
#ifdef __SD_FILE_BROWSER__
UI_BROWSER,
#endif
};
typedef struct {
uint8_t bg;
uint8_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;
#ifdef __USE_SD_CARD__
// Save/Load format enum
enum {
FMT_S1P_FILE=0, FMT_S2P_FILE, FMT_BMP_FILE,
#ifdef __SD_CARD_DUMP_TIFF__
FMT_TIF_FILE,
#endif
FMT_CAL_FILE,
#ifdef __SD_CARD_DUMP_FIRMWARE__
FMT_BIN_FILE,
#endif
#ifdef __SD_CARD_LOAD__
FMT_CMD_FILE,
#endif
};
#endif
// Keypad structures
// Enum for keypads_list
enum {
KM_START = 0, KM_STOP, KM_CENTER, KM_SPAN, KM_CW, KM_STEP, KM_VAR, // frequency input
KM_POINTS,
KM_TOP, KM_nTOP,
KM_BOTTOM, KM_nBOTTOM,
KM_SCALE, KM_nSCALE,
KM_REFPOS, KM_EDELAY, KM_VAR_DELAY, KM_S21OFFSET, KM_VELOCITY_FACTOR,
#ifdef __S11_CABLE_MEASURE__
KM_ACTUAL_CABLE_LEN,
#endif
KM_XTAL, KM_THRESHOLD, KM_VBAT,
#ifdef __S21_MEASURE__
KM_MEASURE_R,
#endif
#ifdef __VNA_Z_RENORMALIZATION__
KM_Z_PORT,
KM_CAL_LOAD_R,
#endif
#ifdef __USE_RTC__
KM_RTC_DATE,
KM_RTC_TIME,
KM_RTC_CAL,
#endif
#ifdef __USE_SD_CARD__
KM_S1P_NAME, KM_S2P_NAME, KM_BMP_NAME, // Must be equal to Save/Load format enum (TODO fix this)
#ifdef __SD_CARD_DUMP_TIFF__
KM_TIF_NAME,
#endif
KM_CAL_NAME,
#ifdef __SD_CARD_DUMP_FIRMWARE__
KM_BIN_NAME,
#endif
#endif
KM_NONE
};
typedef struct {
uint16_t x_offs;
uint16_t y_offs;
uint16_t width;
uint16_t height;
} keypad_pos_t;
typedef struct {
uint8_t size, type;
struct {
uint8_t pos;
uint8_t c;
} buttons[];
} keypads_t;
typedef void (*keyboard_cb_t)(uint16_t data, button_t *b);
#define UI_KEYBOARD_CALLBACK(ui_kb_function_name) void ui_kb_function_name(uint16_t data, button_t *b)
typedef struct {
uint8_t keypad_type;
uint8_t data;
const char *name;
const keyboard_cb_t cb;
} __attribute__((packed)) keypads_list;
// Max keyboard input length
#define NUMINPUT_LEN 12
#if defined(FF_USE_LFN)
#define TXTINPUT_LEN (FF_MAX_LFN - 4)
#else
#define TXTINPUT_LEN (8)
#endif
#if NUMINPUT_LEN + 2 > TXTINPUT_LEN + 1
static char kp_buf[NUMINPUT_LEN+2]; // !!!!!! WARNING size must be + 2 from NUMINPUT_LEN or TXTINPUT_LEN + 1
#else
static char kp_buf[TXTINPUT_LEN+1]; // !!!!!! WARNING size must be + 2 from NUMINPUT_LEN or TXTINPUT_LEN + 1
#endif
static uint8_t ui_mode = UI_NORMAL;
static const keypads_t *keypads;
static uint8_t keypad_mode;
static uint8_t keyboard_temp; // Use for custom keyboard processing
static uint8_t menu_current_level = 0;
static int8_t selection = -1;
// UI menu structure
// Type of menu item:
enum {
MT_NEXT = 0, // reference is next menu or 0 if end
MT_SUBMENU, // reference is submenu button
MT_CALLBACK, // reference is pointer to: void ui_function_name(uint16_t data)
MT_ADV_CALLBACK // reference is pointer to: void ui_function_name(uint16_t data, button_t *b)
};
// 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_WIDTH_MASK 0x07
// Define mask for draw border (if 1 use light color, if 0 dark)
#define BUTTON_BORDER_NO_FILL 0x08
#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)
// 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)
typedef struct {
uint8_t type;
uint8_t data;
char *label;
const void *reference;
} __attribute__((packed)) menuitem_t;
static void ui_mode_normal(void);
static void ui_mode_menu(void);
static void menu_draw(uint32_t mask);
static void ui_mode_keypad(int mode);
static void menu_move_back(bool leave_ui);
static void menu_push_submenu(const menuitem_t *submenu);
static void menu_set_submenu(const menuitem_t *submenu);
static void ui_keyboard_cb(uint16_t data, button_t *b);
static void touch_position(int *x, int *y);
// Icons for UI
#include "icons_menu.c"
static uint16_t get_buttons(void) {
uint16_t cur_button = READ_BUTTONS();
#ifdef __FLIP_DISPLAY__
// swap bits in byte (swap leveler left and right bits for rotated display)
if (VNA_MODE(VNA_MODE_FLIP_DISPLAY) && (((cur_button>>GPIOA_LEVER1)^(cur_button>>GPIOA_LEVER2))&1)) cur_button^= (1<<GPIOA_LEVER1)|(1<<GPIOA_LEVER2);
#endif
return cur_button;
}
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 = get_buttons();
// 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 & BUTTON_PUSH)
status |= EVT_BUTTON_SINGLE_CLICK;
if (button_set & BUTTON_UP)
status |= EVT_UP;
if (button_set & BUTTON_DOWN)
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 = get_buttons();
uint16_t changed = last_button ^ cur_button;
if (dt >= BUTTON_DOWN_LONG_TICKS && (cur_button & BUTTON_PUSH))
return EVT_BUTTON_DOWN_LONG;
if (changed & BUTTON_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 & BUTTON_DOWN)
status |= EVT_DOWN | EVT_REPEAT;
if (cur_button & BUTTON_UP)
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()) 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
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(3);
return adc_single_read(ADC_TOUCH_Y);
}
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
// chThdSleepMilliseconds(3);
return adc_single_read(ADC_TOUCH_X);
}
// 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)
#if HAL_USE_GPT == TRUE
static const GPTConfig gpt3cfg = {
1000, // 1kHz timer clock.
NULL, // Timer callback.
0x0020, // CR2:MMS=02 to output TRGO
0
};
static void init_Timers(void) {
gptStart(&GPTD3, &gpt3cfg); // Init timer 3
gptStartContinuous(&GPTD3, 10); // Start timer 10ms period (use 1kHz clock)
}
#else
static void init_Timers(void) {
initTimers();
startTimer(TIM3, 10); // Start timer 10ms period (use 1kHz clock)
}
#endif
//
// 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:
init_Timers();
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
//*******************************************************************************
// UI functions
//*******************************************************************************
static inline void touch_wait_release(void) {
while (touch_check() != EVT_TOUCH_RELEASED)
;
}
// Draw button function
static void ui_draw_button(uint16_t x, uint16_t y, uint16_t w, uint16_t h, button_t *b) {
uint16_t type = b->border;
uint16_t bw = type & BUTTON_BORDER_WIDTH_MASK;
// Draw border if width > 0
if (bw) {
uint16_t br = LCD_RISE_EDGE_COLOR;
uint16_t bd = LCD_FALLEN_EDGE_COLOR;
lcd_set_background(type&BUTTON_BORDER_TOP ? br : bd);lcd_fill(x, y, w, bw); // top
lcd_set_background(type&BUTTON_BORDER_LEFT ? br : bd);lcd_fill(x, y, bw, h); // left
lcd_set_background(type&BUTTON_BORDER_RIGHT ? br : bd);lcd_fill(x + w - bw, y, bw, h); // right
lcd_set_background(type&BUTTON_BORDER_BOTTOM ? br : bd);lcd_fill(x, y + h - bw, w, bw); // bottom
}
// Set colors for button and text
lcd_set_colors(b->fg, b->bg);
if (type & BUTTON_BORDER_NO_FILL) return;
lcd_fill(x + bw, y + bw, w - (bw * 2), h - (bw * 2));
}
// Draw message box function
void ui_message_box(const char *header, const char *text, uint32_t delay) {
button_t b;
int x , y;
b.bg = LCD_MENU_COLOR;
b.fg = LCD_MENU_TEXT_COLOR;
b.border = BUTTON_BORDER_FLAT|1;
if (header) {// Draw header
ui_draw_button((LCD_WIDTH-MESSAGE_BOX_WIDTH)/2, LCD_HEIGHT/2-40, MESSAGE_BOX_WIDTH, 60, &b);
x = (LCD_WIDTH-MESSAGE_BOX_WIDTH)/2 + 10;
y = LCD_HEIGHT/2-40 + 5;
lcd_drawstring(x, y, header);
request_to_redraw(REDRAW_AREA);
}
if (text) { // Draw window
lcd_set_colors(LCD_MENU_TEXT_COLOR, LCD_FG_COLOR);
lcd_fill((LCD_WIDTH-MESSAGE_BOX_WIDTH)/2+3, LCD_HEIGHT/2-40+FONT_STR_HEIGHT+8, MESSAGE_BOX_WIDTH-6, 60-FONT_STR_HEIGHT-8-3);
x = (LCD_WIDTH-MESSAGE_BOX_WIDTH)/2 + 20;
y = LCD_HEIGHT/2-40 + FONT_STR_HEIGHT + 8 + 14;
lcd_drawstring(x, y, text);
request_to_redraw(REDRAW_AREA);
}
do {
chThdSleepMilliseconds(delay == 0 ? 50 : delay);
} while (delay == 0 && btn_check() != EVT_BUTTON_SINGLE_CLICK && touch_check() != EVT_TOUCH_PRESSED);
}
static void getTouchPoint(uint16_t x, uint16_t y, const char *name, int16_t *data) {
// Clear screen and ask for press
lcd_set_colors(LCD_FG_COLOR, 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 ui_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;
uint16_t p1 = 0, p2 = 2;
#ifdef __FLIP_DISPLAY__
if (VNA_MODE(VNA_MODE_FLIP_DISPLAY)) {p1 = 2, p2 = 0;}
#endif
getTouchPoint(x1, y1, "UPPER LEFT", &config._touch_cal[p1]);
getTouchPoint(x2, y2, "LOWER RIGHT", &config._touch_cal[p2]);
}
void ui_touch_draw_test(void) {
int x0, y0;
int x1, y1;
lcd_set_colors(LCD_FG_COLOR, 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 {
lcd_printf(10, 30, "%3d %3d ", x0, y0);
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, ty;
tx = (LCD_WIDTH - 1 - 2 * CALIBRATION_OFFSET)*(last_touch_x - config._touch_cal[0]) / (config._touch_cal[2] - config._touch_cal[0]) + CALIBRATION_OFFSET;
if (tx<0) tx = 0; else if (tx>=LCD_WIDTH ) tx = LCD_WIDTH -1;
ty = (LCD_HEIGHT- 1 - 2 * CALIBRATION_OFFSET)*(last_touch_y - config._touch_cal[1]) / (config._touch_cal[3] - config._touch_cal[1]) + CALIBRATION_OFFSET;
if (ty<0) ty = 0; else if (ty>=LCD_HEIGHT) ty = LCD_HEIGHT-1;
#ifdef __FLIP_DISPLAY__
if (VNA_MODE(VNA_MODE_FLIP_DISPLAY)) {
tx = LCD_WIDTH - 1 - tx;
ty = LCD_HEIGHT - 1 - ty;
}
#endif
*x = tx;
*y = ty;
}
#ifdef QR_CODE_DRAW
// 31x31 QR code image
// https://github.com/DiSlord/NanoVNA-D
static const uint8_t qr_code_map[] = {
0xff, 0xff, 0xff, 0xfe,
0x80, 0xa2, 0x2e, 0x02,
0xbe, 0xe8, 0xea, 0xfa,
0xa2, 0xaa, 0x4a, 0x8a,
0xa2, 0xce, 0xca, 0x8a,
0xa2, 0xe8, 0x66, 0x8a,
0xbe, 0x93, 0x8a, 0xfa,
0x80, 0xaa, 0xaa, 0x02,
0xff, 0xea, 0xaf, 0xfe,
0xae, 0x58, 0xbb, 0x6a,
0xab, 0x00, 0x60, 0x72,
0xca, 0x63, 0x62, 0x0a,
0xe1, 0xbd, 0xf9, 0x9e,
0x8a, 0xa5, 0x32, 0x7a,
0xbd, 0x8b, 0xba, 0x72,
0xae, 0x40, 0x14, 0x3a,
0xb3, 0xfe, 0x32, 0x7e,
0x98, 0x58, 0xa8, 0xfa,
0xe9, 0x30, 0x96, 0x62,
0x8c, 0x27, 0x37, 0x9a,
0xfb, 0xfd, 0x2a, 0xbe,
0x96, 0xb1, 0x58, 0x16,
0xff, 0xab, 0x9b, 0x8a,
0x80, 0xac, 0x7a, 0xba,
0xbe, 0xf6, 0x33, 0xbe,
0xa2, 0xdc, 0xb0, 0x1a,
0xa2, 0xd4, 0x31, 0x8a,
0xa2, 0x87, 0x0d, 0xb2,
0xbe, 0xc1, 0x2b, 0x1e,
0x80, 0xbf, 0xbb, 0x9a,
0xff, 0xff, 0xff, 0xfe,
};
#endif
static void ui_show_version(void) {
int x = 5, y = 5, i = 1;
int str_height = FONT_STR_HEIGHT + 2;
lcd_set_colors(LCD_FG_COLOR, LCD_BG_COLOR);
lcd_clear_screen();
uint16_t shift = 0b00010010000;
lcd_drawstring_size(BOARD_NAME, x , y, 3);
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++]);
}
uint32_t id0 = *(uint32_t *)0x1FFFF7AC; // MCU id0 address
uint32_t id1 = *(uint32_t *)0x1FFFF7B0; // MCU id1 address
uint32_t id2 = *(uint32_t *)0x1FFFF7B4; // MCU id2 address
lcd_printf(x, y+= str_height, "SN: %08x-%08x-%08x", id0, id1, id2);
lcd_printf(x, y+= str_height, "TCXO = %q" S_Hz, config._xtal_freq);
lcd_printf(LCD_WIDTH - 20*FONT_WIDTH, LCD_HEIGHT - FONT_STR_HEIGHT - 2, "\002\026" "In memory of Maya" "\002\001");
y+=str_height*2;
#ifdef QR_CODE_DRAW
lcd_blitBitmapScale(LCD_WIDTH - 32*3, 5, 31, 31, 3, qr_code_map);
#endif
// 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 ui_enter_dfu(void)
{
touch_stop_watchdog();
int x = 5, y = 20;
lcd_set_colors(LCD_FG_COLOR, 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.");
boardDFUEnter();
}
#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, 6},
[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;
}
static UI_FUNCTION_ADV_CALLBACK(menu_cal_enh_acb) {
(void)data;
if (b) {
b->icon = (cal_status&CALSTAT_ENHANCED_RESPONSE) ? BUTTON_ICON_CHECK : BUTTON_ICON_NOCHECK;
return;
}
// toggle applying correction
cal_status ^= CALSTAT_ENHANCED_RESPONSE;
request_to_redraw(REDRAW_CAL_STATUS);
}
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&= CALSTAT_ENHANCED_RESPONSE; // leave ER state
lastsaveid = NO_SAVE_SLOT;
//set_power(SI5351_CLK_DRIVE_STRENGTH_AUTO);
request_to_redraw(REDRAW_CAL_STATUS);
}
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 (!calibrated) return;
if (b) {
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);
return;
}
// Reset range to calibration
if (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);
else
b->p1.u = data;
if (lastsaveid == data) b->icon = BUTTON_ICON_CHECK;
return;
}
load_properties(data);
}
enum {
MENU_CONFIG_TOUCH_CAL = 0,
MENU_CONFIG_TOUCH_TEST,
MENU_CONFIG_VERSION,
MENU_CONFIG_SAVE,
MENU_CONFIG_RESET,
MENU_CONFIG_LOAD,
};
static UI_FUNCTION_CALLBACK(menu_config_cb) {
switch (data) {
case MENU_CONFIG_TOUCH_CAL:
ui_touch_cal_exec();
break;
case MENU_CONFIG_TOUCH_TEST:
ui_touch_draw_test();
break;
case MENU_CONFIG_VERSION:
ui_show_version();
break;
case MENU_CONFIG_SAVE:
config_save();
menu_move_back(true);
return;
case MENU_CONFIG_RESET:
clear_all_config_prop_data();
NVIC_SystemReset();
break;
#if defined(__SD_CARD_LOAD__) && !defined(__SD_FILE_BROWSER__)
case MENU_CONFIG_LOAD:
if (!sd_card_load_config())
ui_message_box("Error", "No config.ini", 2000);
break;
#endif
}
ui_mode_normal();
request_to_redraw(REDRAW_CLRSCR | REDRAW_AREA | REDRAW_BATTERY | REDRAW_CAL_STATUS | REDRAW_FREQUENCY);
}
#ifdef __DFU_SOFTWARE_MODE__
static UI_FUNCTION_CALLBACK(menu_dfu_cb) {
(void)data;
ui_enter_dfu();
}
#endif
static UI_FUNCTION_ADV_CALLBACK(menu_save_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);
else
b->p1.u = data;
return;
}
if (caldata_save(data) == 0) {
menu_move_back(true);
request_to_redraw(REDRAW_BACKUP | 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 && data != current_trace) // for enabled trace and not current trace
set_active_trace(data); // make active
else //
set_trace_enable(data, !trace[data].enabled); // toggle trace enable
}
extern const menuitem_t menu_marker_s11smith[];
extern const menuitem_t menu_marker_s21smith[];
static uint8_t get_smith_format(void) {return (current_trace != TRACE_INVALID) ? trace[current_trace].smith_format : 0;}
static UI_FUNCTION_ADV_CALLBACK(menu_marker_smith_acb) {
if (b) {
b->icon = get_smith_format() == data ? BUTTON_ICON_GROUP_CHECKED : BUTTON_ICON_GROUP;
b->p1.text = get_smith_format_names(data);
return;
}
if (current_trace == TRACE_INVALID) return;
trace[current_trace].smith_format = data;
request_to_redraw(REDRAW_AREA | REDRAW_MARKER);
}
#define F_S11 0x00
#define F_S21 0x80
static UI_FUNCTION_ADV_CALLBACK(menu_format_acb) {
if (current_trace == TRACE_INVALID) return; // Not apply any for invalid traces
uint16_t format = data & (~F_S21);
uint16_t channel = data & F_S21 ? 1 : 0;
if (b) {
if (trace[current_trace].type == format && trace[current_trace].channel == channel)
b->icon = BUTTON_ICON_CHECK;
if (format == TRC_SMITH) {
uint8_t marker_smith_format = get_smith_format();
if ((channel == 0 && !S11_SMITH_VALUE(marker_smith_format)) ||
(channel == 1 && !S21_SMITH_VALUE(marker_smith_format))) return;
plot_printf(b->label, sizeof(b->label), "%s\n" R_LINK_COLOR "%s", get_trace_typename(TRC_SMITH, marker_smith_format), get_smith_format_names(marker_smith_format));
}
else
b->p1.text = get_trace_typename(format, -1);
return;
}
if (format == TRC_SMITH && trace[current_trace].type == TRC_SMITH && trace[current_trace].channel == channel)
menu_push_submenu(channel == 0 ? menu_marker_s11smith : menu_marker_s21smith);
else
set_trace_type(current_trace, format, channel);
}
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);
request_to_redraw(REDRAW_FREQUENCY | REDRAW_AREA);
}
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;
}
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);
}