Commit 01f14ca9 authored by George Wort's avatar George Wort

Take into account time passed when debouncing button.

parent 3cfba335
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
extern "C" { extern "C" {
#include "lib/ticker.h"
#include "nrf_gpio.h" #include "nrf_gpio.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "microbit/modmicrobit.h" #include "microbit/modmicrobit.h"
...@@ -115,17 +116,19 @@ enum PinTransition ...@@ -115,17 +116,19 @@ enum PinTransition
}; };
static PinTransition update(const microbit_pin_obj_t *pin) { static PinTransition update(const microbit_pin_obj_t *pin) {
int32_t sigma_delta = 1 + (MILLISECONDS_PER_MACRO_TICK / 6);
int32_t sigma = sigmas[pin->number&7]; int32_t sigma = sigmas[pin->number&7];
PinTransition result; PinTransition result;
if (nrf_gpio_pin_read(pin->name)) if (nrf_gpio_pin_read(pin->name))
sigma++; sigma += sigma_delta;
else else
sigma--; sigma -= sigma_delta;
if (sigma < 3) { if (sigma < 3) {
if (sigma < 0) { if (sigma < 0) {
sigma = 0; sigma = 0;
result = LOW_LOW; }
} else if (debounced_high[pin->number&7]) { if (debounced_high[pin->number&7]) {
result = HIGH_LOW; result = HIGH_LOW;
debounced_high[pin->number&7] = false; debounced_high[pin->number&7] = false;
} else { } else {
...@@ -134,8 +137,8 @@ static PinTransition update(const microbit_pin_obj_t *pin) { ...@@ -134,8 +137,8 @@ static PinTransition update(const microbit_pin_obj_t *pin) {
} else if (sigma > 7) { } else if (sigma > 7) {
if (sigma > 12) { if (sigma > 12) {
sigma = 12; sigma = 12;
result = HIGH_HIGH; }
} else if (debounced_high[pin->number&7]) { if (debounced_high[pin->number&7]) {
result = HIGH_HIGH; result = HIGH_HIGH;
} else { } else {
result = LOW_HIGH; result = LOW_HIGH;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment