-
Notifications
You must be signed in to change notification settings - Fork 13
/
Om.Sensor.Android.pas
192 lines (167 loc) · 5.72 KB
/
Om.Sensor.Android.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
unit Om.Sensor.Android; // derived from DW.Sensor.Android,
// to bring cross platform functionality to DW.Sensor ( by Dave Nottage )
// code adapted by Omar ago19
// oct20: Changed from DelphiWorlds native Android sensors to custom Delphi sebsors
interface
uses
System.Sensors, {TCustomMotionSensor,TCustomOrientationSensor}
FMX.Types, {TTimer}
DW.Sensor;
type
TPlatformSensor = class(TCustomPlatformSensor) // iOS
private
private
FIsActive: Boolean;
FSensorStart: TDateTime;
FTimestamp: Int64;
// memoise sensors
fGyroSensor: TCustomMotionSensor;
fMagSensor: TCustomOrientationSensor;
fTimer:TTimer;
procedure TimerTick(Sender: TObject); // use timer to get the sensor values. It would be better to receive sensor notifications, but I don't know how ???
protected
function GetIsActive: Boolean; override;
procedure SetIsActive(const Value: Boolean); override;
procedure SetSensorType(const Value: TSensorType); override;
public
class function IsSensorTypeSupported(const ASensorType: TSensorType): Boolean;
public
constructor Create(const ASensor: TSensor); override;
destructor Destroy; override;
end;
implementation
uses
System.DateUtils, System.SysUtils;
{ TPlatformSensor }
constructor TPlatformSensor.Create(const ASensor: TSensor);
begin
inherited;
FIsActive := false;
FSensorStart := 0;
FTimestamp := 0;
//FListener := TSensorEventListener.Create(Self);
fGyroSensor := nil;
fMagSensor := nil;
fTimer := TTimer.Create(nil);
fTimer.Interval := 100;
fTimer.OnTimer := TimerTick;
fTimer.Enabled := False;
end;
destructor TPlatformSensor.Destroy;
begin
//FListener := nil;
fTimer.Free;
inherited;
end;
function TPlatformSensor.GetIsActive: Boolean;
begin
Result := FIsActive;
end;
class function TPlatformSensor.IsSensorTypeSupported( const ASensorType: TSensorType): Boolean;
begin
Result := ( ASensorType=Accelerometer ) or ( ASensorType= MagneticField); // only these two supported at this time ( the ones I needed )
//TODO: More sensors
end;
// sensor polled in a 100 ms timer
procedure TPlatformSensor.TimerTick(Sender:TObject);
var aVx,aVy,aVz:double;
LValues: TSensorValues;
procedure _NotifyValues;
begin
SetLength(LValues, 3);
LValues[0] := aVx;
LValues[1] := aVy;
LValues[2] := aVz;
if FTimestamp=0 then FSensorStart := Now;
ValuesChanged(LValues, Now ); // call sensor event
end;
begin
case FSensorType of
Accelerometer:
begin
if Assigned(fGyroSensor) and (fGyroSensor.Started) then
begin
aVx := -fGyroSensor.AccelerationX; // Invert gyro signal
aVy := -fGyroSensor.AccelerationY;
aVz := -fGyroSensor.AccelerationZ;
_NotifyValues;
end;
end;
MagneticField:
begin
if Assigned(fMagSensor) and (fMagSensor.Started) then
begin
aVx := fMagSensor.HeadingX; // mag field is the same for iOS and Android, it seems
aVy := fMagSensor.HeadingY;
aVz := fMagSensor.HeadingZ;
_NotifyValues;
end;
end;
//TODO: Other sensors
end;
end;
procedure TPlatformSensor.SetIsActive(const Value: Boolean);
var Sensor:TCustomSensor; aSensors:TSensorArray; aSensorManager: TSensorManager;
begin
if Value = FIsActive then Exit; // <======
if Value then
begin // get and activate the sensor manager
aSensorManager := TSensorManager.Current;
aSensorManager.Activate;
// get magnetic sensor
fMagSensor := nil;
aSensors := TSensorManager.Current.GetSensorsByCategory( TSensorCategory.Orientation ); // get sensor list
for Sensor in aSensors do
begin
if FSensorType=MagneticField then
begin
if TCustomOrientationSensor(Sensor).SensorType = TOrientationSensorType.Compass3D then
fMagSensor := TCustomOrientationSensor(Sensor);
if Assigned(fMagSensor) and (not fMagSensor.Started) then fMagSensor.Start;
fTimer.Enabled := true;
end;
end;
// get acceleration sensor
fGyroSensor := nil;
aSensors := TSensorManager.Current.GetSensorsByCategory( TSensorCategory.Motion ); // get sensor list
for Sensor in aSensors do
begin
if FSensorType=Accelerometer then
begin
if TCustomMotionSensor(Sensor).SensorType = TMotionSensorType.Accelerometer3D then
fGyroSensor := TCustomMotionSensor(Sensor);
if Assigned(fGyroSensor) and (not fGyroSensor.Started) then fGyroSensor.Start;
fTimer.Enabled := true;
end;
end;
end
else begin //setIsACTIVE(FALSE)
{ stop the sensor if it is not stopped }
case FSensorType of
Accelerometer:
begin
if Assigned(fGyroSensor) and (not fGyroSensor.Started) then fGyroSensor.Stop;
fTimer.Enabled := false;
end;
MagneticField:
begin
if Assigned(fMagSensor) and (not fMagSensor.Started) then fMagSensor.Stop;
fTimer.Enabled := false;
end;
end;
end;
FIsActive := Value;
end;
procedure TPlatformSensor.SetSensorType(const Value: TSensorType);
var
LIsActive: Boolean;
begin
if Value=FSensorType then
Exit; // <======
LIsActive := GetIsActive;
SetIsActive(False);
FSensorType := Value; //chg sensor type
FTimestamp := 0;
SetIsActive(LIsActive);
end;
end.