| 1 |
{ |
| 2 |
Copyright 2020-2020 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Activity recognition (detects user stationary, walking, running and so on) (TActivityRecognition). } |
| 17 |
unit CastleActivityRecognition; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses Classes, |
| 24 |
CastleStringUtils, CastleTimeUtils; |
| 25 |
|
| 26 |
type |
| 27 |
{ Current user activity, used by @link(TActivityRecognition.Activity). |
| 28 |
See https://developer.apple.com/documentation/coremotion/cmmotionactivity |
| 29 |
for the underlying iOS docs. } |
| 30 |
TUserActivity = ( |
| 31 |
uaStationary, |
| 32 |
uaWalking, |
| 33 |
uaRunning, |
| 34 |
uaAutomotive, |
| 35 |
uaCycling, |
| 36 |
uaUnknown |
| 37 |
); |
| 38 |
|
| 39 |
TUserActivities = set of TUserActivity; |
| 40 |
|
| 41 |
{ Current user activity, used by @link(TActivityRecognition.ActivityConfidence). |
| 42 |
See https://developer.apple.com/documentation/coremotion/cmmotionactivity |
| 43 |
for the underlying iOS docs. } |
| 44 |
TUserActivityConfidence = ( |
| 45 |
uacLow, |
| 46 |
uacMedium, |
| 47 |
uacHigh |
| 48 |
); |
| 49 |
|
| 50 |
{ Activity recognition (detects user stationary, walking, running and so on) (TActivityRecognition). |
| 51 |
|
| 52 |
Usage: |
| 53 |
|
| 54 |
@orderedList( |
| 55 |
@item(Include the necessary integration code in your Android / iOS project. |
| 56 |
|
| 57 |
For iOS, add the "activity_regonition" service inside CastleEngineManifest.xml. |
| 58 |
See https://castle-engine.io/ios-Services |
| 59 |
and https://github.com/castle-engine/castle-engine/blob/master/tools/build-tool/data/ios/services/activity_recognition/README.md . |
| 60 |
|
| 61 |
Build your project with the Castle Game Engine build tool: |
| 62 |
https://castle-engine.io/build_tool .) |
| 63 |
|
| 64 |
@item(Create an instance of this class. Only a single instance of this class is allowed.) |
| 65 |
|
| 66 |
@item(Call @link(TActivityRecognition.Start).) |
| 67 |
|
| 68 |
@item(Read @link(TActivityRecognition.Activity), @link(TActivityRecognition.PossibleActivities) |
| 69 |
at any point. |
| 70 |
You can assign a callback to @link(TActivityRecognition.OnChange) |
| 71 |
to be notified about changes.) |
| 72 |
) |
| 73 |
} |
| 74 |
TActivityRecognition = class(TComponent) |
| 75 |
private |
| 76 |
FStarted: Boolean; |
| 77 |
FActivityValid: Boolean; |
| 78 |
FActivity: TUserActivity; |
| 79 |
FPossibleActivities: TUserActivities; |
| 80 |
FActivityTime: TDateTime; |
| 81 |
FActivityConfidence: TUserActivityConfidence; |
| 82 |
FOnChange: TNotifyEvent; |
| 83 |
function MessageReceived(const Received: TCastleStringList; |
| 84 |
const ReceivedStream: TMemoryStream): boolean; |
| 85 |
public |
| 86 |
constructor Create(AOwner: TComponent); override; |
| 87 |
destructor Destroy; override; |
| 88 |
|
| 89 |
{ Start recognizing activity. } |
| 90 |
procedure Start; |
| 91 |
|
| 92 |
{ Stop recognizing activity. } |
| 93 |
procedure Stop; |
| 94 |
|
| 95 |
{ Was @link(Start) called (not followed by @link(Stop)). } |
| 96 |
property Started: Boolean read FStarted; |
| 97 |
|
| 98 |
{ Called when current activity changed. |
| 99 |
This is called right after setting these properties: |
| 100 |
|
| 101 |
@unorderedList( |
| 102 |
@itemSpacing Compact |
| 103 |
@item @link(ActivityValid) |
| 104 |
@item @link(Activity) |
| 105 |
@item @link(PossibleActivities) |
| 106 |
@item @link(ActivityTime) |
| 107 |
@item @link(ActivityConfidence) |
| 108 |
) } |
| 109 |
property OnChange: TNotifyEvent read FOnChange write FOnChange; |
| 110 |
|
| 111 |
{ Is some activity detected. |
| 112 |
Note that the detected activity may still be uaUnknown. } |
| 113 |
property ActivityValid: Boolean read FActivityValid; |
| 114 |
|
| 115 |
{ Last detected most possible activity. |
| 116 |
Valid only if @link(ActivityValid). } |
| 117 |
property Activity: TUserActivity read FActivity; |
| 118 |
|
| 119 |
{ Last detected possible activities. |
| 120 |
Valid only if @link(ActivityValid). } |
| 121 |
property PossibleActivities: TUserActivities read FPossibleActivities; |
| 122 |
|
| 123 |
{ The time activity was updated last. |
| 124 |
Valid only if @link(ActivityValid). } |
| 125 |
property ActivityTime: TDateTime read FActivityTime; |
| 126 |
|
| 127 |
{ Confidence in @link(Activity) and @link(PossibleActivities) values. |
| 128 |
Valid only if @link(ActivityValid). } |
| 129 |
property ActivityConfidence: TUserActivityConfidence read FActivityConfidence; |
| 130 |
end; |
| 131 |
|
| 132 |
{ Convert between lowercase activity name and TUserActivity type. } |
| 133 |
function ActivityToStr(const Value: TUserActivity): String; |
| 134 |
{ Convert between lowercase activity name and TUserActivity type. |
| 135 |
@raises Exception In case of invalid String. } |
| 136 |
function StrToActivity(const Value: String): TUserActivity; |
| 137 |
{ Convert between lowercase activity confidence name and TUserActivityConfidence type. } |
| 138 |
function ActivityConfidenceToStr(const Value: TUserActivityConfidence): String; |
| 139 |
{ Convert between lowercase activity confidence name and TUserActivityConfidence type. |
| 140 |
@raises Exception In case of invalid String. } |
| 141 |
function StrToActivityConfidence(const Value: String): TUserActivityConfidence; |
| 142 |
{ Convert TUserActivities to a String, using Delimiter to separate items. } |
| 143 |
function PossibleActivitiesToStr(const Value: TUserActivities; const Delimiter: String): String; |
| 144 |
|
| 145 |
implementation |
| 146 |
|
| 147 |
uses SysUtils, |
| 148 |
CastleMessaging; |
| 149 |
|
| 150 |
{ global routines ------------------------------------------------------------ } |
| 151 |
|
| 152 |
function ActivityToStr(const Value: TUserActivity): String; |
| 153 |
const |
| 154 |
Names: array [TUserActivity] of String = ( |
| 155 |
'stationary', |
| 156 |
'walking', |
| 157 |
'running', |
| 158 |
'automotive', |
| 159 |
'cycling', |
| 160 |
'unknown' |
| 161 |
); |
| 162 |
begin |
| 163 |
Result := Names[Value]; |
| 164 |
end; |
| 165 |
|
| 166 |
function StrToActivity(const Value: String): TUserActivity; |
| 167 |
begin |
| 168 |
for Result := Low(TUserActivity) to High(TUserActivity) do |
| 169 |
if ActivityToStr(Result) = Value then |
| 170 |
Exit; |
| 171 |
raise Exception.CreateFmt('Activity name invalid: "%s"', [Value]); |
| 172 |
end; |
| 173 |
|
| 174 |
function ActivityConfidenceToStr(const Value: TUserActivityConfidence): String; |
| 175 |
const |
| 176 |
Names: array [TUserActivityConfidence] of String = ( |
| 177 |
'low', |
| 178 |
'medium', |
| 179 |
'high' |
| 180 |
); |
| 181 |
begin |
| 182 |
Result := Names[Value]; |
| 183 |
end; |
| 184 |
|
| 185 |
function StrToActivityConfidence(const Value: String): TUserActivityConfidence; |
| 186 |
begin |
| 187 |
for Result := Low(TUserActivityConfidence) to High(TUserActivityConfidence) do |
| 188 |
if ActivityConfidenceToStr(Result) = Value then |
| 189 |
Exit; |
| 190 |
raise Exception.CreateFmt('Activity confidence name invalid: "%s"', [Value]); |
| 191 |
end; |
| 192 |
|
| 193 |
{ Internal, uses delimiter same as iOS/Android service. } |
| 194 |
function StrToPossibleActivities(const Value: String): TUserActivities; |
| 195 |
const |
| 196 |
{ This is a nice separator, as it has really low chance of occuring in non-binary data. |
| 197 |
- It's also not 0, so it will not be confused with "end of string" (Pascal is invulnerable |
| 198 |
to this, and can have #0 in the middle of AnsiString freely |
| 199 |
but I'm not so sure about Java or Objective-C NSString). |
| 200 |
- It's also within ASCII range, so it will not occur within any UTF-8 multibyte sequence |
| 201 |
(UTF-8 treats specially only stuff > 128, and you can search for ASCII substrings disregaring |
| 202 |
the UTF-8 multibyte stuff, as far as I know). } |
| 203 |
PossibleActivitiesDelimiter = #2; |
| 204 |
var |
| 205 |
Values: TCastleStringList; |
| 206 |
S: String; |
| 207 |
begin |
| 208 |
if Value = '' then // otherwise SplitString('', ...) would return one value, equal '' |
| 209 |
Exit([]); |
| 210 |
|
| 211 |
Values := SplitString(Value, PossibleActivitiesDelimiter); |
| 212 |
Result := []; |
| 213 |
for S in Values do |
| 214 |
Include(Result, StrToActivity(S)); |
| 215 |
end; |
| 216 |
|
| 217 |
{ Public, uses configurable delimiter. } |
| 218 |
function PossibleActivitiesToStr(const Value: TUserActivities; const Delimiter: String): String; |
| 219 |
var |
| 220 |
A: TUserActivity; |
| 221 |
begin |
| 222 |
Result := ''; |
| 223 |
for A in Value do |
| 224 |
Result := SAppendPart(Result, Delimiter, ActivityToStr(A)); |
| 225 |
end; |
| 226 |
|
| 227 |
{ TActivityRecognition ------------------------------------------------------- } |
| 228 |
|
| 229 |
constructor TActivityRecognition.Create(AOwner: TComponent); |
| 230 |
begin |
| 231 |
inherited; |
| 232 |
Messaging.OnReceive.Add({$ifdef FPC}@{$endif} MessageReceived); |
| 233 |
end; |
| 234 |
|
| 235 |
destructor TActivityRecognition.Destroy; |
| 236 |
begin |
| 237 |
if Messaging <> nil then |
| 238 |
Messaging.OnReceive.Remove({$ifdef FPC}@{$endif} MessageReceived); |
| 239 |
inherited; |
| 240 |
end; |
| 241 |
|
| 242 |
function TActivityRecognition.MessageReceived(const Received: TCastleStringList; |
| 243 |
const ReceivedStream: TMemoryStream): boolean; |
| 244 |
|
| 245 |
{ Try to get most useful activity from the set of possible activities. |
| 246 |
If Activities contain only one item, return it, |
| 247 |
If Activities are empty, return uaUnknown. |
| 248 |
If Activities contain uaUnknown and another item, return another item. |
| 249 |
Otherwise return uaUnknown. } |
| 250 |
function MostUsefulActivity(Activities: TUserActivities): TUserActivity; |
| 251 |
var |
| 252 |
A, LastA: TUserActivity; |
| 253 |
C: Cardinal; |
| 254 |
begin |
| 255 |
Exclude(Activities, uaUnknown); |
| 256 |
|
| 257 |
LastA := uaStationary; // just to silence Delphi warning |
| 258 |
|
| 259 |
C := 0; |
| 260 |
for A in Activities do |
| 261 |
begin |
| 262 |
LastA := A; |
| 263 |
Inc(C); |
| 264 |
end; |
| 265 |
|
| 266 |
if C = 1 then |
| 267 |
Result := LastA |
| 268 |
else |
| 269 |
Result := uaUnknown; |
| 270 |
end; |
| 271 |
|
| 272 |
begin |
| 273 |
Result := false; |
| 274 |
|
| 275 |
{ Just a quick auto-test of MostUsefulActivity } |
| 276 |
(* |
| 277 |
Assert(MostUsefulActivity([uaUnknown]) = uaUnknown); |
| 278 |
Assert(MostUsefulActivity([]) = uaUnknown); |
| 279 |
Assert(MostUsefulActivity([uaUnknown, uaRunning]) = uaRunning); |
| 280 |
Assert(MostUsefulActivity([uaUnknown, uaWalking]) = uaWalking); |
| 281 |
Assert(MostUsefulActivity([uaRunning]) = uaRunning); |
| 282 |
Assert(MostUsefulActivity([uaWalking]) = uaWalking); |
| 283 |
Assert(MostUsefulActivity([uaUnknown, uaWalking, uaRunning]) = uaUnknown); |
| 284 |
*) |
| 285 |
|
| 286 |
if (Received.Count = 3) and |
| 287 |
(Received[0] = 'activity-recognition-change') then |
| 288 |
begin |
| 289 |
FActivityValid := true; |
| 290 |
FPossibleActivities := StrToPossibleActivities(Received[1]); |
| 291 |
FActivity := MostUsefulActivity(FPossibleActivities); |
| 292 |
FActivityTime := CastleNow; |
| 293 |
FActivityConfidence := StrToActivityConfidence(Received[2]); |
| 294 |
if Assigned(OnChange) then |
| 295 |
OnChange(Self); |
| 296 |
Result := true; |
| 297 |
end; |
| 298 |
end; |
| 299 |
|
| 300 |
procedure TActivityRecognition.Start; |
| 301 |
begin |
| 302 |
Messaging.Send(['activity-recognition', 'start']); |
| 303 |
FStarted := true; |
| 304 |
end; |
| 305 |
|
| 306 |
procedure TActivityRecognition.Stop; |
| 307 |
begin |
| 308 |
Messaging.Send(['activity-recognition', 'stop']); |
| 309 |
FStarted := false; |
| 310 |
end; |
| 311 |
|
| 312 |
end. |