diff options
author | Lars-Dominik Braun <lars@6xq.net> | 2015-01-27 10:05:10 +0100 |
---|---|---|
committer | Lars-Dominik Braun <lars@6xq.net> | 2015-01-29 11:10:28 +0100 |
commit | 9528470d8389840618275814c30e4f64a9e3efa7 (patch) | |
tree | 27d9cf2fe745a517a45053de9c53a7d45a0307c4 | |
parent | 2c40f4a0e5c9e9bda8d3b59553abf01abe97c5cd (diff) | |
download | hourglass-9528470d8389840618275814c30e4f64a9e3efa7.tar.gz hourglass-9528470d8389840618275814c30e4f64a9e3efa7.tar.bz2 hourglass-9528470d8389840618275814c30e4f64a9e3efa7.zip |
accel: Fix shake detection, again
Only two peaks are required. Use int16 to avoid overflow. Check sign before
registering shake in other direction.
-rw-r--r-- | accel.c | 21 |
1 files changed, 13 insertions, 8 deletions
@@ -32,14 +32,15 @@ #define ACCEL_SHAKE_TIMEOUT (10) /* z value */ -static volatile int8_t zval = 0, zvalnormal = 0; +static int8_t zval = 0; +static int16_t zvalnormal = 0; /* number of times shaken (i.e. peak level measured) */ static uint8_t shakeCount = 0; /* if max in one direction direction has been detected give it some time to * wait for max in the other direction */ static uint8_t shakeTimeout = 0; -static int8_t prevShakeVal = 0; +static int16_t prevShakeVal = 0; static uint8_t peakCount = 0; /* horizon position */ @@ -99,23 +100,27 @@ void accelStart () { static void accelProcessShake () { if (shakeTimeout > 0) { --shakeTimeout; - if (abs ((int16_t) prevShakeVal - (int16_t) zvalnormal) > ACCEL_SHAKE_REGISTER) { + if (sign (prevShakeVal) != sign (zvalnormal) && + abs (prevShakeVal - zvalnormal) >= ACCEL_SHAKE_REGISTER) { ++peakCount; shakeTimeout = ACCEL_SHAKE_TIMEOUT; prevShakeVal = zvalnormal; } else if (sign (prevShakeVal) == sign (zvalnormal) && - abs (zvalnormal) > abs (prevShakeVal)) { - /* actually we did not measure the peak yet */ + abs (zvalnormal) >= abs (prevShakeVal)) { + /* actually we did not measure the peak yet/are still experiencing it */ prevShakeVal = zvalnormal; + shakeTimeout = ACCEL_SHAKE_TIMEOUT; } if (shakeTimeout == 0) { - /* just timed out, can register gesture now. XXX: why 3? */ - shakeCount += peakCount/3; + /* just timed out, can register gesture now */ + shakeCount += peakCount/2; + prevShakeVal = 0; + peakCount = 0; } } /* start shake detection */ - if (shakeTimeout == 0 && abs (zvalnormal) > ACCEL_SHAKE_START) { + if (shakeTimeout == 0 && abs (zvalnormal) >= ACCEL_SHAKE_START) { shakeTimeout = ACCEL_SHAKE_TIMEOUT; peakCount = 1; prevShakeVal = zvalnormal; |