...
 
Commits (2)
......@@ -39,6 +39,7 @@
// Config
#define BLINK_RATE 250
#define RUNNING_LIGHT_BLINK_RATE 920
#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;
......@@ -89,7 +91,8 @@ void blink_frame(bool left, bool right, int tone_divisor = 2)
blinker_light_state = !blinker_light_state;
}
blink_speaker_frame(tone_divisor);
if (cycle_time == 1 && !blinker_light_state)
blink_speaker_frame(tone_divisor);
// Actual light
if (left)
......@@ -111,11 +114,25 @@ 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 % 9) || (with_blink && cycle_time < 70) ? HIGH : LOW);
digitalWrite(PIN_OUT_BLINKER_R, !(cycle_time % 9) || (with_blink && cycle_time < 70) ? HIGH : LOW);
digitalWrite(PIN_OUT_LEFT_BUTTON_LED, !(cycle_time % 9) || (with_blink && cycle_time < 70) ? HIGH : LOW);
cycle_time++;
}
void blink_speaker_frame(int divisor)
{
if (!blinker_light_state && cycle_time < 40) {
digitalWrite(PIN_OUT_SPEAKER, !(cycle_time % divisor) ? HIGH : LOW);
}
/* if (!blinker_light_state && cycle_time < 40) { */
/* digitalWrite(PIN_OUT_SPEAKER, !(cycle_time % divisor) ? HIGH : LOW); */
/* } */
tone(PIN_OUT_SPEAKER, 2000, 20);
}
// Each frame, this increments L/R/B hold time variables while the user is holding a button (or both) down.
......@@ -125,7 +142,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 +156,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,22 +194,25 @@ 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)
if (!bg_mode)
bg_mode = BG_MODE_RUNNING_LIGHTS;
else if (bg_mode >= BG_MODE_RUNNING_W_BLINK)
bg_mode = MODE_OFF;
else
bg_mode++;
fg_mode = MODE_OFF;
}
......@@ -183,7 +220,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 +234,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 +252,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 +262,10 @@ 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 if (bg_mode == BG_MODE_RUNNING_W_BLINK)
running_light_frame(true);
else
reset_all();
......