-
Notifications
You must be signed in to change notification settings - Fork 0
/
elektor.c
1678 lines (1494 loc) · 49 KB
/
elektor.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
//
// Code for the Elektor USB SDR
//
// Driver for the USB SDR as described in the May 2007 Elektor magazine.
//
// Based on users_tr.c from Linrad, and on SDRelektor.pas from
// Burkhard Kainka, and ElektorSDR4Linux.c by Markus Goppelt,
// which are available at web-site :- http://www.b-kainka.de
//
// This driver requires the USB FTDI Driver library, libftdi.
//
//
//
// M. J. Keehan, 12th Feb 2013 ([email protected])
//
//
//
// USERGUIDE
//
// Moving the usergraphic:
// Move the usergraphic on the screen by hitting the left side of the box
// with the mousepointer, holding the left mouse button down, and dragging
// it to the desired position. Also works with the upper side.
//
// Setting the VFO frequency:
// Enter a value in Mhz into the upper field in the small Frequency/Gain box.
// Or click on the adjacent up/down arrows to shift the frequency by the
// increment value (default 20kHz).
// Elektor's frequency band is calculated from the frequency entered.
//
// Frequency increment
// The value used in the frequency up/down arrows, default 20kHz, range 1kHz-1MHz.
//
// Aerial Multiplexor (Mux)
// The eight mux settings can be chosen manually by either entering the number
// (0-7) into the Mux box, or by incrementing/decrementing using the associated
// buttons. Choosing the value 8 puts the software into Automatic mode, where
// the Mux is chosen according to the frequency in use.
//
// Pre-Selector
// Elektor's PreSelector option board is catered for. The preselector
// settings are loaded from a file - with the name stored in the variable
// 'preselector_parfile_name' - when Linrad starts.
// The values will be sent to the board when Mux is set to 3,4,5 or 6, or if
// Mux is set to 8 (auto), then the Aerial Mux to use is chosen from the
// available preselector channels if one covers the current tuning frequency.
// The Pre-Selector has a range of 8 bits for its settings, i.e. values 0 - 255.
// This canbe entered manually, or by use of coarse buttons which increment/decrement
// by 10, or by fine buttons which increment/decrement by 1.
// If no file exists at startup, then all preselector values are set to 0, and
// manual input is required for each specific presel channel to tune it.
//
// Pre-Selector tuning
// The preselector can be tuned when Mux=3,4,5,6 or 8.
// When Aerial Mux=8 is selected, preselector tuning points are not saved to file.
// When specific preselector channels are chosen, i.e. Mux=3,4,5 or 6, then when
// preselector tuning is performed, the settings are saved to the parameter file.
// An array of ten values are kept in memory to cover the preselector range.
// The software maintains a guard value at each end of the array, both to
// ensure that the limit values are present (0 & 255), and to make
// interpolation between settings easier to determine when near band edges.
// The software interpolates linearly between the
// array values as the frequency is changed.
// The software tries to keep entries spaced more than ~5% apart by frequency.
// The contents of the preselector parfile can be edited by hand if values
// closer than 5% are required, or change the divisor used in function
// withinXpercent() below.
// To clear all settings and setup anew, edit the file and delete all values
// for the required preselector channel(s), or delete the whole file. Restart
// Linrad and tune the preselector at up to ten signals spread across the
// preselector's tuning range. It is not necessary to find signals which make
// the values 0 and 255 as the software will extrapolate these from whatever
// values are in the array. Neither is it necessary to go through all ten
// tunings, just as many as you need accurate settings for.
// The file format is described in the comments to the function
// "Load_PreSelectFile" at the end of this source.
//
// Fine tune the Crystal
// The Elektor's 10MHz crystal can be fine tuned over the range 0-255 in the
// lowest line of the usergraphic box. Either enter a value by clicking on
// the current value and using the keyboard, or use the up/down arrow keys
// to increment/decrement the current value. The actual frequency change
// represented by this range (0-255) depends upon the crystal itself. I get
// approx +-1.1kHz at 10MHz.
//
// Test signal
// The Elektor provides a test signal at 5MHz on aerial Mux 7, at a strength
// of S9 +40dB.
//
// Note: The usergraphic position, the Xtal fine tune, and frequency increment
// values for each frequency band are saved in a parameterfile and are
// retrieved when Linrad is started.
// The file name is "par_xxx_elektor" (with xxx = rx_mode) and its
// content can be displayed (and changed - carefully) with an editor.
//
//
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include "loadusb.h"
#include "uidef.h"
#include "fft1def.h"
#include "screendef.h"
#include "seldef.h"
#include "rusage.h"
#include "thrdef.h"
#define IIC_EEPROM 208 // Elektor's hardware interface
#define IIC_RAM 210 // Elektor's hardware interface
#define PCF8951_ADDRESS 0x90 // Elektor's preselector interface
#define DAC_ENABLE 0x40 // Elektor's preselector interface
// UserGraphic Button defs.
#define UG_TOP 0
#define UG_BOTTOM 1
#define UG_LEFT 2
#define UG_RIGHT 3
#define UG_INCREASE_OFFS_XTAL 4
#define UG_DECREASE_OFFS_XTAL 5
#define UG_INCREASE_OFFS_KHZ 6
#define UG_DECREASE_OFFS_KHZ 7
#define UG_INCREASE_MUX_NMBR 8
#define UG_DECREASE_MUX_NMBR 9
#define UG_INCREASE_PRESEL 10
#define UG_DECREASE_PRESEL 11
#define UG_INCREASE_PRESELFINE 12
#define UG_DECREASE_PRESELFINE 13
#define MAX_EGRBUTT 14
//Size of messages in user graphic box
#define MAX_MSGSIZE 30
// Elektor Global variables.
// =========================
int FreqWanted;
int Fdti_Data_Count=0;
int Mux = 8; // Automatic mode
int ActiveMux = -1;
int VFOhigh;
int Write_Pins=0;
int PreselUpdated = 0;
unsigned char Div1N;
unsigned char FT_port;
unsigned char FtdiData[65536];
char* preselector_parfile_name={"par_elektor_preselector"};
struct bands
{ int Min[6];
int Max[6];
int VFOhigh[6];
unsigned char Div1N[6];
float High[6];
} bands_struct;
#define NUM_SETTINGS 10 // Allow for 10 PreSelector calibration values
struct preselval {
int freq[NUM_SETTINGS+2]; // adds a guard entry at each end.
int val[NUM_SETTINGS+2]; // ditto.
} PreSelData[4]; // Four pre-selector front-ends.
int PreSelValue = 0;
struct ftdi_context ftdic;
#define MAX_HWAREDRIVER_PARM 16
char *elektordriver_parm_text[MAX_HWAREDRIVER_PARM]={
"egr_xleft ",
"egr_ytop ",
"mux_number ",
"xtal_fine_0.1-0.8 ",
"xtal_fine_0.4-1.6 ",
"xtal_fine_0.8-3.2 ",
"xtal_fine_2-8 ",
"xtal_fine_4-16 ",
"xtal_fine_10-30 ",
"freq_incr_0.1-0.8 ",
"freq_incr_0.4-1.6 ",
"freq_incr_0.8-3.2 ",
"freq_incr_2-8 ",
"freq_incr_4-16 ",
"freq_incr_10-30 ",
"offs_khz_band_BIG ",
};
#define NO_OF_BANDS 6
typedef struct {
int egr_xleft;
int egr_ytop;
int aerial;
int offs_hz[NO_OF_BANDS];
int offs_khz[NO_OF_BANDS];
int offs_khz_band_BIG;
} HWAREDRIVER_PARMS;
HWAREDRIVER_PARMS elektordriver;
typedef struct {
int ytop;
int ybottom;
int xleft;
int xright;
int yborder; // required for move graph
} UG_PARMS;
UG_PARMS egr;
BUTTONS egrbutt[MAX_EGRBUTT];
int egr_old_y1;
int egr_old_y2;
int egr_old_x1;
int egr_old_x2;
int egr_oldx; // required for move graph
int egr_oldy; // required for move graph
int egr_yborder; // required for move graph
int egr_hsiz; // required for move graph
int egr_vsiz; // required for move graph
int egr_band_no;
int elektor_graph_scro;
char egr_msg0[MAX_MSGSIZE]; //messages in usergraph
char egr_msg1[MAX_MSGSIZE];
char egr_msg2[MAX_MSGSIZE];
int egr_msg_color; //switch for message-color in usergraph
// Prototypes
void append_freq(char *, double);
void add_guard_values(int);
void calc_Band(double);
int calc_Mux(double);
void calc_PreselVal(int);
void ChangeElektorFreq(int);
double EFreq(int);
void ftdi_I2C_Init(void);
void ftdi_I2C_Start(void);
void ftdi_I2C_Stop(void);
void ftdi_I2C_Write_Byte(int);
void I2C_Init(void);
void i2c_write_reg(int, int, int);
void init_bands(void);
int init_Elektor(void);
void Load_PreSelectFile(void);
void print_array(void);
void save_presel_to_file(void);
void SCL(int);
void SDA(int);
int Set_Elektor_Atten(int);
int Set_Elektor_Freq(double);
int Set_Elektor_Mux(int);
int Set_Elektor_PreSelect(void);
void SetupBytes(unsigned char);
void Setup_Preselector(void);
void show_elektor_parms(void);
void Start(void);
void Stop(void);
void update_presel_settings(int, int);
int valid_Presel(int);
int withinXpercent(int, int, int);
void WriteData(void *, int);
void elektor_setup(void)
{
int retval;
retval=load_ftdi_library();
if(retval != 0)return;
// Routine to set up elektor from the soundcard setup menu.
clear_screen();
lir_text(3,3,"Elektor selected. (Nothing to do)");
lir_text(3,6,press_any_key);
await_processed_keyboard();
}
// ***************************************************************************
// ROUTINES FOR THE PROCESSING OF A 'MOVABLE' FIXED SIZE USERGRAPH
// The screenposition of the usergraph and its variables are saved
// in the parameterfile "par_xxx_elektor" ( with xxx = rx_mode)
// in the linrad directory
// ***************************************************************************
void check_egr_borders(void) // required for move graph
{
current_graph_minh=egr_vsiz;
current_graph_minw=egr_hsiz;
check_graph_placement((void*)(&egr));
set_graph_minwidth((void*)(&egr));
}
void make_elektor_control_graph(void)
{
pause_thread(THREAD_SCREEN);
check_egr_borders(); // required for move graph
scro[elektor_graph_scro].no=ELEKTOR_GRAPH;
// These are the coordinates of the border lines.
scro[elektor_graph_scro].x1=egr.xleft;
scro[elektor_graph_scro].x2=egr.xright;
scro[elektor_graph_scro].y1=egr.ytop;
scro[elektor_graph_scro].y2=egr.ybottom;
// Each border line is treated as a button.
// That is for the mouse to get hold of them so the window can be moved.
egrbutt[UG_LEFT].x1=egr.xleft;
egrbutt[UG_LEFT].x2=egr.xleft+2;
egrbutt[UG_LEFT].y1=egr.ytop;
egrbutt[UG_LEFT].y2=egr.ybottom;
egrbutt[UG_RIGHT].x1=egr.xright;
egrbutt[UG_RIGHT].x2=egr.xright-2;
egrbutt[UG_RIGHT].y1=egr.ytop;
egrbutt[UG_RIGHT].y2=egr.ybottom;
egrbutt[UG_TOP].x1=egr.xleft;
egrbutt[UG_TOP].x2=egr.xright;
egrbutt[UG_TOP].y1=egr.ytop;
egrbutt[UG_TOP].y2=egr.ytop+2;
egrbutt[UG_BOTTOM].x1=egr.xleft;
egrbutt[UG_BOTTOM].x2=egr.xright;
egrbutt[UG_BOTTOM].y1=egr.ybottom;
egrbutt[UG_BOTTOM].y2=egr.ybottom-2;
// Draw the border lines
graph_borders((void*)&egr,7);
egr_oldx=-10000; // from freq_control
settextcolor(7);
make_button(egr.xleft+27.5*text_width+2,-2+egr.ybottom-1*text_height/2-1,
egrbutt,UG_DECREASE_OFFS_XTAL,25);
make_button(egr.xleft+29*text_width+2,-2+egr.ybottom-1*text_height/2-1,
egrbutt,UG_INCREASE_OFFS_XTAL,24);
make_button(egr.xleft+27.5*text_width+2,-2+egr.ybottom-3*text_height/2-1,
egrbutt,UG_DECREASE_OFFS_KHZ,25);
make_button(egr.xleft+29*text_width+2,-2+egr.ybottom-3*text_height/2-1,
egrbutt,UG_INCREASE_OFFS_KHZ,24);
make_button(egr.xleft+27.5*text_width+2,-2+egr.ybottom-5*text_height/2-1,
egrbutt,UG_DECREASE_MUX_NMBR,25);
make_button(egr.xleft+29*text_width+2,-2+egr.ybottom-5*text_height/2-1,
egrbutt,UG_INCREASE_MUX_NMBR,24);
// presel buttons
make_button(egr.xleft+54*text_width+2,-3+egr.ybottom-1*text_height/2-1,
egrbutt,UG_DECREASE_PRESEL,25);
make_button(egr.xleft+55.5*text_width+2,-3+egr.ybottom-1*text_height/2-1,
egrbutt,UG_DECREASE_PRESELFINE,25);
make_button(egr.xleft+57*text_width+2,-3+egr.ybottom-1*text_height/2-1,
egrbutt,UG_INCREASE_PRESELFINE,24);
make_button(egr.xleft+58.5*text_width+2,-3+egr.ybottom-1*text_height/2-1,
egrbutt,UG_INCREASE_PRESEL,24);
// Draw separator lines in usergraph
// vertical
lir_line(egr.xleft+30.5*text_width,egr.ytop+1.3*text_height,
egr.xleft+30.5*text_width,egr.ybottom,7);
// horizontal
lir_line(egr.xleft,egr.ytop+1.3*text_height,egr.xright,egr.ytop+1.3*text_height,7);
elektor_rx_freq_control();
show_elektor_parms();
resume_thread(THREAD_SCREEN);
}
void blankfill_egrmsg(char *msg)
{ int i;
i=strlen(msg);
if(i>=MAX_MSGSIZE)
lirerr(3413400);
while(i<MAX_MSGSIZE-1)
{
msg[i]=' ';
i++;
}
msg[MAX_MSGSIZE-1]=0;
}
// Show the user parameters on screen
void show_elektor_parms(void)
{ char s[80];
char *mode = "";
if ( elektordriver.offs_khz_band_BIG == 0 ) // if control window does not yet exist, exit.
return;
switch(rx_mode)
{
case MODE_WCW: mode = "WCW"; break;
case MODE_HSMS: mode = "HSMS"; break;
case MODE_QRSS: mode = "QRSS"; break;
case MODE_NCW: mode = "NCW"; break;
case MODE_SSB: mode = "SSB"; break;
case MODE_FM: mode = "FM "; break;
case MODE_AM: mode = "AM "; break;
}
// Note that mouse control is done in the narrowband processing thread
// and that you can not use lir_sleep to wait for hardware response here.
hide_mouse(egr.xleft, egr.xright,egr.ytop,egr.ybottom);
settextcolor(11);
sprintf(s," LINRAD SDR (Elektor USB) %s", mode);
settextcolor(14);
lir_pixwrite(egr.xleft+17.5*text_width,egr.ytop+0.35*text_height,s);
settextcolor(15);
sprintf(s," Aerial Mux: %d ", Mux);
lir_pixwrite(egr.xleft+.5*text_width,egr.ytop+1.5*text_height,s);
egr_msg_color=10;
settextcolor(15);
sprintf(s,"Fine tune Xtal : %d ", elektordriver.offs_hz[egr_band_no]);
s[25]=0;
lir_pixwrite(egr.xleft+1.5*text_width,egr.ytop+1+3.5*text_height,s);
sprintf(s,"Freq incr (Khz): %d ", elektordriver.offs_khz[egr_band_no]);
s[25]=0;
lir_pixwrite(egr.xleft+1.5*text_width,egr.ytop+2.5*text_height,s);
sprintf(egr_msg0,"Elektor Freq: ");
append_freq(egr_msg0,EFreq(FreqWanted)*1000);
//sprintf(egr_msg1,"Linrad Freq: ");
//append_freq(egr_msg1,floor(hwfreq+0.5)*1000);
if ( ActiveMux >= 3 && ActiveMux <= 6 )
sprintf(egr_msg2,"Preselect: %d %5d", ActiveMux-2, PreSelValue);
else
sprintf(egr_msg2,"Preselect: none ");
blankfill_egrmsg(egr_msg0);
//blankfill_egrmsg(egr_msg1);
blankfill_egrmsg(egr_msg2);
egr_msg2[MAX_MSGSIZE-8]=0;
settextcolor(egr_msg_color);
lir_pixwrite(egr.xleft+31*text_width,egr.ytop+1.5*text_height,egr_msg0);
//lir_pixwrite(egr.xleft+31*text_width,egr.ytop+2.5*text_height,egr_msg1);
lir_pixwrite(egr.xleft+31*text_width,egr.ytop+3.5*text_height,egr_msg2);
settextcolor(7);
}
void save_elektor_parms(void)
{ int i;
int *sdr_pi;
char elektordriver_parfil_name[20];
FILE *elektordriver_file;
// Save screenposition, mux-number, and other values
// to the parameterfile on disk
sprintf(elektordriver_parfil_name,"%s_hwd_egr",rxpar_filenames[rx_mode]);
elektordriver_file = fopen(elektordriver_parfil_name,"w");
elektordriver.egr_xleft = egr.xleft;
elektordriver.egr_ytop = egr.ytop;
elektordriver.aerial = Mux;
sdr_pi = (int*)(&elektordriver);
for(i=0; i<MAX_HWAREDRIVER_PARM; i++)
fprintf(elektordriver_file,"%s [%d]\n",elektordriver_parm_text[i],sdr_pi[i]);
parfile_end(elektordriver_file);
save_presel_to_file();
}
void mouse_continue_elektor_graph(void)
{ int i, j; // required for move graph
// Move border lines immediately.
// Other functions must wait until button is released.
// Move fixed size window based on coding in freq_control.c
switch (mouse_active_flag-1)
{
case UG_TOP:
if (egr.ytop!=mouse_y)
break;
case UG_BOTTOM:
if (egr.ybottom!=mouse_y)
break;
case UG_LEFT:
if (egr.xleft!=mouse_x)
break;
case UG_RIGHT:
if (egr.xright==mouse_x)
break;
break;
default:
goto await_release;
}
pause_screen_and_hide_mouse();
graph_borders((void*)&egr,0);
if (egr_oldx==-10000)
{
egr_oldx=mouse_x;
egr_oldy=mouse_y;
}
else
{
i=mouse_x-egr_oldx;
j=mouse_y-egr_oldy;
egr_oldx=mouse_x;
egr_oldy=mouse_y;
egr.ytop+=j;
egr.ybottom+=j;
egr.xleft+=i;
egr.xright+=i;
check_egr_borders();
egr.yborder=(egr.ytop+egr.ybottom)>>1;
}
graph_borders((void*)&egr,15);
resume_thread(THREAD_SCREEN);
if (leftpressed == BUTTON_RELEASED)
goto finish;
return;
await_release:;
if (leftpressed != BUTTON_RELEASED)
return;
// Assuming the user wants to control hardware, allow
// commands only when data is not over the network.
if((ui.network_flag&NET_RX_INPUT) == 0) //for linrad02.23
{
switch (mouse_active_flag-1)
{
case UG_INCREASE_OFFS_XTAL:
if ( elektordriver.offs_hz[egr_band_no] < 255 )
{ elektordriver.offs_hz[egr_band_no]++;
elektor_rx_freq_control();
}
break;
case UG_DECREASE_OFFS_XTAL:
if ( elektordriver.offs_hz[egr_band_no] > 0 )
{ elektordriver.offs_hz[egr_band_no]--;
elektor_rx_freq_control();
}
break;
case UG_INCREASE_OFFS_KHZ:
if ( elektordriver.offs_khz[egr_band_no] < 1000 )
{ elektordriver.offs_khz[egr_band_no]++;
fg.passband_increment = (double) elektordriver.offs_khz[egr_band_no]/1000;
}
break;
case UG_DECREASE_OFFS_KHZ:
if ( elektordriver.offs_khz[egr_band_no] > 0 )
{ elektordriver.offs_khz[egr_band_no]--;
fg.passband_increment = (double) elektordriver.offs_khz[egr_band_no]/1000;
}
break;
case UG_INCREASE_MUX_NMBR:
if (Mux <= 7)
++Mux;
if ( Mux == 8 ) // have to call set-elektor-freq to do the auto mux
Set_Elektor_Freq(fg.passband_center);
else
Set_Elektor_Mux(Mux);
Setup_Preselector();
break;
case UG_DECREASE_MUX_NMBR:
if (Mux > 0)
--Mux;
Set_Elektor_Mux(Mux);
Setup_Preselector();
break;
case UG_INCREASE_PRESELFINE:
if ( ActiveMux >=3 && ActiveMux <= 6 )
if ( PreSelValue < 255 )
{ PreSelValue++;
update_presel_settings(ActiveMux-3, PreSelValue);
Set_Elektor_PreSelect();
}
break;
case UG_DECREASE_PRESELFINE:
if ( ActiveMux >=3 && ActiveMux <= 6 )
if ( PreSelValue > 0 )
{ PreSelValue--;
update_presel_settings(ActiveMux-3, PreSelValue);
Set_Elektor_PreSelect();
}
break;
case UG_INCREASE_PRESEL:
if (ActiveMux >=3 && ActiveMux <= 6 )
{ if ( PreSelValue <= 245 )
PreSelValue+=10;
else
PreSelValue = 255;
update_presel_settings(ActiveMux-3, PreSelValue);
Set_Elektor_PreSelect();
}
break;
case UG_DECREASE_PRESEL:
if ( ActiveMux >=3 && ActiveMux <= 6 )
{ if ( PreSelValue >= 10 )
PreSelValue-=10;
else
PreSelValue = 0;
update_presel_settings(ActiveMux-3, PreSelValue);
Set_Elektor_PreSelect();
}
break;
default: // This should never happen.
lirerr(211053);
break;
}
}
finish:;
hide_mouse(egr_old_x1,egr_old_x2,egr_old_y1,egr_old_y2);
leftpressed=BUTTON_IDLE;
mouse_active_flag=0;
graph_borders((void*)&egr,0);
lir_fillbox(egr_old_x1,egr_old_y1,egr_old_x2-egr_old_x1,egr_old_y2-egr_old_y1,0);
make_elektor_control_graph();
egr_oldx=-10000;
}
void new_elektor_presel(void)
{
if ( numinput_int_data >= 0 && numinput_int_data <= 255 )
{
PreSelValue = numinput_int_data;
Set_Elektor_PreSelect();
pause_thread(THREAD_SCREEN);
show_elektor_parms();
resume_thread(THREAD_SCREEN);
}
}
void new_elektor_mux(void)
{
if ( numinput_int_data >= 0 && numinput_int_data <= 7 )
{
Mux = numinput_int_data;
Set_Elektor_Mux(Mux);
pause_thread(THREAD_SCREEN);
show_elektor_parms();
resume_thread(THREAD_SCREEN);
}
else if ( numinput_int_data == 8 )
{
Mux = numinput_int_data;
Set_Elektor_Freq(fg.passband_center);
}
Setup_Preselector();
}
void new_elektor_offs_xtal(void)
{
if ( numinput_int_data >= 0 && numinput_int_data <= 255 )
{
elektordriver.offs_hz[egr_band_no]=numinput_int_data;
pause_thread(THREAD_SCREEN);
show_elektor_parms();
resume_thread(THREAD_SCREEN);
elektor_rx_freq_control();
}
}
void new_elektor_offs_khz(void)
{
if ( numinput_int_data < 1 )
numinput_int_data = 1;
if ( numinput_int_data > 1000 )
numinput_int_data = 1000;
elektordriver.offs_khz[egr_band_no]=numinput_int_data;
fg.passband_increment = (double) elektordriver.offs_khz[egr_band_no]/1000;
pause_thread(THREAD_SCREEN);
show_elektor_parms();
resume_thread(THREAD_SCREEN);
}
void mouse_on_elektor_graph(void)
{ int event_no;
// First find out if we are on a button or border line.
for(event_no=0; event_no<MAX_EGRBUTT; event_no++)
{
if( egrbutt[event_no].x1 <= mouse_x &&
egrbutt[event_no].x2 >= mouse_x &&
egrbutt[event_no].y1 <= mouse_y &&
egrbutt[event_no].y2 >= mouse_y)
{
egr_old_y1=egr.ytop;
egr_old_y2=egr.ybottom;
egr_old_x1=egr.xleft;
egr_old_x2=egr.xright;
mouse_active_flag=1+event_no;
current_mouse_activity=mouse_continue_elektor_graph;
return;
}
}
mouse_active_flag=1;
if(mouse_x > (egr.xleft+50*text_width) && mouse_x < (egr.xleft+54*text_width))
{
numinput_xpix=egr.xleft+50*text_width;
if( (mouse_y > (egr.ytop+3.8*text_height)) && Mux >=3 && Mux <= 6 )
{
numinput_ypix=egr.ytop+3.6*text_height;
numinput_chars=3;
erase_numinput_txt();
numinput_flag=FIXED_INT_PARM;
par_from_keyboard_routine=new_elektor_presel; // PreSelector tune
}
}
if(mouse_x > egr.xleft+18*text_width && mouse_x < egr.xleft+25*text_width)
{
numinput_xpix=egr.xleft+18.5*text_width;
if(mouse_y > (egr.ytop+3.5*text_height))
{
numinput_ypix=egr.ytop+3.6*text_height;
numinput_chars=4;
erase_numinput_txt();
numinput_flag=FIXED_INT_PARM;
par_from_keyboard_routine=new_elektor_offs_xtal; // Xtal fine tune
}
else if(mouse_y > (egr.ytop+2.5*text_height))
{
numinput_ypix=egr.ytop+(2.4*text_height)+2;
numinput_chars=5;
erase_numinput_txt();
numinput_flag=FIXED_INT_PARM;
par_from_keyboard_routine=new_elektor_offs_khz; // vfo increment
}
else if(mouse_y > (egr.ytop+1.5*text_height))
{
numinput_ypix=egr.ytop+(1.4*text_height)+2;
numinput_chars=1;
erase_numinput_txt();
numinput_flag=FIXED_INT_PARM;
par_from_keyboard_routine=new_elektor_mux; // aerial multiplexor
}
}
else
{
// If we did not select a numeric input by setting numinput_flag
// we have to set mouse_active flag.
// Set the routine to mouse_nothing, we just want to
// set flags when the mouse button is released.
current_mouse_activity=mouse_nothing;
mouse_active_flag=1;
}
}
int init_elektor_control_window(void)
{ int i;
int errcod;
int *sdr_pi;
char elektordriver_parfil_name[20];
FILE *elektordriver_file;
// Get values from parameterfile
sprintf(elektordriver_parfil_name,"%s_hwd_egr",rxpar_filenames[rx_mode]);
errcod = read_sdrpar(elektordriver_parfil_name, MAX_HWAREDRIVER_PARM,
elektordriver_parm_text, (int*)((void*)&elektordriver));
if (errcod != 0)
{
elektordriver_file = fopen(elektordriver_parfil_name,"w");
//set_default_parameter values
elektordriver.egr_xleft = 60*text_width;
elektordriver.egr_ytop = (screen_last_line-4)*text_height;
elektordriver.aerial = 1;
for(i=0; i<NO_OF_BANDS; i++)
{
elektordriver.offs_hz[i] = 127; // middle ~ of range 0-255
elektordriver.offs_khz[i] = 20; // default of 20kHz increments
}
sdr_pi = (int*)(&elektordriver);
for( i=0; i<MAX_HWAREDRIVER_PARM; i++ )
{
fprintf(elektordriver_file,"%s [%d]\n",elektordriver_parm_text[i],sdr_pi[i]);
}
parfile_end(elektordriver_file);
}
Load_PreSelectFile();
Mux = elektordriver.aerial;
// Need to setup some stuff for Elektor before usergraphic is drawn
if(Set_Elektor_Freq(fg.passband_center) != EXIT_SUCCESS)return EXIT_FAILURE;
if(Set_Elektor_Atten(fg.gain) != EXIT_SUCCESS)return EXIT_FAILURE;
egr.xleft = elektordriver.egr_xleft;
egr.ytop = elektordriver.egr_ytop;
egr_msg_color = 10;
egr_msg1[0] = 0;
egr_hsiz = (30+MAX_MSGSIZE)*text_width; // WIDTH OF USERGRAPH
egr_vsiz = (4.8*text_height); // HEIGHT OF USERGRAPH
egr.xright = egr.xleft+egr_hsiz;
egr.ybottom = egr.ytop+egr_vsiz;
if(rx_mode < MODE_TXTEST)
{
elektor_graph_scro = no_of_scro;
make_elektor_control_graph();
no_of_scro++;
if(no_of_scro >= MAX_SCRO)
lirerr(89);
}
elektordriver.offs_khz_band_BIG = 1; // params setup OK now.
return EXIT_SUCCESS;
}
// ***************************************************************************
// END ROUTINES FOR THE PROCESSING OF A 'MOVABLE' FIXED SIZE USERGRAPH
// ***************************************************************************
// Append frequency formatted in Hz or kHz to the end of
// the already present text in the MAX_MSGSIZE buffer.
void append_freq(char *buffer, double fq)
{ int j, freq;
char *src, *dst, *tail = " Hz", stmp[MAX_MSGSIZE];
unsigned u,spaceleft;
freq = (int)fq; // need Hertz, and not fractions of
sprintf(stmp, "%d", freq);
// Find out how much space is left in given buffer.
spaceleft = MAX_MSGSIZE - strlen(buffer) - 1;
// Change freq to kHz if too many digits present as Hz
if ( (strlen(stmp) + strlen(" Hz")) > spaceleft )
{
freq = freq / 1000; // kHz
sprintf(stmp, "%d", freq);
tail = " kHz";
}
// space pad the rest of the buffer, and terminate with the tail.
for ( u=strlen(buffer) ; u<(MAX_MSGSIZE - strlen(tail) -1) ; ++u )
buffer[u] = ' ';
strcpy(&buffer[MAX_MSGSIZE - strlen(tail) -1], tail);
// copy frequency tail first, adding separators every 3rd digit
src = &stmp[strlen(stmp)];
dst = &buffer[MAX_MSGSIZE - strlen(tail) -1];
for ( u=strlen(stmp), j=0 ; u ; --u )
{
*--dst = *--src;
if ( (++j % 3) == 0 && u > 1 ) // but no sep. at front of digits
*--dst = ',';
}
}
// Is the given entry within X% of the current frequency or Preselect value?
// Use simple division - /20 for 5%, /10 for 10%, /32 for 3%...
int withinXpercent(int p, int i, int freq)
{ int ret;
ret = ( abs(PreSelData[p].freq[i]-freq) < (freq/(NUM_SETTINGS+6)) );
// fprintf( stderr,"within(%d,%d)=%d\n",pos,freq,ret);
return ret;
}
// Update preselector tuning value in memory array
void update_presel_settings(int p,int val)
{ int i, j, freq;
freq = floor(hwfreq);
//fprintf( stderr,"up-presel(%d,%d) ",p, val);
// If value is close to zero and array is full, replace first position
if ( val <= (256 / (NUM_SETTINGS + 6)) && PreSelData[p].freq[NUM_SETTINGS] != 0 )
{ PreSelData[p].val[1] = val;
PreSelData[p].freq[1] = freq;
for ( j=2; j<=NUM_SETTINGS; ++j )
if ( PreSelData[p].freq[j] <= freq )
{ PreSelData[p].freq[j] = 0;
PreSelData[p].val[j] = 0;
}
//fprintf( stderr,"PreSelect: 1st replaced\n");
add_guard_values(p);
PreselUpdated = 1;
return;
}
else // else if value is close to limit and array is full, replace last
if ( (val >= 256 - (256 / (NUM_SETTINGS + 6))) && PreSelData[p].freq[NUM_SETTINGS] != 0 )
{ PreSelData[p].val[NUM_SETTINGS] = val;
PreSelData[p].freq[NUM_SETTINGS] = freq;
for ( j=NUM_SETTINGS-1; j>=1; --j )
if ( PreSelData[p].freq[j] >= freq )
{ PreSelData[p].freq[j] = 0;
PreSelData[p].val[j] = 0;
}
//fprintf( stderr,"PreSelect: last replaced\n");
add_guard_values(p);
PreselUpdated = 1;
return;
}
for ( i=1; i<=NUM_SETTINGS; ++i )
{
if ( withinXpercent(p,i,freq) )
{
PreSelData[p].freq[i] = freq;
PreSelData[p].val[i] = val;
//fprintf( stderr,"PreSelect: replaced %d\n",i);
add_guard_values(p);
PreselUpdated = 1;
return;
}
if ( PreSelData[p].freq[i] == 0 ) // no entry yet?
{
PreSelData[p].freq[i] = freq;
PreSelData[p].val[i] = val;
//fprintf( stderr,"PreSelect: new entry %d\n",i);
add_guard_values(p);
PreselUpdated = 1;
return;
}
if ( PreSelData[p].freq[i] > freq ) // Need to insert?
{
for ( j=NUM_SETTINGS; j>i; --j )
{ PreSelData[p].val[j] = PreSelData[p].val[j-1];
PreSelData[p].freq[j] = PreSelData[p].freq[j-1];
}
PreSelData[p].val[i] = val;
PreSelData[p].freq[i] = freq;
//fprintf( stderr,"PreSelect: inserted %d\n",i);
add_guard_values(p);
PreselUpdated = 1;
return;
}
}
// Not found anywhere to put it, force it
if ( freq < PreSelData[p].freq[1] )
{
PreSelData[p].val[1] = val;
PreSelData[p].freq[1] = freq;
//fprintf( stderr,"PreSelect: forced 1\n");
add_guard_values(p);
PreselUpdated = 1;
}
else if ( freq > PreSelData[p].freq[NUM_SETTINGS] )
{
PreSelData[p].val[NUM_SETTINGS] = val;
PreSelData[p].freq[NUM_SETTINGS] = freq;
//fprintf( stderr,"PreSelect: force ten\n");
add_guard_values(p);
PreselUpdated = 1;
}
}
// Save current preselector settings to a file.
void save_presel_to_file(void)
{ int j, p;
FILE *filep;
if ( PreselUpdated && Mux != 8 ) // Only save changes in manual mode
{
if ( (filep=fopen(preselector_parfile_name,"w")) != NULL )
{
// could write out a header for the file, e.g. file documentation?
for ( p=0; p<4; ++p )
{
fprintf(filep, "In%d\n",p+4);
for ( j=1; j<=NUM_SETTINGS; ++j )
if ( PreSelData[p].freq[j] && (PreSelData[p].freq[j] != PreSelData[p].freq[j-1]) )
fprintf(filep, "%d %d\n",PreSelData[p].freq[j], PreSelData[p].val[j]);
}
// could write out a trailer?
fclose(filep);
}
else
fprintf( stderr,"Writing to %s failed: errno=%d\n", preselector_parfile_name, errno);
}
PreselUpdated = 0;
}
//************************************************************
// This is the data entered in the rx gain control box.
// int fg.gain_increment = The increment in dB for clicking
// the arrows in the gain control box.
// The global fg_new_band can be used to allow for different
// settings on different bands.
//
// Uses global data:
// int fg.gain
//************************************************************
void elektor_rx_amp_control(void)
{
fg.gain_increment = 10;