Skip to content

Commit

Permalink
Account for timer rollover in analog button logic (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
kinetic-flow authored Jan 3, 2021
1 parent df2fceb commit ba81dd7
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions arcin/analog_button.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,35 @@ class analog_button {
// State: Center of deadzone
uint32_t center;
bool center_valid;
// times to: reset to zero, reset center to counter
uint32_t t_timeout;

// time to: reset center to counter
uint32_t sustain_timer;
bool sustain_timer_armed;

int8_t state; // -1, 0, 1
int8_t last_delta;

public:
analog_button(uint32_t deadzone, uint32_t sustain_ms, bool clear)
: deadzone(deadzone), sustain_ms(sustain_ms), clear(clear)
{
center = 0;
center_valid = false;
t_timeout = 0;
sustain_timer = 0;
sustain_timer_armed = false;
state = 0;
}

int8_t poll(uint32_t current_value) {
uint32_t now = Time::time();
if (!center_valid) {
center_valid = true;
center = current_value;
}

uint8_t observed = current_value;
int8_t delta = observed - center;
last_delta = delta;

// is the current value sufficiently far away from the center?
uint8_t direction = 0;
if (delta >= (int32_t)deadzone) {
direction = 1;
Expand All @@ -51,12 +55,20 @@ class analog_button {
}

if (direction != 0) {
// turntable is moving -
// keep updating the new center, and keep extending the sustain timer
center = observed;
t_timeout = Time::time() + sustain_ms;
} else if (t_timeout != 0 && Time::time() >= t_timeout) {
state = 0;
center = observed;
t_timeout = 0;
sustain_timer = now + sustain_ms;
sustain_timer_armed = true;
} else if (sustain_timer_armed) {
// check if sustain timer expired
int32_t diff = now - sustain_timer;
if (diff > 0) {
// sustain timer expired, time to reset to neutral
state = 0;
center = observed;
sustain_timer_armed = false;
}
}

if (direction == -state && clear) {
Expand Down

0 comments on commit ba81dd7

Please sign in to comment.