...
 
Commits (4)
...@@ -21,17 +21,18 @@ ...@@ -21,17 +21,18 @@
// Green striped - Right button // Green striped - Right button
// Orange - Left button LED // Orange - Left button LED
// Orange striped - Right button LED // Orange striped - Right button LED
// Blue - Available // Blue - Left handlebar grip tactile switch
// Blue striped - Available // Blue striped - TODO Right handlebar grip tactile switch
// Inputs // Inputs
#define PIN_IN_LEFT_BUTTON 4 #define PIN_IN_LEFT_UTIL_BUTTON 4
#define PIN_IN_RIGHT_BUTTON 5 #define PIN_IN_RIGHT_UTIL_BUTTON 5
#define PIN_IN_SCOOTER_STATUS 2 #define PIN_IN_SCOOTER_STATUS 2
#define PIN_IN_LEFT_GRIP_BUTTON 10
// TODO // TODO
// #define PIN_IN_BLUE_SOLID 10 #define PIN_IN_RIGHT_GRIP_BUTTON 11
// #define PIN_IN_BLUE_STRIPED 11
// Outputs // Outputs
#define PIN_OUT_LEFT_BUTTON_LED 12 #define PIN_OUT_LEFT_BUTTON_LED 12
...@@ -40,7 +41,7 @@ ...@@ -40,7 +41,7 @@
#define PIN_OUT_SPEAKER 6 #define PIN_OUT_SPEAKER 6
#define PIN_OUT_BLINKER_L 7 #define PIN_OUT_BLINKER_L 7
#define PIN_OUT_BLINKER_R 8 #define PIN_OUT_BLINKER_R 8
#define PIN_OUT_WHITES 9 #define PIN_OUT_HORN 9
// Config // Config
#define BLINK_RATE 250 #define BLINK_RATE 250
...@@ -56,29 +57,39 @@ int last_fg_mode = MODE_OFF; ...@@ -56,29 +57,39 @@ int last_fg_mode = MODE_OFF;
int last_bg_mode = MODE_OFF; int last_bg_mode = MODE_OFF;
bool hold_waiting = false; bool hold_waiting = false;
// Just-released variables enum scooter_ui {
bool input_single_left_hit = false; btn_util_left,
bool input_single_right_hit = false; btn_util_right,
bool input_single_both_hit = false; btn_util_both,
btn_grip_left,
btn_grip_right,
btn_grip_both,
bool input_hold_left_hit = false; num_ui_actions
bool input_hold_right_hit = false; };
bool input_hold_both_hit = false;
// Flags calculated by the program for a single frame of loop as either single push & release
// or a hold. The hold bit is flipped ONCE at the frame exactly at the end of HOLD_DOWN_TIME.
// The user can continue to hold, which increments the input in hold_time, but won't flip
// any bit either "hit" array here.
bool input_single_hit[num_ui_actions];
bool input_hold_hit[num_ui_actions];
int hold_time[num_ui_actions];
// Time accumulator variables // Time accumulator variables
int cycle_time = 0; int cycle_time = 0;
int new_input_blocked = 0; int new_input_blocked = 0;
int left_hold_time = 0;
int right_hold_time = 0;
int both_hold_time = 0;
void setup() void setup()
{ {
// Initialize a serial connection for reporting values to the host // 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_LEFT_UTIL_BUTTON, INPUT_PULLUP);
pinMode(PIN_IN_RIGHT_BUTTON, INPUT_PULLUP); pinMode(PIN_IN_RIGHT_UTIL_BUTTON, INPUT_PULLUP);
pinMode(PIN_IN_LEFT_GRIP_BUTTON, INPUT_PULLUP);
pinMode(PIN_IN_RIGHT_GRIP_BUTTON, INPUT_PULLUP);
pinMode(PIN_IN_SCOOTER_STATUS, INPUT_PULLUP); pinMode(PIN_IN_SCOOTER_STATUS, INPUT_PULLUP);
pinMode(PIN_OUT_SPEAKER, OUTPUT); pinMode(PIN_OUT_SPEAKER, OUTPUT);
...@@ -162,43 +173,34 @@ void blink_speaker_frame(int divisor) ...@@ -162,43 +173,34 @@ void blink_speaker_frame(int divisor)
// to be consumed later in the frame. // to be consumed later in the frame.
void read_dual_buttons() void read_dual_buttons()
{ {
input_single_left_hit = false; for (int i = 0; i < num_ui_actions; i++) {
input_single_right_hit = false; input_single_hit[i] = false;
input_single_both_hit = false; input_hold_hit[i] = false;
}
input_hold_left_hit = false;
input_hold_right_hit = false; if (digitalRead(PIN_IN_LEFT_GRIP_BUTTON) == LOW && digitalRead(PIN_IN_RIGHT_UTIL_BUTTON) == LOW)
input_hold_both_hit = false; hold_time[btn_grip_both]++;
else if (digitalRead(PIN_IN_LEFT_GRIP_BUTTON) == LOW)
if (digitalRead(PIN_IN_LEFT_BUTTON) == LOW && digitalRead(PIN_IN_RIGHT_BUTTON) == LOW) hold_time[btn_grip_left]++;
both_hold_time++; else if (digitalRead(PIN_IN_RIGHT_GRIP_BUTTON) == LOW)
else if (digitalRead(PIN_IN_LEFT_BUTTON) == LOW) hold_time[btn_grip_right]++;
left_hold_time++; else if (digitalRead(PIN_IN_LEFT_UTIL_BUTTON) == LOW)
else if (digitalRead(PIN_IN_RIGHT_BUTTON) == LOW) hold_time[btn_util_left]++;
right_hold_time++; else if (digitalRead(PIN_IN_RIGHT_UTIL_BUTTON) == LOW)
hold_time[btn_util_right]++;
else else
{ {
if (left_hold_time && !new_input_blocked && !hold_waiting) for (int i = 0; i < num_ui_actions; i++)
{ {
input_single_left_hit = true; if (hold_time[i] && !new_input_blocked && !hold_waiting)
debounce_subsequent_input(); {
input_single_hit[i] = true;
debounce_subsequent_input();
}
} }
if (right_hold_time && !new_input_blocked && !hold_waiting) for (int i = 0; i < num_ui_actions; i++)
{ hold_time[i] = 0;
input_single_right_hit = true;
debounce_subsequent_input();
}
if (both_hold_time && !new_input_blocked && !hold_waiting)
{
input_single_both_hit = true;
debounce_subsequent_input();
}
both_hold_time = 0;
left_hold_time = 0;
right_hold_time = 0;
if (hold_waiting) if (hold_waiting)
debounce_subsequent_input(); debounce_subsequent_input();
...@@ -206,25 +208,14 @@ void read_dual_buttons() ...@@ -206,25 +208,14 @@ void read_dual_buttons()
hold_waiting = false; hold_waiting = false;
} }
if (left_hold_time == HOLD_DOWN_TIME && !new_input_blocked) for (int i = 0; i < num_ui_actions; i++)
{
input_hold_left_hit = true;
hold_waiting = true;
debounce_subsequent_input();
}
else if (right_hold_time == HOLD_DOWN_TIME && !new_input_blocked)
{
input_hold_right_hit = true;
hold_waiting = true;
debounce_subsequent_input();
}
else if (both_hold_time == HOLD_DOWN_TIME && !new_input_blocked)
{ {
input_hold_both_hit = true; if (hold_time[i] == HOLD_DOWN_TIME && !new_input_blocked)
hold_waiting = true; {
debounce_subsequent_input(); input_hold_hit[i] = true;
hold_waiting = true;
debounce_subsequent_input();
}
} }
} }
...@@ -232,18 +223,18 @@ void set_up_mode() ...@@ -232,18 +223,18 @@ void set_up_mode()
{ {
read_dual_buttons(); read_dual_buttons();
if (input_hold_both_hit) if (input_hold_hit[btn_grip_both])
{ {
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;
} }
else if (input_single_both_hit) else if (input_single_hit[btn_grip_both])
{ {
bg_mode = MODE_OFF; bg_mode = MODE_OFF;
fg_mode = MODE_OFF; fg_mode = MODE_OFF;
} }
// Cycle between running lights // Cycle between running lights
else if (input_hold_left_hit) else if (input_hold_hit[btn_grip_left])
{ {
if (!bg_mode) if (!bg_mode)
bg_mode = BG_MODE_RUNNING_LIGHTS; bg_mode = BG_MODE_RUNNING_LIGHTS;
...@@ -254,14 +245,14 @@ void set_up_mode() ...@@ -254,14 +245,14 @@ void set_up_mode()
fg_mode = MODE_OFF; fg_mode = MODE_OFF;
} }
else if (input_single_left_hit) else if (input_single_hit[btn_grip_left])
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;
else if (input_hold_right_hit) else if (input_hold_hit[btn_util_left])
{ {
bg_mode = bg_mode == BG_MODE_AUXILARY_LIGHTS ? MODE_OFF : BG_MODE_AUXILARY_LIGHTS; bg_mode = bg_mode == BG_MODE_AUXILARY_LIGHTS ? MODE_OFF : BG_MODE_AUXILARY_LIGHTS;
fg_mode = MODE_OFF; fg_mode = MODE_OFF;
} }
else if (input_single_right_hit) else if (input_single_hit[btn_util_left])
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;
} }
...@@ -278,7 +269,7 @@ void reset_all() ...@@ -278,7 +269,7 @@ void reset_all()
digitalWrite(PIN_OUT_BLINKER_R, LOW); digitalWrite(PIN_OUT_BLINKER_R, LOW);
digitalWrite(PIN_OUT_LEFT_BUTTON_LED, LOW); digitalWrite(PIN_OUT_LEFT_BUTTON_LED, LOW);
digitalWrite(PIN_OUT_RIGHT_BUTTON_LED, LOW); digitalWrite(PIN_OUT_RIGHT_BUTTON_LED, LOW);
digitalWrite(PIN_OUT_WHITES, LOW); digitalWrite(PIN_OUT_HORN, LOW);
} }
bool scooter_is_off() bool scooter_is_off()
...@@ -313,6 +304,12 @@ void loop() ...@@ -313,6 +304,12 @@ void loop()
else else
reset_all(); reset_all();
// Horn is driven asynchronously from the rest of the blinking logic
if (hold_time[btn_util_right] > 0)
digitalWrite(PIN_OUT_HORN, HIGH); // TOOOOOOT
else
digitalWrite(PIN_OUT_HORN, LOW);
last_fg_mode = fg_mode; last_fg_mode = fg_mode;
last_bg_mode = bg_mode; last_bg_mode = bg_mode;
......