forked from g0orx/SDTVer050.0
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSDT.h
2623 lines (2338 loc) · 96.6 KB
/
SDT.h
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
#ifndef BEENHERE
#define BEENHERE
#define USE_JSON
//======================================== User section that might need to be changed ===================================
#include "MyConfigurationFile.h" // This file name should remain unchanged
#define VERSION "V050.2" // Change this for updates. If you make this longer than 9 characters, brace yourself for surprises
#define UPDATE_SWITCH_MATRIX 0 // 1 = Yes, redo the switch matrix values, 0 = leave switch matrix values as is from the last change
struct maps
{
char mapNames[50];
float lat;
float lon;
};
extern struct maps myMapFiles[];
#if defined(V12HWR)
#include "RF_CONTROL.h"
#endif // V12HWR
#ifdef DEBUG
#define DEBUG_MESSAGES
#endif
#ifdef DEBUG_MESSAGES
#define Debug(x) Serial.println(x)
#else
#define Debug(x)
#endif
// KI3P: added support for lowpass and bandpass filter boards
#if defined(K9HZ_LPF)
#include "K9HZ_LPF_Control.h"
#endif // K9HZ_LPF
#if defined(V12BPF)
#include "BPF_Control.h"
#endif // V12BPF
//======================================== Library include files ========================================================
#include <Adafruit_GFX.h>
#include "Adafruit_MCP23X17.h"
#include "Fonts/FreeMonoBold24pt7b.h"
#include "Fonts/FreeMonoBold18pt7b.h"
#include "Fonts/FreeMono24pt7b.h"
#include "Fonts/FreeMono9pt7b.h"
//#include <Audio.h> //https://github.com/chipaudette/OpenAudio_ArduinoLibrary
#include <OpenAudio_ArduinoLibrary.h> // AFP 11-01-22
#include <TimeLib.h> // Part of Teensy Time library
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <Metro.h>
#include <Bounce.h>
#include <arm_math.h>
#include <arm_const_structs.h>
// ============ AFP 09-04-23 #include modified Si5351 library
// == Modified Si linbrary must be included in folder with T41 code
#include "si5351.h"
//#include <si5351.h> // https://github.com/etherkit/Si5351Arduino
// Add define variables for drive current and load capacitance
#define SI5351_LOAD_CAPACITANCE SI5351_CRYSTAL_LOAD_8PF
#define SI5351_DRIVE_CURRENT SI5351_DRIVE_2MA
#include <RA8875.h> // https://github.com/mjs513/RA8875/tree/RA8875_t4
#if defined(G0ORX_FRONTPANEL)
#include "G0ORX_Rotary.h"
#else
#include <Rotary.h> // https://github.com/brianlow/Rotary
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <util/crc16.h> // mdrhere
#include <utility/imxrt_hw.h> // for setting I2S freq, Thanks, FrankB!
#include <EEPROM.h>
//======================================== Symbolic Constants for the T41 ===================================================
#define RIGNAME "T41-EP SDT"
#define NUMBER_OF_SWITCHES 18 // Number of push button switches. 16 on older boards
#if !defined(EXCLUDE_BEARING) && !defined(EXCLUDE_BODE)
#define TOP_MENU_COUNT 14 // Menus to process AFP 09-27-22, JJP 7-8-23 AFP 04-12-2
#elif defined(EXCLUDE_BEARING) && !defined(EXCLUDE_BODE)
#define TOP_MENU_COUNT 13
#elif !defined(EXCLUDE_BEARING) && defined(EXCLUDE_BODE)
#define TOP_MENU_COUNT 13
#elif defined(EXCLUDE_BEARING) && defined(EXCLUDE_BODE)
#define TOP_MENU_COUNT 12 // removed Bearing and Bode for memory space
#endif // EXCLUDE_BEARING / EXCLUDE_BODE
#define RIGNAME_X_OFFSET 570 // Pixel count to rig name field // Says we are using a Teensy 4 or 4.1
#define RA8875_DISPLAY 1 // Comment out if not using RA8875 display
#define TEMPMON_ROOMTEMP 25.0f
#define SD_CS BUILTIN_SDCARD // Works on T_3.6 and T_4.1 ...
#define MAX_SD_ITEMS 184 // Number of discrete data items written to EEPROM
//#define STORE_SWITCH_VALUES // Uncomment to save the analog switch values for your push button matrix
#define OFF 0
#define ON 1
//================================ mapping globals and Symbolic constants ================
#define BUFFPIXEL 20 // Use buffer to read image rather than 1 pixel at a time
#define DEGREES2RADIANS 0.01745329
#define RADIANS2DEGREES 57.29578
#define PI_BY_180 0.01745329
#define VALID_EEPROM_DATA 1
#define INVALID_EEPROM_DATA 0
// ================== Use one of the following encoder configurations. Set in MyConfiguratonFile.h
//=================== Encoder pins Jack Purdum W8TEE September 25, 2023
#ifdef FOURSQRP
#define VOLUME_ENCODER_A 2
#define VOLUME_ENCODER_B 3
#define FILTER_ENCODER_A 16
#define FILTER_ENCODER_B 15
#define FINETUNE_ENCODER_A 4
#define FINETUNE_ENCODER_B 5
#define TUNE_ENCODER_A 14
#define TUNE_ENCODER_B 17
#else
#define VOLUME_ENCODER_A 2//5 //2
#define VOLUME_ENCODER_B 3//4 //3
#define FILTER_ENCODER_A 15
#define FILTER_ENCODER_B 14
#define FINETUNE_ENCODER_A 4//17 // 4
#define FINETUNE_ENCODER_B 5//16 // 5
#define TUNE_ENCODER_A 17//2 //16
#define TUNE_ENCODER_B 16//3 // 17
#endif
//
//#define DEBUG_JACK
//#define DEBUG1
//#define DEBUG2
//#define DEBUG3
//#define DEBUG4
//#define DEBUG6
//#define DEBUG7
//#define DEBUG8
//#define DEBUG9
#define NUMBER_OF_ELEMENTS(x) (sizeof(x)/sizeof(x[0])) // Typeless way to find number of elements in the x[] array
#define NEW_SI5351_FREQ_MULT 1UL
//======================================== Symbolic constants ==========================================================
// These constants are used by the voltage divider network so only 1 analog pin is used for the 16 option switches. These may need
// to be changed for the exact value for your system. They are initialized in the INO file.
#define BUSY_ANALOG_PIN 39 // This is the analog pin that controls the 18 switches
#define NOTHING_TO_SEE_HERE 950 // If the analog pin is greater than this value, nothing's going on
#define BOGUS_PIN_READ -1 // If no push button read
#define WIGGLE_ROOM 20 // This is the maximum value that can added to a BUSY_ANALOG_PIN pin read value of a push
// button and still have the switch value be associated with the correct push button.
#define SWITCH_DEBOUNCE_DELAY 50L // Milliseconds for the switch to settle down
#define AUDIO_PLOT_CEILING 119 // (SPECTRUM_BOTTOM - AUDIO_SPECTRUM_TOP)
#define MAX_FAVORITES 13 // Max number of favorite frequencies stored in EEPROM
#define PRIMARY_MENU 0
#define SECONDARY_MENU 1
#define SELECTED_INDEX 3 // This is the index for MENU_OPTION_SELECT
#define PRIMARY_MENU_X 0
#define SECONDARY_MENU_X 250
#define MENUS_Y 0
#define EACH_MENU_WIDTH 260
#define BOTH_MENU_WIDTHS (EACH_MENU_WIDTH * 2 + 30)
/*
#define MENU_OPTION_SELECT 0 // Switches changed for new menu system JJP 3/10/24
#define MAIN_MENU_UP 1
#define BAND_UP 2
#define ZOOM 3
#define RESET_TUNING 4
#define BAND_DN 5
#define SET_MODE 6
#define DEMODULATION 7
#define MAIN_TUNE_INCREMENT 8
#define NOISE_REDUCTION 9
#define NOTCH_FILTER 10
#define FINE_TUNE_INCREMENT 11
#define FILTER 12
#define DECODER_TOGGLE 13
#define UNUSED_1 14
#define UNUSED_2 15
#define UNUSED_3 16
#define UNUSED_4 17
#define CAL_CHANGE_TYPE 16
#define CAL_CHANGE_INC 17
*/
#define MENU_BAILOUT_VALUE 17 // Used to exit from main menu list
#define MENU_OPTION_SELECT 0 // These are the expected values from the switch ladder
#define MAIN_MENU_UP 1
#define BAND_UP 2
#define ZOOM 3
#define MAIN_MENU_DN 4
#define BAND_DN 5
#define FILTER 6
#define DEMODULATION 7
#define SET_MODE 8
#define NOISE_REDUCTION 9
#define NOTCH_FILTER 10
#define NOISE_FLOOR 11
#define FINE_TUNE_INCREMENT 12
#define DECODER_TOGGLE 13
#define MAIN_TUNE_INCREMENT 14
#define RESET_TUNING 15
#define DDE 16
#define BEARING 17
#define CAL_CHANGE_TYPE 16
#define CAL_CHANGE_INC 17
#define CAL_TOGGLE_OUTPUT 14
#define CAL_AUTOCAL 13
#define CAL_TOGGLE_TX_STATE 12
#define CAL_TOGGLE_ATTENUATOR 15
#define BODE_SAVE 15
#define BODE_BAND 16
#define BODE_REF 17
#define BODE_DONE 14
#define BEARING_ERASE 16
#define BEARING_DONE 17
//=======================================================
#define XPIXELS 800 // This is for the 5.0" display
#define YPIXELS 480
#define PIXELHEIGHT 20 // Used in fillRec() to erase a line
#define CHAR_HEIGHT 32
#define PIXELS_PER_EQUALIZER_DELTA 10 // Number of pixeks per detent of encoder for equalizer changes
#define PIXELS_PER_AUDIO_DELTA 10
#define SPECTRUM_LEFT_X 3 // Used to plot left edge of spectrum display AFP 12-14-21
#define WATERFALL_LEFT_X SPECTRUM_LEFT_X
#define SPECT_RES_92 512/92000
#define CLIP_AUDIO_PEAK 115 // The pixel value where audio peak overwrites S-meter
#define SPECTRUM_RES 512
#define SPECTRUM_TOP_Y 100 // Start of spectrum plot space
#define SPECTRUM_HEIGHT 150 // This is the pixel height of spectrum plot area without disturbing the axes
#define SPECTRUM_BOTTOM (SPECTRUM_TOP_Y + SPECTRUM_HEIGHT - 3) // 247 = 100 + 150 - 3
#define AUDIO_SPECTRUM_TOP 129
#define AUDIO_SPECTRUM_BOTTOM SPECTRUM_BOTTOM
#define MAX_WATERFALL_WIDTH 512 // Pixel width of waterfall
#define MAX_WATERFALL_ROWS 170 // Waterfall rows
#define WATERFALL_RIGHT_X (WATERFALL_LEFT_X + MAX_WATERFALL_WIDTH) // 3 + 512
#define WATERFALL_TOP_Y (SPECTRUM_TOP_Y + SPECTRUM_HEIGHT + 5) // 130 + 120 + 5 = 255
#define FIRST_WATERFALL_LINE (WATERFALL_TOP_Y + 20) // 255 + 35 = 290
#define WATERFALL_BOTTOM (FIRST_WATERFALL_LINE + MAX_WATERFALL_ROWS) // 290 + 170 = 460
#define TEMP_X_OFFSET 15
#define TEMP_Y_OFFSET 465 // 480 * 0.97 = 465
#define AGC_Y_OFFSET 292
#define AGC_X_OFFSET 680
#define VOLUME_Y_OFFSET 180
#define INCREMENT_X WATERFALL_RIGHT_X + 25
#define INCREMENT_Y WATERFALL_TOP_Y + 70
#define SPECTRUMCORNER_X INCREMENT_X
#define SPECTRUMCORNER_Y INCREMENT_Y
#define INFORMATION_WINDOW_X WATERFALL_RIGHT_X + 25 // 512 + 25 = 537
#define INFORMATION_WINDOW_Y WATERFALL_TOP_Y + 37 // 255 + 37 = 292
#define BAND_INDICATOR_X WATERFALL_RIGHT_X + 25
#define BAND_INDICATOR_Y WATERFALL_TOP_Y + 37 // 292
#define OPERATION_STATS_X 130
#define OPERATION_STATS_Y 75
#define BAND_SUMMARY_X BAND_INDICATOR_X
#define BAND_SUMMARY_Y 150
#define START_BAND_DATA_X TEMP_X_OFFSET
#define START_BAND_DATA_Y YPIXELS * 0.25
#define X_R_STATUS_X 730
#define X_R_STATUS_Y 70
#define RECEIVE_STATE 1
#define TRANSMIT_STATE 0
#define SMETER_X WATERFALL_RIGHT_X + 16
#define SMETER_Y YPIXELS * 0.22 // 480 * 0.22 = 106
#define SMETER_BAR_HEIGHT 18
#define SMETER_BAR_LENGTH 180
#define SPECTRUM_NOISE_FLOOR (SPECTRUM_TOP_Y + SPECTRUM_HEIGHT - 3)
#define TIME_X (XPIXELS * 0.83) // Upper-left corner for time --G0ORX
#define TIME_Y (YPIXELS * 0.07)
#define WHICH_SIDEBAND_X (XPIXELS * 0.70)
#define WHICH_SIDEBAND_Y (YPIXELS * 0.20)
#define FILTER_PARAMETERS_X (XPIXELS * 0.22)
#define FILTER_PARAMETERS_Y (YPIXELS * 0.213)
#define DEFAULT_EQUALIZER_BAR 100 // Default equalizer bar height
#define FREQUENCY_X 5
#define FREQUENCY_Y 45
#define FREQUENCY_X_SPLIT 280
#define VFO_A 0
#define VFO_B 1
#define VFO_SPLIT 2
#define VFOA_PIXEL_LENGTH 275
#define VFOB_PIXEL_LENGTH 280
#define FREQUENCY_PIXEL_HI 45
#define SPLIT_INCREMENT 500L
// Offsets for status info
#define FIELD_OFFSET_X WATERFALL_RIGHT_X + 118 // X coordinate for field
#define NOTCH_X WATERFALL_RIGHT_X + 58
#define NOTCH_Y WATERFALL_TOP_Y + 90
#define NOISE_REDUCE_X WATERFALL_RIGHT_X + 58
#define NOISE_REDUCE_Y WATERFALL_TOP_Y + 110
//#define AGC_Y_OFFSET 292
//#define AGC_X_OFFSET 680
#define ZOOM_X 705
#define ZOOM_Y 365
//#define RF_GAIN_X 681
//#define RF_GAIN_Y 365
#define RF_GAIN_X 681
#define RF_GAIN_Y WATERFALL_TOP_Y + 90
#define EQUALIZATION_X WATERFALL_RIGHT_X + 35
#define EQUALIZATION_Y WATERFALL_TOP_Y + 130
#define SD_X 707
#define SD_Y 385
#define COMPRESSION_X WATERFALL_RIGHT_X + 33
#define COMPRESSION_Y WATERFALL_TOP_Y + 150
#define DECODER_X WATERFALL_RIGHT_X + 43 // 512 + 43 = 555
#define DECODER_Y WATERFALL_TOP_Y + 190 // 255 + 190 = 345
#define WPM_X WATERFALL_RIGHT_X + 58
#define WPM_Y WATERFALL_TOP_Y + 170
#define NR_X_OFF WATERFALL_RIGHT_X + 80
#define NR_Y_OFF WATERFALL_TOP_Y + 190
#define VOLUME_INFO_FIELD_X 540
#define VOLUME_INFO_FIELD_Y 292
#define SAM_PLL_HILBERT_STAGES 7 // AFP 11-02-22
#define OUT_IDX (3 * SAM_PLL_HILBERT_STAGES) // AFP 11-02-22
#define MAX_DECODE_CHARS 32 // Max chars that can appear on decoder line. Increased to 32. KF5N October 29, 2023
#define DECODER_BUFFER_SIZE 128 // Max chars in binary search string with , . ?
#define DECODER_CAP_VALUE 6.0
#define DITLENGTH_DELTA 5 // Number of milliseconds to change ditLEngth with encoder
#define HISTOGRAM_ELEMENTS 750
#define LOWEST_ATOM_TIME 20 // 60WPM has an atom of 20ms
#define HIGHEST_ATOM_TIME 240 // 5WPM has an atom of 240ms
#define DIT_WEIGHT 0.3 // Previous values account for 90% of average
#define AVERAGE_DIT_WEIGHT 0.7 // The number above and this one must equal 1.0
#define DITLENGTH_OBSERVATIONS 10 // Number of ditlength observations to compute average
#define ADAPTIVE_SCALE_FACTOR 0.8 // The amount of old histogram values are presesrved
#define SCALE_CONSTANT (1.0 / (1.0 - ADAPTIVE_SCALE_FACTOR)) // Insure array has enough observations to scale
#define FILTER_WIDTH 25 // The default filter highlight in spectrum displah
#define ZOOM_2X_BIN_COUNT 187.5 // The 2x bin count for display
#define MAX_AUDIO_VOLUME 100
#define MIN_AUDIO_VOLUME 16 //yours might be different. On my rig, this is where the band noise disappears.
#define AUDIO_POST_PROCESSOR_BANDS 8 // Number of audio segments
#define EEPROM_FAVORITES_X 100
#define EEPROM_FAVORITES_Y 50
#define BANDWIDTH_INDICATOR_Y SPECTRUM_BOTTOM
#define FAST_TUNE_CENTERLINE ((MAX_WATERFALL_WIDTH + SPECTRUM_LEFT_X+6) / 2)
#define DO_NOTHING -1
#define FLOAT_PRECISION 6 // Assumed precision for a float
#define BUFFER_SINE_COUNT 8 // Leads to a 750Hz signal
#define EQUALIZER_CELL_COUNT 14
#define AUDIO_CELL_COUNT 8
#define USE_LOG10FAST
#define MP3
#define TEMPMON_ROOMTEMP 25.0f
#define ENCODER_DELAY 100L // Menu options scroll too fast!
//--------------------- decoding stuff
#define FFT_LENGTH 512
#define NOISE_SAMPLE_SIZE 500
#define SD_MULTIPLIER 3
#define NOISE_MULTIPLIER 0.5 // Signal must be this many time greater than the noise floor
#define STARTING_DITLENGTH 80 // dit length for 15wpm
#define BLACK 0x0000 /* 0, 0, 0 */
#define RA8875_BLUE 0x000F /* 0, 0, 128 */
#define DARK_GREEN 0x03E0 /* 0, 128, 0 */
#define DARKCYAN 0x03EF /* 0, 128, 128 */
#define MAROON 0x7800 /* 128, 0, 0 */
#define PURPLE 0x780F /* 128, 0, 128 */
#define OLIVE 0x7BE0 /* 128, 128, 0 */
#define RA8875_LIGHT_GREY 0xC618 /* 192, 192, 192 */
#define DARK_RED tft.Color565(64,0,0)
#define DARKGREY 0x7BEF /* 128, 128, 128 */
#define BLUE 0x001F /* 0, 0, 255 */
#define RA8875_GREEN 0x07E0 /* 0, 255, 0 */
#define CYAN 0x07FF /* 0, 255, 255 */
#define RED 0xF800 /* 255, 0, 0 */
#define MAGENTA 0xF81F /* 255, 0, 255 */
#define YELLOW 0xFFE0 /* 255, 255, 0 */
#define WHITE 0xFFFF /* 255, 255, 255 */
#define ORANGE 0xFD20 /* 255, 165, 0 */
#define RA8875_GREENYELLOW 0xAFE5 /* 173, 255, 47 */
#define PINK 0xF81F
#define FILTER_WIN 0x10 // Color of SSB filter width
#ifndef FLASHMEM
#define FLASHMEM
#endif
#include <utility/imxrt_hw.h> // for setting I2S freq, Thanks, FrankB!
#define WFM_SAMPLE_RATE 256000.0f
//#define TIMEZONE "EST: " // Set for eastern time
//#define DEFAULTFREQINCREMENT 1000L //Values 10, 50, 100, 250, 1000, 10000 AFP 09-26-22
//#define FAST_TUNE_INCREMENT 50L
#define DEFAULTFREQINDEX 4 // Index 10Hz=> 0, 50Hz=> 1, 100Hz=> 2, 250Hz=> 3,
// 1000Hz=> 4, 10000Hz=> 5, 100000=> 6, 1000000=> 7
#define MAX_FREQ_INDEX 8
#define TEMPMON_ROOMTEMP 25.0f
#define MAX_WPM 60
#define MAX_TONE 1000
#define MIN_TONE 300
#define ENCODER_FACTOR 0.25F // use 0.25f with cheap encoders that have 4 detents per step,
// for other encoders or libs we use 1.0f
#define MAX_ZOOM_ENTRIES 5
//#define FREQ_SEP_CHARACTER ','
//========================================================= Pin Assignments =====================================
//========================================= Pins 0 and 1 are usually reserved for the USB COM port communications
//========================================= On the Teensy 4.1 board, pins GND, 0-12, and pins 13-23, 3.3V, GND, and
//========================================= Vin are "covered up" by the Audio board. However, not all of those pins are
//========================================= actually used by the board. See: https://www.pjrc.com/store/teensy3_audio.html
// KI3P: Teensy shutdown pins
#define BEGIN_TEENSY_SHUTDOWN 0
#define SHUTDOWN_COMPLETE 1
void ShutdownTeensy( void );
//========================================= Display pins
#define BACKLIGHT_PIN 6 // unfortunately connected to 3V3 in DO7JBHs PCB
#define TFT_DC 9
#define TFT_CS 10
#define TFT_MOSI 11
#define TFT_MISO 12
#define TFT_SCLK 13
#define TFT_RST 255
//========================================= Filter Board pins
#define FILTERPIN80M 30 // 80M filter relay
#define FILTERPIN40M 31 // 40M filter relay
#define FILTERPIN20M 28 // 20M filter relay
#define FILTERPIN15M 29 // 15M filter relay
#define RXTX 22 // Transmit/Receive (H=TX,L=RX)
#define CW_ON_OFF 33 // CW on / off (H=ON,L=OFF) (V12 hardware)
#define XMIT_MODE 34 // Transmit mode (H=SSB,L=CW) (V12 hardware)
#define KEY1 35 // Tip for Straight key
#define KEY2 36 // Ring
#define PTT 37 // Transmit/Receive
// KI3P: added mode definitions to make programming easier (V12 hardware)
#define XMIT_SSB 1
#define XMIT_CW 0
#define CAL_OFF 0
#define CAL_ON 1
#define CW_OFF 0
#define CW_ON 1
#define CAL_POWER_LEVEL_W 10
// KI3P: V12 does not have a hardware audio amp mute. It reuses the pin for calibration
#ifdef V12HWR
#define CAL 38 // RX board calibration control (H=CAL,L=normal)
#else
#define MUTE 38 // Mute Audio, HIGH = "On" Audio available from Audio PA, LOW = Mute audio
#endif
//========================================= Switch pins
#define BAND_MENUS 100 // encoder2 button = button3SW
#define BAND_PLUS 101 // BAND+ = button2SW
#define CHANGE_INCREMENT 102 // this is the pushbutton pin of the tune encoder
#define CHANGE_FILTER 103 // this is the pushbutton pin of the filter encoder
#define CHANGE_MODE 104 // Change mode
#define CHANGE_MENU2 105 // this is the pushbutton pin of encoder 3
#define MENU_MINUS 106 // Menu decrement
#define MENU_PLUS 107 // this is the menu button pin
#define CHANGE_NOISE 108 // this is the pushbutton pin of NR
#define CHANGE_DEMOD 109 // this is the push button for demodulation
#define CHANGE_ZOOM 110 // Push button for display zoom feature
#define SET_FREQ_CURSOR 111 // Push button for frequency Cursor feature was 39 for Al
#define NO_MENUS_ACTIVE 0 // No menus displayed
#define PRIMARY_MENU_ACTIVE 1 // A primary menu is active
#define SECONDARY_MENU_ACTIVE 2 // Both primary and secondary menus active
//========================================= Keyer pins
#define KEYER_DAH_INPUT_RING 35 // Ring connection for keyer -- default for righthanded user
#define KEYER_DIT_INPUT_TIP 36 // Tip connection for keyer
#define OPTO_OUTPUT 24 // To optoisolator and keyed circuit
#define STRAIGHT_KEY 0
#define KEYER 1
#define KEYONTIME 500 // AFP17-22 key on time
//========================================================= End Pin Assignments =================================
//===============================================================================================================
#define TMS0_POWER_DOWN_MASK (0x1U)
#define TMS0_POWER_DOWN_SHIFT (0U)
#define TMS1_MEASURE_FREQ(x) (((uint32_t)(((uint32_t)(x)) << 0U)) & 0xFFFFU)
#define TMS0_ALARM_VALUE(x) (((uint32_t)(((uint32_t)(x)) << 20U)) & 0xFFF00000U)
#define TMS02_LOW_ALARM_VALUE(x) (((uint32_t)(((uint32_t)(x)) << 0U)) & 0xFFFU)
#define TMS02_PANIC_ALARM_VALUE(x) (((uint32_t)(((uint32_t)(x)) << 16U)) & 0xFFF0000U)
//#define MAX_NUMCOEF (FFT_LENGTH / 2) + 1 // This is alread defined in AudioFilterConvolution_F32.h at line 110
#undef round
#undef PI
#undef HALF_PI
#undef TWO_PI
#define PI 3.1415926535897932384626433832795f
#define HALF_PI 1.5707963267948966192313216916398f
#define TWO_PI 6.283185307179586476925286766559f
#define TPI TWO_PI
#define PIH HALF_PI
#define FOURPI (2.0f * TPI)
#define SIXPI (3.0f * TPI)
#define Si_5351_clock SI5351_CLK2
#define Si_5351_crystal 25000000L
#define MASTER_CLK_MULT 4ULL // QSD frontend requires 4x clock
#define WITHTERM 1
#define SIGNAL_TAU 0.1
#define ONEM_SIGNAL_TAU (1.0 - SIGNAL_TAU)
#define CW_TIMEOUT 3 // Time, in seconds, to trigger display of last Character received
#define ONE_SECOND (12000 / cw_decoder_config.blocksize) // sample rate / decimation rate / block size
#define SSB_MODE 0
#define CW_MODE 1
#define RECEIVE_MODE 2
#define SSB_RECEIVE 0
#define SSB_XMIT 1
#define CW_RECEIVE 2
#define CW_XMIT 3
// This second set of states are for the loop() modal state machine.
#define SSB_RECEIVE_STATE 0
#define SSB_TRANSMIT_STATE 1
#define CW_RECEIVE_STATE 2
#define CW_TRANSMIT_STRAIGHT_STATE 3
#define CW_TRANSMIT_KEYER_STATE 4
extern int radioState, lastState; // Used by the loop to monitor current state.
#define DECODER_STATE 0 // 0 = off, 1 = on
#define DECODE_OFF 0
#define DECODE_ON 1
#define DIGIMODE_OFF 0
#define CW 1
#define EFR 3
#define DCF77 5
#define SPECTRUM_ZOOM_MIN 0
#define SPECTRUM_ZOOM_1 0
#define SPECTRUM_ZOOM_2 1
#define SPECTRUM_ZOOM_4 2
#define SPECTRUM_ZOOM_8 3
#define SPECTRUM_ZOOM_16 4
#define SPECTRUM_ZOOM_MAX 4
#define SAMPLE_RATE_MIN 6
#define SAMPLE_RATE_8K 0
#define SAMPLE_RATE_11K 1
#define SAMPLE_RATE_16K 2
#define SAMPLE_RATE_22K 3
#define SAMPLE_RATE_32K 4
#define SAMPLE_RATE_44K 5
#define SAMPLE_RATE_48K 6
#define SAMPLE_RATE_50K 7
#define SAMPLE_RATE_88K 8
#define SAMPLE_RATE_96K 9
#define SAMPLE_RATE_100K 10
#define SAMPLE_RATE_101K 11
#define SAMPLE_RATE_176K 12
#define SAMPLE_RATE_192K 13
#define SAMPLE_RATE_234K 14
#define SAMPLE_RATE_256K 15
#define SAMPLE_RATE_281K 16 // ??
#define SAMPLE_RATE_353K 17
#define SAMPLE_RATE_MAX 15
#define TEMPMON_ROOMTEMP 25.0f
#define DEMOD_MIN 0
#define DEMOD_USB 0
#define DEMOD_LSB 1
#define DEMOD_AM 2
#define DEMOD_SAM 3
#define DEMOD_MAX 3 // AFP 11-03-22
#define DEMOD_IQ 4
#define DEMOD_DCF77 29 // set the clock with the time signal station DCF77
#define BROADCAST_BAND 0
#define HAM_BAND 1
#define MISC_BAND 2
#define BUFFER_SIZE 128
#define NOTCHPOS spectrum_y + 6
#define NOTCHL 15
#define NOTCHCOLOUR RA8875_YELLOW
// Menus !
#define MENU_F_HI_CUT 0
#define MENU_SPECTRUM_ZOOM 1
#define MENU_SAMPLE_RATE 2
#define MENU_SAVE_EEPROM 3
#define MENU_LOAD_EEPROM 4
#define MENU_LPF_SPECTRUM 5
#define MENU_SPECTRUM_OFFSET 6
#define MENU_SPECTRUM_DISPLAY_SCALE 7
#define MENU_IQ_AMPLITUDE 8
#define MENU_IQ_PHASE 9
#define MENU_CALIBRATION_FACTOR 10
#define MENU_CALIBRATION_CONSTANT 11
#define MENU_TIME_SET 12
#define MENU_RESET_CODEC 13
#define MENU_SHOW_SPECTRUM 14
#define FIRST_MENU 0
#define LAST_MENU 16 //=================== AFP 04-12-24 V012 RF Atten Plot
#define START_MENU 0
#define MENU_RF_GAIN 15
#define MENU_RF_ATTENUATION 16
#define MENU_BASS 17
#define MENU_MIDBASS 18
#define MENU_MID 19
#define MENU_MIDTREBLE 20
#define MENU_TREBLE 21
#define MENU_NOTCH_1 25
#define MENU_NOTCH_1_BW 26
#define MENU_AGC_MODE 27
#define MENU_AGC_THRESH 28
#define MENU_AGC_DECAY 29
#define MENU_AGC_SLOPE 30
#define MENU_ANR_NOTCH 31
#define MENU_ANR_TAPS 32
#define MENU_ANR_DELAY 33
#define MENU_ANR_MU 34
#define MENU_ANR_GAMMA 35
#define MENU_NB_THRESH 36
#define MENU_NB_TAPS 37
#define MENU_NB_IMPULSE_SAMPLES 38
#define MENU_BIT_NUMBER 39
#define MENU_F_LO_CUT 40
#define MENU_NR_PSI 41
#define MENU_NR_ALPHA 42
#define MENU_NR_BETA 43
#define MENU_NR_USE_X 44
#define MENU_NR_USE_KIM 45
#define MENU_LMS_NR_STRENGTH 46
#define MENU_CPU_SPEED 47
#define MENU_USE_ATAN2 48
#define MENU_NR_KIM 49
#define MENU_NR_SP 50
#define MENU_NR_LMS1 51
#define MENU_NR_LMS2 52
#define MENU_NR_OFF 53
#define FIRST_MENU2 15
#define LAST_MENU2 53
// AGC
#define AGC_OPTIONS 6 // Six options, 0 - 5
#define MAX_SAMPLE_RATE (24000.0)
#define MAX_N_TAU (8)
#define MAX_TAU_ATTACK (0.01)
#define RB_SIZE (int) (MAX_SAMPLE_RATE * MAX_N_TAU * MAX_TAU_ATTACK + 1)
//#define CONFIG_VERSION "mr1" //mdrhere ID of the E settings block, change if structure changes
//#define CONFIG_START 0 // Address start the EEPROM data. (emulated with size of 4K. Actual address managed in library)
//#define TERMCHRXWIDTH 9 // print stuff for text terminal
//#define TERMCHRYWIDTH 10
//#define TERMNROWS 4 // 15
//#define TERMNCOLS 28 // 34
#define CW_TEXT_START_X 5
#define CW_TEXT_START_Y 449 // 480 * 0.97 = 465 - height = 465 - 16 = 449
#define CW_MESSAGE_WIDTH MAX_WATERFALL_WIDTH // 512
#define CW_MESSAGE_HEIGHT 16 // tft.getFontHeight()
#if defined(V12HWR)
#define BAND_630M 0
#define BAND_160M 1
#define BAND_80M 2
#define BAND_60M 3
#define BAND_40M 4
#define BAND_30M 5
#define BAND_20M 6
#define BAND_17M 7
#define BAND_15M 8
#define BAND_12M 9
#define BAND_10M 10
#define BAND_6M 11
#define BAND_4M 12
#define BAND_2M 13
#define BAND_125CM 14
#define BAND_70CM 15
#define BAND_33CM 16
#define BAND_23CM 17
#define FIRST_BAND BAND_630M //WJS 2-18-24
#define LAST_BAND BAND_23CM //WJS 2-18-24
#define NUMBER_OF_BANDS 18 //WJS 2-18-24
#else
#define BAND_80M 0
#define BAND_40M 1
#define BAND_20M 2
#define BAND_17M 3
#define BAND_15M 4
#define BAND_12M 5
#define BAND_10M 6
#define FIRST_BAND BAND_80M
#define LAST_BAND BAND_10M //AFP 1-28-21
#define NUMBER_OF_BANDS 7 //AFP 1-28-21
#endif // V12HWR
//#define STARTUP_BAND BAND_40M //AFP 1-28-21
//#endif
//=== CW Filter ===
// G0ORX - moved to RF_CONTROL source
//extern Adafruit_MCP23X17 mcp;
//------------------------- Global CW Filter declarations ----------
extern arm_biquad_cascade_df2T_instance_f32 S1_CW_Filter;
extern arm_biquad_cascade_df2T_instance_f32 S1_CW_AudioFilter1; //AFP 10-18-22
extern arm_biquad_cascade_df2T_instance_f32 S1_CW_AudioFilter2; //AFP 10-18-22
extern arm_biquad_cascade_df2T_instance_f32 S1_CW_AudioFilter3; //AFP 10-18-22
extern arm_biquad_cascade_df2T_instance_f32 S1_CW_AudioFilter4; //AFP 10-18-22
extern arm_biquad_cascade_df2T_instance_f32 S1_CW_AudioFilter5; //AFP 10-18-22
extern float32_t CW_Filter_state[];
extern float32_t CW_AudioFilter1_state[]; //AFP 10-18-22
extern float32_t CW_AudioFilter2_state[]; //AFP 10-18-22
extern float32_t CW_AudioFilter3_state[]; //AFP 10-18-22
extern float32_t CW_AudioFilter4_state[]; //AFP 10-18-22
extern float32_t CW_AudioFilter5_state[]; //AFP 10-18-22
extern float32_t HP_DC_Filter_Coeffs[];
extern float32_t CW_AudioFilterCoeffs1[]; //AFP 10-18-22
extern float32_t CW_AudioFilterCoeffs2[]; //AFP 10-18-22
extern float32_t CW_AudioFilterCoeffs3[]; //AFP 10-18-22
extern float32_t CW_AudioFilterCoeffs4[]; //AFP 10-18-22
extern float32_t CW_AudioFilterCoeffs5[]; //AFP 10-18-22
#define IIR_CW_ORDER 8
#define IIR_CW_NUMSTAGES 4
extern float32_t CW_Filter_Coeffs[];
extern float32_t HP_DC_Filter_Coeffs[];
extern float32_t HP_DC_Filter_Coeffs2[]; // AFP 11-02-22
//=== end CW Filter ===
#define DISPLAY_S_METER_DBM 0
#define DISPLAY_S_METER_DBMHZ 1
#define N2 100
#define YTOP_LEVEL_DISP 73
#define ADC_BAR 10 // ADC Bar on left, DAC bar on right
#define DAC_BAR 100
#define ANR_DLINE_SIZE 512 //funktioniert nicht, 128 & 256 OK
#define MAX_LMS_TAPS 96
#define MAX_LMS_DELAY 256
#define NR_FFT_L 256
#define NB_FFT_SIZE FFT_LENGTH/2
#define TABLE_SIZE_64 64
#define EEPROM_BASE_ADDRESS 0U
#define CW_SHAPING_NONE 0
#define CW_SHAPING_RISE 1
#define CW_SHAPING_FALL 2
#define CW_SHAPING_ZERO 3
#if !defined(EXCLUDE_BODE)
//=================== AFP 03-30-24 V012 Bode Plot variables
extern int numBodePoints; //Bode
extern float BodePlotValues[]; //Bode
extern float BodePlotFreq[]; //Bode
extern float BodePlotValuesOld[]; //Bode
extern float BodePlotFreqOld[]; //Bode
extern float BodePlotValuesSave[];
extern float BodePlotFreqSave[];
extern float BodeValues[]; // Bode
extern float BodeFreq[];
extern float centerFreqBode;
extern float centerFreqBode;
extern long long pll_freqBode;
extern float BodeValuesMax;
extern float BodeValuesMaxFreq;
extern float BodeValuesRaw[];
extern float BodeFreqRaw[];
extern float32_t bodeResultR; //Bode
extern float32_t audioBodePlot;
extern float32_t audioBodeMax;
int BodeOptions();
void SetFreqBode(); //Bode
void BodePLotter();
void DrawBodePlotContainer(); // Bode
//void ReadFilterEncoder();
void MoveBodeCursor(); // Bode
void SetFreqBode(); // Bode
void ProcessIQDataBode(); //Bode
void SetDDSFreqBode();
long MoveStopFreqBode() ; //Bode
extern float32_t BodePlotterBPFCoeffs[]; // Bode
extern float32_t BodePlotterBPF_state[];
//extern float32_t DMAMEM float_buffer_LBode[];
extern int BodeFreqChange;
extern long stopFreqBode;
extern long stopFreqOld;
extern long freqStartBode;
extern long freqStopBode;
extern int numBodePoints;
extern float refLevelBode;
extern int replotFlag;
extern int lastCurrentFreqPos;
extern int currentFreqPos;
extern float32_t BodePlotterBPF_state[]; // AFP 10-18-22
extern arm_biquad_cascade_df2T_instance_f32 S1_BodePlotFilter;
extern float32_t float_buffer_L_AudioBode[];
//extern int valPin;
extern int pushButtonSwitchIndex;
extern int endBodeFlag;
extern int doneViewing;
long SetBodeStart();
long SetBodeStop();
float SetBodeReference();
extern int plotBodeRefFlag;
extern int freqBodeChangeFlag;
extern int BodePlotFlag;
void DrawPlots();
extern int plotBodeBandFlag;
extern float bodeResultRdB;
extern int levelBodeChangeFlag;
#endif // EXCLUDE_BODE
extern long long pll_freq;
extern int valPin;
extern int currentRF_InAtten; //AFP 04-12-24
extern int currentRF_OutAtten; //AFP 04-12-24
//=================== AFP 03-30-24 V012 Bode Plot end
//=================== AFP 09-04-23 V012 Quad Si5351 variables
extern int Even_Divisor;
extern int oldEven_Divisor;
int EvenDivisor( long freq2 );
//extern long long Clk2SetFreq; // AFP 09-27-22
extern long long Clk1SetFreq; // AFP 09-27-22
extern long long Clk0SetFreq;
extern int multiple;
extern int oldMultiple;
extern unsigned long long pll_min;
extern unsigned long long pll_max;
//extern unsigned long long f_pll_freq;
//extern unsigned long long pll_freq;
extern long long freq;
extern long long oldfreq;
extern long long freq1;
void DrawBands();
//================== Global CW Correlation and FFT Variables =================
extern float32_t audioMaxSquaredAve;
extern float32_t corrResult; //AFP 02-02-22
extern uint32_t corrResultIndex; //AFP 02-02-22
extern float32_t sinBuffer[]; //AFP 02-02-22
extern float32_t sinBuffer2[];
extern float32_t sinBuffer3[];
extern float32_t float_Corr_Buffer[]; //AFP 02-02-22
extern float32_t aveCorrResult; //AFP 02-02-22
extern float32_t magFFTResults[];
extern long tempSigTime;
//extern int audioTemp; KF5N
extern int audioTempPrevious;
extern int filterWidth;
extern int filterWidthX; // The current filter X.
extern int filterWidthY;
extern int x1AdjMax; //AFP 2-6-23
// The current filter Y.
extern float sigStart;
extern float sigDuration;
extern float gapStartData;
extern float gapDurationData;
extern int audioValuePrevious;
extern float goertzelMagnitude;
extern float min_gain_dB, max_gain_dB ; //set desired gain range
extern float gain_dB ; //computed desired gain value in dB
extern boolean use_HP_filter ; //enable the software HP filter to get rid of DC?
extern float knee_dBFS, comp_ratio, attack_sec, release_sec;
extern float32_t corrResultR; //AFP 02-02-22
extern uint32_t corrResultIndexR; //AFP 02-02-22
extern float32_t corrResultL; //AFP 02-02-22
extern uint32_t corrResultIndexL; //AFP 02-02-22
extern float32_t aveCorrResult; //AFP 02-02-22
extern float32_t aveCorrResultR; //AFP 02-06-22
extern float32_t aveCorrResultL; //AFP 02-06-22
extern float32_t float_Corr_BufferR[]; //AFP 02-06-22
extern float32_t float_Corr_BufferL[]; //AFP 02-06-22
extern float32_t combinedCoeff;//AFP 02-06-22
extern int CWCoeffLevelOld;
extern float CWLevelTimer;
extern float CWLevelTimerOld;
extern float32_t combinedCoeff2;
extern float32_t combinedCoeff2Old;
extern float ticMarkTimer;
extern float ticMarkTimerOld;
extern int CWOnState; //AFP 05-17-22
extern long CWFreqShift; //AFP 05-17-22
extern long calFreqShift; //AFP 11-06-22
extern long cwTime0;
extern long cwTime1;
extern long cwTime2;
extern long cwTime3;
extern long cwTime4;
extern long cwTime5;
extern long cwTime6;
//===== New histogram stuff
extern const char *secondaryChoices[][14];
extern int endDitFlag;
extern int endGapFlag;
extern int topDitIndex; //AFP 02-20-22
extern int topDitIndexOld;
extern int topGapIndex;
extern int topGapIndexOld;
extern int32_t signalHistogram[];
extern int32_t gapHistogram[];
extern uint32_t histMaxIndexDitOld;
extern uint32_t histMaxIndexDahOld;
extern uint32_t histMaxDit;
extern uint32_t histMaxIndexDit;
extern uint32_t histMaxDah;
extern uint32_t histMaxIndexDah;
extern float32_t pixel_per_khz ; //AFP
extern int pos_left ;
extern int centerLine;
extern int filterWidth;
extern int h;
extern int atomGapLength;
extern int atomGapLength2;
extern int charGapLength;
extern int charGapLength2;
extern int receiveEQFlag;
extern int xmitEQFlag;
extern int centerTuneFlag;
extern long valRef1;
extern long valRef2;
extern long gapRef1;
extern int valFlag;
extern long signalStartOld;
extern int valCounter;
extern long aveDitLength;
extern long aveDahLength;
extern float thresholdGeometricMean;
extern float thresholdArithmeticMean;
extern float aveAtomGapLength;
extern float thresholdGapGeometricMean;
extern float thresholdGapArithmeticMean;
extern long notchFreq;
extern long notchPosOld;
extern long filter_pos;
extern long last_filter_pos;
//================== Global Excite Variables =================
#define IIR_ORDER 8
#define IIR_NUMSTAGES (IIR_ORDER / 2)
extern arm_biquad_cascade_df2T_instance_f32 s1_Receive ; //AFP 09-23-22
extern arm_biquad_cascade_df2T_instance_f32 s1_Receive2 ; //AFP 11-02-22
extern float32_t HP_DC_Butter_state2[2]; //AFP 11-04-22
extern float32_t HP_DC_Butter_state[6]; //AFP 09-23-22
extern float32_t coeffs192K_10K_LPF_FIR[];
extern float32_t coeffs48K_8K_LPF_FIR[];
extern const uint32_t N_B_EX;
extern float32_t recEQ_Level[];
extern float32_t recEQ_LevelScale[];