APM:Libraries
ExternalLED.cpp
Go to the documentation of this file.
1 /*
2  This program is free software: you can redistribute it and/or modify
3  it under the terms of the GNU General Public License as published by
4  the Free Software Foundation, either version 3 of the License, or
5  (at your option) any later version.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program. If not, see <http://www.gnu.org/licenses/>.
14  */
15 #include "ExternalLED.h"
16 
17 #include "AP_Notify.h"
18 
19 #include <AP_HAL/AP_HAL.h>
20 
21 #if (defined(EXTERNAL_LED_ARMED) && defined(EXTERNAL_LED_GPS) && \
22  defined(EXTERNAL_LED_MOTOR1) && defined(EXTERNAL_LED_MOTOR2))
23 
24 extern const AP_HAL::HAL& hal;
25 
26 bool ExternalLED::init(void)
27 {
28  // return immediately if disabled
29  if (!AP_Notify::flags.external_leds) {
30  return false;
31  }
32 
33  // setup the main LEDs as outputs
38 
39  // turn leds off
44  return true;
45 }
46 
47 /*
48  main update function called at 50Hz
49  */
50 void ExternalLED::update(void)
51 {
52  // return immediately if disabled
53  if (!AP_Notify::flags.external_leds) {
54  return;
55  }
56 
57  // reduce update rate from 50hz to 10hz
58  _counter++;
59  if (_counter < 5) {
60  return;
61  }
62  _counter = 0;
63 
64  // internal counter used to control step of armed and gps led
65  _counter2++;
66  if (_counter2 >= 10) {
67  _counter2 = 0;
68  }
69 
70  // initialising
71  if (AP_Notify::flags.initialising) {
72  // blink arming and gps leds at 5hz
73  switch(_counter2) {
74  case 0:
75  case 2:
76  case 4:
77  case 6:
78  case 8:
79  armed_led(true);
80  gps_led(false);
81  break;
82  case 1:
83  case 3:
84  case 5:
85  case 7:
86  case 9:
87  armed_led(false);
88  gps_led(true);
89  break;
90  }
91  return;
92  }
93 
94  // arming led control
95  if (AP_Notify::flags.armed) {
96  armed_led(true);
97  }else{
98  // blink arming led at 2hz
99  switch(_counter2) {
100  case 0:
101  case 1:
102  case 2:
103  case 5:
104  case 6:
105  case 7:
106  armed_led(false);
107  break;
108  case 3:
109  case 4:
110  case 8:
111  case 9:
112  armed_led(true);
113  break;
114  }
115  }
116 
117  // GPS led control
118  switch (AP_Notify::flags.gps_status) {
119  case 0:
120  // no GPS attached
121  gps_led(false);
122  break;
123  case 1:
124  case 2:
125  // GPS attached but no lock, blink at 4Hz
126  switch(_counter2) { // Pattern: 3(off), 2(on), 3(off), 2(on), repeat
127  case 0:
128  case 1:
129  case 2:
130  case 5:
131  case 6:
132  case 7:
133  gps_led(false);
134  break;
135  case 3:
136  case 4:
137  case 8:
138  case 9:
139  gps_led(true);
140  break;
141  }
142  break;
143  case 3:
144  // solid blue on gps lock
145  gps_led(true);
146  break;
147  }
148 
149  // motor led control
150  // if we are displaying a pattern complete it
151  if (_pattern != NONE) {
153  switch(_pattern) {
154  case NONE:
155  // do nothing
156  break;
157  case FAST_FLASH:
158  switch(_pattern_counter) {
159  case 1:
160  case 3:
161  case 5:
162  case 7:
163  case 9:
164  motor_led1(true);
165  motor_led2(true);
166  break;
167  case 2:
168  case 4:
169  case 6:
170  case 8:
171  motor_led1(false);
172  motor_led2(false);
173  break;
174  case 10:
175  motor_led1(false);
176  motor_led2(false);
177  set_pattern(NONE);
178  break;
179  }
180  break;
181  case OSCILLATE:
182  switch(_pattern_counter) {
183  case 1:
184  motor_led1(true);
185  motor_led2(false);
186  break;
187  case 4:
188  motor_led1(false);
189  motor_led2(true);
190  break;
191  case 6:
192  set_pattern(NONE);
193  break;
194  }
195  break;
196  }
197  }else{
198  if (AP_Notify::flags.failsafe_battery || AP_Notify::flags.failsafe_radio) {
199  // radio or battery failsafe indicated by fast flashing
201  } else {
202  // otherwise do whatever the armed led is doing
205  }
206  }
207 }
208 
209 // set_pattern - sets pattern for motor leds
210 void ExternalLED::set_pattern(LEDPattern pattern_id)
211 {
212  _pattern = pattern_id;
213  _pattern_counter = 0;
214 }
215 
216 // armed_led - set armed light on or off
217 void ExternalLED::armed_led(bool on_off)
218 {
219  if (_flags.armedled_on != on_off) {
220  _flags.armedled_on = on_off;
222  }
223 }
224 
225 // gps_led - set gps light on or off
226 void ExternalLED::gps_led(bool on_off)
227 {
228  if (_flags.gpsled_on != on_off) {
229  _flags.gpsled_on = on_off;
231  }
232 }
233 
234 // motor_led - set motor light on or off
235 void ExternalLED::motor_led1(bool on_off)
236 {
237  if (_flags.motorled1_on != on_off) {
238  _flags.motorled1_on = on_off;
240  }
241 }
242 
243 // motor_led - set motor light on or off
244 void ExternalLED::motor_led2(bool on_off)
245 {
246  if (_flags.motorled2_on != on_off) {
247  _flags.motorled2_on = on_off;
249  }
250 }
251 #else
252 bool ExternalLED::init(void) {return true;}
253 void ExternalLED::update(void) {return;}
254 #endif
#define EXTERNAL_LED_MOTOR1
Definition: vrbrain.h:48
uint8_t _pattern_counter
Definition: ExternalLED.h:59
struct ExternalLED::copterleds_flag_type _flags
void motor_led1(bool on_off)
LEDPattern _pattern
Definition: ExternalLED.h:58
uint8_t _counter2
Definition: ExternalLED.h:57
bool init(void)
const AP_HAL::HAL & hal
Definition: UARTDriver.cpp:37
virtual void write(uint8_t pin, uint8_t value)=0
virtual void pinMode(uint8_t pin, uint8_t output)=0
void set_pattern(LEDPattern pattern_id)
#define HAL_GPIO_LED_OFF
Definition: chibios.h:21
#define HAL_GPIO_OUTPUT
Definition: GPIO.h:8
void gps_led(bool on_off)
uint8_t _counter
Definition: ExternalLED.h:56
static struct notify_flags_and_values_type flags
Definition: AP_Notify.h:117
void armed_led(bool on_off)
AP_HAL::GPIO * gpio
Definition: HAL.h:111
#define EXTERNAL_LED_ARMED
Definition: vrbrain.h:47
#define EXTERNAL_LED_MOTOR2
Definition: vrbrain.h:49
#define EXTERNAL_LED_GPS
Definition: vrbrain.h:46
void update(void)
void motor_led2(bool on_off)