Skip to content

Instantly share code, notes, and snippets.

@olenhad
Last active December 21, 2015 22:29
Show Gist options
  • Save olenhad/6375958 to your computer and use it in GitHub Desktop.
Save olenhad/6375958 to your computer and use it in GitHub Desktop.
// The following is executed every 0.5s
CMAcceleration accel = data.acceleration;
double mag = sqrt(pow(accel.x, 2)+pow(accel.y,2)+pow(accel.z,2));
// The thresholds here are derived from manually experimenting. magnitude is in Gs. 1.0G is expected due to gravity
if (mag < 0.9275 || mag > 1.0903) {
[self incrementActiveCount];
} else {
[self incrementStillCount];
}
// The counts are necessary to reduce false positives. PHONE_STILL_COUNT is set to 120. PHONE_ACTIVE_COUNT is set to 5
- (void)incrementStillCount
{
_stillCount++;
if (_stillCount >= PHONE_STILL_COUNT) {
// changes phone state to still. aka turns location services on
[_stateDelegate phoneIsStill];
_stillCount = 0;
_activeCount = 0;
}
}
- (void)incrementActiveCount
{
_activeCount++;
if (_activeCount >= PHONE_ACTIVE_COUNT) {
//changes phone state to "normal". aka turns location services off
[_stateDelegate phoneMoved];
_activeCount = 0;
_stillCount = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment