forked from ttrftech/NanoVNA
-
Notifications
You must be signed in to change notification settings - Fork 53
/
ui.c
8337 lines (7804 loc) · 251 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]
* Copyright (c) 2020-2022, Erik Kaashoek
* 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 <string.h>
#include <stdlib.h>
#include <math.h>
#pragma GCC push_options
#pragma GCC optimize ("Os")
uistat_t uistat = {
current_trace: 0,
lever_mode: LM_MARKER,
marker_delta: FALSE,
marker_noise: FALSE,
marker_tracking : FALSE,
auto_center_marker : FALSE,
text : "",
};
#define NO_EVENT 0
#define EVT_BUTTON_SINGLE_CLICK 0x01
#define EVT_BUTTON_DOUBLE_CLICK 0x02
#define EVT_BUTTON_DOWN_LONG 0x04
#define EVT_BUTTON_CLICK_RELEASE 0x08
#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( 40) // 40ms
#define BUTTON_DEBOUNCE_TICKS MS2ST( 2) // 2ms
/* lever switch assignment */
#define BIT_UP1 3
#define BIT_PUSH 2
#define BIT_DOWN1 1
#define READ_PORT() palReadPort(GPIOA)
#define BUTTON_MASK 0b1110
static uint16_t last_button = 0b0000;
static uint32_t last_button_down_ticks;
static uint32_t last_button_repeat_ticks;
#define MENU_USE_AUTOHEIGHT
#ifdef MENU_USE_AUTOHEIGHT
static uint16_t menu_button_height = MENU_BUTTON_HEIGHT_N(MENU_BUTTON_MIN);
#endif
volatile uint8_t operation_requested = OP_NONE;
volatile uint8_t break_execute = false;
volatile uint8_t abort_enabled = false;
int8_t previous_marker = MARKER_INVALID;
enum {
UI_NORMAL, UI_MENU, UI_KEYPAD,
#ifdef __SD_FILE_BROWSER__
UI_BROWSER,
#endif
UI_END
};
#define NUMINPUT_LEN 12
#ifdef FF_USE_LFN
#define TXTINPUT_LEN (FF_MAX_LFN - 5)
#else
#define TXTINPUT_LEN (8)
#endif
#if NUMINPUT_LEN + 2 > TXTINPUT_LEN + 1
char kp_buf[NUMINPUT_LEN+2]; // !!!!!! WARNING size must be + 2 from NUMINPUT_LEN or TXTINPUT_LEN + 1
#else
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 uint8_t keypad_mode;
static char *kp_help_text = NULL;
uint8_t menu_current_level = 0;
static int selection = 0;
static const uint8_t slider_bitmap[]=
{
_BMP8(0b11111110),
_BMP8(0b11111110),
_BMP8(0b01111100),
_BMP8(0b00111000),
_BMP8(0b00010000)
};
// 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_CHECK_AUTO 2
#define BUTTON_ICON_CHECK_MANUAL 3
#define BUTTON_ICON_GROUP 4
#define BUTTON_ICON_GROUP_CHECKED 5
#define CHECK_ICON(S) ((S) ? BUTTON_ICON_CHECK : BUTTON_ICON_NOCHECK)
#define GROUP_ICON(S) ((S) ? BUTTON_ICON_GROUP_CHECKED : BUTTON_ICON_GROUP)
#define AUTO_ICON(S) (S>=2?BUTTON_ICON_CHECK_AUTO:S) // Depends on order of ICONs!!!!!
#define BUTTON_BORDER_NONE 0x00
#define BUTTON_BORDER_WIDTH_MASK 0x07
#define BUTTON_BORDER_NO_FILL 0x08
// 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)
// 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 int8_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_keypad(int _keypad_mode);
// static void draw_menu(void);
static void leave_ui_mode(void);
static void erase_menu_buttons(void);
static void ui_process_keypad(void);
static void choose_active_marker(void);
static void menu_move_back(bool leave_ui);
static void draw_button(uint16_t x, uint16_t y, uint16_t w, uint16_t h, ui_button_t *b);
//static const menuitem_t menu_marker_type[];
//#ifdef __USE_SD_CARD__
//svoid save_csv(uint8_t mask);
//#endif
bool isFullScreenMode(void) {
#ifdef __SD_FILE_BROWSER__
return ui_mode == UI_BROWSER;
#else
return false;
#endif
}
int btn_side(void)
{
uint16_t cur_button = READ_PORT() & BUTTON_MASK;
if (cur_button & (1<<BIT_UP1) || cur_button & (1<<BIT_DOWN1))
return true;
return false;
}
static int btn_check(void)
{
systime_t ticks;
#ifdef __WATCHDOG__
wdgReset(&WDGD1);
#endif
// Debounce input
while(TRUE){
ticks = chVTGetSystemTimeX();
if(ticks - last_button_down_ticks > BUTTON_DEBOUNCE_TICKS)
break;
chThdSleepMilliseconds(10);
}
int 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 |= (config.flip ? EVT_DOWN : EVT_UP);
if (button_set & (1<<BIT_DOWN1))
status |= (config.flip ? EVT_UP : EVT_DOWN);
return status;
}
static int btn_wait_release(void)
{
while (TRUE) {
#ifdef __WATCHDOG__
wdgReset(&WDGD1);
#endif
systime_t ticks = chVTGetSystemTimeX();
systime_t dt = ticks - last_button_down_ticks;
// Debounce input
// if (dt < BUTTON_DEBOUNCE_TICKS){
// chThdSleepMilliseconds(10);
// continue;
// }
chThdSleepMilliseconds(1);
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;
else if (changed & (1<<BIT_PUSH)) { // release
last_button = cur_button;
last_button_down_ticks = ticks;
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) {
int status = 0;
if (cur_button & (1<<BIT_DOWN1))
status |= (config.flip ? EVT_UP: EVT_DOWN) | EVT_REPEAT;
if (cur_button & (1<<BIT_UP1))
status |= (config.flip ? EVT_DOWN:EVT_UP) | EVT_REPEAT;
last_button_repeat_ticks = ticks + BUTTON_REPEAT_TICKS;
return status;
}
}
}
#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)
{
#ifdef __WATCHDOG__
wdgReset(&WDGD1);
#endif
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
}
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__
remote_mouse_down = 0;
#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:
#ifdef TINYSA4
gptStart(&GPTD1, &gpt3cfg); // Init timer 1
gptStartContinuous(&GPTD1, 10); // Start timer 1 vs timer 10 interval
#else
gptStart(&GPTD3, &gpt3cfg); // Init timer 3
gptStartContinuous(&GPTD3, 10); // Start timer 3 vs timer 10 interval
#endif
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)
{
#ifdef TINYSA4
if (setting.lock_display)
return EVT_TOUCH_NONE;
#endif
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__
remote_mouse_down = 0;
} else {
stat = remote_mouse_down == 1;
#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
void touch_set(int16_t x, int16_t y) {
last_touch_x = x;
last_touch_y = y;
}
void
touch_wait_release(void)
{
while (touch_check() != EVT_TOUCH_NONE) {
chThdSleepMilliseconds(20);
}
}
#if 0
static inline void
touch_wait_pressed(void)
{
while (touch_check() != EVT_TOUCH_PRESSED)
;
}
#endif
static inline void
touch_wait_released(void)
{
while (touch_check() != EVT_TOUCH_RELEASED)
;
}
#if 0
void
touch_cal_exec(void)
{
int x1, x2, y1, y2;
int old_flip = config.flip;
config.flip = 0;
ili9341_set_foreground(LCD_FG_COLOR);
ili9341_set_background(LCD_BG_COLOR);
ili9341_clear_screen();
ili9341_line(0, 0, 0, 32);
ili9341_line(0, 0, 32, 0);
ili9341_line(0, 0, 32, 32);
ili9341_drawstring("TOUCH UPPER LEFT", 40, 40);
touch_wait_released();
// touch_wait_release();
x1 = last_touch_x;
y1 = last_touch_y;
ili9341_clear_screen();
ili9341_line(LCD_WIDTH-1, LCD_HEIGHT-1, LCD_WIDTH-1, LCD_HEIGHT-32);
ili9341_line(LCD_WIDTH-1, LCD_HEIGHT-1, LCD_WIDTH-32, LCD_HEIGHT-1);
ili9341_line(LCD_WIDTH-1, LCD_HEIGHT-1, LCD_WIDTH-32, LCD_HEIGHT-32);
ili9341_drawstring("TOUCH LOWER RIGHT", LCD_WIDTH-17*(FONT_WIDTH)-30, LCD_HEIGHT-FONT_GET_HEIGHT-35);
touch_wait_released();
// touch_wait_release();
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 / LCD_WIDTH;
config.touch_cal[3] = (y2 - y1) * 16 / LCD_HEIGHT;
config.flip = old_flip;
//redraw_all();
}
#else
#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
ili9341_set_foreground(LCD_FG_COLOR);
ili9341_set_background(LCD_BG_COLOR);
ili9341_clear_screen();
ili9341_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_released();
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;
uint16_t p1 = 0, p2 = 2;
if (config.flip) {p1 = 2, p2 = 0;}
getTouchPoint(x1, y1, "UPPER LEFT", &config.touch_cal[p1]);
getTouchPoint(x2, y2, "LOWER RIGHT", &config.touch_cal[p2]);
config_save(); // Auto save touch calibration
}
#endif
void
touch_draw_test(void)
{
int x0, y0;
int x1, y1;
ili9341_set_foreground(LCD_FG_COLOR);
ili9341_set_background(LCD_BG_COLOR);
ili9341_clear_screen();
ili9341_drawstring("TOUCH PANEL, DRAW LINES, PRESS BUTTON TO FINISH", OFFSETX, LCD_HEIGHT - FONT_GET_HEIGHT);
int old_button_state = 0;
lcd_set_font(FONT_NORMAL);
while (touch_check() != EVT_TOUCH_PRESSED) {
int button_state = READ_PORT() & BUTTON_MASK;
if (button_state != old_button_state) {
lcd_printf(120, 120, "STATE: % 4d ", button_state);
old_button_state = button_state;
}
}
lcd_set_font(FONT_SMALL);
do {
if (touch_check() == EVT_TOUCH_PRESSED){
touch_position(&x0, &y0);
do {
chThdSleepMilliseconds(50);
touch_position(&x1, &y1);
ili9341_line(x0, y0, x1, y1);
x0 = x1;
y0 = y1;
} while (touch_check() != EVT_TOUCH_RELEASED);
}
}while (!(btn_check() & EVT_BUTTON_SINGLE_CLICK));
}
#ifdef TINYSA4
void
touch_notepad(void)
{
int x0, y0;
int x1, y1;
bool first = true;
ili9341_set_foreground(LCD_FG_COLOR);
ili9341_set_background(LCD_BG_COLOR);
ili9341_clear_screen();
ili9341_drawstring("TOUCH PANEL TO DRAW, PRESS JOG BUTTON TO CLEAR, RESTART TO EXIT", OFFSETX, LCD_HEIGHT - FONT_GET_HEIGHT);
lcd_set_font(FONT_SMALL);
while (true) {
do {
if (touch_check() == EVT_TOUCH_PRESSED){
if (first) {
ili9341_clear_screen();
first = false;
}
touch_position(&x0, &y0);
do {
chThdSleepMilliseconds(50);
touch_position(&x1, &y1);
ili9341_line(x0, y0, x1, y1);
x0 = x1;
y0 = y1;
} while (touch_check() != EVT_TOUCH_RELEASED);
}
}while (!(btn_check() & EVT_BUTTON_SINGLE_CLICK));
ili9341_clear_screen();
ili9341_drawstring("TOUCH PANEL TO DRAW, PRESS JOG BUTTON TO CLEAR, RESTART TO EXIT", OFFSETX, LCD_HEIGHT - FONT_GET_HEIGHT);
first = true;
}
}
#endif
void
touch_position(int *x, int *y)
{
#ifdef __REMOTE_DESKTOP__
if (remote_mouse_down) {
*x = last_touch_x;
*y = last_touch_y;
return;
}
#endif
#if 0
int tx = (last_touch_x - config.touch_cal[0]) * 16 / config.touch_cal[2];
int ty = (last_touch_y - config.touch_cal[1]) * 16 / config.touch_cal[3];
#else
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;
#endif
if (config.flip) {
tx = LCD_WIDTH - 1 - tx;
ty = LCD_HEIGHT - 1 - ty;
}
*x = tx;
*y = ty;
}
#ifdef TINYSA4
extern const char *get_hw_version_text(void);
#endif
void
show_version(void)
{
int x = 5, y = 5, i = 0;
ili9341_set_foreground(LCD_FG_COLOR);
ili9341_set_background(LCD_BG_COLOR);
ili9341_clear_screen();
uint16_t shift = 0b00000100001;
// Version text for tinySA3
#ifdef TINYSA3
ili9341_drawstring_10x14(info_about[i++], x , y);
y+=FONT_GET_HEIGHT*3+3-5;
while (info_about[i]) {
do {shift>>=1; y+=5;} while (shift&1);
ili9341_drawstring(info_about[i++], x, y+=FONT_STR_HEIGHT+3-5);
}
if (has_esd)
ili9341_drawstring("ESD protected", x, y+=FONT_STR_HEIGHT + 2);
y+=FONT_STR_HEIGHT + 1;
#endif
// Version text for tinySA4
#ifdef TINYSA4
ili9341_drawstring_10x14(info_about[i++], x , y);
y+=FONT_GET_HEIGHT*3+2-5;
ili9341_drawstring_7x13(info_about[i++], x , y);
while (info_about[i]) {
do {shift>>=1; y+=5;} while (shift&1);
ili9341_drawstring_7x13(info_about[i++], x, y+=bFONT_STR_HEIGHT+2-5);
}
lcd_set_font(FONT_NORMAL);
lcd_printf(x, y+=bFONT_STR_HEIGHT, "HW Version:%s (%d) %s", get_hw_version_text(), adc1_single_read(0), (si5351_available?"SI5351":""));
extern const char *states[];
#define ENABLE_THREADS_COMMAND
#ifdef ENABLE_THREADS_COMMAND
y+=FONT_STR_HEIGHT + 1;
thread_t *tp;
tp = chRegFirstThread();
do {
uint32_t max_stack_use = 0U;
#if (CH_DBG_ENABLE_STACK_CHECK == TRUE) || (CH_CFG_USE_DYNAMIC == TRUE)
uint32_t stklimit = (uint32_t)tp->wabase;
#if CH_DBG_FILL_THREADS == TRUE
uint8_t *p = (uint8_t *)tp->wabase; while(p[max_stack_use]==CH_DBG_STACK_FILL_VALUE) max_stack_use++;
#endif
#else
uint32_t stklimit = 0U;
#endif
lcd_printf(x, y+=bFONT_STR_HEIGHT, "%08x|%08x|%08x|%08x|%4u|%4u|%9s|%12s",
stklimit, (uint32_t)tp->ctx.sp, max_stack_use, (uint32_t)tp,
(uint32_t)tp->refs - 1, (uint32_t)tp->prio, states[tp->state],
tp->name == NULL ? "" : tp->name);
tp = chRegNextThread(tp);
} while (tp != NULL);
#endif
y+=bFONT_STR_HEIGHT + 1;
#endif // TINYSA4
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 __WATCHDOG__
wdgReset(&WDGD1);
#endif
#ifdef TINYSA4
#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 0
uint32_t vbat=adc_vbat_read();
lcd_printf(x, y + bFONT_STR_HEIGHT + 1, "Batt: %d.%03dV", vbat/1000, vbat%1000);
#endif
#endif // TINYSA4
}
lcd_set_font(FONT_SMALL);
}
#ifdef __HAS_DFU__
void
enter_dfu(void)
{
int x = 5, y = 5;
ili9341_set_foreground(LCD_FG_COLOR);
ili9341_set_background(LCD_BG_COLOR);
// leave a last message
ili9341_clear_screen();
ili9341_drawstring_7x13("DFU: Device Firmware Update Mode\n"
"To exit DFU mode, please reset device yourself.", x, y);
// 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 void
select_lever_mode(int mode)
{
if (uistat.lever_mode != mode) {
uistat.lever_mode = mode;
redraw_request |= REDRAW_FREQUENCY | REDRAW_MARKER;
}
}
// type of menu item
enum {
MT_NONE, // sentinel menu
// MT_BLANK, // blank menu (nothing draw)
MT_SUBMENU, // enter to submenu
MT_CALLBACK, // call user function
MT_ADV_CALLBACK, // adv call user function
MT_CANCEL, // menu, step back on one level up
MT_TITLE, // Title
MT_KEYPAD,
MT_REPEATS = 0x08,
MT_ICON = 0x10,
MT_HIGH = 0x20, // Only applicable to high mode
MT_LOW = 0x40, // Only applicable to low mode
MT_FORM = 0x80, // Large button menu
};
//#define MT_BACK 0x40
//#define MT_LEAVE 0x20
#define MT_MASK(x) (0x7 & (x))
#define DATA_STARTS_REPEATS(S,R) (((R)<<4)+(S))
#define MT_CUSTOM_LABEL 0
// Call back functions for MT_CALLBACK type
typedef void (*menuaction_cb_t)(int item, uint16_t data);
#define UI_FUNCTION_CALLBACK(ui_function_name) void ui_function_name(int item, uint16_t data)
typedef void (*menuaction_acb_t)(int item, uint16_t data, ui_button_t *b);
#define UI_FUNCTION_ADV_CALLBACK(ui_function_name) void ui_function_name(int item, uint16_t data, ui_button_t *b)
static freq_t
get_marker_frequency(int marker)
{
if (marker < 0 || marker >= MARKERS_MAX)
return 0;
if (!markers[marker].enabled)
return 0;
return getFrequency(markers[marker].index);
}
static UI_FUNCTION_CALLBACK(menu_marker_op_cb)
{
(void)item;
freq_t freq = get_marker_frequency(active_marker);
if (freq == 0)
return; // no active marker
switch (data) {
case 0: /* MARKER->START */
case 1: /* MARKER->STOP */
case 2: /* MARKER->CENTER */
set_sweep_frequency(data, freq);
if (data == 2) {
uistat.lever_mode = LM_SPAN;
uistat.auto_center_marker = true;
}
break;
case 3: /* MARKERS->SPAN */
{
if (previous_marker == MARKER_INVALID || active_marker == previous_marker) {
// if only 1 marker is active, keep center freq and make span the marker comes to the edge
freq_t center = get_sweep_frequency(ST_CENTER);
freq_t span = center > freq ? center - freq : freq - center;
set_sweep_frequency(ST_SPAN, span * 2);
} else {
// if 2 or more marker active, set start and stop freq to each marker
freq_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: // marker -> ref level
{
float l = actual_t[markers[active_marker].index];
#if 1
user_set_reflevel(l);
#else
float s_max = value(l)/setting.scale;
user_set_reflevel(setting.scale*(floorf(s_max)+2));
#endif
}
break;
#ifdef __VNA__
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;
#endif
}
menu_move_back(true);
redraw_request |= REDRAW_CAL_STATUS;
//redraw_all();
}
static UI_FUNCTION_ADV_CALLBACK(menu_markers_reset_acb)
{
(void)item;
if(b){
b->param_1.i = data;
return;
}
markers_reset();
for (uint8_t i = 0; i< data; i++) {
markers[i].mtype = M_TRACKING;
markers[i].enabled = M_ENABLED;
}
}
static UI_FUNCTION_CALLBACK(menu_marker_search_cb)
{
(void)item;
int i = -1;
if (active_marker == MARKER_INVALID)
return;
markers[active_marker].mtype &= ~M_TRACKING;
switch (data) {
case 0: /* search Left */
i = marker_search_left_min(active_marker);
break;
case 1: /* search right */
i = marker_search_right_min(active_marker);
break;
case 2: /* search Left */
i = marker_search_left_max(active_marker);
break;
case 3: /* search right */
i = marker_search_right_max(active_marker);
break;
case 4: /* peak search */
i = marker_search_max(active_marker);
break;
}
if (i != -1) {
markers[active_marker].index = i;
if (data > 1) // Maximum related
interpolate_maximum(active_marker);
else
markers[active_marker].frequency = getFrequency(i);
}
redraw_marker(active_marker);
// if (setting.jog_jump)
// select_lever_mode(LM_SEARCH); // Jump from maximum to maximum
// else
select_lever_mode(LM_MARKER); // Allow any position with level
}
#if 0
static UI_FUNCTION_ADV_CALLBACK(menu_marker_tracking_acb){
(void)item;
(void)data;
if (active_marker == MARKER_INVALID) return;
if(b){
b->icon = markers[active_marker].mtype & M_TRACKING ? BUTTON_ICON_CHECK : BUTTON_ICON_NOCHECK;
return;
}
markers[active_marker].mtype ^= M_TRACKING;
}
#endif
#ifdef __VNA__
static void
menu_marker_smith_cb(int item, uint8_t data)
{
(void)item;
marker_smith_format = data;
redraw_marker(active_marker);
draw_menu();
}
#endif
static void
active_marker_select(int item) // used only to select an active marker from the modify marker selection menu
{
if (item == -1) {
active_marker = previous_marker;
previous_marker = MARKER_INVALID;
if (active_marker == MARKER_INVALID) {
choose_active_marker();
}
} else {
if (previous_marker != active_marker) {
previous_marker = active_marker;
active_marker = item;
} else {
active_marker = item;
}
}
}
//----------------------- iu_sa.c inserted -------------------
#define FORM_ICON_WIDTH 16
#define FORM_ICON_HEIGHT 16
static const uint8_t left_icons [] =
{
#define I_EMPTY (0*16)
_BMP16(0b0000000000000000),
_BMP16(0b0000000000000000),
_BMP16(0b0000000000000000),
_BMP16(0b0000000000000001),
_BMP16(0b0000000000000001),
_BMP16(0b0000000000000001),
_BMP16(0b0000000000000000),
_BMP16(0b0000000000000000),
_BMP16(0b0000000000000000),
_BMP16(0b0000000000000000),
_BMP16(0b0000000000000001),
_BMP16(0b0000000000000001),
_BMP16(0b0000000000000001),
_BMP16(0b0000000000000000),
_BMP16(0b0000000000000000),
_BMP16(0b0000000000000000),
#define I_HIGH_INPUT (1*16)
// +----------------+
_BMP16(0b0000000000000000), // | |
_BMP16(0b0000000000000000), // | |
_BMP16(0b0000000001100000), // | ** |
_BMP16(0b0000000000111001), // | *** *|
_BMP16(0b0000111111111111), // | *************|
_BMP16(0b0000000000111001), // | *** *|
_BMP16(0b0000000001100000), // | ** |
_BMP16(0b0000000000000000), // | |
_BMP16(0b0000000000000000), // | |
_BMP16(0b0000000000000000), // | |
_BMP16(0b0000000000000001), // | |
_BMP16(0b0000000000000001), // | |
_BMP16(0b0000000000000001), // | |
_BMP16(0b0000000000000000), // | |
_BMP16(0b0000000000000000), // | |
_BMP16(0b0000000000000000), // | |
// +----------------+
#define I_LOW_INPUT (2*16)
// +----------------+
_BMP16(0b0000000000000000), // | |