Commit 478754cd authored by Michael Nelson's avatar Michael Nelson

Correct hold down behavior

parent b4256e0b
......@@ -39,6 +39,7 @@
// Config
#define BLINK_RATE 250
#define RUNNING_LIGHT_BLINK_RATE 420
#define HOLD_DOWN_TIME 350
// State
......@@ -46,15 +47,16 @@ int fg_mode = MODE_OFF;
int bg_mode = MODE_OFF;
int blinker_light_state;
int last_fg_mode = MODE_OFF;
bool hold_waiting = false;
// Just-released variables
bool input_single_left_hit = false;
bool input_single_right_hit = false;
bool input_both_single_hit = false;
bool input_single_both_hit = false;
bool input_hold_left_hit = false;
bool input_hold_right_hit = false;
bool input_both_hold_hit = false;
bool input_hold_both_hit = false;
// Time accumulator variables
int cycle_time = 0;
......@@ -111,6 +113,19 @@ void blink_frame(bool left, bool right, int tone_divisor = 2)
cycle_time++;
}
void running_light_frame(bool with_blink = false)
{
if (cycle_time > RUNNING_LIGHT_BLINK_RATE)
cycle_time = 0;
digitalWrite(PIN_OUT_BLINKER_L, !(cycle_time % 2 || cycle_time < 30) ? HIGH : LOW);
digitalWrite(PIN_OUT_BLINKER_R, !(cycle_time % 2 || cycle_time < 30) ? HIGH : LOW);
digitalWrite(PIN_OUT_LEFT_BUTTON_LED, !(cycle_time % 2 || cycle_time < 30) ? HIGH : LOW);
cycle_time++;
}
void blink_speaker_frame(int divisor)
{
if (!blinker_light_state && cycle_time < 40) {
......@@ -125,7 +140,11 @@ void read_dual_buttons()
{
input_single_left_hit = false;
input_single_right_hit = false;
input_both_single_hit = false;
input_single_both_hit = false;
input_hold_left_hit = false;
input_hold_right_hit = false;
input_hold_both_hit = false;
if (digitalRead(PIN_IN_LEFT_BUTTON) == LOW && digitalRead(PIN_IN_RIGHT_BUTTON) == LOW)
both_hold_time++;
......@@ -135,24 +154,37 @@ void read_dual_buttons()
right_hold_time++;
else
{
if (left_hold_time >= HOLD_DOWN_TIME)
input_hold_left_hit = true;
else if (left_hold_time)
if (left_hold_time && !hold_waiting)
input_single_left_hit = true;
if (right_hold_time >= HOLD_DOWN_TIME)
input_hold_right_hit = true;
else if (right_hold_time)
if (right_hold_time && !hold_waiting)
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;
if (both_hold_time && !hold_waiting)
input_single_both_hit = true;
both_hold_time = 0;
left_hold_time = 0;
right_hold_time = 0;
hold_waiting = false;
}
if (left_hold_time == HOLD_DOWN_TIME)
{
input_hold_left_hit = true;
hold_waiting = true;
}
else if (right_hold_time == HOLD_DOWN_TIME)
{
input_hold_right_hit = true;
hold_waiting = true;
}
else if (both_hold_time == HOLD_DOWN_TIME)
{
input_hold_both_hit = true;
hold_waiting = true;
}
}
......@@ -160,18 +192,18 @@ void set_up_mode()
{
read_dual_buttons();
if (both_hold_time == HOLD_DOWN_TIME)
if (input_hold_both_hit)
{
bg_mode = bg_mode == BG_MODE_HAZARDS ? MODE_OFF : BG_MODE_HAZARDS;
fg_mode = MODE_OFF;
}
else if (input_both_single_hit)
else if (input_single_both_hit)
{
bg_mode = MODE_OFF;
fg_mode = MODE_OFF;
}
// Cycle between running lights
else if (left_hold_time == HOLD_DOWN_TIME)
else if (input_hold_left_hit)
{
bg_mode++;
if (bg_mode > BG_MODE_RUNNING_W_BLINK)
......@@ -183,7 +215,7 @@ void set_up_mode()
{
fg_mode = fg_mode == FG_MODE_BLINK_L ? MODE_OFF : FG_MODE_BLINK_L;
}
else if (right_hold_time == HOLD_DOWN_TIME)
else if (input_hold_right_hit)
{
bg_mode = BG_MODE_AUXILARY_LIGHTS;
fg_mode = MODE_OFF;
......@@ -197,6 +229,7 @@ void set_up_mode()
void reset_all()
{
cycle_time = 0;
blinker_light_state = 0;
#if DEBUG_WITH_BUILTIN_LED
digitalWrite(LED_BUILTIN, LOW);
......@@ -214,9 +247,6 @@ void loop()
delay(1);
set_up_mode();
/* Serial.println(cycle_time); */
/* Serial.println(fg_mode); */
// Reset all lights going directly from one direction to the next.
if (fg_mode != last_fg_mode && fg_mode != MODE_OFF)
reset_all();
......@@ -227,6 +257,8 @@ void loop()
blink_frame(false, true);
else if (bg_mode == BG_MODE_HAZARDS)
blink_frame(true, true, 3);
/* else if (bg_mode == BG_MODE_RUNNING_LIGHTS) */
/* running_light_frame(); */
else
reset_all();
......
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