aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars-Dominik Braun <lars@6xq.net>2015-01-27 10:05:10 +0100
committerLars-Dominik Braun <lars@6xq.net>2015-01-29 11:10:28 +0100
commit9528470d8389840618275814c30e4f64a9e3efa7 (patch)
tree27d9cf2fe745a517a45053de9c53a7d45a0307c4
parent2c40f4a0e5c9e9bda8d3b59553abf01abe97c5cd (diff)
downloadhourglass-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.c21
1 files changed, 13 insertions, 8 deletions
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;