ASMESDC2018
/home/swerve/Github/sdc_2018/omni_robot/LED.cpp
Go to the documentation of this file.
1 
8 #include <Arduino.h>
9 
10 #include "LED.h"
11 #include "Logger.h"
12 
13 LED::LED(int pin_left_red, int pin_left_green, int pin_left_blue,
14  int pin_right_red, int pin_right_green, int pin_right_blue) {
15 
16  setup(pin_left_red, pin_left_green, pin_left_blue,
17  pin_right_red, pin_right_green, pin_right_blue);
18 
19 }
20 
21 void LED::setup(int pin_left_red, int pin_left_green, int pin_left_blue,
22  int pin_right_red, int pin_right_green, int pin_right_blue) {
23 
24  int min_val = MIN_COLOR_VALUE;
25 
26  pins.PIN_LEFT_RED = pin_left_red;
27  pins.PIN_LEFT_GREEN = pin_left_green;
28  pins.PIN_LEFT_BLUE = pin_left_blue;
29  pins.PIN_RIGHT_RED = pin_right_red;
30  pins.PIN_RIGHT_GREEN = pin_right_green;
31  pins.PIN_RIGHT_BLUE = pin_right_blue;
32 
33  pinMode(pins.PIN_LEFT_RED, OUTPUT);
34  pinMode(pins.PIN_LEFT_GREEN, OUTPUT);
35  pinMode(pins.PIN_LEFT_BLUE, OUTPUT);
36  pinMode(pins.PIN_RIGHT_RED, OUTPUT);
37  pinMode(pins.PIN_RIGHT_GREEN, OUTPUT);
38  pinMode(pins.PIN_RIGHT_BLUE, OUTPUT);
39 
40 #ifdef COMMON_ANODE
41  min_val = MAX_COLOR_VALUE;
42 #endif
43 
44  _setColor(min_val, min_val, min_val, SIDE_BOTH);
45  state.led = -1;
46 
47 
48 }
49 
50 void LED::setState(int des_state, bool blink, int side) {
51 
52  if ((des_state < MIN_STATE_VAL) || (des_state > MAX_STATE_VAL)) {
54  return;
55  }
56  state.is_blink = blink;
57  state.side = side;
58 
59  if (state.led == des_state) {
60  if (blink) {
61  _updateBlink();
62  }
63  } else {
64  state.led = des_state;
65  _updateState();
66  }
67 
68 }
69 
70 void LED::_updateState(void) {
71 
72  int red_value = COLOR_NONE;
73  int green_value = COLOR_NONE;
74  int blue_value = COLOR_NONE;
75 
76  int des_state = state.led;
77 
78  if ((state.is_blink == true) && (state.blink == BLINK_OFF)) {
79  des_state = COLOR_NONE;
80  }
81 
82  switch (des_state) {
83  case COLOR_NONE:
84  red_value = MIN_COLOR_VALUE;
85  green_value = MIN_COLOR_VALUE;
86  blue_value = MIN_COLOR_VALUE;
87  logger::displayDebug("LED color set to: Off");
88  break;
89  case COLOR_RED:
90  red_value = MAX_COLOR_VALUE;
91  green_value = MIN_COLOR_VALUE;
92  blue_value = MIN_COLOR_VALUE;
93  logger::displayDebug("LED color set to: Red");
94  break;
95  case COLOR_YELLOW:
96  red_value = MAX_COLOR_VALUE;
97  green_value = int(MAX_COLOR_VALUE/6.0);
98  blue_value = MIN_COLOR_VALUE;
99  logger::displayDebug("LED color set to: Yellow");
100  break;
101  case COLOR_GREEN:
102  red_value = MIN_COLOR_VALUE;
103  green_value = MAX_COLOR_VALUE;
104  blue_value = MIN_COLOR_VALUE;
105  logger::displayDebug("LED color set to: Green");
106  break;
107  }
108 
109 #ifdef COMMON_ANODE
110  red_value = MAX_COLOR_VALUE - red_value;
111  green_value = MAX_COLOR_VALUE - green_value;
112  blue_value = MAX_COLOR_VALUE - blue_value;
113 #endif
114 
115  _setColor(red_value, green_value, blue_value, state.side);
116 
117  state.red = red_value;
118  state.green = green_value;
119  state.blue = blue_value;
120 
121  state.blink_start = millis();
122 
123 }
124 
125 void LED::_updateBlink(void) {
126 
127  if ((millis() - state.blink_start) > BLINK_SPEED) {
128  state.blink = !state.blink;
129  _updateState();
130  }
131 
132 }
133 
134 void LED::_setColor(int red_value, int green_value, int blue_value, int side) {
135 
136  int min_val = MIN_COLOR_VALUE;
137 
138 #ifdef COMMON_ANODE
139  min_val = MAX_COLOR_VALUE;
140 #endif
141 
142  if (side == SIDE_BOTH) {
143  analogWrite(pins.PIN_LEFT_RED, red_value);
144  analogWrite(pins.PIN_LEFT_GREEN, green_value);
145  analogWrite(pins.PIN_LEFT_BLUE, blue_value);
146  analogWrite(pins.PIN_RIGHT_RED, red_value);
147  analogWrite(pins.PIN_RIGHT_GREEN, green_value);
148  analogWrite(pins.PIN_RIGHT_BLUE, blue_value);
149  logger::displayDebug("LED displayed on both sides");
150  } else if (side == SIDE_LEFT) {
151  analogWrite(pins.PIN_LEFT_RED, red_value);
152  analogWrite(pins.PIN_LEFT_GREEN, green_value);
153  analogWrite(pins.PIN_LEFT_BLUE, blue_value);
154  analogWrite(pins.PIN_RIGHT_RED, min_val);
155  analogWrite(pins.PIN_RIGHT_GREEN, min_val);
156  analogWrite(pins.PIN_RIGHT_BLUE, min_val);
157  logger::displayDebug("LED displayed on left side");
158  } else if (side == SIDE_RIGHT) {
159  analogWrite(pins.PIN_LEFT_RED, min_val);
160  analogWrite(pins.PIN_LEFT_GREEN, min_val);
161  analogWrite(pins.PIN_LEFT_BLUE, min_val);
162  analogWrite(pins.PIN_RIGHT_RED, red_value);
163  analogWrite(pins.PIN_RIGHT_GREEN, green_value);
164  analogWrite(pins.PIN_RIGHT_BLUE, blue_value);
165  logger::displayDebug("LED displayed on right side");
166  }
167 
168 }
169 
170 
int side
Definition: LED.h:36
void displayDebug(String)
int void displayInfo(String)
Definition: Logger.cpp:43
int PIN_RIGHT_RED
Definition: LED.h:46
uint32_t blink_start
Definition: LED.h:39
#define COLOR_YELLOW
Definition: LED.h:15
#define COLOR_NONE
Definition: LED.h:13
#define SIDE_BOTH
Definition: LED.h:27
int red
Definition: LED.h:33
int green
Definition: LED.h:34
#define BLINK_OFF
Definition: LED.h:23
#define SIDE_RIGHT
Definition: LED.h:29
int PIN_RIGHT_GREEN
Definition: LED.h:47
int displayError(int)
Definition: Logger.cpp:10
LED(int, int, int, int, int, int)
Definition: LED.cpp:13
#define LED_STATE_ERROR
Definition: Logger.h:20
#define COLOR_RED
Definition: LED.h:14
#define MIN_COLOR_VALUE
Definition: LED.h:20
int PIN_LEFT_RED
Definition: LED.h:43
int led
Definition: LED.h:32
Header file for error codes
void setState(int, bool blink=false, int side=SIDE_BOTH)
Definition: LED.cpp:50
int PIN_LEFT_BLUE
Definition: LED.h:45
#define COLOR_GREEN
Definition: LED.h:16
#define MAX_COLOR_VALUE
Definition: LED.h:21
#define MIN_STATE_VAL
Definition: LED.h:17
#define MAX_STATE_VAL
Definition: LED.h:18
#define SIDE_LEFT
Definition: LED.h:28
int PIN_RIGHT_BLUE
Definition: LED.h:48
LEDState state
Definition: LED.h:60
int PIN_LEFT_GREEN
Definition: LED.h:44
bool is_blink
Definition: LED.h:37
#define BLINK_SPEED
Definition: LED.h:25
void setup(int, int, int, int, int, int)
Definition: LED.cpp:21
Header file for LED class
LEDPins pins
Definition: LED.h:61
int blue
Definition: LED.h:35
bool blink
Definition: LED.h:38