-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunitformspert.pas
More file actions
528 lines (445 loc) · 13.8 KB
/
unitformspert.pas
File metadata and controls
528 lines (445 loc) · 13.8 KB
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
unit UnitFormSpert;
{$mode objfpc}{$H+}
{$modeswitch objectivec1}
interface
uses
Classes,
SysUtils,
Dateutils,
Forms,
CocoaAll,
Controls,
Dialogs,
Menus,
StdCtrls,
ComCtrls,
ExtCtrls,
Buttons,
Graphics,
LCLType,
UnitSettings,
UnitSpertDatabase,
UnitSpert,
UnitFormRig,
UnitFormDebug;
type
{ TFormSpert }
TFormSpert = class(TForm)
ButtonAtuTune: TBitBtn;
BitBtnAtu: TBitBtn;
BitBtnLockTx: TBitBtn;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button5: TButton;
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
GroupBox3: TGroupBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
PanelAtuLC: TPanel;
PanelTemp: TPanel;
PanelPavg: TPanel;
PanelPref: TPanel;
PanelAtuSWR: TPanel;
PanelPmax: TPanel;
PanelPcur: TPanel;
StatusBar1: TStatusBar;
TrackBar1: TTrackBar;
procedure BitBtnLockTxClick(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure BitBtnAtuClick(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormShow(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure ButtonAtuTuneClick(Sender: TObject);
procedure TrackBar1Change(Sender: TObject);
procedure TrackBar1MouseEnter(Sender: TObject);
procedure TrackBar1MouseLeave(Sender: TObject);
private
Configuration: TConfiguration;
TimerFormClose: TTimer;
picBallGreen: TBitmap;
picBallBlue: TBitmap;
private
procedure UpdateUI(Sender: TObject);
procedure TimerFormCloseTimer(Sender: TObject);
public
Spert: TSpert;
SpertDatabase: TSpertDatabase;
public
procedure SetUp(restart: Boolean);
const
MAX_GREEN_SWR = 1;
end;
var
FormSpert: TFormSpert;
implementation
{$R *.lfm}
{ TFormSpert }
procedure TFormSpert.FormCreate(Sender: TObject);
begin
Configuration:=TConfiguration.Create(Application.Location);
Configuration.Load;
Spert:=TSpert.Create(Configuration);
Spert.onState(@UpdateUI);
SpertDatabase:=TSpertDatabase.Create(Configuration);
picBallGreen:=TBitMap.Create;
picBallGreen.LoadFromResourceName(HInstance, 'BALL_GREEN_ICON');
picBallBlue:=TBitMap.Create;
picBallBlue.LoadFromResourceName(HInstance, 'BALL_BLUE_ICON');
end;
procedure TFormSpert.FormShow(Sender: TObject);
begin
NSView(Handle).window.setFrameAutosaveName(NSSTR(ClassName));
GroupBox1.Enabled:=False;
GroupBox2.Enabled:=False;
StatusBar1.Panels.Items[2].Text:= 'Not connected';
BitBtnLockTx.Glyph.Assign(picBallGreen);
BitBtnAtu.Glyph.Assign(picBallBlue);
SpertDatabase.Open;
Spert.Start;
Application.ProcessMessages;
FormDebug.Log('TFormSpert: show');
end;
procedure TFormSpert.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
// Spert will be stopped into background because it's slow and delaying form close
TimerFormClose:=TTimer.Create(nil);
TimerFormClose.Interval:=10;
TimerFormClose.Enabled:=True;
TimerFormClose.OnTimer:=@TimerFormCloseTimer;
end;
procedure TFormSpert.TimerFormCloseTimer(Sender: TObject);
begin
TimerFormClose.Enabled:=False;
Spert.Stop;
Application.ProcessMessages;
SpertDatabase.Close;
end;
procedure TFormSpert.UpdateUI(Sender: TObject);
var
Swr: Double;
begin
BeginFormUpdate;
Application.ProcessMessages;
if Spert.isActive then
begin
GroupBox1.Enabled:=True;
// power
Label1.Caption:='Power ' + IntToStr(Spert.getPwr * 10) + ' W';
if (TrackBar1.Tag = 0) and (Spert.getPwr <> TrackBar1.Position) then
begin
TrackBar1.Enabled:=false;
TrackBar1.Position:=Spert.getPwr;
TrackBar1.Enabled:=true;
end;
// band
if Spert.getBand <> StatusBar1.Panels.Items[0].Text then StatusBar1.Panels.Items[0].Text:=Spert.getBand;
// temp
PanelTemp.Caption:=IntToStr(Spert.getTemp);
if (Spert.getTemp > 0) AND (Spert.getTemp < 40) then
begin
PanelTemp.Color:=clGreen;
PanelTemp.Font.Color:=clWhite;
end;
if (Spert.getTemp >= 40) AND (Spert.getTemp < 45) then
begin
PanelTemp.Color:=clYellow;
PanelTemp.Font.Color:=clBlack;
end;
if (Spert.getTemp >= 45) then
begin
PanelTemp.Color:=clRed;
PanelTemp.Font.Color:=clWhite;
end;
// TX
if Spert.txActive then
begin
PanelPavg.Color:=clGreen;
PanelPavg.Caption:=IntToStr(Spert.getFpwrAvg);
PanelPmax.Color:=clGreen;
PanelPmax.Caption:=IntToStr(Spert.getFpwrMax);
PanelPcur.Color:=clGreen;
PanelPcur.Caption:=IntToStr(Spert.getFpwr);
PanelPref.Caption:=Format('%.1f', [Spert.getRpwrMaxPercent]);
if Spert.getRpwrMaxPercent < 1 then
begin
PanelPref.Color:=clGreen;
PanelPref.Font.Color:=clWhite;
end;
if (Spert.getRpwrMaxPercent >= 1) and (Spert.getRpwrMaxPercent < 5) then
begin
PanelPref.Color:=clYellow;
PanelPref.Font.Color:=clBlack;
end;
if (Spert.getRpwrMaxPercent >= 5) then
begin
PanelPref.Color:=clRed;
PanelPref.Font.Color:=clWhite;
end;
GroupBox1.Enabled:=False;
GroupBox2.Enabled:=False;
end;
if not Spert.txActive then
begin
PanelPavg.Color:=clGray;
PanelPmax.Color:=clGray;
PanelPcur.Color:=clGray;
if PanelPref.Color=clGreen then PanelPref.Color:=clGray; // set back to gray if there was no excessive reflected power
GroupBox1.Enabled:=True;
GroupBox2.Enabled:=True;
end;
// atu presence
GroupBox2.Enabled:=SPert.isAtuPresent;
// atu is starting to tune
if Spert.isAtuTunning then
begin
BitBtnAtu.Enabled:=False;
ButtonAtuTune.Enabled:=False;
PanelAtuLC.Color:=clMaroon;
end;
// atu is not tunning - update state
if not Spert.isAtuTunning then
begin
BitBtnAtu.Enabled:=True;
// atu is on
if Spert.isAtuActive then
begin
BitBtnAtu.Glyph.Assign(picBallGreen);
ButtonAtuTune.Enabled:=True;
end
else
begin
BitBtnAtu.Glyph.Assign(picBallBlue);
ButtonAtuTune.Enabled:=False;
end;
end;
// fan
case Spert.getFan of
SpertFanLevel_50: StatusBar1.Panels.Items[1].Text:='50%';
SpertFanLevel_Fixed: StatusBar1.Panels.Items[1].Text:='Fixed';
SpertFanLevel_Histeresis: StatusBar1.Panels.Items[1].Text:='Histeresis';
SpertFanLevel_Max: StatusBar1.Panels.Items[1].Text:='Max';
end;
// version
StatusBar1.Panels.Items[2].Text:=Spert.getVersion;
{ SWR - interact with PanelAtuSWR/PanelAtuLC }
if not FormRig.Rig.isActive then
begin
if Spert.isTxLocked then Spert.txLockOff; // unlock TX if radio goes off
BitBtnLockTx.Enabled:=True;
// PanelAtuSWR is gray and shows nothing
PanelAtuSWR.Caption:='';
PanelAtuSWR.Color:=clGray;
// PanelAtuLC is always gray - SPert colors don't works as expected and it's "green" do not mean "ATU is tunned"
if not Spert.isAtuTunning then PanelAtuLC.Color:=clGray;
if Spert.isAtuActive and not Spert.isAtuTunning
then PanelAtuLC.Caption:=FloatToStr(Spert.getAtu_L) + 'μH ' + FloatToStr(Spert.getAtu_C) + 'pF'
else PanelAtuLC.Caption:='';
end;
if FormRig.Rig.isActive then
begin
// PanelAtuSWR shows sawed SWR and if ATU is off use colors to prompt what to do, id ATU is on is always green
Swr:=SpertDatabase.estimateSwr(FormRig.Rig.getFrq, 3000); // +-3kHz
if Swr = -1 then
begin
Swr:=SpertDatabase.estimateSwr(FormRig.Rig.getFrq, 30000); // +-30kHz
if Swr = -1
then PanelAtuSWR.Caption:=''
else PanelAtuSWR.Caption:='~' + Format('%.1f', [Swr])
end else
PanelAtuSWR.Caption:=Format('%.1f', [Swr]);
if Spert.isAtuActive
then PanelAtuSWR.Color:=clGreen;
if not Spert.isAtuActive then
begin
if (Swr = -1)
then PanelAtuSWR.Color:=clGray;
if (Swr >= 0) and (Swr <= MAX_GREEN_SWR)
then PanelAtuSWR.Color:=clGreen;
if (Swr > MAX_GREEN_SWR)
then PanelAtuSWR.Color:=clRed;
end;
// PanelAtuLC if ATU is on use saved LC to prompt ATU re-tune, if ATU is off it's gray and shows nothing
if Spert.isAtuActive and not Spert.isAtuTunning then
begin
if (Swr = -1) or (Swr > MAX_GREEN_SWR) then
begin
case SpertDatabase.matchLC(FormRig.Rig.getFrq, 10000, Spert.getAtu_L, Spert.getAtu_C) of
SpertDatabaseLC_Unknown: PanelAtuLC.Color:=clGray;
SpertDatabaseLC_Match: PanelAtuLC.Color:=clGreen;
SpertDatabaseLC_NotMatch: PanelAtuLC.Color:=clRed;
end;
end
else PanelAtuLC.Color:=clGreen;
PanelAtuLC.Caption:=FloatToStr(Spert.getAtu_L) + 'μH ' + FloatToStr(Spert.getAtu_C) + 'pF';
end
else
begin
PanelAtuLC.Color:=clGray;
PanelAtuLC.Caption:='';
end;
if (BitBtnLockTx.Tag = 0) and not Spert.isAtuActive then
begin
// if ATU is not active and SWR is too high lock SPert and disable possibility unlocking it
if (Configuration.Settings.spertLockOnSwr > 0) and (Swr >= Configuration.Settings.spertLockOnSwr) and not Spert.isTxLocked then
begin
Spert.txLockOn;
BitBtnLockTx.Enabled:=False;
end;
if (Configuration.Settings.spertLockOnSwr > 0) and (Swr < Configuration.Settings.spertLockOnSwr) and Spert.isTxLocked then
begin
Spert.txLockOff;
BitBtnLockTx.Enabled:=True;
end;
end;
// unlock SPert if ATU is on, but ignore it if lock manually turned on
if (BitBtnLockTx.Tag = 0) and Spert.isAtuActive and Spert.isTxLocked then
begin
Spert.txLockOff;
BitBtnLockTx.Enabled:=True;
end;
end;
// lock button
if Spert.isTxLocked then BitBtnLockTx.Glyph.Assign(picBallBlue) else BitBtnLockTx.Glyph.Assign(picBallGreen);
end;
if not Spert.isActive then
begin
GroupBox1.Enabled:=False;
GroupBox2.Enabled:=False;
StatusBar1.Panels.Items[0].Text:='';
StatusBar1.Panels.Items[1].Text:='';
StatusBar1.Panels.Items[2].Text:= 'Not connected';
PanelAtuSWR.Color:=clGray;
PanelAtuLC.Color:=clGray;
PanelTemp.Color:=clGray;
PanelTemp.Font.Color:=clWhite;
PanelTemp.Caption:='0';
PanelPavg.Color:=clGray;
PanelPavg.Caption:='0';
PanelPref.Color:=clGray;
PanelPref.Font.Color:=clWhite;
PanelPref.Caption:='0.0';
PanelPmax.Color:=clGray;
PanelPmax.Caption:='0';
PanelPcur.Color:=clGray;
PanelPcur.Caption:='0';
end;
EndFormUpdate;
end;
{ global keyboard shortcuits}
procedure TFormSpert.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
var
ssCmd: TShiftStateEnum;
begin
{$IFDEF DARWIN}
ssCmd := ssMeta;
{$ELSE}
ssCmd := ssCtrl
{$ENDIF}
if (ssCmd in Shift) then begin
if Spert.isActive and not Spert.txActive and BitBtnAtu.Enabled and (Key = VK_A) then BitBtnAtuClick(Sender);
if Spert.isActive and not Spert.txActive and ButtonAtuTune.Enabled and (Key = VK_T) then ButtonAtuTuneClick(Sender);
if Spert.isActive and not Spert.txActive and TrackBar1.Enabled then
begin
case Key of
VK_1: TrackBar1.Position:=10;
VK_2: TrackBar1.Position:=20;
VK_3: TrackBar1.Position:=30;
VK_4: TrackBar1.Position:=40;
VK_5: TrackBar1.Position:=50;
VK_6: TrackBar1.Position:=60;
VK_7: TrackBar1.Position:=70;
VK_8: TrackBar1.Position:=80;
VK_9: TrackBar1.Position:=90;
VK_0: TrackBar1.Position:=100;
end;
end;
end;
end;
{ power: fixed 100W }
procedure TFormSpert.Button1Click(Sender: TObject);
begin
if Spert.isActive and not Spert.txActive and TrackBar1.Enabled
then TrackBar1.Position:=10;
end;
{ power: fixed 300W }
procedure TFormSpert.Button5Click(Sender: TObject);
begin
if Spert.isActive and not Spert.txActive and TrackBar1.Enabled
then TrackBar1.Position:=30;
end;
procedure TFormSpert.BitBtnLockTxClick(Sender: TObject);
begin
if Spert.isTxLocked then
begin
Spert.txLockOff;
BitBtnLockTx.Glyph.Assign(picBallGreen);
BitBtnLockTx.Tag:=0;
end
else
begin
BitBtnLockTx.Glyph.Assign(picBallBlue);
BitBtnLockTx.Tag:=1;
Spert.txLockOn;
end;
end;
{ power: fixed 500W }
procedure TFormSpert.Button2Click(Sender: TObject);
begin
if Spert.isActive and not Spert.txActive and TrackBar1.Enabled
then TrackBar1.Position:=50;
end;
{ power: fixed 700W }
procedure TFormSpert.Button3Click(Sender: TObject);
begin
if Spert.isActive and not Spert.txActive and TrackBar1.Enabled
then TrackBar1.Position:=70;
end;
{ ATU on/by-pass }
procedure TFormSpert.BitBtnAtuClick(Sender: TObject);
begin
if Spert.isActive and not Spert.txActive and BitBtnAtu.Enabled then SPert.toggleAtuState;
end;
{ ATU tune }
procedure TFormSpert.ButtonAtuTuneClick(Sender: TObject);
begin
if Spert.isActive and not Spert.txActive and ButtonAtuTune.Enabled then
begin
Spert.tuneAtu;
ButtonAtuTune.Enabled:=False;
end;
end;
{ PWR slider }
procedure TFormSpert.TrackBar1MouseEnter(Sender: TObject);
begin
TrackBar1.Tag:=1;
end;
procedure TFormSpert.TrackBar1MouseLeave(Sender: TObject);
begin
TrackBar1.Tag:=0;
end;
procedure TFormSpert.TrackBar1Change(Sender: TObject);
begin
if Spert.isActive and not Spert.txActive and TrackBar1.Enabled and (TrackBar1.Position >= 0) and (TrackBar1.Position <= 100) then Spert.setPower(TrackBar1.Position);
end;
{ helpers }
procedure TFormSpert.SetUp(restart: Boolean);
begin
Configuration.Load;
if restart then
begin
Spert.Stop;
Spert.Start;
end;
end;
end.