From 9528470d8389840618275814c30e4f64a9e3efa7 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Tue, 27 Jan 2015 10:05:10 +0100 Subject: accel: Fix shake detection, again Only two peaks are required. Use int16 to avoid overflow. Check sign before registering shake in other direction. --- accel.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'accel.c') diff --git a/accel.c b/accel.c index 152346a..f3f0ce2 100644 --- a/accel.c +++ b/accel.c @@ -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; -- cgit v1.2.3