forked from ttrftech/NanoVNA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.c
2336 lines (2094 loc) · 53.3 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) 2014-2015, 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 <stdlib.h>
#include <string.h>
uistat_t uistat = {
digit: 6,
current_trace: 0,
lever_mode: LM_MARKER,
marker_delta: FALSE,
marker_smith_format: MS_RLC
};
#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 5000 /* 1sec */
#define BUTTON_DOUBLE_TICKS 5000 /* 500ms */
#define BUTTON_REPEAT_TICKS 1000 /* 100ms */
#define BUTTON_DEBOUNCE_TICKS 200
/* 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 uint32_t last_button_down_ticks;
static uint32_t last_button_repeat_ticks;
static int8_t inhibit_until_release = FALSE;
uint8_t operation_requested = OP_NONE;
int8_t previous_marker = -1;
enum {
UI_NORMAL, UI_MENU, UI_NUMERIC, UI_KEYPAD
};
enum {
KM_START, KM_STOP, KM_CENTER, KM_SPAN, KM_CW, KM_SCALE, KM_REFPOS, KM_EDELAY, KM_VELOCITY_FACTOR, KM_SCALEDELAY
};
uint8_t ui_mode = UI_NORMAL;
uint8_t keypad_mode;
int8_t selection = 0;
typedef struct {
uint8_t type;
char *label;
const void *reference;
} menuitem_t;
int8_t last_touch_status = FALSE;
int16_t last_touch_x;
int16_t last_touch_y;
//int16_t touch_cal[4] = { 1000, 1000, 10*16, 12*16 };
//int16_t touch_cal[4] = { 620, 600, 130, 180 };
#define EVT_TOUCH_NONE 0
#define EVT_TOUCH_DOWN 1
#define EVT_TOUCH_PRESSED 2
#define EVT_TOUCH_RELEASED 3
int awd_count;
//int touch_x, touch_y;
#define NUMINPUT_LEN 10
#define KP_CONTINUE 0
#define KP_DONE 1
#define KP_CANCEL 2
char kp_buf[11];
int8_t kp_index = 0;
void ui_mode_normal(void);
void ui_mode_menu(void);
void ui_mode_numeric(int _keypad_mode);
void ui_mode_keypad(int _keypad_mode);
void draw_menu(void);
void leave_ui_mode(void);
void erase_menu_buttons(void);
void ui_process_keypad(void);
static void ui_process_numeric(void);
static void menu_push_submenu(const menuitem_t *submenu);
static int btn_check(void)
{
int cur_button = READ_PORT() & BUTTON_MASK;
int changed = last_button ^ cur_button;
int status = 0;
uint32_t ticks = chVTGetSystemTime();
if (changed & (1<<BIT_PUSH)) {
if (ticks - last_button_down_ticks >= BUTTON_DEBOUNCE_TICKS) {
if (cur_button & (1<<BIT_PUSH)) {
// button released
status |= EVT_BUTTON_SINGLE_CLICK;
if (inhibit_until_release) {
status = 0;
inhibit_until_release = FALSE;
}
}
last_button_down_ticks = ticks;
}
}
if (changed & (1<<BIT_UP1)) {
if ((cur_button & (1<<BIT_UP1))
&& (ticks >= last_button_down_ticks + BUTTON_DEBOUNCE_TICKS)) {
status |= EVT_UP;
}
last_button_down_ticks = ticks;
}
if (changed & (1<<BIT_DOWN1)) {
if ((cur_button & (1<<BIT_DOWN1))
&& (ticks >= last_button_down_ticks + BUTTON_DEBOUNCE_TICKS)) {
status |= EVT_DOWN;
}
last_button_down_ticks = ticks;
}
last_button = cur_button;
return status;
}
static int btn_wait_release(void)
{
while (TRUE) {
int cur_button = READ_PORT() & BUTTON_MASK;
int changed = last_button ^ cur_button;
uint32_t ticks = chVTGetSystemTime();
int status = 0;
if (!inhibit_until_release) {
if ((cur_button & (1<<BIT_PUSH))
&& ticks - last_button_down_ticks >= BUTTON_DOWN_LONG_TICKS) {
inhibit_until_release = TRUE;
return EVT_BUTTON_DOWN_LONG;
}
if ((changed & (1<<BIT_PUSH))
&& ticks - last_button_down_ticks < BUTTON_DOWN_LONG_TICKS) {
return EVT_BUTTON_SINGLE_CLICK;
}
}
if (changed) {
// finished
last_button = cur_button;
last_button_down_ticks = ticks;
inhibit_until_release = FALSE;
return 0;
}
if (ticks - last_button_down_ticks >= BUTTON_DOWN_LONG_TICKS
&& ticks - last_button_repeat_ticks >= BUTTON_REPEAT_TICKS) {
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;
return status;
}
}
}
int
touch_measure_y(void)
{
int v;
// open Y line
palSetPadMode(GPIOB, 1, PAL_MODE_INPUT_PULLDOWN );
palSetPadMode(GPIOA, 7, PAL_MODE_INPUT_PULLDOWN );
// drive low to high on X line
palSetPadMode(GPIOB, 0, PAL_MODE_OUTPUT_PUSHPULL );
palClearPad(GPIOB, 0);
palSetPadMode(GPIOA, 6, PAL_MODE_OUTPUT_PUSHPULL );
palSetPad(GPIOA, 6);
chThdSleepMilliseconds(2);
v = adc_single_read(ADC1, ADC_CHSELR_CHSEL7);
//chThdSleepMilliseconds(2);
//v += adc_single_read(ADC1, ADC_CHSELR_CHSEL7);
return v;
}
int
touch_measure_x(void)
{
int v;
// open X line
palSetPadMode(GPIOB, 0, PAL_MODE_INPUT_PULLDOWN );
palSetPadMode(GPIOA, 6, PAL_MODE_INPUT_PULLDOWN );
// drive low to high on Y line
palSetPadMode(GPIOB, 1, PAL_MODE_OUTPUT_PUSHPULL );
palSetPad(GPIOB, 1);
palSetPadMode(GPIOA, 7, PAL_MODE_OUTPUT_PUSHPULL );
palClearPad(GPIOA, 7);
chThdSleepMilliseconds(2);
v = adc_single_read(ADC1, ADC_CHSELR_CHSEL6);
//chThdSleepMilliseconds(2);
//v += adc_single_read(ADC1, ADC_CHSELR_CHSEL6);
return v;
}
void
touch_prepare_sense(void)
{
// open Y line
palSetPadMode(GPIOB, 1, PAL_MODE_INPUT_PULLDOWN );
palSetPadMode(GPIOA, 7, PAL_MODE_INPUT_PULLDOWN );
// force high X line
palSetPadMode(GPIOB, 0, PAL_MODE_OUTPUT_PUSHPULL );
palSetPad(GPIOB, 0);
palSetPadMode(GPIOA, 6, PAL_MODE_OUTPUT_PUSHPULL );
palSetPad(GPIOA, 6);
}
void
touch_start_watchdog(void)
{
touch_prepare_sense();
adc_start_analog_watchdogd(ADC1, ADC_CHSELR_CHSEL7);
}
int
touch_status(void)
{
touch_prepare_sense();
return adc_single_read(ADC1, ADC_CHSELR_CHSEL7) > TOUCH_THRESHOLD;
}
int touch_check(void)
{
int stat = touch_status();
if (stat) {
chThdSleepMilliseconds(10);
int x = touch_measure_x();
int y = touch_measure_y();
if (touch_status()) {
last_touch_x = x;
last_touch_y = y;
}
touch_prepare_sense();
}
if (stat != last_touch_status) {
last_touch_status = stat;
if (stat) {
return EVT_TOUCH_PRESSED;
} else {
return EVT_TOUCH_RELEASED;
}
} else {
if (stat)
return EVT_TOUCH_DOWN;
else
return EVT_TOUCH_NONE;
}
}
void touch_wait_release(void)
{
int status;
/* wait touch release */
do {
status = touch_check();
} while(status != EVT_TOUCH_RELEASED);
}
extern void ili9341_line(int, int, int, int, int);
void
touch_cal_exec(void)
{
int status;
int x1, x2, y1, y2;
adc_stop(ADC1);
ili9341_fill(0, 0, 320, 240, 0);
ili9341_line(0, 0, 0, 32, 0xffff);
ili9341_line(0, 0, 32, 0, 0xffff);
ili9341_drawstring_5x7("TOUCH UPPER LEFT", 10, 10, 0xffff, 0x0000);
do {
status = touch_check();
} while(status != EVT_TOUCH_RELEASED);
x1 = last_touch_x;
y1 = last_touch_y;
ili9341_fill(0, 0, 320, 240, 0);
ili9341_line(320-1, 240-1, 320-1, 240-32, 0xffff);
ili9341_line(320-1, 240-1, 320-32, 240-1, 0xffff);
ili9341_drawstring_5x7("TOUCH LOWER RIGHT", 230, 220, 0xffff, 0x0000);
do {
status = touch_check();
} while(status != EVT_TOUCH_RELEASED);
x2 = last_touch_x;
y2 = last_touch_y;
config.touch_cal[0] = x1;
config.touch_cal[1] = y1;
config.touch_cal[2] = (x2 - x1) * 16 / 320;
config.touch_cal[3] = (y2 - y1) * 16 / 240;
//redraw_all();
touch_start_watchdog();
}
void
touch_draw_test(void)
{
int status;
int x0, y0;
int x1, y1;
adc_stop(ADC1);
ili9341_fill(0, 0, 320, 240, 0);
ili9341_drawstring_5x7("TOUCH TEST: DRAG PANEL", OFFSETX, 233, 0xffff, 0x0000);
do {
status = touch_check();
} while(status != EVT_TOUCH_PRESSED);
touch_position(&x0, &y0);
do {
status = touch_check();
touch_position(&x1, &y1);
ili9341_line(x0, y0, x1, y1, 0xffff);
x0 = x1;
y0 = y1;
chThdSleepMilliseconds(50);
} while(status != EVT_TOUCH_RELEASED);
touch_start_watchdog();
}
void
touch_position(int *x, int *y)
{
*x = (last_touch_x - config.touch_cal[0]) * 16 / config.touch_cal[2];
*y = (last_touch_y - config.touch_cal[1]) * 16 / config.touch_cal[3];
}
void
show_version(void)
{
int x = 5, y = 5;
adc_stop(ADC1);
ili9341_fill(0, 0, 320, 240, 0);
ili9341_drawstring_size(BOARD_NAME, x, y, 0xffff, 0x0000, 4);
y += 25;
ili9341_drawstring_5x7("2016-2019 Copyright @edy555", x, y += 10, 0xffff, 0x0000);
ili9341_drawstring_5x7("Licensed under GPL. See: https://github.com/ttrftech/NanoVNA", x, y += 10, 0xffff, 0x0000);
ili9341_drawstring_5x7("Version: " VERSION, x, y += 10, 0xffff, 0x0000);
ili9341_drawstring_5x7("Build Time: " __DATE__ " - " __TIME__, x, y += 10, 0xffff, 0x0000);
y += 5;
ili9341_drawstring_5x7("Kernel: " CH_KERNEL_VERSION, x, y += 10, 0xffff, 0x0000);
ili9341_drawstring_5x7("Compiler: " PORT_COMPILER_NAME, x, y += 10, 0xffff, 0x0000);
ili9341_drawstring_5x7("Architecture: " PORT_ARCHITECTURE_NAME " Core Variant: " PORT_CORE_VARIANT_NAME, x, y += 10, 0xffff, 0x0000);
ili9341_drawstring_5x7("Port Info: " PORT_INFO, x, y += 10, 0xffff, 0x0000);
ili9341_drawstring_5x7("Platform: " PLATFORM_NAME, x, y += 10, 0xffff, 0x0000);
while (true) {
if (touch_check() == EVT_TOUCH_PRESSED)
break;
if (btn_check() & EVT_BUTTON_SINGLE_CLICK)
break;
}
touch_start_watchdog();
}
void
enter_dfu(void)
{
adc_stop(ADC1);
int x = 5, y = 5;
// leave a last message
ili9341_fill(0, 0, 320, 240, 0);
ili9341_drawstring_5x7("DFU: Device Firmware Update Mode", x, y += 10, 0xffff, 0x0000);
ili9341_drawstring_5x7("To exit DFU mode, please reset device yourself.", x, y += 10, 0xffff, 0x0000);
// see __early_init in ./NANOVNA_STM32_F072/board.c
*((unsigned long *)BOOT_FROM_SYTEM_MEMORY_MAGIC_ADDRESS) = BOOT_FROM_SYTEM_MEMORY_MAGIC;
NVIC_SystemReset();
}
// type of menu item
enum {
MT_NONE,
MT_BLANK,
MT_SUBMENU,
MT_CALLBACK,
MT_CANCEL,
MT_CLOSE
};
typedef void (*menuaction_cb_t)(int item);
static void menu_move_back(void);
static void
menu_calop_cb(int item)
{
switch (item) {
case 0: // OPEN
cal_collect(CAL_OPEN);
break;
case 1: // SHORT
cal_collect(CAL_SHORT);
break;
case 2: // LOAD
cal_collect(CAL_LOAD);
break;
case 3: // ISOLN
cal_collect(CAL_ISOLN);
break;
case 4: // THRU
cal_collect(CAL_THRU);
break;
}
selection = item+1;
draw_cal_status();
draw_menu();
}
static void
menu_caldone_cb(int item)
{
extern const menuitem_t menu_save[];
//extern const menuitem_t menu_cal[];
(void)item;
cal_done();
draw_cal_status();
menu_move_back();
menu_push_submenu(menu_save);
}
static void
menu_cal2_cb(int item)
{
switch (item) {
case 2: // RESET
cal_status = 0;
break;
case 3: // CORRECTION
// toggle applying correction
if (cal_status)
cal_status ^= CALSTAT_APPLY;
draw_menu();
break;
}
draw_cal_status();
//menu_move_back();
}
static void
menu_recall_cb(int item)
{
if (item < 0 || item >= 5)
return;
if (caldata_recall(item) == 0) {
menu_move_back();
ui_mode_normal();
update_grid();
draw_cal_status();
}
}
static void
menu_config_cb(int item)
{
switch (item) {
case 0:
touch_cal_exec();
redraw_frame();
request_to_redraw_grid();
draw_menu();
break;
case 1:
touch_draw_test();
redraw_frame();
request_to_redraw_grid();
draw_menu();
break;
case 2:
config_save();
menu_move_back();
ui_mode_normal();
break;
case 3:
show_version();
redraw_frame();
request_to_redraw_grid();
draw_menu();
}
}
static void
menu_dfu_cb(int item)
{
switch (item) {
case 0:
enter_dfu();
}
}
static void
menu_save_cb(int item)
{
if (item < 0 || item >= 5)
return;
if (caldata_save(item) == 0) {
menu_move_back();
ui_mode_normal();
draw_cal_status();
}
}
static void
choose_active_trace(void)
{
int i;
if (trace[uistat.current_trace].enabled)
// do nothing
return;
for (i = 0; i < 4; i++)
if (trace[i].enabled) {
uistat.current_trace = i;
return;
}
}
static void
menu_trace_cb(int item)
{
if (item < 0 || item >= 4)
return;
if (trace[item].enabled) {
if (item == uistat.current_trace) {
// disable if active trace is selected
trace[item].enabled = FALSE;
choose_active_trace();
} else {
// make active selected trace
uistat.current_trace = item;
}
} else {
trace[item].enabled = TRUE;
uistat.current_trace = item;
}
request_to_redraw_grid();
draw_menu();
}
static void
menu_format_cb(int item)
{
switch (item) {
case 0:
set_trace_type(uistat.current_trace, TRC_LOGMAG);
break;
case 1:
set_trace_type(uistat.current_trace, TRC_PHASE);
break;
case 2:
set_trace_type(uistat.current_trace, TRC_DELAY);
break;
case 3:
set_trace_type(uistat.current_trace, TRC_SMITH);
break;
case 4:
set_trace_type(uistat.current_trace, TRC_SWR);
break;
}
request_to_redraw_grid();
ui_mode_normal();
//redraw_all();
}
static void
menu_format2_cb(int item)
{
switch (item) {
case 0:
set_trace_type(uistat.current_trace, TRC_POLAR);
break;
case 1:
set_trace_type(uistat.current_trace, TRC_LINEAR);
break;
case 2:
set_trace_type(uistat.current_trace, TRC_REAL);
break;
case 3:
set_trace_type(uistat.current_trace, TRC_IMAG);
break;
case 4:
set_trace_type(uistat.current_trace, TRC_R);
break;
case 5:
set_trace_type(uistat.current_trace, TRC_X);
break;
}
request_to_redraw_grid();
ui_mode_normal();
}
static void
menu_channel_cb(int item)
{
if (item < 0 || item >= 2)
return;
set_trace_channel(uistat.current_trace, item);
menu_move_back();
ui_mode_normal();
}
static void
menu_transform_window_cb(int item)
{
// TODO
switch (item) {
case 0:
domain_mode = (domain_mode & ~TD_WINDOW) | TD_WINDOW_MINIMUM;
ui_mode_normal();
break;
case 1:
domain_mode = (domain_mode & ~TD_WINDOW) | TD_WINDOW_NORMAL;
ui_mode_normal();
break;
case 2:
domain_mode = (domain_mode & ~TD_WINDOW) | TD_WINDOW_MAXIMUM;
ui_mode_normal();
break;
}
}
static void
menu_transform_cb(int item)
{
int status;
switch (item) {
case 0:
if ((domain_mode & DOMAIN_MODE) == DOMAIN_TIME) {
domain_mode = (domain_mode & ~DOMAIN_MODE) | DOMAIN_FREQ;
} else {
domain_mode = (domain_mode & ~DOMAIN_MODE) | DOMAIN_TIME;
}
uistat.lever_mode = LM_MARKER;
draw_frequencies();
ui_mode_normal();
break;
case 1:
domain_mode = (domain_mode & ~TD_FUNC) | TD_FUNC_LOWPASS_IMPULSE;
ui_mode_normal();
break;
case 2:
domain_mode = (domain_mode & ~TD_FUNC) | TD_FUNC_LOWPASS_STEP;
ui_mode_normal();
break;
case 3:
domain_mode = (domain_mode & ~TD_FUNC) | TD_FUNC_BANDPASS;
ui_mode_normal();
break;
case 5:
status = btn_wait_release();
if (status & EVT_BUTTON_DOWN_LONG) {
ui_mode_numeric(KM_VELOCITY_FACTOR);
ui_process_numeric();
} else {
ui_mode_keypad(KM_VELOCITY_FACTOR);
ui_process_keypad();
}
break;
}
}
static void
choose_active_marker(void)
{
int i;
for (i = 0; i < 4; i++)
if (markers[i].enabled) {
active_marker = i;
return;
}
active_marker = -1;
}
static void
menu_scale_cb(int item)
{
int status;
int km = KM_SCALE + item;
if (km == KM_SCALE && trace[uistat.current_trace].type == TRC_DELAY) {
km = KM_SCALEDELAY;
}
status = btn_wait_release();
if (status & EVT_BUTTON_DOWN_LONG) {
ui_mode_numeric(km);
ui_process_numeric();
} else {
ui_mode_keypad(km);
ui_process_keypad();
}
}
static void
menu_stimulus_cb(int item)
{
int status;
switch (item) {
case 0: /* START */
case 1: /* STOP */
case 2: /* CENTER */
case 3: /* SPAN */
case 4: /* CW */
uistat.lever_mode = item == 3 ? LM_SPAN : LM_CENTER;
status = btn_wait_release();
if (status & EVT_BUTTON_DOWN_LONG) {
ui_mode_numeric(item);
ui_process_numeric();
} else {
ui_mode_keypad(item);
ui_process_keypad();
}
break;
case 5: /* PAUSE */
toggle_sweep();
//menu_move_back();
//ui_mode_normal();
draw_menu();
break;
}
}
static int32_t
get_marker_frequency(int marker)
{
if (marker < 0 || marker >= 4)
return -1;
if (!markers[marker].enabled)
return -1;
return frequencies[markers[marker].index];
}
static void
menu_marker_op_cb(int item)
{
int32_t freq = get_marker_frequency(active_marker);
if (freq < 0)
return; // no active marker
switch (item) {
case 0: /* MARKER->START */
set_sweep_frequency(ST_START, freq);
break;
case 1: /* MARKER->STOP */
set_sweep_frequency(ST_STOP, freq);
break;
case 2: /* MARKER->CENTER */
set_sweep_frequency(ST_CENTER, freq);
break;
case 3: /* MARKERS->SPAN */
{
if (previous_marker == -1 || active_marker == previous_marker) {
// if only 1 marker is active, keep center freq and make span the marker comes to the edge
int32_t center = get_sweep_frequency(ST_CENTER);
int32_t span = center - freq;
if (span < 0) span = -span;
set_sweep_frequency(ST_SPAN, span * 2);
} else {
// if 2 or more marker active, set start and stop freq to each marker
int32_t freq2 = get_marker_frequency(previous_marker);
if (freq2 < 0)
return;
if (freq > freq2) {
freq2 = freq;
freq = get_marker_frequency(previous_marker);
}
set_sweep_frequency(ST_START, freq);
set_sweep_frequency(ST_STOP, freq2);
}
}
break;
case 4: /* MARKERS->EDELAY */
{
if (uistat.current_trace == -1)
break;
float (*array)[2] = measured[trace[uistat.current_trace].channel];
float v = groupdelay_from_array(markers[active_marker].index, array);
set_electrical_delay(electrical_delay + (v / 1e-12));
}
break;
}
ui_mode_normal();
draw_cal_status();
//redraw_all();
}
static void
menu_marker_search_cb(int item)
{
int i;
if (active_marker == -1)
return;
switch (item) {
case 0: /* maximum */
case 1: /* minimum */
i = marker_search(item);
if (i != -1)
markers[active_marker].index = i;
draw_menu();
break;
case 2: /* search Left */
i = marker_search_left(markers[active_marker].index);
if (i != -1)
markers[active_marker].index = i;
draw_menu();
break;
case 3: /* search right */
i = marker_search_right(markers[active_marker].index);
if (i != -1)
markers[active_marker].index = i;
draw_menu();
break;
}
redraw_marker(active_marker, TRUE);
uistat.lever_mode = LM_SEARCH;
}
static void
menu_marker_smith_cb(int item)
{
uistat.marker_smith_format = item;
redraw_marker(active_marker, TRUE);
draw_menu();
}
void
active_marker_select(int item)
{
if (item == -1) {
active_marker = previous_marker;
previous_marker = -1;
if (active_marker == -1) {
choose_active_marker();
}
} else {
if (previous_marker != active_marker)
previous_marker = active_marker;
active_marker = item;
}
}
static void
menu_marker_sel_cb(int item)
{
if (item >= 0 && item < 4) {
if (markers[item].enabled) {
if (item == active_marker) {
// disable if active trace is selected
markers[item].enabled = FALSE;
active_marker_select(-1);
} else {
active_marker_select(item);
}
} else {
markers[item].enabled = TRUE;
active_marker_select(item);
}
} else if (item == 4) { /* all off */
markers[0].enabled = FALSE;
markers[1].enabled = FALSE;
markers[2].enabled = FALSE;
markers[3].enabled = FALSE;
previous_marker = -1;
active_marker = -1;
} else if (item == 5) { /* marker delta */
uistat.marker_delta = !uistat.marker_delta;
}
redraw_marker(active_marker, TRUE);
draw_menu();
uistat.lever_mode = LM_MARKER;
}
const menuitem_t menu_calop[] = {
{ MT_CALLBACK, "OPEN", menu_calop_cb },
{ MT_CALLBACK, "SHORT", menu_calop_cb },
{ MT_CALLBACK, "LOAD", menu_calop_cb },
{ MT_CALLBACK, "ISOLN", menu_calop_cb },
{ MT_CALLBACK, "THRU", menu_calop_cb },
{ MT_CALLBACK, "DONE", menu_caldone_cb },
{ MT_CANCEL, S_LARROW" BACK", NULL },
{ MT_NONE, NULL, NULL } // sentinel
};
const menuitem_t menu_save[] = {
{ MT_CALLBACK, "SAVE 0", menu_save_cb },
{ MT_CALLBACK, "SAVE 1", menu_save_cb },
{ MT_CALLBACK, "SAVE 2", menu_save_cb },
{ MT_CALLBACK, "SAVE 3", menu_save_cb },
{ MT_CALLBACK, "SAVE 4", menu_save_cb },
{ MT_CANCEL, S_LARROW" BACK", NULL },
{ MT_NONE, NULL, NULL } // sentinel
};
const menuitem_t menu_cal[] = {
{ MT_SUBMENU, "CALIBRATE", menu_calop },
{ MT_SUBMENU, "SAVE", menu_save },
{ MT_CALLBACK, "RESET", menu_cal2_cb },
{ MT_CALLBACK, "CORRECTION", menu_cal2_cb },
{ MT_CANCEL, S_LARROW" BACK", NULL },
{ MT_NONE, NULL, NULL } // sentinel
};
const menuitem_t menu_trace[] = {
{ MT_CALLBACK, "TRACE 0", menu_trace_cb },
{ MT_CALLBACK, "TRACE 1", menu_trace_cb },
{ MT_CALLBACK, "TRACE 2", menu_trace_cb },
{ MT_CALLBACK, "TRACE 3", menu_trace_cb },
{ MT_CANCEL, S_LARROW" BACK", NULL },
{ MT_NONE, NULL, NULL } // sentinel
};
const menuitem_t menu_format2[] = {
{ MT_CALLBACK, "POLAR", menu_format2_cb },
{ MT_CALLBACK, "LINEAR", menu_format2_cb },
{ MT_CALLBACK, "REAL", menu_format2_cb },
{ MT_CALLBACK, "IMAG", menu_format2_cb },
{ MT_CALLBACK, "RESISTANCE", menu_format2_cb },
{ MT_CALLBACK, "REACTANCE", menu_format2_cb },
{ MT_CANCEL, S_LARROW" BACK", NULL },
{ MT_NONE, NULL, NULL } // sentinel
};
const menuitem_t menu_format[] = {
{ MT_CALLBACK, "LOGMAG", menu_format_cb },
{ MT_CALLBACK, "PHASE", menu_format_cb },
{ MT_CALLBACK, "DELAY", menu_format_cb },
{ MT_CALLBACK, "SMITH", menu_format_cb },
{ MT_CALLBACK, "SWR", menu_format_cb },
{ MT_SUBMENU, S_RARROW" MORE", menu_format2 },
//{ MT_CALLBACK, "LINEAR", menu_format_cb },