-
Notifications
You must be signed in to change notification settings - Fork 20
/
LangstoneGUI.c
3610 lines (3160 loc) · 89.2 KB
/
LangstoneGUI.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
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <time.h>
#include <iio.h>
#include <unistd.h>
#include <wiringPi.h>
#include <stdio.h>
#include <string.h>
#include "Graphics.h"
#include "Touch.h"
#include "Mouse.h"
#include "mcp23017.c"
#include "Morse.c"
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
//#define PLUTOIP "ip:pluto.local"
char plutoip[30];
const char *i2cNode1 = "/dev/i2c-1"; //linux i2c node for the Hyperpixel display on Pi 4
const char *i2cNode2 = "/dev/i2c-11"; //linux i2c node for the Hyperpixel display on Pi 4
int mcp23017_addr = 0x20; //MCP23017 I2C Address
void setFreq(double fr);
void displayFreq(double fr);
void setFreqInc();
void setTx(int ptt);
void setPtts(int p);
void setMode(int mode);
void setVolume(int vol);
void setSquelch(int sql);
void setSSBMic(int mic);
void setFMMic(int mic);
void setRxFilter(int low,int high);
void setTxFilter(int low,int high);
void setBandBits(int b);
void processTouch();
void processMouse(int mbut);
void initGUI();
void sendTxFifo(char * s);
void sendRxFifo(char * s);
void initFifos();
void initUDP(void);
void initPluto(void);
void setPlutoRxFreq(long long rxfreq);
void setPlutoTxFreq(long long rxfreq);
void setHwRxFreq(double fr);
void setHwTxFreq(double fr);
void PlutoTxEnable(int txon);
void PlutoRxEnable(int rxon);
void detectHw();
int buttonTouched(int bx,int by);
void setKey(int k);
void displayMenu(void);
void displaySetting(int se);
void changeSetting(void);
void processGPIO(void);
void initGPIO(void);
void initMCP23017(int add);
int readConfig(void);
int writeConfig(void);
int satMode(void);
int splitMode(void);
int satSplitMode(void);
int txvtrMode(void);
int duplexMode(void);
int multMode(void);
void setMoni(int m);
void initSDR(void);
void setFFTPipe(int cntl);
void setTxFFTPipe(int ctrl);
void waterfall(void);
void clearWaterfall(void);
void P_Meter(void);
void S_Meter(void);
void setRit(int rit);
void setInputMode(int n);
void gen_palette(char colours[][3],int num_grads);
void setPlutoTxAtt(int att);
void setPlutoRxGain(int gain);
int readPlutoRxGain(void);
void setBand(int b);
void setPlutoGpo(int p);
void setTxPin(int v);
long long runTimeMs(void);
void clearPopUp(void);
void displayPopupMode(void);
void displayPopupBand(void);
void send1750(void);
void setCTCSS(int t);
void displayError(char*st);
int minGain(double freq);
int maxGain(double freq);
void setDialLock(int d);
void setBeacon(int b);
int firstpass=1;
double freq;
double freqInc=0.001;
#define numband 12
int band=3;
double bandFreq[numband] = {70.200,144.200,432.200,1296.200,2320.200,2400.100,3400.100,5760.100,10368.200,24048.200,47088.2,10489.55};
double bandTxOffset[numband]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-9936.0,-23616.0,-46656.0,-10069.5};
double bandRxOffset[numband]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-9936.0,-23616.0,-46656.0,-10345.0};
double bandRepShift[numband]={0,-0.6,1.6,-6.0,0,0,0,0,0,0,0,0};
int bandTxHarmonic[numband]={1,1,1,1,1,1,1,1,1,1,1,1};
int bandRxHarmonic[numband]={1,1,1,1,1,1,1,1,1,1,1,1};
int bandMode[numband]={0,0,0,0,0,0,0,0,0,0,0,0};
int bandBitsRx[numband]={0,1,2,3,4,5,6,7,8,9,10,11};
int bandBitsTx[numband]={0,1,2,3,4,5,6,7,8,9,10,11};
int bandSquelch[numband]={30,30,30,30,30,30,30,30,30,30,30,30};
int bandFFTRef[numband]={-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10};
int bandTxAtt[numband]={0,0,0,0,0,0,0,0,0,0,0,0};
int bandRxGain[numband]={100,100,100,100,100,100,100,100,100,100,100,100}; //100 is automatic gain
int bandDuplex[numband]={0,0,0,0,0,0,0,0,0,0,0,0};
int bandCTCSS[numband]={0,0,0,0,0,0,0,0,0,0,0,0};
float bandSmeterZero[numband]={-80,-80,-80,-80,-80,-80,-80,-80,-80,-80,-80,-80};
int bandSSBFiltLow[numband]={300,300,300,300,300,300,300,300,300,300,300,300};
int bandSSBFiltHigh[numband]={3000,3000,3000,3000,3000,3000,3000,3000,3000,3000,3000,3000};
#define minFreq 0.0
#define maxFreq 99999.99999
#define minHwFreq 69.9
#define maxHwFreq 5999.99999
#define nummode 6
int mode=0;
int lastmode=0;
char * modename[nummode]={"USB","LSB","CW ","CWN","FM ","AM "};
enum {USB,LSB,CW,CWN,FM,AM};
#define numSettings 20
char * settingText[numSettings]={"Rx Gain= ","SSB Mic Gain= ","FM Mic Gain= ","Repeater Shift= ","CTCSS= ", "Rx Offset= ","Rx Harmonic Mixing= "," Tx Offset= ","Tx Harmonic Mixing= ","Band Bits (Rx)= ","Band Bits (Tx)= ","Copy Band Bits to Pluto=","FFT Ref= ","Tx Att= ","S-Meter Zero= ", "SSB Rx Filter Low= ", "SSB Rx Filter High= ","CW Ident= ", "CWID Carrier= ", "CW Break-In Hang Time= "};
enum {RX_GAIN,SSB_MIC,FM_MIC,REP_SHIFT,CTCSS,RX_OFFSET,RX_HARMONIC,TX_OFFSET,TX_HARMONIC,BAND_BITS_RX,BAND_BITS_TX,BAND_BITS_TO_PLUTO,FFT_REF,TX_ATT,S_ZERO,SSB_FILT_LOW,SSB_FILT_HIGH,CWID,CW_CARRIER,BREAK_IN_TIME};
int settingNo=RX_GAIN;
int setIndex=0;
int maxSetIndex=10;
enum {FREQ,SETTINGS,VOLUME,SQUELCH,RIT};
int inputMode=FREQ;
#define NUMCTCSS 52
int CTCSSTone[NUMCTCSS] = {0,670,693,719,744,770,797,825,854,885,915,948,974,1000,1035,1072,1109,1148,1188,1230,1273,1318,1365,1413,1462,1500,1514,1567,1598,1622,1655,1679,1713,1738,1773,1799,1835,1862,1899,1928,1966,1995,2035,2065,2107,2181,2257,2291,2336,2418,2503,2541};
//GUI Layout values X and Y coordinates for each group of buttons.
#define volButtonX 660
#define volButtonY 300
#define sqlButtonX 30
#define sqlButtonY 300
#define ritButtonX 670
#define ritButtonY 60
#define funcButtonsY 429
#define funcButtonsX 30
#define buttonHeight 50
#define buttonSpaceY 55
#define buttonWidth 100
#define buttonSpaceX 105
#define freqDisplayX 80
#define freqDisplayY 55
#define freqDisplayCharWidth 48
#define freqDisplayCharHeight 54
#define txX 600
#define txY 15
#define modeX 200
#define modeY 15
#define txvtrX 400
#define txvtrY 15
#define moniX 500
#define moniY 15
#define settingX 200
#define settingY 390
#define popupX 30
#define popupY 374
#define FFTX 140
#define FFTY 216
#define sMeterX 2
#define sMeterY 5
#define sMeterWidth 170
#define sMeterHeight 40
#define errorX 200
#define errorY 240
#define diallockX 316
#define diallockY 15
int ptt=0;
int ptts=0;
int moni=0;
int fifofd;
int sendBeacon=0;
int dotCount=0;
int transmitting=0;
int dialLock=0;
int keyDownTimer=0;
int CWIDkeyDownTime=1000; //time to put key down between CW Idents (100 per second)
#define configDelay 500 //delay before config is written after tuning (5 Seconds)
int configCounter=configDelay;
int twoButTimer=0;
int lastBut=0;
int breakInTimer=0;
int breakInTime=100;
long long lastLOhz;
long long lastClock;
long progStartTime=0;
int lastKey=1;
int sMeterType = 0;
int volume=20;
#define maxvol 100
int squelch=20;
#define maxsql 100
int rit=0;
#define minrit -9990
#define maxrit 9990
int SSBMic=50;
#define maxSSBMic 100
int FMMic=50;
#define maxFMMic 100
int TxAtt=0;
int tuneDigit=8;
#define maxTuneDigit 11
#define TXDELAY 10000 //10ms delay between setting Tx output bit and sending tx command to SDR
#define RXDELAY 10000 //10ms delay between sending rx command to SDR and setting Tx output bit low.
#define BurstLength 500000 //length of 1750Hz Burst 500ms
char mousePath[20];
char touchPath[20];
int mousePresent;
int touchPresent;
int plutoPresent;
int portsdownPresent;
int hyperPixelPresent;
int MCP23017Present;
int bandBitsToPluto=0; //copy low 3 band bits to pluto IO1-IO3
int popupSel=0;
int popupFirstBand;
enum {NONE,MODE,BAND,BEACON};
#define pttPin 0 // Wiring Pi pin number. Physical pin is 11
#define keyPin 1 //Wiring Pi pin number. Physical pin is 12
#define txPin 29 //Wiring Pi pin number. Physical pin is 40
#define bandPin1 31 //Wiring Pi pin number. Physical pin is 28
#define bandPin1alt 26 //Wiring Pi pin number. Physical pin is 32 Bandpin1 is copied to both of these pins to retain compatibility with Portsdown.
#define bandPin2 24 //Wiring Pi pin number. Physical pin is 35
#define bandPin3 7 //Wiring Pi pin number. Physical pin is 7
#define bandPin4 6 //Wiring Pi pin number. Physical pin is 22
#define bandPin5 4 //Wiring Pi pin number. Physical pin is 16
#define bandPin6 5 //Wiring Pi pin number. Physical pin is 18
#define bandPin7 12 //Wiring Pi pin number. Physical pin is 19
#define bandPin8 13 //Wiring Pi pin number. Physical pin is 21
#define i2cPttPin 0 //MCP23017 PTT Input if fitted Port A bit 0
#define i2cKeyPin 1 //MCP23017 Key Input if fitted Port A bit 1
#define i2cTxPin 7 //MCP23017 TX Output if fitted
//MCP23017 uses port B for band bits.
int plutoGpo=0;
//robs Waterfall
float inbuf[2];
FILE *fftstream;
FILE *txfftstream;
float buf[512][130];
int points=512;
int rows=130;
int FFTRef = -30;
int spectrum_rows=80;
unsigned char * palette;
int HzPerBin=94; //calculated from FFT width and number of samples. Width=48000 number of samples =512
int bwBarStart=3;
int bwBarEnd=34;
float sMeter; //peak reading S meter.
float sMeterPeak;
struct iio_context *plutoctx;
struct iio_device *plutophy;
//UDP server to receive FFT data from GNU RAdio
#define RXPORT 7373
#define TXPORT 7374
int main(int argc, char* argv[])
{
strcpy(plutoip,"ip:");
char * penv = getenv("PLUTO_IP");
if(penv==NULL)
{
strcpy(penv,"pluto.local");
}
strcat(plutoip,penv);
printf("plutoip = %s\n",plutoip);
initUDP();
lastClock=0;
readConfig();
detectHw();
initPluto();
initFifos();
initScreen();
initGPIO();
if(touchPresent) initTouch(touchPath);
if(mousePresent) initMouse(mousePath);
initGUI();
initSDR();
// RGB Vals Black > Blue > Green > Yellow > Red 4 gradients //number of gradients is varaible
gen_palette((char [][3]){ {0,0,0},{0,0,255},{0,255,0},{255,255,0},{255,0,0}},4);
setFFTPipe(1); //Turn on FFT Stream from GNU RAdio Receiver
while(1)
{
processGPIO();
if(touchPresent)
{
if(getTouch()==1)
{
processTouch();
}
}
if(mousePresent)
{
int but=getMouse();
if(but>0)
{
processMouse(but);
}
if(twoButTimer>0)
{
twoButTimer--;
if(twoButTimer==0)
{
lastBut=0;
}
}
}
if(sendBeacon==2)
{
dotCount=dotCount+1;
if(dotCount==1)
{
setKey(1);
}
if(dotCount==12)
{
setKey(0);
}
if(dotCount==25)
{
dotCount=0;
}
}
if(sendBeacon==1) //sending CWID
{
if(keyDownTimer>0)
{
if((keyDownTimer>100) &&( keyDownTimer < CWIDkeyDownTime-100)) //Key down between Idents
{
setKey(1);
}
else
{
setKey(0);
}
keyDownTimer--;
}
else
{
int ret=morseKey(); //get the next key from morse string
if(ret==-1) // Ident finished
{
keyDownTimer=CWIDkeyDownTime + 100; //key down for this time between idents. Add 1 second to force a minimum key up gap between idents.
}
else
{
setKey(ret);
}
}
}
waterfall();
if(configCounter>0) //save the config after 5 seconds of inactivity.
{
configCounter=configCounter-1;
if(configCounter==0)
{
writeConfig();
}
}
if(firstpass==1)
{
setTx(1); //seems to be needed to initialise Pluto
setTx(0);
firstpass=0;
}
while(runTimeMs() < (lastClock + 10)) //delay until the next iteration at 100 per second (10ms)
{
usleep(100);
}
lastClock=runTimeMs();
}
}
long long runTimeMs(void)
{
struct timeval tt;
gettimeofday(&tt,NULL);
if(progStartTime==0)
{
progStartTime=tt.tv_sec;
}
tt.tv_sec=tt.tv_sec - progStartTime;
return ((tt.tv_sec*1000) + (tt.tv_usec/1000));
}
void gen_palette(char colours[][3], int num_grads){
//allocate some memory, size of palette
palette = malloc(sizeof(char)*256*3);
int diff[3];
float scale=256/(num_grads);
int pos=0;
for(int i=0;i<num_grads;i++){
//get differences in colours for current gradient
diff[0]=(colours[i+1][0]-colours[i][0])/(scale-1);
diff[1]=(colours[i+1][1]-colours[i][1])/(scale-1);
diff[2]=(colours[i+1][2]-colours[i][2])/(scale-1);
//create the palette built up of multiple gradients
for(int n=0;n<scale;n++){
palette[pos*3+2]=colours[i][0]+(n*diff[0]);
palette[pos*3+1]=colours[i][1]+(n*diff[1]);
palette[pos*3]=colours[i][2]+(n*diff[2]);
pos++;
}
}
}
void waterfall()
{
int level,level2;
int ret;
int baselevel;
int bwbaroffset;
float scaling;
int fftref;
int centreShift=0;
//check if data avilable to read
if((transmitting==1) && (satSplitMode()==0))
{
ret = fread(&inbuf,sizeof(float),1,txfftstream);
fftref=10;
}
else
{
ret = fread(&inbuf,sizeof(float),1,fftstream);
fftref=FFTRef;
}
if(ret>0)
{
//shift buffer
for(int r=rows-1;r>0;r--)
{
for(int p=0;p<points;p++)
{
buf[p][r]=buf[p][r-1];
}
}
buf[0+points/2][0]=inbuf[0]; //use the read value
//Read in float values, shift centre and store in buffer 1st 'row'
for(int p=1;p<points;p++)
{
if((transmitting==1) && (satSplitMode()==0))
{
fread(&inbuf,sizeof(float),1,txfftstream);
}
else
{
fread(&inbuf,sizeof(float),1,fftstream);
}
if(p<points/2)
{
buf[p+points/2][0]=inbuf[0];
}else
{
buf[p-points/2][0]=inbuf[0];
}
}
if(((mode==CW)||(mode==CWN)) && (transmitting==1) && (satSplitMode()==0))
{
bwbaroffset=800/HzPerBin;
}
else
{
bwbaroffset=0;
}
//use raw values to calculate S meter value
//use highest reading within the receiver bandwidth
sMeterPeak=-200;
for (int p=points/2+bwBarStart-bwbaroffset;p<points/2+bwBarEnd-bwbaroffset;p++)
{
if(buf[p][0]>sMeterPeak)
{
sMeterPeak=buf[p][0];
}
}
//RF level adjustment
baselevel=fftref-80;
scaling = 255.0/(float)(fftref-baselevel);
//draw waterfall
for(int r=0;r<rows;r++)
{
for(int p=0;p<points;p++)
{
//limit values displayed to range specified
if (buf[p][r]<baselevel) buf[p][r]=baselevel;
if (buf[p][r]>fftref) buf[p][r]=fftref;
//scale to 0-255
level = (buf[p][r]-baselevel)*scaling;
setPixel(p+FFTX,FFTY+20+r,palette[level*3+2],palette[level*3+1],palette[level*3]);
}
}
//draw spectrum bars
scaling = spectrum_rows/(float)(fftref-baselevel);
for(int p=0;p<points-1;p++)
{
//limit values displayed to range specified
if (buf[p][0]<baselevel) buf[p][0]=baselevel;
if (buf[p][0]>fftref) buf[p][0]=fftref;
//scale to display height
level = (baselevel)*scaling;
level = (buf[p][0]-baselevel)*scaling;
drawLine(p+FFTX, FFTY, p+FFTX, FFTY-level,0,255,0); //draw the signal bar in Green
drawLine(p+FFTX, FFTY-level-1, p+FFTX, FFTY-spectrum_rows,0,0,0); // fill the rest of the bar with black (saves having to clear the whole area)
}
//draw Bandwidth indicator
int p=points/2;
if (((mode==CW) || (mode==CWN)) && (transmitting==0 && satSplitMode()== 0))
{
centreShift=800/HzPerBin;
}
else
{
centreShift=0;
}
drawLine(p+FFTX+bwBarStart-bwbaroffset, FFTY-spectrum_rows+5, p+FFTX+bwBarStart-bwbaroffset, FFTY-spectrum_rows,255,140,0);
drawLine(p+FFTX+bwBarStart-bwbaroffset, FFTY-spectrum_rows, p+FFTX+bwBarEnd-bwbaroffset, FFTY-spectrum_rows,255,140,0);
drawLine(p+FFTX+bwBarEnd-bwbaroffset, FFTY-spectrum_rows+5, p+FFTX+bwBarEnd-bwbaroffset, FFTY-spectrum_rows,255,140,0);
//draw centre line (displayed frequency)
drawLine(p+FFTX+centreShift, FFTY-10, p+FFTX+centreShift, FFTY-spectrum_rows,255,0,0);
//draw Scale.
setForeColour(0,255,0);
textSize=1;
gotoXY(p+centreShift+FFTX-12,FFTY+8);
displayStr(" 0 ");
gotoXY(p+centreShift+FFTX-10000/HzPerBin-24,FFTY+8);
displayStr(" -10k ");
gotoXY(p+centreShift+FFTX-20000/HzPerBin-24,FFTY+8);
displayStr(" -20k ");
gotoXY(p+centreShift+FFTX+10000/HzPerBin-24,FFTY+8);
displayStr(" +10k ");
gotoXY(p+centreShift+FFTX+20000/HzPerBin-24,FFTY+8);
displayStr(" +20k ");
if((transmitting==0) || (satSplitMode()==1))
{
S_Meter();
}
else
{
P_Meter();
}
}
}
void S_Meter(void)
{
char smStr[10];
static int sMeterCount=0;
sMeterPeak=sMeterPeak-bandSmeterZero[band]; //adjust offset to give positive values for s-meter
int dbOver=0;
int sValue=0;
if(bandRxGain[band]==100) //if we are in RF AGC mode
{
sMeterPeak=sMeterPeak + maxGain(freq) - readPlutoRxGain(); //compensate for reduced gain due to AGC action
}
if(sMeterPeak < 0) sMeterPeak=0;
if(sMeterPeak >= sMeter)
{
sMeter=sMeterPeak; //fast attack
}
else
{
if(sMeter > 0) sMeter=sMeter-2; //slow decay
}
if(sMeter<55)
{
sValue=sMeter/6;
}
else
{
sValue=9;
dbOver=sMeter-54;
}
//Draw S meter
drawLine(sMeterX,sMeterY,sMeterX+sMeterWidth,sMeterY,255,255,255);
drawLine(sMeterX,sMeterY,sMeterX,sMeterY+sMeterHeight,255,255,255);
drawLine(sMeterX,sMeterY+sMeterHeight,sMeterX+sMeterWidth,sMeterY+sMeterHeight,255,255,255);
drawLine(sMeterX+sMeterWidth,sMeterY,sMeterX+sMeterWidth,sMeterY+sMeterHeight,255,255,255);
for(int ln=0;ln<10;ln++)
{
if(sMeter<55)
{
drawLine(sMeterX+5,sMeterY+5+ln,sMeterX+6+sMeter*2,sMeterY+5+ln,0,255,0);
drawLine(sMeterX+6+sMeter*2,sMeterY+5+ln,sMeterX+6+160,sMeterY+5+ln,0,0,0);
}
else
{
int redbit=sMeterX+6+110+(sMeter-55)*2;
if(redbit > (sMeterX+6+160)) redbit= sMeterX+6+160;
drawLine(sMeterX+5,sMeterY+5+ln,sMeterX+6+110,sMeterY+5+ln,0,255,0);
drawLine(sMeterX+6+110,sMeterY+5+ln,redbit,sMeterY+5+ln,255,0,0);
drawLine(redbit,sMeterY+5+ln,sMeterX+6+160,sMeterY+5+ln,0,0,0);
}
}
sMeterCount++;
if(sMeterCount>5)
{
sMeterCount=0;
textSize=2;
setForeColour(0,255,0);
gotoXY(sMeterX+10,sMeterY+20);
if (sMeterType == 0)
{
sprintf(smStr,"S%d",sValue);
displayStr(smStr);
if(dbOver>0)
{
sprintf(smStr,"+%ddB ",dbOver);
displayStr(smStr);
}
else
{
displayStr(" ");
}
}
else
{
sprintf(smStr,"%.0f dB ",sMeter);
displayStr(smStr);
}
}
}
void P_Meter(void)
{
char smStr[10];
static int sMeterCount=0;
sMeterPeak=(sMeterPeak+50)*2.1;
if(sMeterPeak < 0) sMeterPeak=0;
if(sMeterPeak >100) sMeterPeak=100; // Now Scaled between 0 and 100
if(sMeterPeak >= sMeter)
{
sMeter=sMeterPeak; //fast attack
}
else
{
if(sMeter > 0)
{
if((mode==CW) || (mode==CWN))
{
sMeter=sMeter-10; //fast decay for CW
}
else if((mode==USB) || (mode==LSB))
{
sMeter=sMeter-3; //Slow Decay for SSB.
}
else
{
sMeter=sMeter-0.5; //very slow decay
}
}
}
if(sMeter<0) sMeter=0;
//Draw PO meter
drawLine(sMeterX,sMeterY,sMeterX+sMeterWidth,sMeterY,255,255,255);
drawLine(sMeterX,sMeterY,sMeterX,sMeterY+sMeterHeight,255,255,255);
drawLine(sMeterX,sMeterY+sMeterHeight,sMeterX+sMeterWidth,sMeterY+sMeterHeight,255,255,255);
drawLine(sMeterX+sMeterWidth,sMeterY,sMeterX+sMeterWidth,sMeterY+sMeterHeight,255,255,255);
for(int ln=0;ln<10;ln++)
{
drawLine(sMeterX+5,sMeterY+5+ln,sMeterX+6+sMeter*1.5,sMeterY+5+ln,0,255,0);
drawLine(sMeterX+6+sMeter*1.5,sMeterY+5+ln,sMeterX+6+160,sMeterY+5+ln,0,0,0);
}
sMeterCount++;
if(sMeterCount>5)
{
sMeterCount=0;
textSize=2;
setForeColour(0,255,0);
gotoXY(sMeterX+10,sMeterY+20);
displayStr("Tx Level");
}
}
void detectHw()
{
FILE * fp;
char * ln=NULL;
size_t len=0;
ssize_t rd;
int p;
char handler[2][10];
char * found;
p=0;
mousePresent=0;
touchPresent=0;
portsdownPresent=0;
fp=fopen("/proc/bus/input/devices","r");
while ((rd=getline(&ln,&len,fp)!=-1))
{
if(ln[0]=='N') //name of device
{
p=0;
if((strstr(ln,"FT5406")!=NULL) || (strstr(ln,"pi-ts")!=NULL)) //Found Raspberry Pi TouchScreen entry
{
p=1;
hyperPixelPresent=0;
}
if(strstr(ln,"Goodix")!=NULL) //Found Hyperpixel TouchScreen entry
{
p=1;
hyperPixelPresent=1;
}
}
if(ln[0]=='H') //handlers
{
if(strstr(ln,"mouse")!=NULL)
{
found=strstr(ln,"event");
strcpy(handler[p],found);
handler[p][6]=0;
if(p==0)
{
sprintf(mousePath,"/dev/input/%s",handler[0]);
mousePresent=1;
}
if(p==1)
{
sprintf(touchPath,"/dev/input/%s",handler[1]);
touchPresent=1;
}
}
}
}
fclose(fp);
if(ln) free(ln);
if ((fp = fopen("/home/pi/rpidatv/bin/rpidatvgui", "r"))) //test to see if Portsdown file is present. If so we change the exit behaviour.
{
fclose(fp);
portsdownPresent=1;
}
if ((fp = fopen("/home/pi/hyperpixel4/install.sh", "r"))) //test to see if Hyperpixel4 file is present. If so we dont use the GPIO pins.
{
fclose(fp);
hyperPixelPresent=1;
}
else
{
hyperPixelPresent=0;
}
plutoPresent=1; //this will be reset by setPlutoFreq if Pluto is not present.
// try to initialise MCP23017 i2c chip for additonal I/O
// this will set or reset MCP23017Present flag
initMCP23017(mcp23017_addr);
}
void displayError(char*st)
{
gotoXY(errorX,errorY);
setForeColour(255,0,0);
textSize=2;
displayStr(st);
}
void initPluto(void)
{
plutoctx = iio_create_context_from_uri(plutoip);
if(plutoctx==NULL)
{
plutoPresent=0;
displayError("Pluto not responding");
return;
}
else
{
plutophy = iio_context_find_device(plutoctx, "ad9361-phy");
}
}
void setPlutoRxFreq(long long rxfreq)
{
int ret;
if(plutoPresent)
{
ret=iio_channel_attr_write_longlong(iio_device_find_channel(plutophy, "altvoltage0", true),"frequency", rxfreq); //Rx LO Freq
if(ret<0)
{
displayError("Pluto not responding");
}
}
}
void setPlutoTxFreq(long long txfreq)
{
int ret;
if(plutoPresent)
{
ret=iio_channel_attr_write_longlong(iio_device_find_channel(plutophy, "altvoltage1", true),"frequency", txfreq); //Tx LO Freq
if(ret<0)
{
displayError("Pluto not responding");
}
}
}
void setPlutoTxAtt(int att)
{
if(plutoPresent)
{
iio_channel_attr_write_double(iio_device_find_channel(plutophy, "voltage0", true),"hardwaregain", (double)att); //set Tx Attenuator
}
}
void setPlutoRxGain(int gain)
{
if(plutoPresent)
{
if(gain>maxGain(freq))
{
iio_channel_attr_write(iio_device_find_channel(plutophy, "voltage0", false),"gain_control_mode", "slow_attack"); //set Auto Gain
}
else
{
iio_channel_attr_write(iio_device_find_channel(plutophy, "voltage0", false),"gain_control_mode", "manual"); //set Manual Gain control
iio_channel_attr_write_double(iio_device_find_channel(plutophy, "voltage0", false),"hardwaregain", (double)gain); //set Rx Gain
}
}
}
int readPlutoRxGain(void)
{
double ret;
if(plutoPresent)
{
iio_channel_attr_read_double(iio_device_find_channel(plutophy, "voltage0", false),"hardwaregain", &ret); //Read current Rx Gain
return (int) ret;
}
else
{
return 73;
}
}
void PlutoTxEnable(int txon)
{
if(plutoPresent)
{
if(txon==0)
{
iio_channel_attr_write_bool(iio_device_find_channel(plutophy, "altvoltage1", true),"powerdown", true); //turn off TX LO