-
Notifications
You must be signed in to change notification settings - Fork 0
/
t41.cs
468 lines (432 loc) · 12.5 KB
/
t41.cs
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
using System.Globalization;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using T41_Control;
using Microsoft.UI.Dispatching;
using T41_UI.Views;
using Microsoft.UI.Xaml;
namespace T41_Radio;
public class T41 : INotifyPropertyChanged {
public event PropertyChangedEventHandler? PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
// Supported modes and bands
// {SSB_MODE, CW_MODE, DATA_MODE}
// {DEMOD_USB, DEMOD_LSB, DEMOD_AM, DEMOD_NFM, DEMOD_PSK31, DEMOD_PSK31_WAV, DEMOD_FT8, DEMOD_FT8_WAV, DEMOD_SAM}
// {BAND_80M, BAND_40M, BAND_20M, BAND_17M, BAND_15M, BAND_12M, BAND_10M }
private MainPage mainPage;
private T41Control t41Ctl;
private bool resetCenterFreqFlag = false;
//private bool initializedFlag = false;
// T41 data
private int activeVFO = 0; // VFO A
private bool centerTuneActive = false;
private int freqIndex = 6;
private int ftIndex = 3;
private int freqIncrement = 100000;
private int ftIncrement = 500;
//private long TxRxFreq = 7048000;
private long centerFreq = 7048000;
private long currentFreqA = 7048000;
private long currentFreqB = 7030000;
private int audioVolume = 30;
private int currentDemod = 1; // DEMOD_LSB
private int xmtMode = 0; // SSB_MODE;
private int currentBand = 1; // BAND_40M;
//private int currentBandA = 1; //BAND_40M;
//private int currentBandB = 1; //BAND_40M;
private int currentNF = 0;
private int agcMode = 1;
private int liveNoiseFloorFlag = 0;
private int transmitPowerLevel = 1;
//private int fLoCut = -200;
//private int fHiCut = -3000;
private bool dataFlag = false;
private int ncoFreq = 0;
public T41(MainPage mp) {
t41Ctl = new T41Control(this, mp, DispatcherQueue.GetForCurrentThread());
mainPage = mp;
DispatcherTimer timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
timer.Tick += (s, e) => NotifyPropertyChanged(nameof(Clock));
timer.Start();
}
private int ValidateAGC(int band) {
if(band > 4) return 0;
if(band < 0) return 4;
return band;
}
public int AGCMode {
get { return agcMode; }
set { agcMode = ValidateAGC(value); NotifyPropertyChanged(); }
}
public int CurrentNF {
get { return currentNF; }
set { currentNF = value; NotifyPropertyChanged(); }
}
public int LiveNoiseFloorFlag {
get { return liveNoiseFloorFlag; }
set { liveNoiseFloorFlag = value; NotifyPropertyChanged(); }
}
public bool DataFlag { // true = T41 flagged to send spectrum data periodically
get { return dataFlag; }
set { dataFlag = value; NotifyPropertyChanged(); }
}
public int FLoCut { get; set; } = -200;
public int FHiCut { get; set; } = -3000;
// *** TODO: need to account for mode in bandwidth bar display
private double dbm = 0;
private int smeter = 2;
public int SMeter {
get { return smeter; }
set { smeter = value; NotifyPropertyChanged(); }
}
public string Dbm {
get { return dbm.ToString("F1"); }
set { NotifyPropertyChanged(); }
}
//private DateTime time = new DateTime();
//public string timeString = "";
public string Clock => "PST: " + DateTime.Now.ToString("H:mm:ss");
public bool CenterTuneActive {
get { return centerTuneActive; }
set { centerTuneActive = value; NotifyPropertyChanged(); }
}
int[] selectFreq = { 10, 50, 100, 250, 1000, 10000, 100000, 1000000 };
private int ValidateFreqIndex(int index) {
if (index > 7) return 0;
if (index < 0) return 7;
return index;
}
public int FreqIndex {
get { return freqIndex; }
set {
freqIndex = ValidateFreqIndex(value);
NotifyPropertyChanged();
FreqIncrement = selectFreq[freqIndex];
}
}
public int FreqIncrement {
get { return freqIncrement; }
set { freqIncrement = value; NotifyPropertyChanged(); }
}
private int[] selectFT = { 10, 50, 250, 500 };
private int ValidateFtIndex(int index) {
if (index > 3)return 0;
if (index < 0) return 3;
return index;
}
public int FtIndex {
get { return ftIndex; }
set {
ftIndex = ValidateFtIndex(value);
NotifyPropertyChanged();
FtIncrement = selectFT[ftIndex];
}
}
public int FtIncrement {
get { return ftIncrement; }
set { ftIncrement = value; NotifyPropertyChanged(); }
}
private int ValidateActiveVFO(int vfo) {
if(vfo < 0) return 1;
if(vfo > 1) return 0;
return vfo;
}
public int ActiveVFO {
get { return activeVFO; }
set {
//activeVFO = value == 1 ? 0 : 1; // this toggles activeVFO
activeVFO = ValidateActiveVFO(value);
NCOFreq = 0;
if(activeVFO == 1) {
//centerFreq = TxRxFreq = currentFreqB;
centerFreq = currentFreqB;
} else {
//centerFreq = TxRxFreq = currentFreqA;
centerFreq = currentFreqA;
}
resetCenterFreqFlag = true; // flag callback that a reset is needed
NotifyPropertyChanged();
}
}
public long CurrentFreqA {
get { return currentFreqA; }
set { currentFreqA = value; NotifyPropertyChanged(); }
}
public long CurrentFreqB {
get { return currentFreqB; }
set { currentFreqB = value; NotifyPropertyChanged(); }
}
public int NCOFreq {
get { return ncoFreq; }
set {
ncoFreq = value;
NotifyPropertyChanged();
NotifyPropertyChanged(nameof(BwLeft));
NotifyPropertyChanged(nameof(BwLine));
}
}
public long CenterFreq {
get { return centerFreq; }
set { centerFreq = value; NotifyPropertyChanged(); }
}
public int AudioVolume {
get { return audioVolume; }
set { audioVolume = value; NotifyPropertyChanged(); }
}
public bool BandChangeUp { get; private set; }
private int ValidateBand(int band) {
if(band > 6) return 0;
if(band < 0) return 6;
return band;
}
public int CurrentBand {
get { return currentBand; }
set {
BandChangeUp = value > currentBand;
currentBand = ValidateBand(value);
resetCenterFreqFlag = true; // flag callback that a reset is needed
NotifyPropertyChanged(); }
}
private int ValidateMode(int mode) {
if(mode > 2) return 0;
if(mode < 0) return 2;
return mode;
}
public int XmtMode {
get { return xmtMode; }
set { xmtMode = ValidateMode(value); NotifyPropertyChanged(); }
}
private int ValidateDemod(int demod) {
if(demod > 8) return 0;
if(demod < 0) return 8;
return demod;
}
public int CurrentDemod {
get { return currentDemod; }
set {
currentDemod = ValidateDemod(value);
NotifyPropertyChanged();
NotifyPropertyChanged(nameof(BwLeft));
NotifyPropertyChanged(nameof(BwValueLeft));
NotifyPropertyChanged(nameof(BwValueRight));
}
}
private int ValidatePwr(int pwr) {
if(pwr < 1) return 1;
if(pwr > 20) return 20;
return pwr;
}
public int TransmitPowerLevel {
get { return transmitPowerLevel; }
set { transmitPowerLevel = ValidatePwr(value); NotifyPropertyChanged(); }
}
public void ResetTuning() {
CenterFreq += NCOFreq;
NCOFreq = 0;
//DrawBandwidthBar();
}
// zero out 1000s part of VFO A frequency
public void ZeroFreqA() {
if (activeVFO == 0) {
//int delta = currentFreqA % 1000;
CurrentFreqA -= currentFreqA % 1000;
CenterFreq = currentFreqA;
}
}
public void IncFreqA(int change) {
if (activeVFO == 0) {
if(centerTuneActive) {
CurrentFreqA += FreqIncrement * change;
CenterFreq = CurrentFreqA;
} else {
NCOFreq += FtIncrement * change;
CurrentFreqA = CenterFreq + NCOFreq;
}
}
}
// zero out 1000s part of VFO B frequency
public void ZeroFreqB() {
if (activeVFO == 1) {
//int delta = currentFreqB % 1000;
CurrentFreqB -= currentFreqB % 1000;
CenterFreq = currentFreqB;
}
}
public void IncFreqB(int change) {
if (activeVFO == 1) {
if(centerTuneActive) {
CurrentFreqB += FreqIncrement * change;
CenterFreq = CurrentFreqB;
} else {
NCOFreq += FtIncrement * change;
CurrentFreqB = CenterFreq + NCOFreq;
}
}
}
// Update properties from IFData
// using properties to keep both the display and T41 updated results
// in some churn here; it's reduced by only updating changed values
// and on startup by setting default values the same as on the T41
public void SetState(IFData data) {
if(activeVFO != data.activeVFO) {
ActiveVFO = data.activeVFO;
}
if (activeVFO == 0) {
if(currentFreqA != data.TxRxFreq) {
CurrentFreqA = CenterFreq = data.TxRxFreq;
CurrentFreqB = data.inactiveVFOFreq;
}
} else {
if(currentFreqB != data.TxRxFreq) {
CurrentFreqB = CenterFreq = data.TxRxFreq;
CurrentFreqA = data.inactiveVFOFreq;
}
}
NCOFreq = 0;
if(currentBand != data.currentBand) {
CurrentBand = data.currentBand;
}
if(xmtMode != data.xmtMode) {
XmtMode = data.xmtMode;
}
if(currentDemod != data.mode) {
CurrentDemod = data.mode;
}
if(liveNoiseFloorFlag != data.liveNoiseFloorFlag) {
LiveNoiseFloorFlag = data.liveNoiseFloorFlag;
}
if(currentNF != data.currentNoiseFloor) {
// the current noise floor property isn't used directly on screen
// so we can just update the private field. This prevents sending
// this back to the T41 via t41Ctl.
currentNF = data.currentNoiseFloor;
}
if(agcMode != data.AGCMode) {
AGCMode = data.AGCMode;
}
mainPage.SetActiveVFO(activeVFO);
}
public void SetFreqA(long freq) {
CurrentFreqA = freq;
if(resetCenterFreqFlag) {
CenterFreq = freq;
NCOFreq = 0;
resetCenterFreqFlag = false;
}
}
public void SetFreqB(long freq) {
CurrentFreqB = freq;
if(resetCenterFreqFlag) {
CenterFreq = freq;
NCOFreq = 0;
resetCenterFreqFlag = false;
}
}
public void SetAsState(ASData data) {
//TxRxFreq = data.TxRxFreq;
// AS command doesn't change the center frequency
// use SetFreq to handle events that need to set it
if (activeVFO == 0) {
if(currentFreqA != data.TxRxFreq) {
SetFreqA(data.TxRxFreq);
}
} else {
if(currentFreqA != data.TxRxFreq) {
SetFreqA(data.TxRxFreq);
}
}
if(currentBand != data.currentBand) {
CurrentBand = data.currentBand;
}
if(xmtMode != data.xmtMode) {
XmtMode = data.xmtMode;
}
if(currentDemod != data.mode) {
CurrentDemod = data.mode;
}
}
public void SetDbm(int value) {
dbm = ((double)value) / 10.0;
//dbm = (double)smeter;
NotifyPropertyChanged(nameof(Dbm));
//NotifyPropertyChanged("SMeter");
}
public void SetSmeter(int value) {
SMeter = (int)(value * 1.5);
//smeter = (int)(value * 1.5);
//smeter = value;
//dbm = (double)smeter;
//NotifyPropertyChanged("Dbm");
//NotifyPropertyChanged("SMeter");
}
// properties related to bandwidth bar
public double BwLeft {
get {
double factor = 0; // USB and data modes
switch(currentDemod) {
case 1:
factor = -1;
break;
case 2:
case 3:
case 8:
factor = -0.5;
break;
default:
break;
}
return ((double)(Math.Abs(FHiCut) * factor + (double)ncoFreq) / 96000.0 + 0.5) * 768.0;
}
}
public double BwWidth {
get { return (double)Math.Abs(FHiCut) / 96000.0 * 768.0; }
}
public double BwLine {
get { return ((double)ncoFreq / 96000.0 + 0.5 ) * 768.0; }
}
public string BwValueLeft {
get {
int filter = Math.Abs(FLoCut); // USB and data modes
switch(currentDemod) {
case 1:
filter = FHiCut;
break;
case 2:
case 3:
case 8:
filter = Math.Abs(FHiCut) * 2;
break;
default:
break;
}
return ((double)filter / 1000.0).ToString("F1") + "kHz";
}
}
public string BwValueRight {
get {
int filter = Math.Abs(FHiCut); // USB and data modes
switch(currentDemod) {
case 1:
filter = FLoCut;
break;
case 2:
case 3:
case 8:
//filter = Math.Abs(FHiCut) * 2;
return "";
//break;
default:
break;
}
return ((double)filter / 1000.0).ToString("F1") + "kHz";
}
}
public string[] GetPorts() {
return t41Ctl.Ports;
}
public bool Connect(string item) {
return t41Ctl.Connect(item);
}
}