Commit e21ac3cb authored by Michael Nelson's avatar Michael Nelson

Simplify & rejigger to fit handlebar blinkers

parent 9389ccc1
#include <avr/power.h>
#define MODE_OFF 0
#define DEBUG_WITH_BUILTIN_LED false
#define DEBUG_WITH_BUILTIN_LED true
// Foreground Modes
#define FG_MODE_BLINK_L 1
......@@ -15,29 +15,21 @@
// Pins 2-6 are for itmes on the handlebars.
// Ethernet coloring plan:
// Brown - Ground
// Brown striped - Speaker
// Green - Left button
// Green striped - Right button
// Orange - Left button LED
// Orange striped - Right button LED
// Blue - Left handlebar grip tactile switch
// Blue striped - TODO Right handlebar grip tactile switch
// Brown - 5V Ground
// Brown striped - 5V Speaker
// Green - 12V Left Blinker
// Green striped - 12V Right Blinker
// Orange - 12V Ground
// Orange striped - 5V Horn button
// Blue - 5V Left handlebar grip tactile switch
// Blue striped - 5V Right handlebar grip tactile switch
// Inputs
#define PIN_IN_LEFT_UTIL_BUTTON 4
#define PIN_IN_RIGHT_UTIL_BUTTON 5
#define PIN_IN_SCOOTER_STATUS 2
#define PIN_IN_LEFT_GRIP_BUTTON 10
// TODO
#define PIN_IN_RIGHT_GRIP_BUTTON 11
#define PIN_IN_HORN_BUTTON 9
// Outputs
#define PIN_OUT_LEFT_BUTTON_LED 12
#define PIN_OUT_RIGHT_BUTTON_LED 13
#define PIN_OUT_SPEAKER 6
#define PIN_OUT_BLINKER_L 7
#define PIN_OUT_BLINKER_R 8
......@@ -58,14 +50,12 @@ int last_bg_mode = MODE_OFF;
bool hold_waiting = false;
enum scooter_ui {
btn_util_left,
btn_util_right,
btn_util_both,
btn_grip_left,
btn_grip_right,
btn_grip_both,
btn_horn,
num_ui_actions
};
......@@ -86,17 +76,12 @@ void setup()
// Initialize a serial connection for reporting values to the host
Serial.begin(9600);
pinMode(PIN_IN_LEFT_UTIL_BUTTON, INPUT_PULLUP);
pinMode(PIN_IN_RIGHT_UTIL_BUTTON, INPUT_PULLUP);
pinMode(PIN_IN_LEFT_GRIP_BUTTON, INPUT_PULLUP);
pinMode(PIN_IN_LEFT_GRIP_BUTTON, INPUT);
pinMode(PIN_IN_RIGHT_GRIP_BUTTON, INPUT_PULLUP);
pinMode(PIN_IN_SCOOTER_STATUS, INPUT_PULLUP);
pinMode(PIN_OUT_SPEAKER, OUTPUT);
pinMode(PIN_OUT_BLINKER_L, OUTPUT);
pinMode(PIN_OUT_BLINKER_R, OUTPUT);
pinMode(PIN_OUT_LEFT_BUTTON_LED, OUTPUT);
pinMode(PIN_OUT_RIGHT_BUTTON_LED, OUTPUT);
/* clock_prescale_set(clock_div_2); */
......@@ -121,16 +106,10 @@ void blink_frame(bool left, bool right, int tone_divisor = 2)
// Actual light
if (left)
{
digitalWrite(PIN_OUT_BLINKER_L, blinker_light_state ? LOW : HIGH);
digitalWrite(PIN_OUT_LEFT_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);
......@@ -147,7 +126,9 @@ void running_light_frame(bool with_blink = false)
digitalWrite(PIN_OUT_BLINKER_L, !(cycle_time % 20) || (with_blink && cycle_time < 70) ? HIGH : LOW);
digitalWrite(PIN_OUT_BLINKER_R, !(cycle_time % 20) || (with_blink && cycle_time < 70) ? HIGH : LOW);
digitalWrite(PIN_OUT_LEFT_BUTTON_LED, !(cycle_time % 20) || (with_blink && cycle_time < 70) ? HIGH : LOW);
#if DEBUG_WITH_BUILTIN_LED
digitalWrite(LED_BUILTIN, !(cycle_time % 20) || (with_blink && cycle_time < 70) ? HIGH : LOW);
#endif
cycle_time++;
}
......@@ -157,7 +138,9 @@ void auxiliary_light_frame()
digitalWrite(PIN_OUT_BLINKER_L, HIGH);
digitalWrite(PIN_OUT_BLINKER_R, HIGH);
digitalWrite(PIN_OUT_RIGHT_BUTTON_LED, HIGH);
#if DEBUG_WITH_BUILTIN_LED
digitalWrite(LED_BUILTIN, HIGH);
#endif
}
void blink_speaker_frame(int divisor)
......@@ -178,16 +161,14 @@ void read_dual_buttons()
input_hold_hit[i] = false;
}
if (digitalRead(PIN_IN_LEFT_GRIP_BUTTON) == LOW && digitalRead(PIN_IN_RIGHT_UTIL_BUTTON) == LOW)
if (digitalRead(PIN_IN_LEFT_GRIP_BUTTON) == LOW && digitalRead(PIN_IN_RIGHT_GRIP_BUTTON) == LOW)
hold_time[btn_grip_both]++;
else if (digitalRead(PIN_IN_LEFT_GRIP_BUTTON) == LOW)
hold_time[btn_grip_left]++;
else if (digitalRead(PIN_IN_RIGHT_GRIP_BUTTON) == LOW)
hold_time[btn_grip_right]++;
else if (digitalRead(PIN_IN_LEFT_UTIL_BUTTON) == LOW)
hold_time[btn_util_left]++;
else if (digitalRead(PIN_IN_RIGHT_UTIL_BUTTON) == LOW)
hold_time[btn_util_right]++;
else if (digitalRead(PIN_IN_HORN_BUTTON) == LOW)
hold_time[btn_horn]++;
else
{
for (int i = 0; i < num_ui_actions; i++)
......@@ -224,21 +205,10 @@ void set_up_mode()
read_dual_buttons();
if (input_hold_hit[btn_grip_both])
{
bg_mode = bg_mode == BG_MODE_HAZARDS ? MODE_OFF : BG_MODE_HAZARDS;
fg_mode = MODE_OFF;
}
else if (input_single_hit[btn_grip_both])
{
bg_mode = MODE_OFF;
fg_mode = MODE_OFF;
}
// Cycle between running lights
else if (input_hold_hit[btn_grip_left])
{
if (!bg_mode)
bg_mode = BG_MODE_RUNNING_LIGHTS;
else if (bg_mode >= BG_MODE_RUNNING_W_BLINK)
bg_mode = BG_MODE_HAZARDS;
else if (bg_mode >= BG_MODE_AUXILARY_LIGHTS)
bg_mode = MODE_OFF;
else
bg_mode++;
......@@ -247,12 +217,7 @@ void set_up_mode()
}
else if (input_single_hit[btn_grip_left])
fg_mode = fg_mode == FG_MODE_BLINK_L ? MODE_OFF : FG_MODE_BLINK_L;
else if (input_hold_hit[btn_util_left])
{
bg_mode = bg_mode == BG_MODE_AUXILARY_LIGHTS ? MODE_OFF : BG_MODE_AUXILARY_LIGHTS;
fg_mode = MODE_OFF;
}
else if (input_single_hit[btn_util_left])
else if (input_single_hit[btn_grip_right])
fg_mode = fg_mode == FG_MODE_BLINK_R ? MODE_OFF : FG_MODE_BLINK_R;
}
......@@ -267,21 +232,11 @@ void reset_all()
digitalWrite(PIN_OUT_BLINKER_L, LOW);
digitalWrite(PIN_OUT_BLINKER_R, LOW);
digitalWrite(PIN_OUT_LEFT_BUTTON_LED, LOW);
digitalWrite(PIN_OUT_RIGHT_BUTTON_LED, LOW);
digitalWrite(PIN_OUT_HORN, LOW);
}
bool scooter_is_off()
{
return digitalRead(PIN_IN_SCOOTER_STATUS) == LOW;
}
void loop()
{
if (scooter_is_off())
return;
delay(1);
set_up_mode();
......@@ -305,7 +260,7 @@ void loop()
reset_all();
// Horn is driven asynchronously from the rest of the blinking logic
if (hold_time[btn_util_right] > 0)
if (hold_time[btn_horn] > 0)
digitalWrite(PIN_OUT_HORN, HIGH); // TOOOOOOT
else
digitalWrite(PIN_OUT_HORN, LOW);
......
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