From 5ac97925a2c1c4c4b9913616a85c1a121dc95c74 Mon Sep 17 00:00:00 2001 From: Joey Wunderlich Date: Tue, 9 Apr 2019 14:19:33 -0700 Subject: [PATCH] Fix shake button (#1983) * initial fix for shake button * minor cleanup * always run if forced * refactor a bit * only ever enqueue if gesture is different from previous --- sim/state/accelerometer.ts | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/sim/state/accelerometer.ts b/sim/state/accelerometer.ts index 0b064c58..8baba149 100644 --- a/sim/state/accelerometer.ts +++ b/sim/state/accelerometer.ts @@ -258,32 +258,32 @@ namespace pxsim { updateGesture() { // Determine what it looks like we're doing based on the latest sample... let g = this.instantaneousPosture(); - this.setGesture(g); - } - private setGesture(g: number) { // Perform some low pass filtering to reduce jitter from any detected effects - if (g == this.currentGesture) { - if (this.sigma < DAL.MICROBIT_ACCELEROMETER_GESTURE_DAMPING) - this.sigma++; - } - else { + if (g != this.currentGesture) { this.currentGesture = g; this.sigma = 0; + } else if (this.sigma < DAL.MICROBIT_ACCELEROMETER_GESTURE_DAMPING) { + ++this.sigma; } - // If we've reached threshold, update our record and raise the relevant event... - if (this.currentGesture != this.lastGesture && this.sigma >= DAL.MICROBIT_ACCELEROMETER_GESTURE_DAMPING) { + if (this.sigma >= DAL.MICROBIT_ACCELEROMETER_GESTURE_DAMPING) { + this.enqueueCurrentGesture(); + } + } + + forceGesture(gesture: number) { + this.currentGesture = gesture; + this.enqueueCurrentGesture(); + } + + private enqueueCurrentGesture() { + if (this.currentGesture != this.lastGesture) { this.lastGesture = this.currentGesture; board().bus.queue(DAL.MICROBIT_ID_GESTURE, this.lastGesture); } } - forceGesture(g: number) { - this.sigma = DAL.MICROBIT_ACCELEROMETER_GESTURE_DAMPING + 1; - this.setGesture(g); - } - /** * Reads the X axis value of the latest update from the accelerometer. * @param system The coordinate system to use. By default, a simple cartesian system is provided.