microbit-dal
Dependencies: BLE_API mbed-dev-bin nRF51822
Fork of microbit-dal by
Diff: source/drivers/MicroBitAccelerometer.cpp
- Revision:
- 65:f7ebabf23e15
- Parent:
- 56:1311cac15dda
--- a/source/drivers/MicroBitAccelerometer.cpp Wed Jul 13 12:18:43 2016 +0100 +++ b/source/drivers/MicroBitAccelerometer.cpp Wed Jul 13 12:18:45 2016 +0100 @@ -348,23 +348,43 @@ shake.z = !shake.z; } - if (shakeDetected && shake.count < MICROBIT_ACCELEROMETER_SHAKE_COUNT_THRESHOLD && ++shake.count == MICROBIT_ACCELEROMETER_SHAKE_COUNT_THRESHOLD) - shake.shaken = 1; - - if (++shake.timer >= MICROBIT_ACCELEROMETER_SHAKE_DAMPING) + // If we detected a zero crossing in this sample period, count this. + if (shakeDetected && shake.count < MICROBIT_ACCELEROMETER_SHAKE_COUNT_THRESHOLD) { - shake.timer = 0; - if (shake.count > 0) + shake.count++; + + if (shake.count == 1) + shake.timer = 0; + + if (shake.count == MICROBIT_ACCELEROMETER_SHAKE_COUNT_THRESHOLD) { - if(--shake.count == 0) - shake.shaken = 0; + shake.shaken = 1; + shake.timer = 0; + return MICROBIT_ACCELEROMETER_EVT_SHAKE; } } - // Shake events take the highest priority, as under high levels of change, other events - // are likely to be transient. - if (shake.shaken) - return MICROBIT_ACCELEROMETER_EVT_SHAKE; + // measure how long we have been detecting a SHAKE event. + if (shake.count > 0) + { + shake.timer++; + + // If we've issued a SHAKE event already, and sufficient time has assed, allow another SHAKE event to be issued. + if (shake.shaken && shake.timer >= MICROBIT_ACCELEROMETER_SHAKE_RTX) + { + shake.shaken = 0; + shake.timer = 0; + shake.count = 0; + } + + // Decay our count of zero crossings over time. We don't want them to accumulate if the user performs slow moving motions. + else if (!shake.shaken && shake.timer >= MICROBIT_ACCELEROMETER_SHAKE_DAMPING) + { + shake.timer = 0; + if (shake.count > 0) + shake.count--; + } + } if (instantaneousAccelerationSquared() < MICROBIT_ACCELEROMETER_FREEFALL_THRESHOLD) return MICROBIT_ACCELEROMETER_EVT_FREEFALL; @@ -433,6 +453,12 @@ // Determine what it looks like we're doing based on the latest sample... uint16_t g = instantaneousPosture(); + if (g == MICROBIT_ACCELEROMETER_EVT_SHAKE) + { + MicroBitEvent e(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_SHAKE); + return; + } + // Perform some low pass filtering to reduce jitter from any detected effects if (g == currentGesture) {