summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeith Packard <keithp@keithp.com>2018-05-28 13:07:06 -0700
committerKeith Packard <keithp@keithp.com>2018-05-28 13:07:06 -0700
commita4dbc940cc6c8ff5565e8af21f2dcb4ae090380c (patch)
tree9abf96b0ea4c1f1b5fea458ea97e99ab1c42b359
parent7e2a2849f58e98adc1114bb8f3a6319408d93691 (diff)
altos/drivers: Make quadrature debounce longer by default. Fix state tracking
This increases the default debounce time for quadrature encoders to 30ms, which cleans up the mechanical encoders on TeleLCO v0.2. Also change state tracking to explicitly check for expected state values to avoid mis-triggering. Signed-off-by: Keith Packard <keithp@keithp.com>
-rw-r--r--src/drivers/ao_quadrature.c24
1 files changed, 14 insertions, 10 deletions
diff --git a/src/drivers/ao_quadrature.c b/src/drivers/ao_quadrature.c
index deecfb79..d9cdfe7c 100644
--- a/src/drivers/ao_quadrature.c
+++ b/src/drivers/ao_quadrature.c
@@ -39,20 +39,24 @@ static struct ao_debounce ao_debounce_state[AO_QUADRATURE_COUNT][2];
#define pinb(q) AO_QUADRATURE_ ## q ## _B ## _PIN
#define isr(q) ao_quadrature_isr_ ## q
-#define DEBOUNCE 10
+#ifndef AO_QUADRATURE_DEBOUNCE
+#define AO_QUADRATURE_DEBOUNCE 30
+#endif
static uint8_t
ao_debounce(uint8_t cur, struct ao_debounce *debounce)
{
- if (cur == debounce->state)
- debounce->count = 0;
- else {
- if (++debounce->count == DEBOUNCE) {
- debounce->state = cur;
- debounce->count = 0;
- }
+#if AO_QUADRATURE_DEBOUNCE > 0
+ if (debounce->count > 0) {
+ debounce->count--;
+ } else if (cur != debounce->state) {
+ debounce->state = cur;
+ debounce->count = AO_QUADRATURE_DEBOUNCE;
}
return debounce->state;
+#else
+ return cur;
+#endif
}
static uint16_t
@@ -84,9 +88,9 @@ _ao_quadrature_set(uint8_t q, uint8_t new) {
uint8_t old = ao_quadrature_state[q];
if (old != new && new == 0) {
- if (old & 2)
+ if (old == 2)
_ao_quadrature_queue(q, 1);
- else if (old & 1)
+ else if (old == 1)
_ao_quadrature_queue(q, -1);
}
ao_quadrature_state[q] = new;