-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMain.pas
530 lines (474 loc) · 14.5 KB
/
Main.pas
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
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Registry, StdCtrls, ExtCtrls, VaClasses, VaComm, Mask, AdvSpin,
IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, SyncObjs,
TeEngine, Series, TeeProcs, Chart, ComCtrls;
type
THabitatThread = class(TThread)
public
procedure Execute; override;
end;
type
TSSDVThread = class(TThread)
public
procedure Execute; override;
end;
type
TForm1 = class(TForm)
Label1: TLabel;
ComboBox1: TComboBox;
pnlCommStatus: TPanel;
VaComm1: TVaComm;
Label6: TLabel;
pnlRSSI: TPanel;
btnSet: TButton;
Label14: TLabel;
edtFrequency: TEdit;
Label15: TLabel;
Panel1: TPanel;
lstCommands: TListBox;
tmrCommands: TTimer;
Label2: TLabel;
cmbMode: TComboBox;
Panel2: TPanel;
Label3: TLabel;
cmbCoding: TComboBox;
Label4: TLabel;
cmbSpreading: TComboBox;
Label5: TLabel;
cmbBandwidth: TComboBox;
Label7: TLabel;
cmbImplicit: TComboBox;
Label8: TLabel;
pblSentenceCount: TPanel;
pnlFrequencyError: TPanel;
Label9: TLabel;
pnlPacketRSSI: TPanel;
Label10: TLabel;
Label11: TLabel;
pnlPacketSNR: TPanel;
chkOnline: TCheckBox;
IdHTTP1: TIdHTTP;
Panel3: TPanel;
Label12: TLabel;
edtCallsign: TEdit;
Label13: TLabel;
pnlSSDVCount: TPanel;
Label16: TLabel;
pnlSSDVQueue: TPanel;
tmrScreenUpdates: TTimer;
IdHTTP2: TIdHTTP;
Label17: TLabel;
pnlAverageRSSI: TPanel;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
lstPackets: TListBox;
Button1: TButton;
Label19: TLabel;
pnlScanFrequency: TPanel;
Chart1: TChart;
Series4: TFastLineSeries;
tmrSearch: TTimer;
Button2: TButton;
pnlSearchFrequency: TPanel;
Panel4: TPanel;
ProgressBar1: TProgressBar;
chkAFC: TCheckBox;
procedure FormCreate(Sender: TObject);
procedure ComboBox1CloseUp(Sender: TObject);
procedure VaComm1RxChar(Sender: TObject; Count: Integer);
procedure btnSetClick(Sender: TObject);
procedure tmrCommandsTimer(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure tmrScreenUpdatesTimer(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure tmrSearchTimer(Sender: TObject);
private
{ Private declarations }
Scanning, Searching: Boolean;
SearchFrequencyError, SearchFrequency, MinSearchFrequency, MaxSearchFrequency, SearchFrequencyStep: Double;
SearchPacketCount: Integer;
procedure NextScan;
procedure NextSearch(Test: Boolean);
procedure StopSearch;
procedure ProcessLine(Line: AnsiString);
procedure ApplyAFC;
public
{ Public declarations }
end;
var
Form1: TForm1;
CritSSDV, CritHabitat: TCriticalSection;
SSDVPackets: TStringList;
HabitatSentence: String;
implementation
{$R *.dfm}
procedure TForm1.btnSetClick(Sender: TObject);
begin
lstCommands.Items.Clear;
lstCommands.Items.Add('~F' + edtFrequency.Text);
lstCommands.Items.Add('~M' + IntToStr(cmbMode.ItemIndex));
if cmbCoding.ItemIndex > 0 then begin
lstCommands.Items.Add('~E' + IntToStr(cmbCoding.ItemIndex+4));
end;
if cmbSpreading.ItemIndex > 0 then begin
lstCommands.Items.Add('~S' + IntToStr(cmbSpreading.ItemIndex+5));
end;
if cmbBandwidth.ItemIndex > 0 then begin
lstCommands.Items.Add('~B' + cmbBandwidth.Text);
end;
if cmbImplicit.ItemIndex > 0 then begin
lstCommands.Items.Add('~I' + IntToStr(cmbImplicit.ItemIndex-1));
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
MinSearchFrequency := 434;
MaxSearchFrequency := 434.7;
SearchFrequencyStep := 0.0125;
SearchFrequency := MinSearchFrequency - SearchFrequencyStep;
Scanning := True;
Chart1.Series[0].Clear;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
MinSearchFrequency := StrToFloat(edtFrequency.Text) - 0.010;
MaxSearchFrequency := StrToFloat(edtFrequency.Text) + 0.010;
SearchFrequencyStep := 0.002;
SearchFrequency := MinSearchFrequency - SearchFrequencyStep;
Searching := True;
NextSearch(False);
end;
procedure TForm1.NextScan;
begin
SearchFrequency := SearchFrequency + SearchFrequencyStep;
if SearchFrequency > MaxSearchFrequency then begin
pnlScanFrequency.Caption := '';
Scanning := False;
btnSetClick(nil);
end else begin
pnlScanFrequency.Caption := FormatFloat('.0000', SearchFrequency);
lstCommands.Items.Add('~F' + pnlScanFrequency.Caption);
end;
end;
procedure TForm1.NextSearch(Test: Boolean);
begin
SearchFrequency := SearchFrequency + SearchFrequencyStep;
if Test and (SearchFrequency > MaxSearchFrequency) then begin
pnlSearchFrequency.Caption := '';
ProgressBar1.Position := 0;
Searching := False;
btnSetClick(nil);
end else begin
pnlSearchFrequency.Caption := FormatFloat('.0000', SearchFrequency);
ProgressBar1.Position := Round(((SearchFrequency + SearchFrequencyStep - MinSearchFrequency) / (MaxSearchFrequency + SearchFrequencyStep - MinSearchFrequency)) * 100);
lstCommands.Items.Add('~F' + pnlSearchFrequency.Caption);
SearchPacketCount := 0;
tmrSearch.Enabled := True;
end;
end;
procedure TForm1.StopSearch;
begin
edtFrequency.Text := FormatFloat('0.0000', SearchFrequency + SearchFrequencyError / 1000);
ProgressBar1.Position := 0;
pnlSearchFrequency.Caption := '';
Searching := False;
btnSetClick(nil);
end;
procedure TForm1.ComboBox1CloseUp(Sender: TObject);
begin
VaComm1.Close;
if ComboBox1.ItemIndex >= 0 then begin
try
VaComm1.DeviceName := ComboBox1.Text;
VaComm1.Open;
pnlCommStatus.Caption := VaComm1.DeviceName + ' open';
except
pnlCommStatus.Caption := VaComm1.DeviceName + ' failed to open';
end;
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
CritSSDV.Free;
CritHabitat.Free;
SSDVPackets.Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
reg: TRegistry;
st: Tstrings;
i: Integer;
begin
CritSSDV := TCriticalSection.Create;
CritHabitat := TCriticalSection.Create;
SSDVPackets := TStringList.Create;
THabitatThread.Create(False);
TSSDVThread.Create(False);
ComboBox1.Items.Clear;
reg := TRegistry.Create;
try
reg.RootKey := HKEY_LOCAL_MACHINE;
reg.OpenKeyReadOnly('hardware\devicemap\serialcomm');
st := TstringList.Create;
try
reg.GetValueNames(st);
for i := 0 to st.Count - 1 do begin
ComboBox1.Items.Add(reg.Readstring(st.strings[i]));
end;
finally
st.Free;
end;
reg.CloseKey;
finally
reg.Free;
end;
end;
function GetString(var Line: AnsiString; Delimiter: String=','): AnsiString;
var
Position: Integer;
begin
Position := Pos(Delimiter, string(Line));
if Position > 0 then begin
Result := Copy(Line, 1, Position-1);
Line := Copy(Line, Position+Length(Delimiter), Length(Line));
end else begin
Result := Line;
Line := '';
end;
end;
procedure UploadSentence(Telemetry: String);
var
URL, FormAction, Callsign, Temp: String;
Params: TStringList;
begin
URL := 'http://habitat.habhub.org/transition';
FormAction := 'payload_telemetry';
Callsign := Form1.edtCallsign.Text;
// Parameters
Params := TStringList.Create;
// Params.Add('Submit=' + FormAction);
Params.Add('callsign=' + Callsign);
Params.Add('string=' + Telemetry + #10);
Params.Add('string_type=ascii');
Params.Add('metadata={}');
Params.Add('time_created=');
// Post it
Form1.IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';
Form1.IdHTTP1.Response.KeepAlive := False;
Temp := Form1.IdHTTP1.Post(URL + '/' + FormAction, Params);
Params.Free;
end;
procedure UploadSSDV(Packets: TStringList);
var
URL, FormAction, Callsign, Temp, json: String;
JsonToSend: TStringStream;
i: Integer;
begin
URL := 'http://ssdv.habhub.org/api/v0';
FormAction := 'packets';
Callsign := Form1.edtCallsign.Text;
// Create json with the base64 data in hex, the tracker callsign and the current timestamp
json :=
'{' +
'"type": "packets",' +
'"packets":[';
for i := 0 to Packets.Count-1 do begin
if i > 0 then json := json + ',';
json := json +
'{' +
'"type": "packet",' +
'"packet":' + '"55' + Packets[i] + '",' +
'"encoding": "hex",' +
'"received": "' + FormatDateTime('yyyy-mm-dd"T"hh:nn:ss"Z"', Now) + '",' +
'"receiver": "' + Callsign + '"' +
'}';
end;
json := json + ']}';
// Need the JSON as a stream
JsonToSend := TStringStream.Create(Json, TEncoding.UTF8);
// Post it
try
Form1.IdHTTP2.Request.ContentType := 'application/json';
Form1.IdHTTP2.Request.ContentEncoding := 'UTF-8';
Form1.IdHTTP2.Response.KeepAlive := False;
Temp := Form1.IdHTTP2.Post(URL + '/' + FormAction, JsonToSend);
finally
JsonToSend.Free;
end;
// Params.Free;
end;
procedure TForm1.ProcessLine(Line: AnsiString);
var
Command: AnsiString;
begin
Command := UpperCase(GetString(Line, '='));
if Command = 'CURRENTRSSI' then begin
pnlRSSI.Caption := string(Line + 'dBm');
if pnlAverageRSSI.Caption = '' then begin
pnlAverageRSSI.Caption := string(Line + 'dBm');
end else begin
pnlAverageRSSI.Caption := IntToStr(Round(StrToFloat(Copy(pnlAverageRSSI.Caption, 1, Length(pnlAverageRSSI.Caption)-3)) * 0.8 + StrToFloat(Line) * 0.2)) + 'dBm';
end;
if Scanning then begin
if SearchFrequency > MinSearchFrequency then begin
Chart1.Series[0].AddXY(SearchFrequency - 434.0, StrToFloat(Line));
end;
NextScan;
end;
end else if Command = 'MESSAGE' then begin
lstPackets.Items.Add(Line);
lstPackets.ItemIndex := lstPackets.Items.Count-1;
Inc(SearchPacketCount);
pblSentenceCount.Caption := IntToStr(StrToIntdef(pblSentenceCount.Caption, 0) + 1);
if chkOnline.Checked then begin
if Line[1] = '%' then begin
Line[1] := '$';
end;
CritHabitat.Enter;
try
HabitatSentence := Line;
finally
CritHabitat.Leave;
end;
end;
ApplyAFC;
end else if Command = 'HEX' then begin
if (Copy(Line,1,2) = '66') or (Copy(Line,1,2) = 'E6') then begin
lstPackets.Items.Add(Line);
lstPackets.ItemIndex := lstPackets.Items.Count-1;
if Copy(Line,1,2) = 'E6' then begin
Line[1] := '6';
end;
pnlSSDVCount.Caption := IntToStr(StrToIntdef(pnlSSDVCount.Caption, 0) + 1);
if chkOnline.Checked then begin
CritSSDV.Enter;
try
SSDVPackets.Add(Line);
finally
CritSSDV.Leave;
end;
end;
end;
end else if Command = 'FREQERR' then begin
pnlFrequencyError.Caption := Line + ' kHz';
SearchFrequencyError := StrToFloat(Line);
end else if Command = 'PACKETRSSI' then begin
pnlPacketRSSI.Caption := Line;
end else if Command = 'PACKETSNR' then begin
pnlPacketSNR.Caption := Line;
end else begin
// lstPackets.Items.Add('Unknown: ' + Line);
end;
end;
procedure TForm1.ApplyAFC;
begin
if chkAFC.Checked then begin
if (not Scanning) and (not Searching) then begin
if Abs(SearchFrequencyError) > 1 then begin
edtFrequency.Text := FormatFloat('0.000', StrToFloat(edtFrequency.Text) + SearchFrequencyError / 1000);
lstCommands.Items.Add('~F' + edtFrequency.Text);
end;
end;
end;
end;
procedure TForm1.tmrCommandsTimer(Sender: TObject);
begin
if lstCommands.Items.Count > 0 then begin
VaComm1.WriteText(lstCommands.Items[0] + #13);
lstCommands.Items.Delete(0);
end;
end;
procedure TForm1.tmrScreenUpdatesTimer(Sender: TObject);
begin
CritSSDV.Enter;
try
pnlSSDVQueue.Caption := IntToStr(SSDVPackets.Count);
finally
CritSSDV.Leave;
end;
end;
procedure TForm1.tmrSearchTimer(Sender: TObject);
begin
tmrSearch.Enabled := False;
if SearchPacketCount > 1 then begin
StopSearch;
end else begin
NextSearch(True);
end;
end;
procedure TForm1.VaComm1RxChar(Sender: TObject; Count: Integer);
const
Buffer: AnsiString = '';
var
i: Integer;
Character: AnsiChar;
begin
for i := 1 to Count do begin
VaComm1.ReadChar(Character);
try
if (Character = Chr(10)) or (Character = Chr(13)) then begin
if Length(Buffer) > 0 then begin
ProcessLine(Buffer);
Buffer := '';
end;
end else begin
if Length(Buffer) < 1000 then begin
Buffer := Buffer + Character;
end;
end;
except
end;
end;
end;
procedure THabitatThread.Execute;
var
Sentence: String;
begin
while not Application.Terminated do begin
Sentence := '';
CritHabitat.Enter;
try
if HabitatSentence <> '' then begin
Sentence := HabitatSentence;
end;
finally
HabitatSentence := '';
CritHabitat.Leave;
end;
if Sentence <> '' then begin
UploadSentence(Sentence);
end else begin
sleep(100);
end;
end;
end;
procedure TSSDVThread.Execute;
var
Packets: TStringList;
begin
Packets := TStringList.Create;
while not Application.Terminated do begin
Packets.Clear;
CritSSDV.Enter;
try
if SSDVPackets.Count > 0 then begin
Packets.Assign(SSDVPackets);
SSDVPackets.Clear;
end;
finally
CritSSDV.Leave;
end;
if Packets.Count > 0 then begin
UploadSSDV(Packets);
end else begin
sleep(100);
end;
end;
Packets.Free;
end;
end.