-
Notifications
You must be signed in to change notification settings - Fork 3
/
LoRaBluetoothSettings.pas
346 lines (288 loc) · 10.5 KB
/
LoRaBluetoothSettings.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
unit LoRaBluetoothSettings;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
SettingsBase, FMX.ScrollBox, FMX.Memo, FMX.Objects, FMX.Controls.Presentation,
FMX.TMSCustomEdit, FMX.TMSEdit, Miscellaneous, Math, System.Bluetooth,
FMX.ListBox, System.Bluetooth.Components, FMX.Memo.Types;
type
TfrmBluetoothSettings = class(TfrmSettingsBase)
chkSSDV: TLabel;
Label2: TLabel;
edtFrequency: TTMSFMXEdit;
btnModeDown: TLabel;
Label3: TLabel;
edtMode: TTMSFMXEdit;
btnModeUp: TLabel;
Label1: TLabel;
edtCallsign: TTMSFMXEdit;
edtDevice: TTMSFMXEdit;
Label4: TLabel;
cmbDevices: TComboBox;
BluetoothLE1: TBluetoothLE;
chkAFC: TLabel;
rectProgressHolder: TRectangle;
rectProgressBar: TRectangle;
btnSearch: TButton;
tmrReceive: TTimer;
tmrSearch: TTimer;
rectRx: TRectangle;
lblRx: TLabel;
chkSondehub: TLabel;
procedure FormCreate(Sender: TObject);
procedure btnApplyClick(Sender: TObject);
procedure btnCancelClick(Sender: TObject);
procedure btnModeDownClick(Sender: TObject);
procedure btnModeUpClick(Sender: TObject);
procedure cmbDevicesClosePopup(Sender: TObject);
procedure edtDeviceClick(Sender: TObject);
procedure BluetoothLE1DiscoverLEDevice(const Sender: TObject;
const ADevice: TBluetoothLEDevice; Rssi: Integer;
const ScanResponse: TScanResponse);
procedure edtFrequencyExit(Sender: TObject);
procedure tmrReceiveTimer(Sender: TObject);
procedure tmrSearchTimer(Sender: TObject);
procedure btnSearchClick(Sender: TObject);
procedure edtCallsignChangeTracking(Sender: TObject);
procedure edtDeviceChangeTracking(Sender: TObject);
procedure chkSondehubClick(Sender: TObject);
private
{ Private declarations }
Searching: Boolean;
SearchCentreFrequency: Double;
SearchPacketCount, SearchStep: Integer;
procedure SendSettingsToDevice;
procedure UpdateBluetoothList;
procedure NextSearch;
procedure StopSearch;
protected
procedure ApplyChanges; override;
procedure CancelChanges; override;
public
{ Public declarations }
procedure LoadForm; override;
end;
var
frmBluetoothSettings: TfrmBluetoothSettings;
implementation
uses SourcesForm;
{$R *.fmx}
procedure TfrmBluetoothSettings.ApplyChanges;
begin
SetSettingString(Group, 'Device', edtDevice.Text);
SetSettingString(Group, 'Frequency', edtFrequency.Text);
SetSettingString(Group, 'Mode', edtMode.Text);
SetSettingString(Group, 'Callsign', edtCallsign.Text);
SetSettingBoolean(Group, 'AFC', LCARSLabelIsChecked(chkAFC));
SetSettingBoolean(Group, 'Sondehub', LCARSLabelIsChecked(chkSondehub));
SetSettingBoolean(Group, 'SSDV', LCARSLabelIsChecked(chkSSDV));
inherited;
end;
procedure TfrmBluetoothSettings.BluetoothLE1DiscoverLEDevice(
const Sender: TObject; const ADevice: TBluetoothLEDevice; Rssi: Integer;
const ScanResponse: TScanResponse);
var
i, Index: Integer;
ShortLine, LongLine: String;
begin
for i := 0 to BluetoothLE1.DiscoveredDevices.Count-1 do begin
// {$IFDEF IOS}
// ShortLine := Copy(BluetoothLE1.DiscoveredDevices.Items[i].Identifier, 26, 2) + ':' +
// Copy(BluetoothLE1.DiscoveredDevices.Items[i].Identifier, 28, 2) + ':' +
// Copy(BluetoothLE1.DiscoveredDevices.Items[i].Identifier, 30, 2) + ':' +
// Copy(BluetoothLE1.DiscoveredDevices.Items[i].Identifier, 32, 2) + ':' +
// Copy(BluetoothLE1.DiscoveredDevices.Items[i].Identifier, 34, 2) + ':' +
// Copy(BluetoothLE1.DiscoveredDevices.Items[i].Identifier, 36, 2);
// {$ELSE}
// ShortLine := BluetoothLE1.DiscoveredDevices.Items[i].Identifier; // Address
// {$ENDIF}
// if Length(BluetoothLE1.DiscoveredDevices.Items[i].DeviceName) > 2 then begin
// LongLine := ShortLine + ': ' + BluetoothLE1.DiscoveredDevices.Items[i].DeviceName;
// end else begin
// LongLine := ShortLine;
// end;
// Index := cmbDevices.Items.IndexOf(LongLine);
// if Index < 0 then begin
// // Now try short version
// Index := cmbDevices.Items.IndexOf(ShortLine);
// if Index < 0 then begin
// Index := cmbDevices.Items.Add(LongLine);
// end else begin
// cmbDevices.Items[Index] := LongLine;
// end;
// end;
ShortLine := BluetoothLE1.DiscoveredDevices.Items[i].DeviceName;
Index := cmbDevices.Items.IndexOf(ShortLine);
if Index < 0 then begin
Index := cmbDevices.Items.Add(ShortLine);
end;
cmbDevices.ItemIndex := cmbDevices.Items.IndexOf(edtDevice.Text);
end;
end;
procedure TfrmBluetoothSettings.btnApplyClick(Sender: TObject);
begin
inherited;
FixFloatEditBox(edtFrequency);
SendSettingsToDevice;
end;
procedure TfrmBluetoothSettings.SendSettingsToDevice;
begin
frmSources.SendParameterToSource(BLUETOOTH_SOURCE, 'F', edtFrequency.Text);
frmSources.SendParameterToSource(BLUETOOTH_SOURCE, 'M', edtMode.Text);
end;
procedure TfrmBluetoothSettings.tmrReceiveTimer(Sender: TObject);
var
PacketCount: Integer;
begin
PacketCount := frmSources.GetPacketCount(BLUETOOTH_SOURCE);
Inc(SearchPacketCount, PacketCount);
if PacketCount > 0 then begin
rectRx.Visible := True;
frmSources.ResetPacketCount(BLUETOOTH_SOURCE);
end else begin
rectRx.Visible := False;
end;
end;
procedure TfrmBluetoothSettings.tmrSearchTimer(Sender: TObject);
begin
tmrSearch.Enabled := False;
if SearchPacketCount > 1 then begin
StopSearch;
end else begin
NextSearch;
end;
end;
procedure TfrmBluetoothSettings.btnCancelClick(Sender: TObject);
begin
inherited;
//
end;
procedure TfrmBluetoothSettings.btnModeDownClick(Sender: TObject);
begin
edtMode.Text := IntToStr(Max(0,Min(7,StrToIntDef(edtMode.Text, 0)-1)));
SetingsHaveChanged;
end;
procedure TfrmBluetoothSettings.btnModeUpClick(Sender: TObject);
begin
edtMode.Text := IntToStr(Max(0,Min(7,StrToIntDef(edtMode.Text, 0)+1)));
SetingsHaveChanged;
end;
procedure TfrmBluetoothSettings.btnSearchClick(Sender: TObject);
begin
btnSearch.Enabled := False;
rectProgressBar.Width := 0;
// rectProgressHolder.Visible := True;
SearchCentreFrequency := StrToFloat(edtFrequency.Text);
SearchStep := -6;
Searching := True;
NextSearch;
end;
procedure TfrmBluetoothSettings.CancelChanges;
begin
edtDevice.Text := GetSettingString(Group, 'Device', '');
edtFrequency.Text := GetSettingString(Group, 'Frequency', '');
edtMode.Text := GetSettingString(Group, 'Mode', '');
edtCallsign.Text := GetSettingString(Group, 'Callsign', '');
CheckLCARSLabel(chkSondehub, GetSettingBoolean(Group, 'Sondehub', False));
CheckLCARSLabel(chkSSDV, GetSettingBoolean(Group, 'SSDV', False));
CheckLCARSLabel(chkAFC, GetSettingBoolean(Group, 'AFC', False));
SendSettingsToDevice;
inherited;
end;
procedure TfrmBluetoothSettings.chkSondehubClick(Sender: TObject);
begin
SetingsHaveChanged;
LCARSLabelClick(Sender);
end;
procedure TfrmBluetoothSettings.cmbDevicesClosePopup(Sender: TObject);
begin
if cmbDevices.ItemIndex >= 0 then begin
if edtDevice.Text <> cmbDevices.Items[cmbDevices.ItemIndex] then begin
edtDevice.Text := cmbDevices.Items[cmbDevices.ItemIndex];
SetingsHaveChanged;
end;
end;
end;
procedure TfrmBluetoothSettings.edtCallsignChangeTracking(Sender: TObject);
begin
SetingsHaveChanged;
end;
procedure TfrmBluetoothSettings.edtDeviceChangeTracking(Sender: TObject);
begin
SetingsHaveChanged;
end;
procedure TfrmBluetoothSettings.edtDeviceClick(Sender: TObject);
begin
cmbDevices.DropDown;
end;
procedure TfrmBluetoothSettings.edtFrequencyExit(Sender: TObject);
begin
FixFloatEditBox(edtFrequency);
end;
procedure TfrmBluetoothSettings.FormCreate(Sender: TObject);
begin
inherited;
Group := 'LoRaBluetooth';
end;
procedure TfrmBluetoothSettings.LoadForm;
begin
inherited;
UpdateBluetoothList;
end;
procedure TfrmBluetoothSettings.UpdateBluetoothList;
var
i: Integer;
{$IF Defined(MSWINDOWS) or Defined(ANDROID)}
Bluetooth1: TBluetooth;
{$ENDIF}
begin
cmbDevices.Items.Clear;
{$IF Defined(MSWINDOWS) or Defined(ANDROID)}
try
Bluetooth1 := TBluetooth.Create(nil);
Bluetooth1.Enabled := True;
if Bluetooth1.LastPairedDevices <> nil then begin
for i := 0 to Bluetooth1.LastPairedDevices.Count-1 do begin
// cmbDevices.Items.Add(IntToStr(Ord(Bluetooth1.LastPairedDevices[i].State)) + ': ' + Bluetooth1.LastPairedDevices[i].DeviceName + ' - ' + Bluetooth1.LastPairedDevices[i].Address);
cmbDevices.Items.Add(Bluetooth1.LastPairedDevices[i].DeviceName);
end;
end;
Bluetooth1.Free;
finally
end;
{$ELSE}
BluetoothLE1.Enabled := True;
BluetoothLE1.DiscoverDevices(5000);
{$ENDIF}
cmbDevices.ItemIndex := cmbDevices.Items.IndexOf(edtDevice.Text);
end;
procedure TfrmBluetoothSettings.NextSearch;
begin
Inc(SearchStep);
if SearchStep > 5 then begin
// pnlSearchFrequency.Caption := '';
rectProgressBar.Width := 0;
// rectProgressHolder.Visible := False;
Searching := False;
frmSources.SendParameterToSource(BLUETOOTH_SOURCE, 'F', edtFrequency.Text);
btnSearch.Enabled := True;
end else begin
// pnlSearchFrequency.Caption := FormatFloat('.0000', SearchFrequency);
rectProgressBar.Width := (SearchStep + 6) * rectProgressHolder.Width / 12;
edtFrequency.Text := FormatFloat('0.000#', SearchCentreFrequency + SearchStep * 0.002);
frmSources.SendParameterToSource(BLUETOOTH_SOURCE, 'F', edtFrequency.Text);
SearchPacketCount := 0;
tmrSearch.Enabled := True;
end;
end;
procedure TfrmBluetoothSettings.StopSearch;
begin
edtFrequency.Text := FormatFloat('0.000#', StrToFloat(edtFrequency.Text) + frmSources.FrequencyError(BLUETOOTH_SOURCE) / 1000);
frmSources.SendParameterToSource(BLUETOOTH_SOURCE, 'F', FormatFloat('0.000#', SearchCentreFrequency + SearchStep * 0.002));
rectProgressBar.Width := 0;
// rectProgressHolder.Visible := False;
btnSearch.Enabled := True;
Searching := False;
end;
end.