Last active
December 21, 2015 22:29
-
-
Save olenhad/6375958 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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