-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMain.pas
More file actions
1108 lines (936 loc) · 36.7 KB
/
Copy pathMain.pas
File metadata and controls
1108 lines (936 loc) · 36.7 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
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
unit Main;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Objects,
FMX.Controls.Presentation, FMX.StdCtrls, FMX.ScrollBox, FMX.Memo, System.IOUtils,
Math, Source, Miscellaneous, SourcesForm, Uploaders, Debug,
Base, Splash, Map, Direction, Payloads, Uplink,
{$IFDEF ANDROID}
Androidapi.JNIBridge, AndroidApi.JNI.Media,
Androidapi.JNI.JavaTypes, Androidapi.JNI.GraphicsContentViewText, Androidapi.Helpers, Androidapi.JNI.Net,
FMX.Helpers.Android, FMX.Platform.Android, AndroidApi.Jni.App,
AndroidAPI.jni.OS, FMX.TMSBaseGroup,
System.Permissions,
{$ENDIF}
Settings, Tawhiri, FMX.TMSFNCTypes, FMX.TMSFNCUtils, FMX.TMSFNCGraphics,
FMX.TMSFNCGraphicsTypes, FMX.TMSFNCHTMLImageContainer, FMX.TMSFNCRadioButton,
FMX.TMSFNCPageControl, FMX.TMSFNCCustomControl, FMX.TMSFNCTabSet,
FMX.TMSCustomEdit, FMX.TMSEdit, FMX.TMSFNCMapsCommonTypes,
FMX.TMSFNCWebBrowser, FMX.TMSFNCMaps, FMX.TMSFNCGoogleMaps;
type
TPayload = record
Previous: THABPosition;
Position: THABPosition;
Colour: TAlphaColor;
ColourName: String;
SourceMask: Integer;
SSDVCount: Integer;
PredictionIndex: Integer;
end;
type
TSource = record
Button: TButton;
LastPositionAt: TDateTime;
end;
type
TfrmMain = class(TForm)
pnlCentre: TRectangle;
StyleBook1: TStyleBook;
btnLoRaSerial: TButton;
btnSondehub: TButton;
btnGPS: TButton;
tmrUpdates: TTimer;
tmrResize: TTimer;
btnLoRaBluetooth: TButton;
tmrLoad: TTimer;
rectMain: TRectangle;
btnSettings: TButton;
btnMap: TButton;
btnDirection: TButton;
btnPayloads: TButton;
rectTopBar: TRectangle;
crcTopRight: TCircle;
rctTopRight: TRectangle;
crcTopLeft: TCircle;
rectTopLeft: TRectangle;
recButtons: TRectangle;
rectBottomBar: TRectangle;
crcBottomRight: TCircle;
rectBottomRight: TRectangle;
crcBottomLeft: TCircle;
rectBottomLeft: TRectangle;
rectSources: TRectangle;
btnUDP: TButton;
btnUplink: TButton;
pnlMap: TRectangle;
FNCMap: TTMSFNCGoogleMaps;
btnSondehubUpload: TButton;
btnSSDV: TButton;
procedure FormCreate(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure btnMapClick(Sender: TObject);
procedure btnPayloadsClick(Sender: TObject);
procedure btnDirectionClick(Sender: TObject);
procedure btnSettingsClick(Sender: TObject);
procedure tmrUpdatesTimer(Sender: TObject);
procedure btnSources(Sender: TObject);
procedure tmrResizeTimer(Sender: TObject);
procedure tmrLoadTimer(Sender: TObject);
procedure pnlCentreResize(Sender: TObject);
procedure rectTopBarResize(Sender: TObject);
procedure rectBottomBarResize(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure btnUplinkClick(Sender: TObject);
procedure rectTopLeftClick(Sender: TObject);
procedure FNCMapMapInitialized(Sender: TObject);
procedure FNCMapElementContainers0Actions0Execute(Sender: TObject;
AEventData: TTMSFNCMapsEventData);
procedure FNCMapElementContainers0Actions2Execute(Sender: TObject;
AEventData: TTMSFNCMapsEventData);
procedure FNCMapElementContainers0Actions1Execute(Sender: TObject;
AEventData: TTMSFNCMapsEventData);
procedure btnUploaders(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
DesignWidth: Double;
CurrentForm: TfrmBase;
Payloads: Array[1..3] of TPayload;
SelectedPayload: Integer;
Sources: Array[0..8] of TSource;
PayloadIndex: Array[1..8] of Integer;
ChasePosition: THABPosition;
Predictor: TTawhiri;
ScalingFactor: Double;
procedure LoadMapIfNotLoaded;
procedure ShowSelectedButton(Button: TButton);
function PlacePayloadInList(SourceID: Integer; var Position: THABPosition): Integer;
function FindOrAddPayload(Position: THABPosition): Integer;
procedure DoPayloadCalcs(Previous: THabPosition; var Position: THABPosition);
procedure UpdatePayloadList;
procedure Navigate(TargetLatitude, TargetLongitude, CurrentLatitude, CurrentLongitude: Double; OffRoad: Boolean = False);
procedure ShowRouteOnMap(TargetLatitude, TargetLongitude, CurrentLatitude, CurrentLongitude: Double);
procedure WhereIsBalloon(PayloadIndex: Integer);
procedure WhereAreBalloons;
procedure ShowSelectedMapButton(ElementID: String);
{$IF Defined(IOS) or Defined(ANDROID)}
procedure PlaySound(Flag: Integer);
{$ENDIF}
public
{ Public declarations }
function LoadForm(Button: TButton; NewForm: TfrmBase): Boolean;
procedure SondehubUploadStatus(Active, OK: Boolean);
procedure SSDVUploadStatus(Active, OK: Boolean);
procedure NewPosition(SourceID: Integer; Position: THABPosition);
function BalloonColour(PayloadIndex: Integer): TAlphaColor;
function BalloonIconName(PayloadIndex: Integer; Target: Boolean=False): String;
procedure NavigateToPayload(PayloadIndex: Integer; UsePrediction: Boolean; OffRoad: Boolean = False);
procedure ShowRouteToPayload(PayloadIndex: Integer; UsePrediction: Boolean);
procedure ShowSourceStatus(SourceID: Integer; Active, Recent: Boolean);
procedure SelectPayload(PayloadIndex: Integer);
function GetChasePosition(var Latitude: Double; var Longitude: Double; var Altitude: Double): Boolean;
function GetSourceMask(PayloadIndex: Integer): Integer;
procedure ResizeFonts(AForm: TForm);
procedure LoadSettingsPage(PageIndex: Integer);
procedure UpdateCarUploadSettings;
end;
var
frmMain: TfrmMain;
implementation
{$R *.fmx}
function FontScale: Single;
{$IFDEF ANDROID}
var
Resources: JResources;
Configuration: JConfiguration;
{$ENDIF ANDROID}
{$IFDEF IOS}
var
f: UIFontDescriptor;
{$ENDIF IOS}
begin
Result := 1.0;
{$IFDEF ANDROID}
if TAndroidHelper.Context <> nil then
begin
Resources := TAndroidHelper.Context.getResources;
if Resources <> nil then
begin
Configuration := Resources.getConfiguration;
if Configuration <> nil then
Result := Configuration.fontScale;
end;
end;
{$ENDIF ANDROID}
{$IFDEF IOS}
f := TUIFontDescriptor.OCClass.preferredFontDescriptorWithTextStyle(
UIFontTextStyleBody);
Result := f.pointSize / 17.0;
{$ENDIF IOS}
end;
procedure TfrmMain.btnDirectionClick(Sender: TObject);
begin
LoadForm(TButton(Sender), frmDirection);
end;
procedure TfrmMain.btnMapClick(Sender: TObject);
begin
LoadForm(TButton(Sender), nil);
end;
procedure TfrmMain.btnPayloadsClick(Sender: TObject);
begin
if LoadForm(TButton(Sender), frmPayloads) then begin
frmPayloads.NewSelection(SelectedPayload);
end;
end;
procedure TfrmMain.btnSettingsClick(Sender: TObject);
begin
LoadSettingsPage(0);
end;
procedure TfrmMain.btnSources(Sender: TObject);
begin
LoadForm(nil, frmSources);
end;
procedure TfrmMain.btnUplinkClick(Sender: TObject);
begin
if LoadForm(TButton(Sender), frmUplink) then begin
frmUplink.NewSelection(SelectedPayload);
end;
end;
procedure TfrmMain.btnUploaders(Sender: TObject);
begin
LoadForm(nil, frmUploaders);
end;
procedure TfrmMain.LoadMapIfNotLoaded;
begin
if frmMap = nil then begin
try
frmMap := TfrmMap.Create(Self);
except
on E : Exception do begin
if frmDebug <> nil then begin
frmDebug.Debug(E.ClassName + '/' + E.Message);
end;
end;
end;
end;
end;
procedure TfrmMain.FormActivate(Sender: TObject);
const
FirstTime: Boolean = True;
begin
if FirstTime then begin
FirstTime := False;
// Debug form, but only if we're a debug build
{$IFDEF DEBUG}
frmDebug := TfrmDebug.Create(Self);
{$ENDIF}
// Splash form
frmSplash := TfrmSplash.Create(Self);
LoadForm(nil, frmSplash);
tmrLoad.Enabled := True;
ScalingFactor := 0.9 * pnlCentre.Width / DesignWidth;
ResizeFonts(Self);
end;
end;
procedure TfrmMain.FormCreate(Sender: TObject);
begin
INIFileName := TPath.Combine(DataFolder, 'HABExplora.ini');
CurrentForm := nil;
//{$IFDEF IOS}
// rectMain.Margins.Top := 18;
//{$ENDIF}
DesignWidth := pnlCentre.Width;
Payloads[1].Colour := TAlphaColorRec.Blue;
Payloads[2].Colour := TAlphaColorRec.Red;
Payloads[3].Colour := TAlphaColorRec.Lime;
Payloads[1].ColourName := 'blue';
Payloads[2].ColourName := 'red';
Payloads[3].ColourName :='green';
InitialiseSettings;
// Source info
Sources[GPS_SOURCE].Button := btnGPS;
Sources[SERIAL_SOURCE].Button := btnLoRaSerial;
Sources[BLUETOOTH_SOURCE].Button := btnLoRaBluetooth;
Sources[SONDEHUB_SOURCE].Button := btnSondehub;
Sources[UDP_SOURCE].Button := btnUDP;
{$IFDEF MSWINDOWS}
FullScreen := False;
{$ELSE}
FullScreen := True;
{$ENDIF}
Predictor := TTawhiri.Create;
end;
procedure TfrmMain.FormDestroy(Sender: TObject);
begin
inherited;
CloseSettings;
Predictor.Free;
end;
procedure TfrmMain.FormResize(Sender: TObject);
begin
rectTopBar.Height := frmMain.Height / 15;
rectBottomBar.Height := frmMain.Height / 15;
end;
function TfrmMain.LoadForm(Button: TButton; NewForm: TfrmBase): Boolean;
begin
if (Button <> btnSettings) and (frmSettings <> nil) then begin
frmSettings.Free;
frmSettings := nil;
end;
pnlCentre.Visible := NewForm <> nil;
pnlMap.Visible := NewForm = nil;
if NewForm <> nil then begin
ShowSelectedButton(Button);
if CurrentForm <> nil then begin
CurrentForm.HideForm;
end;
CurrentForm := NewForm;
NewForm.pnlMain.Parent := pnlCentre;
NewForm.LoadForm;
ResizeFonts(NewForm);
Result := True;
end else begin
Result := False;
end;
end;
procedure TfrmMain.ShowSelectedButton(Button: TButton);
begin
btnPayloads.TextSettings.Font.Style := btnPayloads.TextSettings.Font.Style - [TFontStyle.fsUnderline];
btnMap.TextSettings.Font.Style := btnMap.TextSettings.Font.Style - [TFontStyle.fsUnderline];
btnDirection.TextSettings.Font.Style := btnDirection.TextSettings.Font.Style - [TFontStyle.fsUnderline];
btnSettings.TextSettings.Font.Style := btnSettings.TextSettings.Font.Style - [TFontStyle.fsUnderline];
btnUplink.TextSettings.Font.Style := btnUplink.TextSettings.Font.Style - [TFontStyle.fsUnderline];
if Button <> nil then begin
Button.TextSettings.Font.Style := Button.TextSettings.Font.Style + [TFontStyle.fsUnderline];
end;
end;
{$IFDEF ANDROID}
procedure TfrmMain.PlaySound(Flag: Integer);
var
Volume, ADuration: Integer;
StreamType: Integer;
ToneType: Integer;
ToneGenerator: JToneGenerator;
begin
Volume := TJToneGenerator.JavaClass.MAX_VOLUME;
ADuration := 100 * Flag;
StreamType := TJAudioManager.JavaClass.STREAM_ALARM;
ToneType := TJToneGenerator.JavaClass.TONE_DTMF_0;
ToneGenerator := TJToneGenerator.JavaClass.init(StreamType, Volume);
ToneGenerator.startTone(ToneType, ADuration);
end;
{$ENDIF}
{$IFDEF IOS}
procedure TfrmMain.PlaySound(Flag: Integer);
begin
Beep;
end;
{$ENDIF}
procedure TfrmMain.pnlCentreResize(Sender: TObject);
begin
tmrResize.Enabled := True;
end;
procedure TfrmMain.rectBottomBarResize(Sender: TObject);
begin
rectBottomBar.Margins.Left := rectBottomBar.Height / 2;
rectBottomBar.Margins.Right := rectBottomBar.Height / 2;
rectSources.Margins.Left := - rectBottomBar.Height / 2;
// rectStatus.Width := rectBottomBar.Width / 5;
// rectStatus.Margins.Right := -rectStatus.Height * 0.67;
crcBottomLeft.Width := crcBottomLeft.Height;
crcBottomLeft.Margins.Left := -crcBottomLeft.Height/2;
crcBottomRight.Width := crcBottomRight.Height;
crcBottomRight.Margins.Right := -crcBottomRight.Height/2;
rectBottomLeft.Margins.Bottom := crcBottomLeft.Height / 2;
rectBottomRight.Margins.Bottom := crcTopLeft.Height / 2;
end;
procedure TfrmMain.rectTopBarResize(Sender: TObject);
begin
rectTopBar.Margins.Left := rectTopBar.Height / 2;
rectTopBar.Margins.Right := rectTopBar.Height / 2;
crcTopLeft.Width := crcTopLeft.Height;
crcTopLeft.Margins.Left := -crcTopLeft.Height/2;
crcTopRight.Width := crcTopRight.Height;
crcTopRight.Margins.Right := -crcTopRight.Height/2;
rectTopLeft.Margins.Top := crcTopRight.Height / 2;
rctTopRight.Margins.Top := crcTopRight.Height / 2;
recButtons.Margins.Left := -crcTopRight.Height / 2;
recButtons.Margins.Right := recButtons.Margins.Left;
end;
procedure TfrmMain.rectTopLeftClick(Sender: TObject);
begin
LoadForm(nil, frmSplash);
end;
procedure TfrmMain.SondehubUploadStatus(Active, OK: Boolean);
begin
if Active then begin
btnSondehubUpload.Opacity := 1.0;
if OK then begin
btnSondehubUpload.TextSettings.FontColor := TAlphaColorRec.Green;
end else begin
btnSondehubUpload.TextSettings.FontColor := TAlphaColorRec.Red;
end;
end else begin
btnSondehubUpload.Opacity := 0.7;
btnSondehubUpload.TextSettings.FontColor := TAlphaColorRec.Black;
end;
end;
procedure TfrmMain.SSDVUploadStatus(Active, OK: Boolean);
begin
if Active then begin
btnSSDV.Opacity := 1.0;
if OK then begin
btnSSDV.TextSettings.FontColor := TAlphaColorRec.Green;
end else begin
btnSSDV.TextSettings.FontColor := TAlphaColorRec.Red;
end;
end else begin
btnSSDV.Opacity := 0.7;
btnSSDV.TextSettings.FontColor := TAlphaColorRec.Black;
end;
end;
procedure TfrmMain.WhereIsBalloon(PayloadIndex: Integer);
begin
if ((ChasePosition.Latitude <> 0) or (ChasePosition.Longitude <> 0)) and Payloads[PayloadIndex].Position.InUse then begin
Payloads[PayloadIndex].Position.DirectionValid := True;
// Horizontal distance to payload
Payloads[PayloadIndex].Position.Distance := CalculateDistance(Payloads[PayloadIndex].Position.Latitude,
Payloads[PayloadIndex].Position.Longitude,
ChasePosition.Latitude, ChasePosition.Longitude);
// Direction to payload
Payloads[PayloadIndex].Position.Direction := CalculateDirection(Payloads[PayloadIndex].Position.Latitude,
Payloads[PayloadIndex].Position.Longitude,
ChasePosition.Latitude, ChasePosition.Longitude);
Payloads[PayloadIndex].Position.Elevation := CalculateElevation(ChasePosition.Latitude, ChasePosition.Longitude, ChasePosition.Altitude,
Payloads[PayloadIndex].Position.Latitude, Payloads[PayloadIndex].Position.Longitude, Payloads[PayloadIndex].Position.Altitude);
if Payloads[PayloadIndex].Position.PredictionType <> ptNone then begin
// Horizontal distance to payload
Payloads[PayloadIndex].Position.PredictionDistance := CalculateDistance(ChasePosition.Latitude, ChasePosition.Longitude,
Payloads[PayloadIndex].Position.PredictedLatitude,
Payloads[PayloadIndex].Position.PredictedLongitude);
// Direction to payload
Payloads[PayloadIndex].Position.PredictionDirection := CalculateDirection(Payloads[PayloadIndex].Position.PredictedLatitude,
Payloads[PayloadIndex].Position.PredictedLongitude,
ChasePosition.Latitude, ChasePosition.Longitude) -
ChasePosition.Direction;
end;
end else begin
Payloads[PayloadIndex].Position.DirectionValid := False;
end;
end;
procedure TfrmMain.WhereAreBalloons;
var
PayloadIndex: Integer;
begin
for PayloadIndex := Low(Payloads) to High(Payloads) do begin
WhereIsBalloon(PayloadIndex);
end;
end;
procedure TfrmMain.NewPosition(SourceID: Integer; Position: THABPosition);
var
Index: Integer;
begin
if Position.InUse then begin
ShowSourceStatus(SourceID, True, True);
Index := PlacePayloadInList(SourceID, Position);
if Position.IsChase or (Index > 0) then begin
if Position.IsChase then begin
// Chase car only
ChasePosition := Position;
WhereAreBalloons;
// lblGPS.Text := FormatDateTime('hh:nn:ss', Position.TimeStamp);
end else begin
// Payloads only
WhereIsBalloon(Index);
Position := Payloads[Index].Position; // Get updated position back
if frmPayloads <> nil then begin
try
frmPayloads.NewPosition(Index, Position);
except
//
end;
end;
// Landing prediction needed ?
if Position.PredictionType = ptNone then begin
if Payloads[Index].PredictionIndex <= 0 then begin
Payloads[Index].PredictionIndex := Predictor.AddPayload(Position.PayloadID);
end;
if Payloads[Index].PredictionIndex > 0 then begin
Predictor.UpdatePayload(Payloads[Index].PredictionIndex,
Position.Latitude,
Position.Longitude,
Position.Altitude,
30000, // Burst
Position.AscentRate,
5);
end;
end;
end;
// Payloads and Chase Car
if frmDirection <> nil then begin
try
frmDirection.NewPosition(Index, Position);
except
//
end;
end;
if frmMap <> nil then begin
try
if (Position.Latitude <> 0) or (Position.Longitude <> 0) then begin
frmMap.NewPosition(Index, Position);
end;
except
//
end;
end;
end;
end else begin
Index := PayloadIndex[SourceID];
if (Index >= Low(Payloads)) and (Index <= High(Payloads)) then begin
if Position.IsSSDV then begin
Inc(Payloads[Index].SSDVCount);
Position.SSDVCount := Payloads[Index].SSDVCount;
end;
// Update RSSI etc
if frmPayloads <> nil then begin
try
Position.FlightMode := Payloads[Index].Position.FlightMode;
frmPayloads.NewPosition(Index, Position);
except
//
end;
end;
end;
end;
end;
function TfrmMain.PlacePayloadInList(SourceID: Integer; var Position: THABPosition): Integer;
var
NewMask, Index: Integer;
PayloadChanged: Boolean;
begin
Result := 0;
if Position.InUse and not Position.IsChase then begin
Index := FindOrAddPayload(Position);
PayloadIndex[SourceID] := Index;
if Index > 0 then begin
NewMask := Payloads[Index].SourceMask or ((1 shl (SourceID * 2)) shl Position.Channel);
if NewMask <> Payloads[Index].SourceMask then begin
Payloads[Index].SourceMask := NewMask;
if frmUplink <> nil then frmUplink.NewSelection(SelectedPayload);
end;
// Update forms with payload list, if it has changed
if (not Payloads[Index].Position.InUse) or (Position.PayloadID <> Payloads[Index].Position.PayloadID) then begin
PayloadChanged := True;
end else begin
PayloadChanged := False;
end;
// if (Position.TimeStamp - Payloads[Index].Previous.TimeStamp) >= 1/86400 then begin
if (Position.TimeStamp > Payloads[Index].Position.TimeStamp) or (Position.PayloadID <> Payloads[Index].Position.PayloadID) then begin
// Retrieve previous flight mode
Position.FlightMode := Payloads[Index].Previous.FlightMode;
// Calculate ascent rate etc
DoPayloadCalcs(Payloads[Index].Previous, Position);
// Update buttons
// Payloads[Index].Button.Text := Position.PayloadID;
// Payloads[Index].Button.Opacity := 1;
// Store new position
Payloads[Index].Previous := Payloads[Index].Position;
Position.TelemetryCount := Payloads[Index].Previous.TelemetryCount + 1;
Payloads[Index].Position := Position;
Payloads[Index].Previous.FlightMode := Position.FlightMode;
if PayloadChanged and (SourceID <> 4) then begin
UpdatePayloadList;
end;
Result := Index;
end;
end;
end;
end;
//procedure TfrmMain.ShowSelectedPayloadPosition;
//begin
// with Payloads[SelectedPayload].Position do begin
// frmMain.lblPayload.Text := FormatDateTime('hh:nn:ss', TimeStamp) + ' ' +
// Format('%2.6f', [Latitude]) + ',' +
// Format('%2.6f', [Longitude]) + ' at ' +
// Format('%.0f', [Altitude]) + 'm';
// end;
//end;
procedure TfrmMain.tmrLoadTimer(Sender: TObject);
begin
tmrLoad.Enabled := False;
// Main forms
frmPayloads := TfrmPayloads.Create(Self);
frmDirection := TfrmDirection.Create(Self);
frmUplink := TfrmUplink.Create(Self);
frmUploaders := TfrmUploaders.Create(Self);
LoadMapIfNotLoaded;
// Sources Form
frmSources := TfrmSources.Create(Self);
// Ask for GPS permission
{$IFDEF ANDROID}
frmSources.SetGPSStatus('Requesting GPS Permission');
(*
PermissionsService.RequestPermissions([JStringToString(TJManifest_permission.JavaClass.ACCESS_FINE_LOCATION)],
procedure(const APermissions: TArray<string>; const AGrantResults: TArray<TPermissionStatus>) begin
if (Length(AGrantResults) = 1) and (AGrantResults[0] = TPermissionStatus.Granted) then begin
// activate or deactivate the location sensor }
frmSources.EnableGPS;
end else begin
frmSources.lblGPS.Text := 'No GPS Permission';
end;
end);
*)
PermissionsService.RequestPermissions([JStringToString(TJManifest_permission.JavaClass.ACCESS_FINE_LOCATION)],
procedure(const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray) begin
if (Length(AGrantResults) = 1) and (AGrantResults[0] = TPermissionStatus.Granted) then begin
// activate or deactivate the location sensor }
frmSources.EnableGPS;
end else begin
frmSources.SetGPSStatus('No GPS Permission');
end;
end);
{$ENDIF}
{$IFDEF IOS}
btnLoRaBluetooth.Text := 'BLE';
btnLoRaSerial.Text := '';
crcLoRaSerial.Visible := False;
frmSources.EnableGPS;
{$ENDIF}
frmSources.EnableCompass;
frmSplash.UnveilSplash;
// lblGPS.Text := '';
end;
procedure TfrmMain.tmrResizeTimer(Sender: TObject);
//var
// FontSize: Double;
begin
tmrResize.Enabled := False;
// FontSize := lblGPS.Size.Height * 36/64;
// Source Buttons
(*
btnTablet.Font.Size := FontSize;
lblGPS.Font.Size := FontSize;
btnLoRaSerial.Font.Size := FontSize;
btnGPS.Font.Size := FontSize;
lblGPS.Font.Size := FontSize;
// Menu buttons
btnPayloads.Font.Size := FontSize;
btnMap.Font.Size := FontSize;
btnDirection.Font.Size := FontSize;
btnSettings.Font.Size := FontSize;
*)
end;
procedure TfrmMain.tmrUpdatesTimer(Sender: TObject);
var
SourceID, Index: Integer;
PredictedAltitude: Double;
begin
if frmPayloads <> nil then begin
for Index := Low(Payloads) to High(Payloads) do begin
if Payloads[Index].Position.InUse and (Payloads[Index].Position.ReceivedAt > 0) then begin
frmPayloads.ShowTimeSinceUpdate(Index, Now - Payloads[Index].Position.ReceivedAt, Payloads[Index].Position.Repeated);
// Prediction done ?
if Payloads[Index].Position.PredictionType = ptNone then begin
if Payloads[Index].PredictionIndex > 0 then begin
if Predictor.PredictionUpdated(Payloads[Index].PredictionIndex) then begin
Predictor.GetPrediction(Index, Payloads[Index].Position.PredictedLatitude, Payloads[Index].Position.PredictedLongitude, PredictedAltitude);
Payloads[Index].Position.PredictionType := ptTawhiri;
// Move marker on map
if frmMap <> nil then frmMap.NewPosition(Index, Payloads[Index].Position);
// Updated in payload box
if frmPayloads <> nil then frmPayloads.NewPosition(Index, Payloads[Index].Position);
end;
end;
end;
end;
end;
end;
for SourceID := Low(Sources) to High(Sources) do begin
if (Now - Sources[SourceID].LastPositionAt) > 60/86400 then begin
ShowSourceStatus(SourceID, Sources[SourceID].LastPositionAt > 0, False);
end;
end;
end;
function TfrmMain.FindOrAddPayload(Position: THABPosition): Integer;
var
i: Integer;
begin
// Look for same payload
for i := Low(Payloads) to High(Payloads) do begin
if Payloads[i].Position.InUse then begin
if Position.PayloadID = Payloads[i].Position.PayloadID then begin
Result := i;
Exit;
end;
end;
end;
// Look for empty slot
for i := Low(Payloads) to High(Payloads) do begin
if not Payloads[i].Position.InUse then begin
Result := i;
Payloads[i].Position.InUse := True;
SelectPayload(i);
Exit;
end;
end;
// Look for oldest payload
Result := Low(Payloads);
for i := Low(Payloads)+1 to High(Payloads) do begin
if Payloads[i].Position.TimeStamp < Payloads[Result].Position.TimeStamp then begin
// This one is older than oldest so far
Result := i;
end;
end;
end;
procedure TfrmMain.ShowSelectedMapButton(ElementID: String);
begin
FNCMap.ExecuteJavaScript('document.getElementById("btnCar").disabled = false');
FNCMap.ExecuteJavaScript('document.getElementById("btnHAB").disabled = false');
FNCMap.ExecuteJavaScript('document.getElementById("btnFree").disabled = false');
FNCMap.ExecuteJavaScript('document.getElementById("' + ElementID + '").disabled = true');
end;
procedure TfrmMain.FNCMapElementContainers0Actions0Execute(Sender: TObject;
AEventData: TTMSFNCMapsEventData);
begin
ShowSelectedMapButton('btnCar');
frmMap.btnCarClick(Sender);
end;
procedure TfrmMain.FNCMapElementContainers0Actions1Execute(Sender: TObject;
AEventData: TTMSFNCMapsEventData);
begin
ShowSelectedMapButton('btnHAB');
frmMap.btnPayloadClick(Sender);
end;
procedure TfrmMain.FNCMapElementContainers0Actions2Execute(Sender: TObject;
AEventData: TTMSFNCMapsEventData);
begin
ShowSelectedMapButton('btnFree');
frmMap.btnFreeClick(Sender);
end;
procedure TfrmMain.FNCMapMapInitialized(Sender: TObject);
begin
frmMap.MapInitialized;
end;
procedure TfrmMain.DoPayloadCalcs(Previous: THabPosition; var Position: THABPosition);
const
FlightModes: Array[0..8] of String = ('Idle', 'Launched', 'Descending', 'Homing', 'Direct To Target', 'Downwind', 'Upwind', 'Landing', 'Landed');
begin
Position.AscentRate := (Position.Altitude - Previous.Altitude) / (86400 * (Position.TimeStamp - Previous.TimeStamp));
Position.MaxAltitude := max(Position.Altitude, Previous.MaxAltitude);
// Flight mode
case Previous.FlightMode of
fmIdle: begin
if ((Position.AscentRate > 2.0) and (Position.Altitude > 100)) or
(Position.Altitude > 5000) or
(Abs(Position.AscentRate) > 20) or
((Position.AscentRate > 1.0) and (Position.Altitude > 300)) then begin
Position.FlightMode := fmLaunched;
end;
end;
fmLaunched: begin
if Position.AscentRate < -4 then begin
Position.FlightMode := fmDescending;
end;
end;
fmDescending: begin
if Position.AscentRate > -1 then begin
Position.FlightMode := fmLanded;
end;
end;
fmLanded: begin
if ((Position.AscentRate > 3.0) and (Position.Altitude > 100)) or
((Position.AscentRate > 2.0) and (Position.Altitude > 500)) then begin
Position.FlightMode := fmLaunched;
end;
end;
end;
if Position.FlightMode = fmDescending then begin
Position.DescentTime := CalculateDescentTime(Position.Altitude, -Position.AscentRate, ChasePosition.Altitude) / 86400;
end;
end;
function TfrmMain.BalloonColour(PayloadIndex: Integer): TAlphaColor;
begin
if (PayloadIndex >= Low(Payloads)) and (PayloadIndex <= High(Payloads)) then begin
Result := Payloads[PayloadIndex].Colour;
end else begin
Result := Payloads[Low(Payloads)].Colour;
end;
end;
function TfrmMain.BalloonIconName(PayloadIndex: Integer; Target: Boolean=False): String;
begin
Result := 'balloon-' + Payloads[PayloadIndex].ColourName;
if Target then begin
Result := 'x-' + Payloads[PayloadIndex].ColourName;
end else begin
case Payloads[PayloadIndex].Position.FlightMode of
fmIdle: Result := 'payload-' + Payloads[PayloadIndex].ColourName;
fmLaunched: Result := 'balloon-' + Payloads[PayloadIndex].ColourName;
fmDescending: Result := 'parachute-' + Payloads[PayloadIndex].ColourName;
fmLanded: Result := 'payload-' + Payloads[PayloadIndex].ColourName;
end;
end;
end;
{$IFDEF ANDROID}
procedure OpenURL(const URL: string);
var
Intent: JIntent;
begin
Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW, TJnet_Uri.JavaClass.parse(StringToJString(URL)));
try
SharedActivity.startActivity(Intent);
except
end;
end;
{$ENDIF ANDROID}
procedure TfrmMain.Navigate(TargetLatitude, TargetLongitude, CurrentLatitude, CurrentLongitude: Double; OffRoad: Boolean = False);
begin
{$IFDEF ANDROID}
if OffRoad then begin
// This offers us Backcountry Navigator etc.
OpenURL('geo:' + TargetLatitude.ToString + ',' + TargetLongitude.ToString);
end else begin
// This offers us Sygic etc.
OpenURL('google.navigation:q=' + TargetLatitude.ToString + ',' + TargetLongitude.ToString);
end;
{$ELSE}
ShowRouteOnMap(TargetLatitude, TargetLongitude, CurrentLatitude, CurrentLongitude);
{$ENDIF ANDROID}
end;
procedure TfrmMain.ShowRouteOnMap(TargetLatitude, TargetLongitude, CurrentLatitude, CurrentLongitude: Double);
begin
if frmMap <> nil then begin
if LoadForm(btnMap, frmMap) then begin
frmMap.ShowDirections(TargetLatitude, TargetLongitude, CurrentLatitude, CurrentLongitude);
end;
end;
end;
procedure TfrmMain.NavigateToPayload(PayloadIndex: Integer; UsePrediction: Boolean; Offroad: Boolean = False);
begin
if PayloadIndex > 0 then begin
if UsePrediction then begin
Navigate(Payloads[PayloadIndex].Position.PredictedLatitude,
Payloads[PayloadIndex].Position.PredictedLongitude,
ChasePosition.Latitude,
ChasePosition.Longitude,
OffRoad);
end else begin
Navigate(Payloads[PayloadIndex].Position.Latitude,
Payloads[PayloadIndex].Position.Longitude,
ChasePosition.Latitude,
ChasePosition.Longitude,
OffRoad);
end;
end;
end;
procedure TfrmMain.ShowRouteToPayload(PayloadIndex: Integer; UsePrediction: Boolean);
begin
if PayloadIndex > 0 then begin
if UsePrediction then begin
ShowRouteOnMap(Payloads[PayloadIndex].Position.PredictedLatitude,
Payloads[PayloadIndex].Position.PredictedLongitude,
ChasePosition.Latitude,
ChasePosition.Longitude);
end else begin
ShowRouteOnMap(Payloads[PayloadIndex].Position.Latitude,
Payloads[PayloadIndex].Position.Longitude,
ChasePosition.Latitude,
ChasePosition.Longitude);
end;
end;
end;
procedure TfrmMain.UpdatePayloadList;
var
PayloadList: String;
i: Integer;
begin
PayloadList := '';
for i := Low(Payloads) to High(Payloads) do begin