diff --git a/BL_config.h b/BL_config.h index 17aa47c..df76e1e 100644 --- a/BL_config.h +++ b/BL_config.h @@ -9,12 +9,6 @@ #ifndef config_h #define BL_config_h -#include "WProgram.h" - -/** \file - Configuration for AMUP sketches. This file holds constants that define variables that are used across multiple. - */ - // switch states #define OFF 0 // equivalent to LOW #define ON 1 // equivalent to HIGH diff --git a/Bright_Lights.pde b/Bright_Lights.pde index 2c70921..d54beb5 100644 --- a/Bright_Lights.pde +++ b/Bright_Lights.pde @@ -1,9 +1,7 @@ -// Things to change -// Parse serial messages to control color and fun states - #include "BL_config.h" #include +#include #include #include #include @@ -43,58 +41,127 @@ #define REMOTE_output_range REMOTE_output_max - REMOTE_output_min // CONSTANTS: scroll and strobe min and max range values -#define STROBE_inter_min 5 -#define STROBE_inter_max 80 -#define SCROLL_inter_min 5 -#define SCROLL_inter_max 300 +#define STROBE_speed_min 5 +#define STROBE_speed_max 80 +#define SCROLL_speed_min 5 +#define SCROLL_speed_max 300 #define SCROLL_width_min 1 #define SCROLL_width_max NUM_RGB_LED-1 -// CONSTANTS: saving the EEPROM location where the R, G, B color values are stored -int const EEPROM_hsb_address[NUM_color_ctrls] = {3,5,7}; // assigns address for saving rgb color values -int const EEPROM_scroll_address[NUM_scroll_ctrls] = {9,11,13}; // assigns address for saving speed, direction, width -int const EEPROM_strobe_address[NUM_strobe_ctrls] = {15}; // assigns address for saving speed, direction, width - -// CONSTANTS: arrays that hold the pin numbers of each led on the TLC5940 LED drivers -// on the rgb_pins array the r, g, b pin for each led are grouped together -int const rgb_pins[NUM_RGB_LED*3] = {26,25,24, 29,28,27, 0,31,30, 3,2,1, 6,5,4, 9,8,7, 12,11,10, 15,14,13}; +// MESSAGE CONTANTS +#define MSG_LEN_realtime NUM_RGB_LED*3 +#define MSG_LEN_color 9 +#define MSG_LEN_scroll 3 +#define MSG_LEN_strobe 1 +#define MSG_LEN_longest MSG_LEN_realtime + +// MESSAGE HEADERS +#define CONNECT_MSG_confirm 255 +#define STATUS_MSG_request 254 +#define MODE_MSG_realtime 253 +#define MODE_MSG_off 252 +#define SET_MSG_hsb 129 +#define MODE_MSG_color_hsb 192 +#define MODE_MSG_strobe 194 +#define MODE_MSG_scroll 193 +#define END_MSG 128 + + +class Bright_Lights { + + private: + int* rgb_pins; + int* EEPROM_addresses; + Switch* switches; + AnalogSwitch pot; + +// NewSoftSerial blueSerial; + + int active_mode; + bool new_mode; + int hsb_vals[NUM_color_ctrls]; // holds the hsb color values + int rgb_vals[NUM_color_ctrls]; // holds the rgb color values + int p_control_hsb; // current hsb parameter being controlled by physical control + + boolean data_saved; // holds if current data has been saved + long last_save; // holds last time data was saved + int save_interval; // interval between time changes being made and saved + + int p_control_strobe_scroll; // holds the current fun mode (strobe or scroll) + + // strobe control variables + int strobe_speed; + long strobe_last_switch; + bool strobe_on; + + // scroll control variables + int scroll_speed; + int scroll_direction; + int scroll_width; + long scroll_last_switch; + int scroll_led_pos; -// VARIABLES: overall mode variables - int active_mode = MODE_off; // holds the current mode state - bool new_mode = false; // holds whether the mode has changed (either active or fun) - // variable used to drive the soft takeover on the potentiometer + + bool soft_takeover_complete; + int takeover_direction; + + byte msg_type; + byte serial_msg[MSG_LEN_realtime]; + int byte_count; + bool reading_msg_flag; + + void save_data(); + void load_data(); + void soft_set_hsb_color(int, int, int, int); + void set_hsb_color(int, int, int, int); + void set_hsb_color(int, int); + void convertHSB(); + + void soft_set_strobe_speed(int, int, int); + void set_strobe_speed(int, int, int); + void soft_set_scroll_speed(int, int, int); + void set_scroll_speed(int, int, int); + void set_scroll_direction(int, int, int); + void set_scroll_width(int, int, int); + void strobe_active(); + void scroll_active(); + void scroll_led_array(int, int*, int); + + void select_color_param_for_physical_ctrl(); + int select_fun_mode_for_physical_ctrl(); + bool check_soft_takeover(int, int); + + void lights_on_realtime(byte*); + void lights_on_all(); + void lights_on_single_only(int); + void lights_on_multiple(int*, int); + void lights_on_single(int); + void lights_off_all(); + void lights_off_single(int); + void blink_delay(int); + + void parse_serial_msg(byte, byte*); + void serial_write(byte); + void send_status_message(); -// VARIABLES: color control state variable; active rgb or hsb parameter variables; and hsb and rgb value arrays - int p_control_hsb = 2; // holds hsb parameter currently controlled by the potentiometer - int hsb_vals[NUM_color_ctrls] = {0,0,0}; // holds the hsb values - int rgb_vals[NUM_color_ctrls] = {0,0,0}; // holds the rgb values + void control_lights(); + void handle_serial(); + void handle_physical_input(); + + public: + Bright_Lights(int*, Switch*, int, int); + void set_EEPROM(int*); + void run(); - long last_save = 0; // holds last time color was changed - boolean data_saved = false; // holds if current color has been saved - int save_interval = 1500; +}; -// VARIABLES: fun mode control state variable; strobe and scroll variables - int p_control_strobe_scroll = 0; // holds the current fun mode (strobe or scroll) - - // strobe control variables - int strobe_speed = STROBE_inter_max; - long strobe_last_switch = 0; - bool strobe_on = false; - - // scroll control variables - int scroll_speed = SCROLL_inter_max; - int scroll_direction = 3; - int scroll_width = 3; - long scroll_last_switch = 0; - int scroll_led_pos = 0; -// OBJECTS: switch and analog switch objects corresponding to physical switches and potentiometers +int EEPROM_addresses[NUM_color_ctrls+NUM_scroll_ctrls+NUM_strobe_ctrls] = {3,5,7,9,11,13,15}; // assigns address for saving rgb color values +int _rgb_pins[NUM_RGB_LED*3] = {26,25,24, 29,28,27, 0,31,30, 3,2,1, 6,5,4, 9,8,7, 12,11,10, 15,14,13}; Switch switches[NUM_switches] = {Switch(ID_strobe_switch, A0), Switch(ID_color_switch, A1)}; -AnalogSwitch pot = AnalogSwitch(ID_potentiometer, A2); - -// OBJECTS: create a soft serial port object for bluetooth connection NewSoftSerial blueSerial = NewSoftSerial(2,4); +Bright_Lights bright_lights = Bright_Lights (_rgb_pins, switches, 2, 4); /********************* SETUP method @@ -102,22 +169,11 @@ NewSoftSerial blueSerial = NewSoftSerial(2,4); the potentiometer object; and it loads the saved color */ void setup() { - // initiliaze both serial ports - Serial.begin(57600); - blueSerial.begin(57600); - serial_write(255); - - // initialized the LED driver (TLC5940) - Tlc.init(); + Serial.begin(57600); + blueSerial.begin(57600); + Tlc.init(); - // initialize the potentiometer - pot.invert_switch(true); - pot.set_analog_range(0, 1023); - pot.set_output_range(POT_output_range); - - // load colors from EEPROM - load_data(); -// load_fun_mode(); + bright_lights.set_EEPROM(EEPROM_addresses); } @@ -129,14 +185,63 @@ void setup() { when in any other mode. */ void loop() { + bright_lights.run(); +} + +Bright_Lights::Bright_Lights(int* led_pin_array, Switch* switch_array, int bt_rx, int bt_tx) : pot(ID_potentiometer, A2) { +//Bright_Lights::Bright_Lights(int* led_pin_array, InputElement* switch_array, int bt_rx, int bt_tx) : blueSerial(bt_rx, bt_tx) { + rgb_pins = led_pin_array; + switches = switch_array; + + active_mode = MODE_off; + new_mode = false; + for (int i = 0; i < NUM_color_ctrls; i++ ) { hsb_vals[i] = 0; } + for (int i = 0; i < NUM_color_ctrls; i++ ) { rgb_vals[i] = 0; } + p_control_hsb = 2; + data_saved = 0; + last_save = true; + save_interval = 1500; + + p_control_strobe_scroll = 0; + strobe_speed = STROBE_speed_max; + strobe_last_switch = 0; + strobe_on = false; + + // scroll control variables + scroll_speed = SCROLL_speed_max; + scroll_direction = 0; + scroll_width = SCROLL_width_min; + scroll_last_switch = 0; + scroll_led_pos = 0; + + soft_takeover_complete = false; + takeover_direction = 0; + + + msg_type = 0; + byte_count = 0; + reading_msg_flag = false; +} + +void Bright_Lights::set_EEPROM(int* addresses) { + + EEPROM_addresses = addresses; + // load colors from EEPROM + load_data(); + + // send ready to connect byte to controller + serial_write(CONNECT_MSG_confirm); + +} + +void Bright_Lights::run() { handle_serial(); - handle_physical_input(); +// handle_physical_input(); if (active_mode != MODE_realtime) { control_lights(); } save_data(); -} - +} diff --git a/EEPROM_Data.pde b/EEPROM_Data.pde index 8f636fe..15923d2 100644 --- a/EEPROM_Data.pde +++ b/EEPROM_Data.pde @@ -1,33 +1,31 @@ /********************* SAVE COLORS - This methods saves the current color to the EEPROM, so that it can - feature the saved color on startup. + This methods saves the current color, strobe and scroll preferences to the EEPROM. */ -void save_data(){ - // fix code so that it only saves when color has not changed for a while +void Bright_Lights::save_data(){ if (!data_saved && (millis() - last_save) > save_interval) { byte temp_array[] = {0,0}; int2bytes(hsb_vals[0], temp_array); - for (int i; i < 2; i++) { EEPROM.write(EEPROM_hsb_address[0]+i, temp_array[i]); } + for (int i; i < 2; i++) { EEPROM.write(EEPROM_addresses[0]+i, temp_array[i]); } int2bytes(hsb_vals[1], temp_array); - for (int i; i < 2; i++) { EEPROM.write(EEPROM_hsb_address[1]+i, temp_array[i]); } + for (int i; i < 2; i++) { EEPROM.write(EEPROM_addresses[1]+i, temp_array[i]); } int2bytes(hsb_vals[2], temp_array); - for (int i; i < 2; i++) { EEPROM.write(EEPROM_hsb_address[2]+i, temp_array[i]); } + for (int i; i < 2; i++) { EEPROM.write(EEPROM_addresses[2]+i, temp_array[i]); } int2bytes(scroll_speed, temp_array); - for (int i; i < 2; i++) { EEPROM.write(EEPROM_scroll_address[0]+i, temp_array[i]); } + for (int i; i < 2; i++) { EEPROM.write(EEPROM_addresses[3]+i, temp_array[i]); } int2bytes(scroll_direction, temp_array); - for (int i; i < 2; i++) { EEPROM.write(EEPROM_scroll_address[1]+i, temp_array[i]); } + for (int i; i < 2; i++) { EEPROM.write(EEPROM_addresses[4]+i, temp_array[i]); } int2bytes(scroll_width, temp_array); - for (int i; i < 2; i++) { EEPROM.write(EEPROM_scroll_address[2]+i, temp_array[i]); } + for (int i; i < 2; i++) { EEPROM.write(EEPROM_addresses[5]+i, temp_array[i]); } int2bytes(strobe_speed, temp_array); - for (int i; i < 2; i++) { EEPROM.write(EEPROM_strobe_address[0]+i, temp_array[i]); } + for (int i; i < 2; i++) { EEPROM.write(EEPROM_addresses[6]+i, temp_array[i]); } data_saved = true; last_save = millis(); @@ -36,56 +34,39 @@ void save_data(){ /********************* - LOAD COLORS - This methods loads the saved color from the EEPROM, so that it can - feature the saved color on startup. + LOAD DATA + This methods loads the saved color, scroll and strobe information from the EEPROM. */ -void load_data() { - byte load_array[2] = {EEPROM.read(EEPROM_hsb_address[0]), EEPROM.read(EEPROM_hsb_address[0]+1)}; +void Bright_Lights::load_data() { + byte load_array[2] = {0,0}; + + load_array[0] = EEPROM.read(EEPROM_addresses[0]); + load_array[1] = EEPROM.read(EEPROM_addresses[0]+1); hsb_vals[0] = bytes2int(load_array); - load_array[0] = EEPROM.read(EEPROM_hsb_address[1]); - load_array[1] = EEPROM.read(EEPROM_hsb_address[1]+1); + load_array[0] = EEPROM.read(EEPROM_addresses[1]); + load_array[1] = EEPROM.read(EEPROM_addresses[1]+1); hsb_vals[1] = bytes2int(load_array); - load_array[0] = EEPROM.read(EEPROM_hsb_address[2]); - load_array[1] = EEPROM.read(EEPROM_hsb_address[2]+1); + load_array[0] = EEPROM.read(EEPROM_addresses[2]); + load_array[1] = EEPROM.read(EEPROM_addresses[2]+1); hsb_vals[2] = bytes2int(load_array); convertHSB(); - load_array[0] = EEPROM.read(EEPROM_scroll_address[0]); - load_array[1] = EEPROM.read(EEPROM_scroll_address[0]+1); + load_array[0] = EEPROM.read(EEPROM_addresses[3]); + load_array[1] = EEPROM.read(EEPROM_addresses[3]+1); scroll_speed = bytes2int(load_array); - load_array[0] = EEPROM.read(EEPROM_scroll_address[1]); - load_array[1] = EEPROM.read(EEPROM_scroll_address[1]+1); + load_array[0] = EEPROM.read(EEPROM_addresses[4]); + load_array[1] = EEPROM.read(EEPROM_addresses[4]+1); scroll_direction = bytes2int(load_array); - load_array[0] = EEPROM.read(EEPROM_scroll_address[2]); - load_array[1] = EEPROM.read(EEPROM_scroll_address[2]+1); + load_array[0] = EEPROM.read(EEPROM_addresses[5]); + load_array[1] = EEPROM.read(EEPROM_addresses[5]+1); scroll_width = bytes2int(load_array); - load_array[0] = EEPROM.read(EEPROM_strobe_address[0]); - load_array[1] = EEPROM.read(EEPROM_strobe_address[0]+1); + load_array[0] = EEPROM.read(EEPROM_addresses[6]); + load_array[1] = EEPROM.read(EEPROM_addresses[6]+1); strobe_speed = bytes2int(load_array); } - -//void load_fun_mode() { -// byte fun_load[] = {EEPROM.read(EEPROM_scroll_address[0]), EEPROM.read(EEPROM_scroll_address[0]+1)}; -// scroll_speed = bytes2int(fun_load); -// -// fun_load[0] = EEPROM.read(EEPROM_scroll_address[1]); -// fun_load[1] = EEPROM.read(EEPROM_scroll_address[1]+1); -// scroll_direction = bytes2int(fun_load); -// -// fun_load[0] = EEPROM.read(EEPROM_scroll_address[2]); -// fun_load[1] = EEPROM.read(EEPROM_scroll_address[2]+1); -// scroll_width = bytes2int(fun_load); -// -// fun_load[0] = EEPROM.read(EEPROM_strobe_address[0]); -// fun_load[1] = EEPROM.read(EEPROM_strobe_address[0]+1); -// strobe_speed = bytes2int(fun_load); -//} - - diff --git a/Process_Color.pde b/Process_Color.pde index 63d707c..be6e9f2 100644 --- a/Process_Color.pde +++ b/Process_Color.pde @@ -7,7 +7,7 @@ which specific parameter from either of these modes is currently controlled by the potentiometer. */ -void soft_set_hsb_color(int active_control, int new_val, int min_val, int max_val) { +void Bright_Lights::soft_set_hsb_color(int active_control, int new_val, int min_val, int max_val) { if (active_control == 0) new_val = map(new_val, min_val, max_val, 0, 360); else new_val = map(new_val, min_val, max_val, 0, 100); @@ -16,14 +16,14 @@ void soft_set_hsb_color(int active_control, int new_val, int min_val, int max_va } } -void set_hsb_color(int active_control, int new_val, int min_val, int max_val) { +void Bright_Lights::set_hsb_color(int active_control, int new_val, int min_val, int max_val) { if (active_control == 0) new_val = map(new_val, min_val, max_val, 0, 360); else new_val = map(new_val, min_val, max_val, 0, 100); if (new_val < 3) new_val = 0; set_hsb_color(active_control, new_val); } -void set_hsb_color(int active_control, int new_val) { +void Bright_Lights::set_hsb_color(int active_control, int new_val) { hsb_vals[active_control] = new_val; convertHSB(); data_saved = false; @@ -33,7 +33,7 @@ void set_hsb_color(int active_control, int new_val) { CONVERT HSB Convert HSB colors into RGB colors. */ -void convertHSB() { +void Bright_Lights::convertHSB() { H2R_HSBtoRGB(hsb_vals[0], hsb_vals[1], hsb_vals[2], rgb_vals); for (int i = 0; i < NUM_color_ctrls; i++) rgb_vals[i] = map(rgb_vals[i], 0, 255, 0, LED_max_level); } diff --git a/Process_Fun_Mode.pde b/Process_Fun_Mode.pde index c072db6..def7fea 100644 --- a/Process_Fun_Mode.pde +++ b/Process_Fun_Mode.pde @@ -2,7 +2,7 @@ SOFT SET STROBE SPEED: sets the speed of the strobing lights with a soft takeover. Called by physical controls when initially switched to the strobe control mode. */ -void soft_set_strobe_speed (int new_val, int min_val, int max_val) { +void Bright_Lights::soft_set_strobe_speed (int new_val, int min_val, int max_val) { if (check_soft_takeover(strobe_speed, new_val)) { set_strobe_speed (new_val, min_val, max_val); } @@ -11,8 +11,8 @@ void soft_set_strobe_speed (int new_val, int min_val, int max_val) { /********************* SET STROBE SPEED: sets the speed of the strobing lights. */ -void set_strobe_speed (int new_val, int min_val, int max_val) { - strobe_speed = map(new_val, min_val, max_val, STROBE_inter_min, STROBE_inter_max); +void Bright_Lights::set_strobe_speed (int new_val, int min_val, int max_val) { + strobe_speed = map(new_val, min_val, max_val, STROBE_speed_min, STROBE_speed_max); data_saved = false; } @@ -20,7 +20,7 @@ void set_strobe_speed (int new_val, int min_val, int max_val) { SOFT SET SCROLL SPEED: sets the speed of the scrolling lights with a soft takeover. Called by physical controls when initially switched to the scroll control mode. */ -void soft_set_scroll_speed (int new_val, int min_val, int max_val) { +void Bright_Lights::soft_set_scroll_speed (int new_val, int min_val, int max_val) { if (check_soft_takeover(scroll_speed, new_val)) { set_scroll_speed (new_val, min_val, max_val); } @@ -29,8 +29,8 @@ void soft_set_scroll_speed (int new_val, int min_val, int max_val) { /********************* SET SCROLL SPEED: sets the speed of the scrolling lights. */ -void set_scroll_speed (int new_val, int min_val, int max_val) { - scroll_speed = map(new_val, min_val, max_val, SCROLL_inter_min, SCROLL_inter_max); +void Bright_Lights::set_scroll_speed (int new_val, int min_val, int max_val) { + scroll_speed = map(new_val, min_val, max_val, SCROLL_speed_min, SCROLL_speed_max); data_saved = false; } @@ -38,8 +38,8 @@ void set_scroll_speed (int new_val, int min_val, int max_val) { SET SCROLL DIRECTION: sets the direction of the scrolling lights. Possible directions include: (0) to the left; (1) to the right; (2) to the center; (3) from the center */ -void set_scroll_direction (int new_val, int min_val, int max_val) { - scroll_direction = map(new_val, min_val, max_val, 0, 4); +void Bright_Lights::set_scroll_direction (int new_val, int min_val, int max_val) { + scroll_direction = map(new_val, min_val, max_val, 0, 3); data_saved = false; } @@ -47,8 +47,8 @@ void set_scroll_direction (int new_val, int min_val, int max_val) { SET SCROLL WIDTH: sets how many of the leds will be on while scrolling. The width can range from 1 led is on, to all leds except 1 are one. */ -void set_scroll_width (int new_val, int min_val, int max_val) { - scroll_width = map(new_val, min_val, max_val, 1, (NUM_RGB_LED-1)); +void Bright_Lights::set_scroll_width (int new_val, int min_val, int max_val) { + scroll_width = map(new_val, min_val, max_val, SCROLL_width_min, SCROLL_width_max); data_saved = false; } @@ -57,7 +57,7 @@ void set_scroll_width (int new_val, int min_val, int max_val) { Controls the state of the lights when in strobe mode. This method is responsible for timing and setting the leds to turn and and off. */ -void strobe_active() { +void Bright_Lights::strobe_active() { long current_time = millis(); // determine if it is time to change the state of the lights and if so change the state flag @@ -78,11 +78,11 @@ void strobe_active() { Controls the lights when in scroll mode based. Responsible for timing and determining which lights should be turned on or off based on the current scroll direction, speed, and width. */ -void scroll_active() { +void Bright_Lights::scroll_active() { long current_time = millis(); boolean new_state = false; - // determine if it is time to the move the current scroll position, and if so move it + // determine if it is time to the move the current scroll position based on the direction if (current_time - scroll_last_switch > scroll_speed) { if (scroll_direction == 0) { scroll_led_pos++; @@ -128,7 +128,7 @@ void scroll_active() { SCROLL LED ARRAY The scroll LED array . */ -void scroll_led_array(int current_pos, int* led_array, int array_length) { +void Bright_Lights::scroll_led_array(int current_pos, int* led_array, int array_length) { for (int i = 0; i < array_length; i ++) { switch (scroll_direction) { case 0: @@ -153,8 +153,6 @@ void scroll_led_array(int current_pos, int* led_array, int array_length) { current_pos++; if (current_pos >= NUM_RGB_LED/2) current_pos = 0; break; - default: - break; } } } diff --git a/Process_Input.pde b/Process_Input.pde index 6c0bd0c..dcbf7b6 100644 --- a/Process_Input.pde +++ b/Process_Input.pde @@ -5,10 +5,11 @@ is routed to the appropriate functions that control the current mode and state of the color, or fun mode. */ -void handle_physical_input() { +void Bright_Lights::handle_physical_input() { // check current mode based on switch state for(int i = 0; i < NUM_switches; i ++) { if (switches[i].available()) { + if (i <= 1) { new_mode = true; int cur_state = switches[i].get_state(); if (i == ID_strobe_switch && cur_state == HIGH) { @@ -16,18 +17,22 @@ void handle_physical_input() { } else if (i == ID_color_switch && cur_state == HIGH) { active_mode = MODE_color; select_color_param_for_physical_ctrl(); - } else if (switches[i].get_state() == LOW) { - active_mode = MODE_off; + } else if ((i == ID_color_switch && cur_state == LOW) || (i == ID_strobe_switch && cur_state == LOW) ) { +// active_mode = MODE_off; + serial_write(155); + } + } } } - // check pot state and route value to appropriate function if (pot.available()) { if (active_mode == MODE_strobe) { soft_set_strobe_speed(pot.get_state(), POT_output_min, POT_output_max); } else if (active_mode == MODE_scroll) { soft_set_scroll_speed(pot.get_state(), POT_output_min, POT_output_max);} else if (active_mode == MODE_color) { soft_set_hsb_color(p_control_hsb, pot.get_state(), POT_output_min, POT_output_max); } } + + // save data if changes have been made to color and light mode } @@ -37,7 +42,7 @@ void handle_physical_input() { The state of the color_control variable determines whether we are alternating between controls for different RGB, or HSB values. */ -void select_color_param_for_physical_ctrl() { +void Bright_Lights::select_color_param_for_physical_ctrl() { p_control_hsb++; if (p_control_hsb >= NUM_color_ctrls) p_control_hsb = 0; blink_delay(p_control_hsb+1); @@ -49,7 +54,7 @@ void select_color_param_for_physical_ctrl() { This method is called whenever the switch is toggled to the fun mode select side. When method is called it toggles between the strobe and scroll mode. */ -int select_fun_mode_for_physical_ctrl() { +int Bright_Lights::select_fun_mode_for_physical_ctrl() { if (p_control_strobe_scroll == MODE_strobe) p_control_strobe_scroll = MODE_scroll; else p_control_strobe_scroll = MODE_strobe; return p_control_strobe_scroll; @@ -60,10 +65,7 @@ int select_fun_mode_for_physical_ctrl() { CHECK SOFT TAKEOVER Enables soft takeover behavior when switching the parameter that the potentiometer is controlling. */ -bool soft_takeover_complete = false; -int takeover_direction = 0; - -boolean check_soft_takeover(int old_val, int new_val) { +bool Bright_Lights::check_soft_takeover(int old_val, int new_val) { if (new_mode) { soft_takeover_complete = false; diff --git a/Process_Output.pde b/Process_Output.pde index a329cca..2aa61e2 100644 --- a/Process_Output.pde +++ b/Process_Output.pde @@ -4,11 +4,12 @@ Based on active mode status, it either turns on the lights, turns off the lights, or calls the fun_mode_on method to handle the fun mode. */ -void control_lights() { +void Bright_Lights::control_lights() { if (active_mode == MODE_off) lights_off_all(); else if (active_mode == MODE_color) lights_on_all(); else if (active_mode == MODE_strobe) strobe_active(); else if (active_mode == MODE_scroll) scroll_active(); + } @@ -19,7 +20,7 @@ void control_lights() { ** Realtime mode enables a remote device to control the lights on bright words using a serial connection. */ -void lights_on_realtime(byte* new_data) { +void Bright_Lights::lights_on_realtime(byte* new_data) { Tlc.clear(); for (int i = 0; i < 24; i++) { Tlc.set(rgb_pins[i], map(int(new_data[i]), 0, 127, 0, LED_max_level)); @@ -32,7 +33,8 @@ void lights_on_realtime(byte* new_data) { LIGHTS ON ALL Turns on all lights to the current color set in the rgb_vals array. */ -void lights_on_all() { +void Bright_Lights::lights_on_all() { +// serial_write(byte(131)); Tlc.clear(); for (int i = 0; i < NUM_RGB_LED; i++) { int led_offset = i * 3; @@ -49,7 +51,7 @@ void lights_on_all() { Turns on a single light to the current color saved in the rgb_vals array and turns off all other lights. */ -void lights_on_single_only(int current_led) { +void Bright_Lights::lights_on_single_only(int current_led) { Tlc.clear(); for (int i = 0; i < NUM_RGB_LED; i++) { if (current_led == i) { @@ -67,7 +69,7 @@ void lights_on_single_only(int current_led) { LIGHTS ON MULTIPLE Turns on all the lights on the led array */ -void lights_on_multiple(int* led_array, int array_length) { +void Bright_Lights::lights_on_multiple(int* led_array, int array_length) { Tlc.clear(); for (int j = 0; j < array_length; j++) { @@ -85,7 +87,7 @@ void lights_on_multiple(int* led_array, int array_length) { Turns on a single light to current color saved in the rgb_vals array, while leaving all other lights unchanged. */ -void lights_on_single(int current_led) { +void Bright_Lights::lights_on_single(int current_led) { int led_offset = current_led * 3; Tlc.set(rgb_pins[led_offset+0], rgb_vals[R]); Tlc.set(rgb_pins[led_offset+1], rgb_vals[G]); @@ -98,7 +100,7 @@ void lights_on_single(int current_led) { LIGHTS OFF ALL Turns all lights. */ -void lights_off_all() { +void Bright_Lights::lights_off_all() { Tlc.clear(); Tlc.update(); } @@ -108,7 +110,7 @@ void lights_off_all() { LIGHTS OFF SINGLE Turns off a single lights only. */ -void lights_off_single(int current_led) { +void Bright_Lights::lights_off_single(int current_led) { int led_offset = current_led * 3; Tlc.set(rgb_pins[led_offset+0], 0); Tlc.set(rgb_pins[led_offset+1], 0); @@ -123,7 +125,7 @@ void lights_off_single(int current_led) { Method is used to identify which color parameter is being controlled, when the parameter is changed. */ -void blink_delay(int blinks) { +void Bright_Lights::blink_delay(int blinks) { for (int i = 0; i < blinks; i++) { lights_on_all(); delay(400); diff --git a/Serial_Com.pde b/Serial_Com.pde index f64e6e5..27dfbd0 100644 --- a/Serial_Com.pde +++ b/Serial_Com.pde @@ -1,31 +1,9 @@ -// CONSTANTS: message sizes -#define MSG_LEN_realtime NUM_RGB_LED*3 -#define MSG_LEN_color 9 -#define MSG_LEN_scroll 3 -#define MSG_LEN_strobe 1 -#define MSG_LEN_longest MSG_LEN_realtime - -// CONSTANTS: message header types -#define CONNECT_MSG_confirm 255 -#define STATUS_MSG_request 254 -#define MODE_MSG_realtime 253 -#define MODE_MSG_off 252 -#define SET_MSG_hsb 129 -#define MODE_MSG_color_hsb 192 -#define MODE_MSG_strobe 194 -#define MODE_MSG_scroll 193 -#define END_MSG 128 - - -byte msg_type; -byte serial_msg[MSG_LEN_realtime]; -int byte_count = 0; -bool reading_msg_flag = false; // CONSIDER USING END BYTE TO SIMPLIFY THIS METHOD. // then I can just read until the end byte is received, and then send the message to the parse // message method to sort out what actions to take in response. -void handle_serial() { +void Bright_Lights::handle_serial() { + if (Serial.available() || blueSerial.available()){ while(Serial.available() || blueSerial.available()) { byte new_byte; @@ -76,7 +54,8 @@ void handle_serial() { } } -void parse_serial_msg(byte msg_header, byte* msg_body) { +void Bright_Lights::parse_serial_msg(byte msg_header, byte* msg_body) { + switch(msg_header) { case MODE_MSG_realtime: active_mode = MODE_realtime; @@ -94,11 +73,14 @@ void parse_serial_msg(byte msg_header, byte* msg_body) { break; case MODE_MSG_color_hsb: + serial_write(msg_header); + serial_write(active_mode); // set the active_mode to color mode if (active_mode != MODE_color) { new_mode = true; } active_mode = MODE_color; + serial_write(active_mode); // go through the message and convert the bites to integers for (int i = 0; i < 3; i++){ byte temp_byte_array[] = {0,0,0}; @@ -106,28 +88,32 @@ void parse_serial_msg(byte msg_header, byte* msg_body) { for (int j = 0; j < 3; j++){ temp_byte_array[j] = msg_body[index_offset + j]; } set_hsb_color(i, bytes2int_127(temp_byte_array), 0, 1000); } + serial_write(active_mode); + break; case MODE_MSG_scroll: if (active_mode != MODE_scroll) { new_mode = true; - active_mode = MODE_scroll; } + active_mode = MODE_scroll; set_scroll_speed(int(msg_body[0]), 0, 127); - set_scroll_direction(int(msg_body[1]), 0, 4); + set_scroll_direction(int(msg_body[1]), 0, 3); set_scroll_width(int(msg_body[2]), 0, 127); break; case MODE_MSG_strobe: if (active_mode != MODE_strobe) { new_mode = true; - active_mode = MODE_strobe; } + active_mode = MODE_strobe; set_strobe_speed(int(msg_body[0]), 0, 127); break; default: break; } + serial_write(active_mode); + } //void serial_println(char* string_print) { @@ -135,12 +121,12 @@ void parse_serial_msg(byte msg_header, byte* msg_body) { // Serial.println(string_print); //} -void serial_write(byte send_byte) { +void Bright_Lights::serial_write(byte send_byte) { blueSerial.print(send_byte, BYTE); Serial.print(send_byte, BYTE); } -void send_status_message() { +void Bright_Lights::send_status_message() { // initialize byte array that will hold converted integers byte converted_int[] = {0,0,0}; @@ -161,11 +147,11 @@ void send_status_message() { for (int i = 0; i < 3; i++) serial_write(converted_int[i]); // send serial message with strobe header and status - int2bytes_127(map(strobe_speed, STROBE_inter_min, STROBE_inter_max, 0, 127), converted_int); + int2bytes_127(map(strobe_speed, STROBE_speed_min, STROBE_speed_max, 0, 127), converted_int); for (int i = 0; i < 3; i++) serial_write(converted_int[i]); // send serial message with scroll header and status - int2bytes_127(map(scroll_speed, SCROLL_inter_min, SCROLL_inter_max, 0, 127), converted_int); + int2bytes_127(map(scroll_speed, SCROLL_speed_min, SCROLL_speed_max, 0, 127), converted_int); for (int i = 0; i < 3; i++) serial_write(converted_int[i]); serial_write(byte(scroll_direction)); int2bytes_127(map(scroll_width, 0, (NUM_RGB_LED-1), 0, 127), converted_int);