Commit c7181d05 authored by Michael Nelson's avatar Michael Nelson

Rework input single/hold to work for many (grip switches)

parent af7963d6
......@@ -22,16 +22,17 @@
// Orange - Left button LED
// Orange striped - Right button LED
// Blue - Left handlebar grip tactile switch
// Blue striped - Available
// Blue striped - TODO Right handlebar grip tactile switch
// Inputs
#define PIN_IN_LEFT_BUTTON 4
#define PIN_IN_RIGHT_BUTTON 5
#define PIN_IN_LEFT_UTIL_BUTTON 4
#define PIN_IN_RIGHT_UTIL_BUTTON 5
#define PIN_IN_SCOOTER_STATUS 2
// TODO
#define PIN_IN_LEFT_GRIP_BUTTON 10
// #define PIN_IN_BLUE_STRIPED 11
// TODO
#define PIN_IN_RIGHT_GRIP_BUTTON 11
// Outputs
#define PIN_OUT_LEFT_BUTTON_LED 12
......@@ -56,32 +57,39 @@ int last_fg_mode = MODE_OFF;
int last_bg_mode = MODE_OFF;
bool hold_waiting = false;
// Just-released variables
bool input_single_left_hit = false;
bool input_single_right_hit = false;
bool input_single_both_hit = false;
enum scooter_ui {
btn_util_left,
btn_util_right,
btn_util_both,
btn_grip_left,
btn_grip_right,
btn_grip_both,
bool input_hold_left_hit = false;
bool input_hold_right_hit = false;
bool input_hold_both_hit = false;
num_ui_actions
};
// 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
int cycle_time = 0;
int new_input_blocked = 0;
int left_hold_time = 0;
int right_hold_time = 0;
int both_hold_time = 0;
void setup()
{
// Initialize a serial connection for reporting values to the host
Serial.begin(9600);
pinMode(PIN_IN_LEFT_BUTTON, INPUT_PULLUP);
pinMode(PIN_IN_RIGHT_BUTTON, INPUT_PULLUP);
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_RIGHT_GRIP_BUTTON, INPUT_PULLUP);
pinMode(PIN_IN_SCOOTER_STATUS, INPUT_PULLUP);
pinMode(PIN_OUT_SPEAKER, OUTPUT);
......@@ -165,43 +173,34 @@ void blink_speaker_frame(int divisor)
// to be consumed later in the frame.
void read_dual_buttons()
{
input_single_left_hit = false;
input_single_right_hit = false;
input_single_both_hit = false;
input_hold_left_hit = false;
input_hold_right_hit = false;
input_hold_both_hit = false;
for (int i = 0; i < num_ui_actions; i++) {
input_single_hit[i] = false;
input_hold_hit[i] = false;
}
if (digitalRead(PIN_IN_LEFT_GRIP_BUTTON) == LOW && digitalRead(PIN_IN_RIGHT_BUTTON) == LOW)
both_hold_time++;
if (digitalRead(PIN_IN_LEFT_GRIP_BUTTON) == LOW && digitalRead(PIN_IN_RIGHT_UTIL_BUTTON) == LOW)
hold_time[btn_grip_both]++;
else if (digitalRead(PIN_IN_LEFT_GRIP_BUTTON) == LOW)
left_hold_time++;
else if (digitalRead(PIN_IN_RIGHT_BUTTON) == LOW)
right_hold_time++;
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 (left_hold_time && !new_input_blocked && !hold_waiting)
for (int i = 0; i < num_ui_actions; i++)
{
input_single_left_hit = true;
debounce_subsequent_input();
if (hold_time[i] && !new_input_blocked && !hold_waiting)
{
input_single_hit[i] = true;
debounce_subsequent_input();
}
}
if (right_hold_time && !new_input_blocked && !hold_waiting)
{
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;
for (int i = 0; i < num_ui_actions; i++)
hold_time[i] = 0;
if (hold_waiting)
debounce_subsequent_input();
......@@ -209,25 +208,14 @@ void read_dual_buttons()
hold_waiting = false;
}
if (left_hold_time == HOLD_DOWN_TIME && !new_input_blocked)
{
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)
for (int i = 0; i < num_ui_actions; i++)
{
input_hold_both_hit = true;
hold_waiting = true;
debounce_subsequent_input();
if (hold_time[i] == HOLD_DOWN_TIME && !new_input_blocked)
{
input_hold_hit[i] = true;
hold_waiting = true;
debounce_subsequent_input();
}
}
}
......@@ -235,18 +223,18 @@ void set_up_mode()
{
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;
fg_mode = MODE_OFF;
}
else if (input_single_both_hit)
else if (input_single_hit[btn_grip_both])
{
bg_mode = MODE_OFF;
fg_mode = MODE_OFF;
}
// Cycle between running lights
else if (input_hold_left_hit)
else if (input_hold_hit[btn_grip_left])
{
if (!bg_mode)
bg_mode = BG_MODE_RUNNING_LIGHTS;
......@@ -257,16 +245,15 @@ void set_up_mode()
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;
// TODO Temporary disabling testing horn
/* else if (input_hold_right_hit) */
/* { */
/* bg_mode = bg_mode == BG_MODE_AUXILARY_LIGHTS ? MODE_OFF : BG_MODE_AUXILARY_LIGHTS; */
/* fg_mode = MODE_OFF; */
/* } */
/* else if (input_single_right_hit) */
/* fg_mode = fg_mode == FG_MODE_BLINK_R ? MODE_OFF : FG_MODE_BLINK_R; */
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])
fg_mode = fg_mode == FG_MODE_BLINK_R ? MODE_OFF : FG_MODE_BLINK_R;
}
void reset_all()
......@@ -292,8 +279,8 @@ bool scooter_is_off()
void loop()
{
/* if (scooter_is_off()) */
/* return; */
if (scooter_is_off())
return;
delay(1);
set_up_mode();
......@@ -317,9 +304,9 @@ void loop()
else
reset_all();
// TODO Temporary testing horn
if (right_hold_time > 0)
digitalWrite(PIN_OUT_HORN, HIGH);
// 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);
......
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