-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLazyShakerForm.pas
174 lines (146 loc) · 3.63 KB
/
LazyShakerForm.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
unit LazyShakerForm;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
System.Sensors,
{$IFDEF VER270}
System.Sensors.Components,
{$ELSE}
FMX.Sensors,
{$ENDIF}
FMX.Layouts, FMX.Memo, FMX.Media,
FMX.StdCtrls, System.Actions, FMX.ActnList, FMX.StdActns, FMX.Objects;
type
TLazyShakeSensor = class
strict private
type
TSensorData = double;
var
FMotionSensor: TMotionSensor;
FTimer: TTimer;
FOnShake: TNotifyEvent;
// acceleration apart from gravity
FAccel: TLazyShakeSensor.TSensorData;
// current acceleration including gravity
FAccelCurrent: TLazyShakeSensor.TSensorData;
// last acceleration including gravity
FAccelLast: TLazyShakeSensor.TSensorData;
procedure DoOnSensorDataChanged(Sender: TObject);
procedure DoTriggerShakeEvent;
public
procedure InitSensors;
procedure DeInitSensors;
constructor Create;
destructor Destroy; override;
property OnShake: TNotifyEvent read FOnShake write FOnShake;
end;
TfrmLazyShaker = class(TForm)
MediaPlayer1: TMediaPlayer;
ActionList1: TActionList;
FileExit1: TFileExit;
Button1: TButton;
Image1: TImage;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
strict private
FShakeSensor: TLazyShakeSensor;
procedure PlaySound(const aFile: string);
procedure PlayDefaultSound;
procedure DoOnShake(Sender: TObject);
end;
var
frmLazyShaker: TfrmLazyShaker;
implementation
uses
IoUtils;
{$R *.fmx}
procedure TfrmLazyShaker.DoOnShake(Sender: TObject);
begin
PlayDefaultSound;
end;
procedure TfrmLazyShaker.FormCreate(Sender: TObject);
begin
FShakeSensor:= TLazyShakeSensor.Create;
FShakeSensor.OnShake := DoOnShake;
FShakeSensor.InitSensors;
end;
procedure TfrmLazyShaker.FormDestroy(Sender: TObject);
begin
try
FShakeSensor.DeInitSensors;
except
// act bad - eat exception
end;
FreeAndNil(FShakeSensor);
end;
procedure TfrmLazyShaker.PlayDefaultSound;
var
s: string;
begin
s := TPath.GetDocumentsPath;
s := TPath.Combine(s, 'ElevatorBell.mp3');
PlaySound(s);
end;
procedure TfrmLazyShaker.PlaySound(const aFile: string);
begin
if FileExists(aFile) then
begin
MediaPlayer1.Stop;
MediaPlayer1.Clear;
MediaPlayer1.Volume := 100;
MediaPlayer1.FileName := aFile;
MediaPlayer1.CurrentTime := 0;
MediaPlayer1.Play;
end;
end;
{ TLazyShakeSensor }
constructor TLazyShakeSensor.Create;
begin
FAccel:=0;
FAccelCurrent:=0.9;
FAccelLast:=0.9;
end;
procedure TLazyShakeSensor.DeInitSensors;
begin
FTimer.Enabled := False;
FreeAndNil(FTimer);
FreeAndNil(FMotionSensor);
end;
destructor TLazyShakeSensor.Destroy;
begin
inherited;
end;
procedure TLazyShakeSensor.DoOnSensorDataChanged(Sender: TObject);
var
tmpSensor: TCustomMotionSensor;
x,y,z: TSensorData;
begin
if FMotionSensor.Sensor<>nil then
begin
tmpSensor := FMotionSensor.Sensor;
x := tmpSensor.AccelerationX;
y := tmpSensor.AccelerationY;
z := tmpSensor.AccelerationZ;
FAccelLast := FAccelCurrent;
FAccelCurrent := Sqrt(x*x+y*y+z*z);
FAccel := FAccel*0.9+(FAccelCurrent-FAccelLast);
if FAccel >1.3 then
DoTriggerShakeEvent;
end;
end;
procedure TLazyShakeSensor.DoTriggerShakeEvent;
begin
if Assigned(FOnShake) then
FOnShake(Self);
end;
procedure TLazyShakeSensor.InitSensors;
begin
FMotionSensor := TMotionSensor.Create(nil);
FMotionSensor.Active := True;
FTimer := TTimer.Create(nil);
FTimer.OnTimer := DoOnSensorDataChanged;
FTimer.Interval := 50;
FTimer.Enabled := True;
end;
end.