-
Notifications
You must be signed in to change notification settings - Fork 3
/
QCX.ino
1815 lines (1566 loc) · 58.9 KB
/
QCX.ino
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
/*
* QCX with Teensy 3.6, Audio shield, and ILI9341 touchscreen added on QCX Dev Board.
*
* A note: putting capacitors in C4 C7 caused the front end to pick up screen draw noise. Line In is
* connected to those nodes for connection of I and Q. The difference with and without is substantial.
*
* to work on
* Try distributed waterfall writes for maybe less noise
* Fix my antenna cable or purchase a new one.
* Wire a fet to switch dit or dah to ground for hellschreiber
* See if TX works and power level ok.
* make a hole in the top case
* Add a USB external wire
* CW, RTTY, PSK31 decoders - software only
* Fancy S meter in text area when not in a decode mode ( cw, rtty, psk, hell )
* Hellschrieber RX and TX
*
*
Free pins when using audio board and display.
Teensy 3.6. Many more pins unused but not populated with headers.
0,1 used for Serial1 Rx, Tx
2 mute switch
3
4
5
8 used for touch CS
16 A2
17 A3
*******************************************************************************/
#include <ILI9341_t3.h>
#include <XPT2046_Touchscreen.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include "hilbert.h"
#define QCX_MUTE 2 // pin 2 high mutes qcx audio
// #define USE_USB_AUDIO // not needed for self contained radio but works really slick
// with HDSDR. Can include later.
// added peak2. peak1 and peak2 used to avoid exceeding int16 size in the usb,lsb adders.
// GUItool: begin automatically generated code
AudioInputI2S IQ_in; //xy=68.66668701171875,360.3333740234375
AudioFilterFIR PhaseI; //xy=206,292
AudioFilterFIR PhaseQ; //xy=212,458
#ifdef USE_USB_AUDIO
AudioOutputUSB usb1; //xy=361.66668701171875,369
#endif
AudioFilterFIR H45plus; //xy=364.66668701171875,293.3333435058594
AudioFilterFIR H45minus; //xy=375.66668701171875,456.3333740234375
AudioMixer4 USBmixer; //xy=546.6666870117188,299.3333435058594
AudioMixer4 LSBmixer; //xy=549.6666870117188,450.3333740234375
AudioAnalyzeFFT256 LSBscope; //xy=557.8095550537109,571.9046897888184
AudioAnalyzeFFT256 USBscope; //xy=560.5238571166992,184.1904640197754
AudioMixer4 SSBselect; //xy=729.6666870117188,367.3333740234375
AudioAnalyzePeak peak1; //xy=749.5238647460938,249.61903381347656
AudioAnalyzePeak peak2; //xy=749,584
AudioFilterFIR IF12r7; //xy=751.8095397949219,518.6190452575684
AudioEffectRectifier AMdet; //xy=894.6666641235352,517.6189670562744
AudioFilterBiquad BandWidth; //xy=900.666748046875,368.3333740234375
AudioAnalyzeRMS rms1; //xy=964.666748046875,265.33331298828125
AudioOutputI2S LineOut; //xy=1074.666748046875,462.3333740234375
AudioConnection patchCord1(IQ_in, 0, PhaseI, 0);
AudioConnection patchCord2(IQ_in, 1, PhaseQ, 0);
AudioConnection patchCord3(PhaseI, H45plus);
#ifdef USE_USB_AUDIO
AudioConnection patchCord4(PhaseI, 0, usb1, 0);
AudioConnection patchCord6(PhaseQ, 0, usb1, 1);
#endif
AudioConnection patchCord5(PhaseQ, H45minus);
AudioConnection patchCord7(H45plus, 0, USBmixer, 1);
AudioConnection patchCord8(H45plus, 0, LSBmixer, 1);
AudioConnection patchCord9(H45minus, 0, USBmixer, 2);
AudioConnection patchCord10(H45minus, 0, LSBmixer, 2);
AudioConnection patchCord11(USBmixer, USBscope);
AudioConnection patchCord12(USBmixer, 0, SSBselect, 1);
AudioConnection patchCord13(USBmixer, peak1);
AudioConnection patchCord14(USBmixer, IF12r7);
AudioConnection patchCord15(LSBmixer, LSBscope);
AudioConnection patchCord16(LSBmixer, 0, SSBselect, 2);
AudioConnection patchCord17(LSBmixer, peak2);
AudioConnection patchCord18(SSBselect, BandWidth);
AudioConnection patchCord19(IF12r7, AMdet);
AudioConnection patchCord20(AMdet, 0, SSBselect, 3);
AudioConnection patchCord21(BandWidth, rms1);
AudioConnection patchCord22(BandWidth, 0, LineOut, 0);
//AudioConnection patchCord23(BandWidth, 0, LineOut, 1);
AudioControlSGTL5000 codec; //xy=227.6666717529297,181.3333342075348
// GUItool: end automatically generated code
// we have 65k colors. How to pick one? Limit to just 16.
// 4bpp pallett 0-3, 4-7, 8-11, 12-15
const uint16_t EGA[] = {
ILI9341_BLACK, ILI9341_NAVY, ILI9341_DARKGREEN, ILI9341_DARKCYAN,
ILI9341_MAROON, ILI9341_PURPLE, ILI9341_OLIVE, ILI9341_LIGHTGREY,
ILI9341_DARKGREY, ILI9341_BLUE, ILI9341_GREEN, ILI9341_CYAN,
ILI9341_RED, ILI9341_MAGENTA, ILI9341_YELLOW, ILI9341_WHITE
};
uint16_t GRAY[16];
// waterfall pallet, try just sliding bits
//const uint16_t WF[] = {
// 0b0000000000000000, 0b0000000000000001,0b0000000000000011,0b0000000000000111,
// 0b0000000000001111, 0b0000000000011111,0b0000000000111111,0b0000000001111111,
// 0b0000000011111111, 0b0000000111111111,0b0000001111111111,0b0000011111111111,
// 0b0000111111111111, 0b0001111111111111,0b0011111111111111,0b0111111111111111,
// };
const uint16_t WF[] = {
0b0000000000000000, 0b0000000000000001,0b0000000000000011,0b0000000000000111,
0b0000000000011111, 0b0000000001111110,0b0000000111111000,0b0000011111100000,
0b1111110000000000, 0b1111111000000000,0b1111111100000000,0b1111111110000000,
0b1111111111000000, 0b1111111111110000,0b1111111111111100,0b1111111111111111,
};
// alternate display connections for use with audio board
#define CS_PIN 8 // touchscreen
#define TFT_DC 20
#define TFT_CS 21
#define TFT_RST 255 // 255 = unused, connect to 3.3V
#define TFT_MOSI 7
#define TFT_SCLK 14
#define TFT_MISO 12
ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCLK, TFT_MISO);
XPT2046_Touchscreen ts(CS_PIN);
#define cat Serial1
// radio defines for vfo_mode.
#define VFO_A 1
#define VFO_B 2
#define VFO_SPLIT 4
#define VFO_RIT 8
#define VFO_CW 16
#define VFO_LSB 32
#define VFO_USB 64
#define VFO_AM 128
// radio variables
int32_t vfo_a, vfo_b, rit, stp = 100, rit_stp = 10;
uint8_t vfo_mode; // undefined, should pick up value on init
// cat polling
#define QUFA 0b00000001
#define QUFB 0b00000010
#define QUIF 0b00101100
#define QUTB 0b01000000
uint32_t cmd_tm; // command interval timer
uint8_t qu_flags;
// printing cw decode
#define DECODE_LINES 5
#define D_LEFT 8 // margins
#define D_BOTTOM 238 //
#define D_SIZE 20 // line spacing
char dtext[DECODE_LINES][26]; // buffer
int dline = DECODE_LINES - 1; // current working line bottom of the screen
int dpos; // current cursor position
// screen owners
#define DECODE 0
#define KEYBOARD 1
#define MENUS 2
uint8_t screen_owner = DECODE;
char kb[5][10] = {
{ '1','2','3','4','5','6','7','8','9','0' },
{ 'Q','W','E','R','T','Y','U','I','O','P' },
{ 'A','S','D','F','G','H','J','K','L', 8 }, // 8 is backspace char
{ 'Z','X','C','V','B','N','M',',','.','/' },
{ '*','*',' ',' ','?','=',' ',' ',' ',' ' }
};
// Menu's
void (* menu_dispatch )( int32_t ); // pointer to function that is processing screen touches
struct menu {
char title[16];
const char *menu_item[8];
int param[8];
int y_size; // x size will be half the screen, two items on a line for now
int color;
int current;
};
// mode menu items
const char m_qcx[] = " CW QCX"; // qcx audio sampled on A2 via audio library
const char m_cw[] = " CW sdr";
const char m_lsb[] = " LSB";
const char m_usb[] = " USB";
const char m_am[] = " AM";
const char m_sam[] = " ";
const char m_data[]= " ";
const char m_phase[] = "Phase";
struct menu mode_menu_data = {
{ "SDR Mode" },
{ m_qcx,m_cw,m_lsb,m_usb,m_am,m_sam,m_data,m_phase },
{0,1,2,3,4,-1,-1,7},
48,
ILI9341_PURPLE,
2
};
const char w_6k[] = " 6000";
const char w_4k[] = " 4500";
const char w_3600[] = " 3600";
const char w_3300[] = " 3300";
const char w_3000[] = " 3000";
const char w_2700[] = " 2700";
const char w_2400[] = " 2400";
const char w_1100[] = " 1100";
struct menu band_width_menu_data = {
{ "Band Width" },
{ w_6k, w_4k, w_3600, w_3300, w_3000, w_2700, w_2400, w_1100 },
{ 6000, 4500, 3600, 3300, 3000, 2700, 2400, 1100 },
48,
ILI9341_PURPLE,
4
};
uint8_t ManInTheMiddle; // act as a USB -> <- QCX serial repeater for CAT commands
// phase correction FIRs. Apparently sometimes the I2s audio samples are 1 step out of phase
// and waterfall shows very little opposite sideband suppression
int16_t PhaseIfir[2] = { 32767, 0 }; // swap constants to change phasing
int16_t PhaseQfir[2] = { 32767, 0 };
int peak_atn = 15; // auto attenuation of line_input, atn will be peak_atn - 15
float agc_gain = 1.0;
int mux_selected;
float svalue;
float rms_value;
// keyboard transmit
#define TBUFSIZE 16
char tbuf[TBUFSIZE]; // power of two buffer
int t_in, t_out;
uint8_t tx_in_progress;
/********************************************************************************/
void setup() {
int i,j,r,g,b;
Serial.begin(38400); // for debug or PC control
cat.begin(38400); // qcx CAT control
// build the gray scale pallett
for( i = 0; i < 16; ++i ){
r = 2*i + 1; g = 4*i + 3; b = 2*i + 1;
GRAY[i] = r << 11 | g << 5 | b;
}
// clear the decode text buffer with spaces
for( i = 0; i < DECODE_LINES; ++i ){
for( j = 0; j < 25; ++j ) dtext[i][j] = ' ';
dtext[i][25] = 0; // terminate as a string
}
pinMode(QCX_MUTE,OUTPUT);
digitalWriteFast(QCX_MUTE,HIGH);
tft.begin();
tft.fillScreen(ILI9341_BLACK);
tft.setRotation(1);
ts.begin(); // touchscreen
ts.setRotation(1);
tft.setTextSize(2); // sign on message, else see a blank screen until qcx boots.
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(80,200);
tft.print("QCX+ +SDR");
menu_dispatch = &hidden_menu; // function pointer for screen touch
vfo_mode = VFO_LSB;
// Setup the audio shield
AudioNoInterrupts();
AudioMemory(20);
codec.enable();
codec.volume(0.5); // headphone volume, not used eventually
codec.unmuteHeadphone();
codec.inputSelect( AUDIO_INPUT_LINEIN );
// codec analog gains
codec.lineInLevel(15); // 0 to 15, used as attenuator, 3.12v to 0.24v
codec.lineOutLevel(30); // 13 to 31 with 13 the loudest. 3.16v to 1.16v
//codec.adcHighPassFilterDisable(); // less noise ? don't notice any change
//codec.adcHighPassFilterFreeze(); // try this one
//H45plus.begin(h45p,HILBERT_SIZE);
//H45minus.begin(h45m,HILBERT_SIZE);
H45plus.begin(h90p,HILBERT_SIZE); // try the 90deg and 0deg phase shift filters
H45minus.begin(h00m,HILBERT_SIZE); // Maybe just a little bit better
PhaseI.begin(PhaseIfir,2); // Phasing change delays if needed
PhaseQ.begin(PhaseQfir,2);
USBmixer.gain(1,1.0); // add signals get USB. Lower gain if addition causes clipping.
USBmixer.gain(2,1.0); // although will want to lower line in gain first
LSBmixer.gain(1,1.0); // sub signals get LSB
LSBmixer.gain(2,-1.0);
SSBselect.gain(0,0.0); // turn off QCX native audio
SSBselect.gain(1,0.0); // turn off USB
SSBselect.gain(2,1.0); // LSB is default on startup
SSBselect.gain(3,0.0); // turn off AM audio
mux_selected = 2;
BandWidth.setLowpass(0,3000,0.67); // use these or actual butterworth Q's
BandWidth.setLowpass(1,3000,1.10);
BandWidth.setLowpass(2,3000,0.707);
BandWidth.setLowpass(3,3000,1.00); // 4 IIR lowpass cascade
IF12r7.begin(AM12r7fir,30); // AM filter at 12.7k audio IF frequency
AudioInterrupts();
USBscope.averageTogether(50); // or 40 for faster waterfall
LSBscope.averageTogether(50);
}
// touch the screen top,middle,bottom to bring up different menus. Assign menu_dispatch.
// This is default touch processing. No menu on the screen.
void hidden_menu( int32_t t ){
int32_t yt, xt;
screen_owner = MENUS;
yt = t & 0xff;
xt = t >> 8;
// check the y value of touch to see what menu area
if( yt < 60 ){
menu_display( &mode_menu_data );
menu_dispatch = &mode_menu; // screen touch goes to mode_menu() now
}
else if ( yt < 140 ){
menu_display( &band_width_menu_data );
menu_dispatch = &band_width_menu;
}
else if( yt > 190 && xt > 270 && (vfo_mode & VFO_CW) ){ // keyboard CW sending
menu_dispatch = &key_tx;
key_tx(0);
}
else menu_cleanup(); // not active part of the screen, return to normal op.
}
void mode_menu( int32_t t ){
int selection;
selection = touch_decode( t, mode_menu_data.y_size );
if( mode_menu_data.param[selection] != -1 ){
if( selection != 7 ) mode_menu_data.current = selection; // added phasing to this menu
selection = mode_menu_data.param[selection]; // perhaps it should be elsewhere
if( selection != 7 ) vfo_mode &= 0x0f; // save the qcx mode flags, clear the sdr mode flags
switch( selection ){
case 0: // QCX audio selected
vfo_mode |= VFO_CW;
digitalWriteFast(QCX_MUTE,LOW); // select qcx audio
SSBselect.gain(1,0.0); // mute LSB,USB,AM
SSBselect.gain(2,0.0);
SSBselect.gain(3,0.0);
mux_selected = 0;
break;
case 1: vfo_mode |= VFO_CW;
digitalWriteFast(QCX_MUTE,HIGH);
SSBselect.gain(1,agc_gain); // turn on USB
SSBselect.gain(2,0.0);
SSBselect.gain(3,0.0);
mux_selected = 1;
break;
case 2: vfo_mode |= VFO_LSB;
digitalWriteFast(QCX_MUTE,HIGH);
SSBselect.gain(1,0.0);
SSBselect.gain(2,agc_gain); // turn on LSB
SSBselect.gain(3,0.0);
mux_selected = 2;
break;
case 3: vfo_mode |= VFO_USB;
digitalWriteFast(QCX_MUTE,HIGH);
SSBselect.gain(1,agc_gain); // turn on USB
SSBselect.gain(2,0.0);
SSBselect.gain(3,0.0);
mux_selected = 1;
break;
case 4: vfo_mode |= VFO_AM;
digitalWriteFast(QCX_MUTE,HIGH);
SSBselect.gain(1,0.0);
SSBselect.gain(2,0.0);
SSBselect.gain(3,agc_gain); // turn on AM
mux_selected = 3;
break;
case 7: PhaseChange(1); break; // phasing correction
}
}
menu_cleanup();
}
void menu_cleanup(){
// exit touch menu and back to normal screen
menu_dispatch = &hidden_menu;
tft.fillScreen(ILI9341_BLACK);
screen_owner = DECODE;
vfo_mode_disp();
vfo_freq_disp();
}
void band_width_menu( int32_t t ){
int sel;
sel = touch_decode( t, band_width_menu_data.y_size );
if( band_width_menu_data.param[sel] != -1 ){
band_width_menu_data.current = sel;
sel = band_width_menu_data.param[sel];
//Serial.println(sel);
BandWidth.setLowpass(0,sel,0.67); // 0.51 butterworth constants
BandWidth.setLowpass(1,sel,1.10); // 0.60
BandWidth.setLowpass(2,sel,0.707); // 0.90
BandWidth.setLowpass(3,sel,1.00); // 2.56
}
menu_cleanup();
}
void decode_menu( int32_t t ){
}
// I2S audio sometimes starts with I and Q out of order
void PhaseChange(uint8_t chg){
static int val;
if( chg ){
if( ++val > 2 ) val = 0; // rotate through the settings
}
// print
tft.setTextSize( 1 );
tft.setCursor(262,66);
tft.setTextColor(EGA[14],0);
tft.print("Ph: ");
switch( val ){
case 0:
PhaseIfir[0] = 32767; PhaseIfir[1] = 0; // normal in phase
PhaseQfir[0] = 32767; PhaseQfir[1] = 0;
tft.print("1010");
break;
case 1:
PhaseIfir[0] = 32767; PhaseIfir[1] = 0;
PhaseQfir[0] = 0; PhaseQfir[1] = 32767; // delay Q ( delay I if fir runs backward )
tft.print("1001");
break;
case 2:
PhaseIfir[0] = 0; PhaseIfir[1] = 32767; // delay I
PhaseQfir[0] = 32767; PhaseQfir[1] = 0;
tft.print("0110");
break;
}
}
void menu_display( struct menu *m ){ // display any of the menus on the screen
int i,x,y; // other functions handle selections
tft.setTextColor( ILI9341_WHITE, m->color ); // text is white on background color
tft.fillScreen(ILI9341_BLACK); // border of menu items is black
// title box
tft.fillRect(5,5,320-10,m->y_size-10,m->color);
tft.setCursor( 10,10 );
if( m->y_size > 45 ) tft.setTextSize(3);
else tft.setTextSize(2);
tft.print(m->title);
// draw some menu boxes, two per row for now
y = m->y_size; x = 0;
for( i = 0; i < 8; ++i ){
if( m->menu_item[i][0] == 0 ) break; // null option
if( y + m->y_size-10 > 239 ) break; // screen is full
tft.fillRect(x+5,y+5,160-10,m->y_size-10,m->color);
tft.setCursor( x+10,y+10 );
if( i == m->current ) tft.setTextColor( ILI9341_YELLOW, m->color );
else tft.setTextColor( ILI9341_WHITE, m->color );
tft.print(m->menu_item[i]);
x += 160;
if( x >= 320 ) x = 0, y += m->y_size;
}
}
int touch_decode( int32_t t, int y_size ){ // selection for 2 items wide touch menu
int32_t x,y;
y = t & 0xff;
x = t >> 8;
y -= y_size; // top row is the title !!! if sub menus needed, need to decode title touch also
y /= y_size; // row of the touch
x /= 160; // column
return 2*y + x; // two menu items per row
}
void loop() {
static int c_state;
int32_t t;
// poll radio once a second, process qu_flags as a priority
if( ( millis() - cmd_tm > 1000 ) && ManInTheMiddle == 0 ){
if( tx_in_progress || t_in != t_out ) cat.print("KY;");
else if( qu_flags & QUIF ) cat.print("IF;");
else if( qu_flags & QUFB ) cat.print("FB;");
else if( qu_flags & QUFA ) cat.print("FA;");
else if( qu_flags & QUTB && screen_owner == DECODE && (vfo_mode & VFO_CW ) ) cat.print("TB;");
else{
switch(c_state){ // poll radio round robin for missed packet recovery
case 0: case 2: case 4: case 6: case 8:
case 10: case 12: case 14: cat.print("IF;"); break;
case 1: cat.print("FA;"); break;
case 3: cat.print("QU1;"); break; // enable flags
case 5: cat.print("FB;"); break;
case 7: if( screen_owner == DECODE && (vfo_mode & VFO_CW ) ) cat.print("TB1;");
// else cat.print("TB0;"); keep enabled but don't get the info unless CW mode
break; // enable/disable decode flag
case 9: if( screen_owner == DECODE && (vfo_mode & VFO_CW ) ) cat.print("TB;");
break;
}
++c_state;
c_state &= 15;
}
cmd_tm = millis();
}
if( cat.available() ) radio_control();
if( Serial.available() ){
char c = Serial.read();
if( ManInTheMiddle ) cat.write(c); // CAT command repeater
else if( c == ';' ) ManInTheMiddle = 1; // looks like a CAT command on USB serial
}
t = touch();
if( t ){
// goto where menu_dispatch() points
(*menu_dispatch)(t); // off to whoever owns the touchscreen
}
USBwaterfall();
LSBwaterfall();
auto_atn(); // front end gain reduction if rcv very loud signals
agc();
// balance_schmoo(); // !!! testing
}
// avoid overload. This looks at 40khz of signal on USB and LSB so not a replacement for AGC
// both sideband signals feed the bandscope even if not listened to
void auto_atn(){ // lower front end gain if signals approach overload
static uint32_t tm;
static int no_chg_cnt; // slowly raise gain
if( millis() - tm < 10 ) return;
if( peak1.available() == 0 ) return;
if( peak2.available() == 0 ) return;
++no_chg_cnt;
tm = millis();
if( peak_atn > 0 && (peak1.read() > 0.9 || peak2.read() > 0.9) ){ // lower gain on strong signal
--peak_atn;
codec.lineInLevel(peak_atn);
vfo_mode_disp();
no_chg_cnt = 0;
}
// raise gain if no strong signals for awhile
if( no_chg_cnt > 200 && peak_atn < 15 ){ // 200 == 2 seconds
++peak_atn;
codec.lineInLevel(peak_atn);
vfo_mode_disp();
no_chg_cnt = 0;
}
}
#define AGC_FLOOR 0.10 // 0.15
#define AGC_SLOPE 6.0 // 8.0
#define AGC_HANG 300
void agc(){
static uint32_t tm;
static float sig;
static int hang;
float reading;
int ch;
if( millis() - tm < 1 ) return; // working at 1000hz
if( rms1.available() == 0 ) return;
tm = millis();
ch = 0;
rms_value = reading = rms1.read();
if( reading > sig && reading > AGC_FLOOR ){ // attack
sig = sig + 0.001;
ch = 1;
hang = 0;
}
else if( sig > AGC_FLOOR && hang++ > AGC_HANG ){ // decay
sig = sig - 0.00005;
ch = 1;
}
if( ch ){
agc_gain = sig - AGC_FLOOR;
agc_gain = agc_gain * AGC_SLOPE;
agc_gain = 1.0 - agc_gain;
SSBselect.gain(mux_selected,agc_gain);
// Serial.println(agc_gain);
if( screen_owner == DECODE ){
tft.setTextSize(1);
tft.setTextColor(EGA[14],0);
tft.setCursor(262,114);
tft.print("Sig: ");
tft.print(sig);
}
}
smeter();
}
void smeter(){
static uint8_t onscreen;
float angle;
int x,y;
static int lastx = 160,lasty = 239;
int diff;
float tvalue;
float ftemp;
if( screen_owner != DECODE || (vfo_mode & VFO_CW) ){ // show the meter ?
onscreen = 0;
return;
}
if( onscreen == 0 ){ // init the meter area
onscreen = 1;
tft.fillRect(5, 129, 310, 110, EGA[0]);
for( ftemp = 135; ftemp > 45; ftemp -= 0.2 ){
y = 240 - 90 * sin(ftemp/57.3);
x = 160 + 180 * cos(ftemp/57.3);
tft.drawPixel(x,y,EGA[15]);
tft.drawPixel(x,y-1,EGA[15]);
tft.drawPixel(x,y+1,EGA[15]);
if( int(ftemp) == 90 ) tft.drawLine(x,y,x,y-7,EGA[15]);
if( int(ftemp) >= 133 ) tft.drawLine(x,y,x-4,y-6,EGA[15]);
if( int(ftemp) <= 46 ) tft.drawLine(x,y,x+4,y-6,EGA[15]);
if( int(ftemp) == 123 ) tft.drawLine(x,y,x-3,y-6,EGA[15]);
if( int(ftemp) == 111 ) tft.drawLine(x,y,x-2,y-6,EGA[15]);
if( int(ftemp) == 99 ) tft.drawLine(x,y,x-1,y-6,EGA[15]);
if( int(ftemp) == 75 ) tft.drawLine(x,y,x+1,y-6,EGA[15]);
if( int(ftemp) == 60 ) tft.drawLine(x,y,x+2,y-6,EGA[15]);
tft.setTextColor(EGA[15],0);
tft.setTextSize(2);
tft.setCursor(25,150);
tft.write('1');
tft.setCursor(90,131);
tft.write('5');
tft.setCursor(156,128);
tft.write('9');
tft.setCursor(210,131);
tft.print("20");
tft.setCursor(250,140);
tft.print("40");
}
}
tvalue = rms_value/agc_gain; // what the signal would be without the agc
ftemp = map( peak_atn, 0, 15, 312, 24 ); // voltage ratio due to front end attenuation
tvalue = ftemp * tvalue / 24.0; // what the signal would be without the attenuator
tvalue *= 160; // fudge factor scaling to make it look good
tvalue = log10(tvalue);
if( tvalue > svalue ) svalue += 0.02; // smooth out the meter movement
if( tvalue < svalue ) svalue -= 0.005;
//angle = map( svalue*10, 1, 120, 1350, 450 ); // S1 to S12 mapped to 90 deg +- 45 scaled by 10.
angle = map( svalue * 100, 1, 300, 13500, 4500 ); // more fudging
angle /= 100.0;
// Serial.println(angle); // !!! debug, remove
if( angle < 45 ) angle = 45;
if( angle > 135 ) angle = 135;
y = 240 - 80 * sin(angle/57.3);
x = 160 + 180 * cos(angle/57.3);
diff = x - lastx;
if( diff < 0 ) diff = -diff;
if( diff > 2 ){
tft.drawLine(lastx-1,lasty,160,380,EGA[0]);
tft.drawLine(lastx,lasty,160,380,EGA[0]);
tft.drawLine(lastx+1,lasty,160,380,EGA[0]);
tft.drawLine(x-1,y,160,380,EGA[12]);
tft.drawLine(x,y,160,380,EGA[12]); // lines clip to screen ok ?
tft.drawLine(x+1,y,160,380,EGA[12]);
lastx = x;
lasty = y;
}
}
// send cw via a touchscreen keyboard
void key_tx( int32_t t ){
int x,y;
char c;
static char buff[3][25]; // screen display
static int pos;
if( screen_owner != KEYBOARD ){ // need to display the keyboard
screen_owner = KEYBOARD;
tft.fillScreen(ILI9341_BLACK);
// tft.fillRect(0, 80, 320, 160, EGA[0]);
tft.setTextSize(2);
tft.setTextColor( ILI9341_YELLOW, ILI9341_MAROON );
for( y = 0; y < 5; ++y ){
for( x = 0; x < 10; ++x ){
tft.fillRect( 32*x +3, 32*y +3 +80, 32-6, 32-6, ILI9341_MAROON );
tft.setCursor( 32*x +6, 32*y +6 +80);
if( kb[y][x] == 8 ) tft.print("BS");
else tft.write(kb[y][x]);
}
}
// fixups for rx and space
tft.fillRect( 0, 80 + 3 + 4*32, 32+32 - 6, 32-6, ILI9341_MAROON );
tft.setCursor( 6, 80 + 6 + 4*32 );
tft.print("RX");
tft.fillRect( 6*32+3, 80 + 4*32+3, 4*32 -6, 32-6 , ILI9341_MAROON);
tft.setCursor( 6*32 + 6, 80 + 4*32 + 6 );
tft.print("SPACE");
return; // ignore this touch
}
x = t >> 8; y = t & 0xff;
// tft.setCursor(80-12,80-12);
y -= 80;
x /= 32; y /= 32;
c = ' ';
if( x >= 0 && x < 10 && y >= 0 && y < 5 ){
c = kb[y][x];
if( ++pos >= 24 ){ // scroll text
buff[2][24] = 0;
for(y = 0; y < 2; ++y){
for( x = 0; x < 25; ++x ) buff[y][x] = buff[y+1][x];
}
for( x = 0; x < 24; ++x ) buff[2][x] = ' ';
buff[2][24] = 0;
tft.setTextSize(2);
tft.setTextColor(EGA[14],0);
for( y = 0; y < 3; ++y ){
tft.setCursor( 5, 5 + 22*y );
buff[y][24] = 0;
tft.print(&buff[y][0]);
}
pos = 0;
}
tft.setCursor( 5 + 12*pos, 5 + 22 * 2 );
tft.setTextSize(2);
tft.setTextColor(EGA[15],0);
tft.write( c );
buff[2][pos] = c;
tbuf[t_in++] = c;
t_in &= (TBUFSIZE-1);
cat.print("KY;");
}
if( c == '*' ){
//tft.fillRect(0, 80, 320, 160, EGA[0]);
//screen_owner = DECODE;
menu_cleanup();
//cat.print("TB1;");
}
}
int32_t touch(){
static uint32_t tm;
static int16_t x,y;
static uint8_t z;
// control rate of updates, library is at 3 ms.
if( millis() - tm < 5 ) return 0;
tm = millis();
if( ts.touched() ){
++z;
TS_Point p = ts.getPoint();
//ts.readData(&x,&y,&z);
x += map( p.x, 250, 3650, 0, 320 );
y += map( p.y, 330, 3700, 0, 240 );
//x = p.x; y = p.y;
}
else z = 0;
if( z == 0 ) x = y = 0;
if( z == 4 ){
x >>= 2;
y >>= 2;
return ( x << 8 ) | y;
}
return 0;
}
void vfo_mode_disp(){
int a,b,s,r;
int u,l,c,m;
if( screen_owner != DECODE ) return;
a = b = s = r = 0; // set colors of the vfo mode
c = u = l = m = 0;
if( vfo_mode & VFO_B ) b = 10;
if( vfo_mode & VFO_A ) a = 10;
if( vfo_mode & VFO_SPLIT ) s = 10;
if( vfo_mode & VFO_RIT ) r = 10;
if( vfo_mode & VFO_CW ) c = 10;
if( vfo_mode & VFO_LSB ) l = 10;
if( vfo_mode & VFO_USB ) u = 10;
if( vfo_mode & VFO_AM ) m = 10;
tft.setTextSize(1);
tft.setTextColor(EGA[4+a],0);
tft.setCursor(15,2);
tft.print("VFO A");
tft.setCursor(60,2);
tft.setTextColor(EGA[4+b],0);
tft.print("VFO B");
tft.setCursor(105,2);
tft.setTextColor(EGA[4+s],0);
tft.print("Split");
tft.setCursor(150,2);
tft.setTextColor(EGA[4+c],0);
tft.print("CW");
tft.setCursor(175,2);
tft.setTextColor(EGA[4+u],0);
tft.print("USB");
tft.setCursor(205,2);
tft.setTextColor(EGA[4+l],0);
tft.print("LSB");
tft.setCursor(235,2);
tft.setTextColor(EGA[4+m],0);
tft.print("AM");
tft.setCursor(280,2);
tft.setTextColor(EGA[4+r],0);
tft.print("RIT");
tft.drawLine(0,61,640,60,EGA[4]);
tft.drawLine(0,62,640,61,EGA[4]);
PhaseChange(0); // just print current value
tft.setCursor(262,78); // print bandwidth ( spacing 12 pixel ? )
tft.setTextSize(1);
tft.setTextColor(EGA[14],0);
tft.print("BW: ");
tft.print(band_width_menu_data.menu_item[band_width_menu_data.current]);
tft.setCursor(262,90);
tft.print("Stp: ");
if(stp <= 1000 ) p_leading(stp,4);
tft.setCursor(262,102);
tft.print("Atn: ");
tft.print(peak_atn - 15); tft.write(' ');
tft.setCursor(262,114);
//tft.print("Sig: ");
//tft.print(rms1.read());
}
void p_leading( int val, int digits ){ // print a number with leading zeros without using sprintf
int test;
test = pow(10,digits-1);
while( val < test && test >= 10 ){
tft.write('0');
test /= 10;
}
tft.print(val);
}
// alternate freq display. Simulate something that looks like the Argonaut V
/* segments */
#define A_ 1
#define B_ 2
#define C_ 4
#define D_ 8
#define E_ 16
#define F_ 32
#define G_ 64
#define DP_ 128
uint8_t segment[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x58,0x5e,0x79,0x71};
void vfo_freq_disp(){
int32_t vfo;
int32_t digit;
int i,mult;
if( screen_owner != DECODE ) return;
if( vfo_mode & VFO_B ) vfo = vfo_b;
else vfo = vfo_a;
if( (vfo_mode & VFO_AM ) == VFO_AM ) vfo += 13000; // AM IF is 12.7 but sound better 1k low
if( (vfo_mode & VFO_CW ) == 0 ) vfo -= 700;
// 40 meter radio, ignore 10 meg digit for now
mult = 1000000;
for( i = 0; i < 7; ++i ){
digit = vfo / mult;
disp_segments(i,digit);
vfo -= digit*mult;
mult /= 10;
}
tft.setTextSize(2);
tft.setTextColor(EGA[10],0);
int val = rit;
if( val < 0 ){
val = - val;
tft.setTextColor(EGA[12],0); // change color instead of displaying the minus sign
}
tft.setCursor(265,20);
p_leading(val,4);
}
void disp_segments( int pos, int32_t digit ){ // ?? maybe a font table would be better
uint16_t color;
digit = segment[digit];
color = ( digit & A_ ) ? EGA[10] : GRAY[1] ;
draw_A( pos,color );
color = ( digit & B_ ) ? EGA[10] : GRAY[1] ;
draw_B( pos,color );
color = ( digit & C_ ) ? EGA[10] : GRAY[1] ;
draw_C( pos,color );
color = ( digit & D_ ) ? EGA[10] : GRAY[1] ;
draw_D( pos,color );
color = ( digit & E_ ) ? EGA[10] : GRAY[1] ;
draw_E( pos,color );
color = ( digit & F_ ) ? EGA[10] : GRAY[1] ;
draw_F( pos,color );
color = ( digit & G_ ) ? EGA[10] : GRAY[1] ;
draw_G( pos,color );
color = ( pos == 0 || pos == 3 ) ? EGA[10] : GRAY[1] ;
draw_DP( pos, color);
}
#define VB 20 //20
#define VS 35 //40
#define HB 20 //20
#define HS 32 //30
// slanted verticals have strange looking jaggies, try all horizontal lines
void draw_A( int pos, uint16_t color ){
int zz;
int i;
zz = HB + HS * pos;
for( i = 0; i < 4; ++i ) tft.drawFastHLine( zz+12,VB+i,13,color);
}
void draw_G( int pos, uint16_t color ){
int zz;
int i,k;
zz = HB + HS * pos;
for( i = 0; i < 4; ++i ){
k = ( i == 1 || i == 2 ) ? 2 : 0;