Commit b4256e0b authored by Michael Nelson's avatar Michael Nelson

Better pattern for toggling hazards.

Still triggering a L/R signal tho...
parent 7bf79cc3
...@@ -48,10 +48,13 @@ int blinker_light_state; ...@@ -48,10 +48,13 @@ int blinker_light_state;
int last_fg_mode = MODE_OFF; int last_fg_mode = MODE_OFF;
// Just-released variables // Just-released variables
bool left_input_ended = false; bool input_single_left_hit = false;
bool right_input_ended = false; bool input_single_right_hit = false;
bool both_input_ended = false; bool input_both_single_hit = false;
bool blocked = false;
bool input_hold_left_hit = false;
bool input_hold_right_hit = false;
bool input_both_hold_hit = false;
// Time accumulator variables // Time accumulator variables
int cycle_time = 0; int cycle_time = 0;
...@@ -116,12 +119,13 @@ void blink_speaker_frame(int divisor) ...@@ -116,12 +119,13 @@ void blink_speaker_frame(int divisor)
} }
// Each frame, this increments L/R/B hold time variables while the user is holding a button (or both) down. // Each frame, this increments L/R/B hold time variables while the user is holding a button (or both) down.
// It also checks for the release. In that case it sets L/R/B input ended to true for just that frame. // It also checks for the release. In that case it flips on either input_single_* or input_hold_* variables
// to be consumed later in the frame.
void read_dual_buttons() void read_dual_buttons()
{ {
left_input_ended = false; input_single_left_hit = false;
right_input_ended = false; input_single_right_hit = false;
both_input_ended = false; input_both_single_hit = false;
if (digitalRead(PIN_IN_LEFT_BUTTON) == LOW && digitalRead(PIN_IN_RIGHT_BUTTON) == LOW) if (digitalRead(PIN_IN_LEFT_BUTTON) == LOW && digitalRead(PIN_IN_RIGHT_BUTTON) == LOW)
both_hold_time++; both_hold_time++;
...@@ -131,9 +135,20 @@ void read_dual_buttons() ...@@ -131,9 +135,20 @@ void read_dual_buttons()
right_hold_time++; right_hold_time++;
else else
{ {
if (left_hold_time) left_input_ended = true; if (left_hold_time >= HOLD_DOWN_TIME)
if (right_hold_time) right_input_ended = true; input_hold_left_hit = true;
if (both_hold_time) both_input_ended = true; else if (left_hold_time)
input_single_left_hit = true;
if (right_hold_time >= HOLD_DOWN_TIME)
input_hold_right_hit = true;
else if (right_hold_time)
input_single_right_hit = true;
if (both_hold_time >= HOLD_DOWN_TIME)
input_both_hold_hit = true;
else if (both_hold_time)
input_both_single_hit = true;
both_hold_time = 0; both_hold_time = 0;
left_hold_time = 0; left_hold_time = 0;
...@@ -149,13 +164,11 @@ void set_up_mode() ...@@ -149,13 +164,11 @@ void set_up_mode()
{ {
bg_mode = bg_mode == BG_MODE_HAZARDS ? MODE_OFF : BG_MODE_HAZARDS; bg_mode = bg_mode == BG_MODE_HAZARDS ? MODE_OFF : BG_MODE_HAZARDS;
fg_mode = MODE_OFF; fg_mode = MODE_OFF;
blocked = true;
} }
else if (both_input_ended && both_hold_time < HOLD_DOWN_TIME && !blocked) else if (input_both_single_hit)
{ {
bg_mode = MODE_OFF; bg_mode = MODE_OFF;
fg_mode = MODE_OFF; fg_mode = MODE_OFF;
blocked = false;
} }
// Cycle between running lights // Cycle between running lights
else if (left_hold_time == HOLD_DOWN_TIME) else if (left_hold_time == HOLD_DOWN_TIME)
...@@ -165,23 +178,19 @@ void set_up_mode() ...@@ -165,23 +178,19 @@ void set_up_mode()
bg_mode = BG_MODE_RUNNING_LIGHTS; bg_mode = BG_MODE_RUNNING_LIGHTS;
fg_mode = MODE_OFF; fg_mode = MODE_OFF;
blocked = true;
} }
else if (left_input_ended && left_hold_time < HOLD_DOWN_TIME) else if (input_single_left_hit)
{ {
fg_mode = fg_mode == FG_MODE_BLINK_L ? MODE_OFF : FG_MODE_BLINK_L; fg_mode = fg_mode == FG_MODE_BLINK_L ? MODE_OFF : FG_MODE_BLINK_L;
blocked = false;
} }
else if (right_hold_time == HOLD_DOWN_TIME) else if (right_hold_time == HOLD_DOWN_TIME)
{ {
bg_mode = BG_MODE_AUXILARY_LIGHTS; bg_mode = BG_MODE_AUXILARY_LIGHTS;
fg_mode = MODE_OFF; fg_mode = MODE_OFF;
blocked = true;
} }
else if (right_input_ended && right_hold_time < HOLD_DOWN_TIME) else if (input_single_right_hit)
{ {
fg_mode = fg_mode == FG_MODE_BLINK_R ? MODE_OFF : FG_MODE_BLINK_R; fg_mode = fg_mode == FG_MODE_BLINK_R ? MODE_OFF : FG_MODE_BLINK_R;
blocked = false;
} }
} }
......
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