Commit 034ab245 authored by Michael Nelson's avatar Michael Nelson

Add initial files

parent a15a6868
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; http://docs.platformio.org/page/projectconf.html
[env:uno]
platform = atmelavr
board = uno
framework = arduino
#include <avr/power.h>
#define MODE_OFF 0
#define DEBUG_WITH_BUILTIN_LED true
// Foreground Modes
#define FG_MODE_BLINK_L 1
#define FG_MODE_BLINK_R 2
// Background modes
#define BG_MODE_HAZARDS 1
#define BG_MODE_RUNNING_LIGHTS 2
#define BG_MODE_RUNNING_W_BLINK 3
#define BG_MODE_AUXILARY_LIGHTS 4
// Inputs
#define PIN_IN_LEFT_BUTTON 4
#define PIN_IN_RIGHT_BUTTON 5
// Outputs
#define PIN_OUT_SPEAKER 6
#define PIN_OUT_BLINKER_L 7
#define PIN_OUT_BLINKER_R 8
#define PIN_OUT_WHITES 9
// Config
#define BLINK_RATE 100
#define HOLD_DOWN_TIME 350
// State
int fg_mode = MODE_OFF;
int bg_mode = MODE_OFF;
int blinker_light_state;
// Just-released variables
bool left_just_released = false;
bool right_just_released = false;
bool both_just_released = false;
// Time accumulator variables
int cycle_time = 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_OUT_SPEAKER, OUTPUT);
pinMode(PIN_OUT_BLINKER_L, OUTPUT);
pinMode(PIN_OUT_BLINKER_R, OUTPUT);
/* clock_prescale_set(clock_div_2); */
Serial.println("Started.");
}
void blink_frame()
{
if (cycle_time > BLINK_RATE)
{
cycle_time = 0;
blinker_light_state = !blinker_light_state;
}
/* blink_speaker_frame(); */
digitalWrite(fg_mode == FG_MODE_BLINK_R ? PIN_OUT_BLINKER_R : PIN_OUT_BLINKER_L, blinker_light_state ? LOW : HIGH);
#if DEBUG_WITH_BUILTIN_LED
digitalWrite(LED_BUILTIN, blinker_light_state ? LOW : HIGH);
#endif
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()
{
if (blinker_light_state && cycle_time < 1000 && !(cycle_time % 80)) {
digitalWrite(PIN_OUT_SPEAKER, HIGH);
}
else if (blinker_light_state && cycle_time < 1000) {
digitalWrite(PIN_OUT_SPEAKER, LOW);
}
else {
if (cycle_time < 1000 && !(cycle_time % 40)) {
digitalWrite(PIN_OUT_SPEAKER, HIGH);
} else if (cycle_time < 1000) {
digitalWrite(PIN_OUT_SPEAKER, LOW);
}
}
}
void read_dual_buttons()
{
left_just_released = false;
right_just_released = false;
both_just_released = false;
if (digitalRead(PIN_IN_LEFT_BUTTON) == LOW && digitalRead(PIN_IN_RIGHT_BUTTON) == LOW)
both_hold_time++;
else if (digitalRead(PIN_IN_LEFT_BUTTON) == LOW)
left_hold_time++;
else if (digitalRead(PIN_IN_RIGHT_BUTTON) == LOW)
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;
both_hold_time = 0;
left_hold_time = 0;
right_hold_time = 0;
}
}
void set_up_mode()
{
read_dual_buttons();
if (both_hold_time == HOLD_DOWN_TIME)
{
bg_mode = BG_MODE_HAZARDS;
fg_mode = MODE_OFF;
}
else if (both_just_released && both_hold_time < HOLD_DOWN_TIME)
{
bg_mode = MODE_OFF;
fg_mode = MODE_OFF;
}
// Cycle between running lights
else if (left_hold_time == HOLD_DOWN_TIME)
{
bg_mode++;
if (bg_mode > BG_MODE_RUNNING_W_BLINK)
bg_mode = BG_MODE_RUNNING_LIGHTS;
fg_mode = MODE_OFF;
}
else if (left_just_released && left_hold_time < HOLD_DOWN_TIME)
fg_mode = fg_mode == FG_MODE_BLINK_L ? MODE_OFF : FG_MODE_BLINK_L;
else if (right_hold_time == HOLD_DOWN_TIME)
{
bg_mode = BG_MODE_AUXILARY_LIGHTS;
fg_mode = MODE_OFF;
}
else if (right_just_released && right_hold_time < HOLD_DOWN_TIME)
fg_mode = fg_mode == FG_MODE_BLINK_R ? MODE_OFF : FG_MODE_BLINK_R;
}
void loop()
{
set_up_mode();
Serial.println(bg_mode);
/* Serial.println(fg_mode); */
if (fg_mode == FG_MODE_BLINK_L || fg_mode == FG_MODE_BLINK_R)
blink_frame();
/* else if (fg_mode == HAZARDS_MODE) */
/* hazard_frame(); */
else
{
cycle_time = 0;
#if DEBUG_WITH_BUILTIN_LED
digitalWrite(LED_BUILTIN, LOW);
#endif
digitalWrite(PIN_OUT_BLINKER_L, LOW);
digitalWrite(PIN_OUT_BLINKER_R, LOW);
digitalWrite(PIN_OUT_WHITES, 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