Commit 7bf79cc3 authored by Michael Nelson's avatar Michael Nelson

Work out holding down buttons to switch bg mode WIP

parent 0169ac62
......@@ -48,9 +48,10 @@ int blinker_light_state;
int last_fg_mode = MODE_OFF;
// Just-released variables
bool left_just_released = false;
bool right_just_released = false;
bool both_just_released = false;
bool left_input_ended = false;
bool right_input_ended = false;
bool both_input_ended = false;
bool blocked = false;
// Time accumulator variables
int cycle_time = 0;
......@@ -61,7 +62,7 @@ int both_hold_time = 0;
void setup()
{
// Initialize a serial connection for reporting values to the host
/* Serial.begin(9600); */
Serial.begin(9600);
pinMode(PIN_IN_LEFT_BUTTON, INPUT_PULLUP);
pinMode(PIN_IN_RIGHT_BUTTON, INPUT_PULLUP);
......@@ -77,7 +78,7 @@ void setup()
/* Serial.println("Started."); */
}
void blink_frame()
void blink_frame(bool left, bool right, int tone_divisor = 2)
{
if (cycle_time > BLINK_RATE)
{
......@@ -85,15 +86,20 @@ void blink_frame()
blinker_light_state = !blinker_light_state;
}
blink_speaker_frame();
blink_speaker_frame(tone_divisor);
// Actual light
digitalWrite(fg_mode == FG_MODE_BLINK_R ? PIN_OUT_BLINKER_R : PIN_OUT_BLINKER_L,
blinker_light_state ? LOW : HIGH);
if (left)
{
digitalWrite(PIN_OUT_BLINKER_L, blinker_light_state ? LOW : HIGH);
digitalWrite(PIN_OUT_LEFT_BUTTON_LED, blinker_light_state ? LOW : HIGH);
}
// Indicator on button
digitalWrite(fg_mode == FG_MODE_BLINK_R ? PIN_OUT_LEFT_BUTTON_LED : PIN_OUT_RIGHT_BUTTON_LED,
blinker_light_state ? LOW : HIGH);
if (right)
{
digitalWrite(PIN_OUT_BLINKER_R, blinker_light_state ? LOW : HIGH);
digitalWrite(PIN_OUT_RIGHT_BUTTON_LED, blinker_light_state ? LOW : HIGH);
}
#if DEBUG_WITH_BUILTIN_LED
digitalWrite(LED_BUILTIN, blinker_light_state ? LOW : HIGH);
......@@ -102,37 +108,20 @@ void blink_frame()
cycle_time++;
}
void hazard_frame()
{
if (cycle_time > BLINK_RATE) {
cycle_time = 0;
blinker_light_state = !blinker_light_state;
}
blink_speaker_frame();
if (blinker_light_state) {
digitalWrite(LED_BUILTIN, HIGH);
} else {
digitalWrite(LED_BUILTIN, LOW);
}
/* digitalWrite(light_side_is_right ? PIN_OUT_BLINKER_R : PIN_OUT_BLINKER_L, blinker_light_state ? LOW : HIGH); */
/* digitalWrite(LED_BUILTIN, blinker_light_state ? LOW : HIGH); */
}
void blink_speaker_frame()
void blink_speaker_frame(int divisor)
{
if (!blinker_light_state && cycle_time < 40) {
digitalWrite(PIN_OUT_SPEAKER, !(cycle_time % 2) ? HIGH : LOW);
digitalWrite(PIN_OUT_SPEAKER, !(cycle_time % divisor) ? HIGH : LOW);
}
}
// 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.
void read_dual_buttons()
{
left_just_released = false;
right_just_released = false;
both_just_released = false;
left_input_ended = false;
right_input_ended = false;
both_input_ended = false;
if (digitalRead(PIN_IN_LEFT_BUTTON) == LOW && digitalRead(PIN_IN_RIGHT_BUTTON) == LOW)
both_hold_time++;
......@@ -142,9 +131,9 @@ void read_dual_buttons()
right_hold_time++;
else
{
if (left_hold_time) left_just_released = true;
if (right_hold_time) right_just_released = true;
if (both_hold_time) both_just_released = true;
if (left_hold_time) left_input_ended = true;
if (right_hold_time) right_input_ended = true;
if (both_hold_time) both_input_ended = true;
both_hold_time = 0;
left_hold_time = 0;
......@@ -158,13 +147,15 @@ void set_up_mode()
if (both_hold_time == HOLD_DOWN_TIME)
{
bg_mode = BG_MODE_HAZARDS;
bg_mode = bg_mode == BG_MODE_HAZARDS ? MODE_OFF : BG_MODE_HAZARDS;
fg_mode = MODE_OFF;
blocked = true;
}
else if (both_just_released && both_hold_time < HOLD_DOWN_TIME)
else if (both_input_ended && both_hold_time < HOLD_DOWN_TIME && !blocked)
{
bg_mode = MODE_OFF;
fg_mode = MODE_OFF;
blocked = false;
}
// Cycle between running lights
else if (left_hold_time == HOLD_DOWN_TIME)
......@@ -174,16 +165,24 @@ void set_up_mode()
bg_mode = BG_MODE_RUNNING_LIGHTS;
fg_mode = MODE_OFF;
blocked = true;
}
else if (left_just_released && left_hold_time < HOLD_DOWN_TIME)
else if (left_input_ended && left_hold_time < HOLD_DOWN_TIME)
{
fg_mode = fg_mode == FG_MODE_BLINK_L ? MODE_OFF : FG_MODE_BLINK_L;
blocked = false;
}
else if (right_hold_time == HOLD_DOWN_TIME)
{
bg_mode = BG_MODE_AUXILARY_LIGHTS;
fg_mode = MODE_OFF;
blocked = true;
}
else if (right_just_released && right_hold_time < HOLD_DOWN_TIME)
else if (right_input_ended && right_hold_time < HOLD_DOWN_TIME)
{
fg_mode = fg_mode == FG_MODE_BLINK_R ? MODE_OFF : FG_MODE_BLINK_R;
blocked = false;
}
}
void reset_all()
......@@ -209,13 +208,16 @@ void loop()
/* 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();
if (fg_mode == FG_MODE_BLINK_L || fg_mode == FG_MODE_BLINK_R)
blink_frame();
/* else if (fg_mode == HAZARDS_MODE) */
/* hazard_frame(); */
if (fg_mode == FG_MODE_BLINK_L)
blink_frame(true, false);
else if (fg_mode == FG_MODE_BLINK_R)
blink_frame(false, true);
else if (bg_mode == BG_MODE_HAZARDS)
blink_frame(true, true, 3);
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