@@ -119,78 +119,104 @@ static void clear_leds()
119
119
// Piezo Enables Tone on reaching low battery or current alert
120
120
// ///////////////////////////////////////////////////////////////////////////////////////////
121
121
122
+ #define COPTER_LEDS_BITMASK_ENABLED 0x01 // bit #0
123
+ #define COPTER_LEDS_BITMASK_GPS 0x02 // bit #1
124
+ #define COPTER_LEDS_BITMASK_AUX 0x04 // bit #2
125
+ #define COPTER_LEDS_BITMASK_BEEPER 0x08 // bit #3
126
+ #define COPTER_LEDS_BITMASK_BATT_OSCILLATE 0x10 // bit #4
127
+ #define COPTER_LEDS_BITMASK_MOTOR_NAV_BLINK 0x20 // bit #5
128
+ #define COPTER_LEDS_BITMASK_GPS_NAV_BLINK 0x40 // bit #6
122
129
123
130
#if COPTER_LEDS == ENABLED
131
+ static void copter_leds_init (void )
132
+ {
133
+ pinMode (COPTER_LED_1, OUTPUT); // Motor LED
134
+ pinMode (COPTER_LED_2, OUTPUT); // Motor LED
135
+ pinMode (COPTER_LED_3, OUTPUT); // Motor LED
136
+ pinMode (COPTER_LED_4, OUTPUT); // Motor LED
137
+ pinMode (COPTER_LED_5, OUTPUT); // Motor or Aux LED
138
+ pinMode (COPTER_LED_6, OUTPUT); // Motor or Aux LED
139
+ pinMode (COPTER_LED_7, OUTPUT); // Motor or GPS LED
140
+ pinMode (COPTER_LED_8, OUTPUT); // Motor or GPS LED
141
+
142
+ if (!(g.copter_leds_mode & COPTER_LEDS_BITMASK_BEEPER)) {
143
+ piezo_beep ();
144
+ }
145
+ }
146
+
124
147
static void update_copter_leds (void )
125
148
{
126
149
if (g.copter_leds_mode == 0 ) {
127
150
copter_leds_reset (); // method of reintializing LED state
128
151
}
129
152
130
- if ( bitRead (g.copter_leds_mode , 0 ) ) {
131
- if (motors.armed () == true ) {
132
- if (ap.low_battery == true ) {
133
- if ( bitRead (g.copter_leds_mode , 4 )) {
153
+ // motor leds control
154
+ if (g.copter_leds_mode & COPTER_LEDS_BITMASK_ENABLED) {
155
+ if (motors.armed ()) {
156
+ if (ap.low_battery ) {
157
+ if (g.copter_leds_mode & COPTER_LEDS_BITMASK_BATT_OSCILLATE) {
134
158
copter_leds_oscillate (); // if motors are armed, but battery level is low, motor leds fast blink
135
159
} else {
136
160
copter_leds_fast_blink (); // if motors are armed, but battery level is low, motor leds oscillate
137
161
}
138
- } else if ( !bitRead (g.copter_leds_mode , 5 ) ) {
139
- copter_leds_on (); // if motors are armed, battery level OK, all motor leds ON
140
- } else if ( bitRead (g.copter_leds_mode , 5 ) ) {
141
- if ( copter_leds_nav_blink >0 ) {
142
- copter_leds_slow_blink (); // if nav command was seen, blink LEDs.
162
+ } else {
163
+ if (g.copter_leds_mode & COPTER_LEDS_BITMASK_MOTOR_NAV_BLINK) {
164
+ if (copter_leds_nav_blink > 0 ) {
165
+ copter_leds_slow_blink (); // if nav command was seen, blink LEDs.
166
+ } else {
167
+ copter_leds_on ();
168
+ }
143
169
} else {
144
- copter_leds_on ();
170
+ copter_leds_on (); // if motors are armed, battery level OK, all motor leds ON
145
171
}
146
172
}
147
173
} else {
148
174
copter_leds_slow_blink (); // if motors are not armed, blink motor leds
149
175
}
150
176
}
151
177
152
- if ( bitRead (g.copter_leds_mode , 1 ) ) {
178
+ // GPS led control
179
+ if (g.copter_leds_mode & COPTER_LEDS_BITMASK_GPS) {
153
180
154
181
// GPS LED on if we have a fix or Blink GPS LED if we are receiving data
155
182
// ---------------------------------------------------------------------
156
183
switch (g_gps->status ()) {
157
184
158
- case (2 ):
185
+ case GPS::NO_GPS:
186
+ copter_leds_GPS_off (); // if no valid GPS signal, turn GPS LED off
187
+ break ;
188
+
189
+ case GPS::NO_FIX:
190
+ copter_leds_GPS_slow_blink (); // if GPS has valid reads, but no fix, blink GPS LED slow
191
+ break ;
192
+
193
+ case GPS::GPS_OK_FIX_2D:
194
+ case GPS::GPS_OK_FIX_3D:
159
195
if (ap.home_is_set ) {
160
- if ( !bitRead (g.copter_leds_mode , 6 ) ) {
161
- copter_leds_GPS_on (); // Turn GPS LEDs on when gps has valid fix AND home is set
162
- } else if (bitRead (g.copter_leds_mode , 6 ) ) {
163
- if ( copter_leds_nav_blink >0 ) {
196
+ if (g.copter_leds_mode & COPTER_LEDS_BITMASK_GPS_NAV_BLINK) {
197
+ if (copter_leds_nav_blink >0 ) {
164
198
copter_leds_GPS_slow_blink (); // if nav command was seen, blink LEDs.
165
199
} else {
166
200
copter_leds_GPS_on ();
167
201
}
202
+ } else {
203
+ copter_leds_GPS_on (); // Turn GPS LEDs on when gps has valid fix AND home is set
168
204
}
169
205
} else {
170
206
copter_leds_GPS_fast_blink (); // if GPS has fix, but home is not set, blink GPS LED fast
171
207
}
172
208
break ;
173
-
174
- case (1 ):
175
-
176
- copter_leds_GPS_slow_blink (); // if GPS has valid reads, but no fix, blink GPS LED slow
177
-
178
- break ;
179
-
180
- default :
181
- copter_leds_GPS_off (); // if no valid GPS signal, turn GPS LED off
182
- break ;
183
209
}
184
210
}
185
211
186
- if ( bitRead (g.copter_leds_mode , 2 ) ) {
187
- if (200 <= g.rc_7 .control_in && g.rc_7 .control_in < 400 ) {
212
+ // AUX led control
213
+ if (g.copter_leds_mode & COPTER_LEDS_BITMASK_AUX) {
214
+ if (ap_system.CH7_flag ) {
188
215
copter_leds_aux_on (); // if sub-control of Ch7 is high, turn Aux LED on
189
- } else if (g. rc_7 . control_in < 200 ) {
216
+ } else {
190
217
copter_leds_aux_off (); // if sub-control of Ch7 is low, turn Aux LED off
191
218
}
192
219
}
193
-
194
220
}
195
221
196
222
static void copter_leds_reset (void ) {
@@ -205,17 +231,17 @@ static void copter_leds_reset(void) {
205
231
}
206
232
207
233
static void copter_leds_on (void ) {
208
- if ( ! bitRead (g.copter_leds_mode , 2 ) ) {
234
+ if (! (g.copter_leds_mode & COPTER_LEDS_BITMASK_AUX) ) {
209
235
digitalWriteFast (COPTER_LED_1, COPTER_LED_ON);
210
236
}
211
237
#if CONFIG_HAL_BOARD == HAL_BOARD_APM2
212
- if ( ! bitRead (g.copter_leds_mode , 3 ) ) {
238
+ if (! (g.copter_leds_mode & COPTER_LEDS_BITMASK_BEEPER) ) {
213
239
digitalWriteFast (COPTER_LED_2, COPTER_LED_ON);
214
240
}
215
241
#else
216
242
digitalWriteFast (COPTER_LED_2, COPTER_LED_ON);
217
243
#endif
218
- if ( ! bitRead (g.copter_leds_mode , 1 ) ) {
244
+ if (! (g.copter_leds_mode & COPTER_LEDS_BITMASK_GPS ) ) {
219
245
digitalWriteFast (COPTER_LED_3, COPTER_LED_ON);
220
246
}
221
247
digitalWriteFast (COPTER_LED_4, COPTER_LED_ON);
@@ -226,17 +252,17 @@ static void copter_leds_on(void) {
226
252
}
227
253
228
254
static void copter_leds_off (void ) {
229
- if ( ! bitRead (g.copter_leds_mode , 2 ) ) {
255
+ if (! (g.copter_leds_mode & COPTER_LEDS_BITMASK_AUX) ) {
230
256
digitalWriteFast (COPTER_LED_1, COPTER_LED_OFF);
231
257
}
232
258
#if CONFIG_HAL_BOARD == HAL_BOARD_APM2
233
- if ( ! bitRead (g.copter_leds_mode , 3 ) ) {
259
+ if (! (g.copter_leds_mode & COPTER_LEDS_BITMASK_BEEPER) ) {
234
260
digitalWriteFast (COPTER_LED_2, COPTER_LED_OFF);
235
261
}
236
262
#else
237
263
digitalWriteFast (COPTER_LED_2, COPTER_LED_OFF);
238
264
#endif
239
- if ( ! bitRead (g.copter_leds_mode , 1 ) ) {
265
+ if (! (g.copter_leds_mode & COPTER_LEDS_BITMASK_GPS) ) {
240
266
digitalWriteFast (COPTER_LED_3, COPTER_LED_OFF);
241
267
}
242
268
digitalWriteFast (COPTER_LED_4, COPTER_LED_OFF);
@@ -247,43 +273,47 @@ static void copter_leds_off(void) {
247
273
}
248
274
249
275
static void copter_leds_slow_blink (void ) {
250
- copter_leds_motor_blink++; // this increments once every 1/10 second because it is in the 10hz loop
251
- if ( 0 < copter_leds_motor_blink && copter_leds_motor_blink < 6 ) { // when the counter reaches 5 (1/2 sec), then toggle the leds
276
+ copter_leds_motor_blink++; // this increments once every 1/10 second because it is in the 10hz loop
277
+
278
+ if ( 0 < copter_leds_motor_blink && copter_leds_motor_blink < 6 ) { // when the counter reaches 5 (1/2 sec), then toggle the leds
252
279
copter_leds_off ();
253
- if ( bitRead (g.copter_leds_mode , 5 ) && !bitRead (g.copter_leds_mode , 6 ) && copter_leds_nav_blink >0 ) { // if blinking is called by the Nav Blinker...
254
- copter_leds_nav_blink--; // decrement the Nav Blink counter
280
+
281
+ // if blinking is called by the Nav Blinker decrement the Nav Blink counter
282
+ if ((g.copter_leds_mode & COPTER_LEDS_BITMASK_MOTOR_NAV_BLINK) && !(g.copter_leds_mode & COPTER_LEDS_BITMASK_GPS_NAV_BLINK) && copter_leds_nav_blink >0 ) {
283
+ copter_leds_nav_blink--;
255
284
}
256
285
}else if (5 < copter_leds_motor_blink && copter_leds_motor_blink < 11 ) {
257
286
copter_leds_on ();
287
+ }else {
288
+ copter_leds_motor_blink = 0 ; // start blink cycle again
258
289
}
259
- else copter_leds_motor_blink = 0 ; // start blink cycle again
260
290
}
261
291
262
- static void copter_leds_fast_blink (void ) {
263
- copter_leds_motor_blink++; // this increments once every 1/10 second because it is in the 10hz loop
264
- if ( 0 < copter_leds_motor_blink && copter_leds_motor_blink < 3 ) { // when the counter reaches 3 (1/5 sec), then toggle the leds
292
+ static void copter_leds_fast_blink (void ) {
293
+ copter_leds_motor_blink++; // this increments once every 1/10 second because it is in the 10hz loop
294
+ if ( 0 < copter_leds_motor_blink && copter_leds_motor_blink < 3 ) { // when the counter reaches 3 (1/5 sec), then toggle the leds
265
295
copter_leds_on ();
266
296
}else if (2 < copter_leds_motor_blink && copter_leds_motor_blink < 5 ) {
267
297
copter_leds_off ();
298
+ }else {
299
+ copter_leds_motor_blink = 0 ; // start blink cycle again
268
300
}
269
- else copter_leds_motor_blink = 0 ; // start blink cycle again
270
301
}
271
302
272
-
273
303
static void copter_leds_oscillate (void ) {
274
- copter_leds_motor_blink++; // this increments once every 1/10 second because it is in the 10hz loop
275
- if ( 0 < copter_leds_motor_blink && copter_leds_motor_blink < 3 ) { // when the counter reaches 3 (1/5 sec), then toggle the leds
276
- if ( !bitRead (g.copter_leds_mode , 2 ) ) {
304
+ copter_leds_motor_blink++; // this increments once every 1/10 second because it is in the 10hz loop
305
+ if ( 0 < copter_leds_motor_blink && copter_leds_motor_blink < 3 ) { // when the counter reaches 3 (1/5 sec), then toggle the leds
306
+ if ( !(g.copter_leds_mode & COPTER_LEDS_BITMASK_AUX) ) {
277
307
digitalWriteFast (COPTER_LED_1, COPTER_LED_ON);
278
308
}
279
309
#if CONFIG_HAL_BOARD == HAL_BOARD_APM2
280
- if ( !bitRead (g.copter_leds_mode , 3 ) ) {
310
+ if ( !(g.copter_leds_mode & COPTER_LEDS_BITMASK_BEEPER) ) {
281
311
digitalWriteFast (COPTER_LED_2, COPTER_LED_ON);
282
312
}
283
313
#else
284
314
digitalWriteFast (COPTER_LED_2, COPTER_LED_ON);
285
315
#endif
286
- if ( !bitRead (g.copter_leds_mode , 1 ) ) {
316
+ if ( !(g.copter_leds_mode & COPTER_LEDS_BITMASK_GPS) ) {
287
317
digitalWriteFast (COPTER_LED_3, COPTER_LED_OFF);
288
318
}
289
319
digitalWriteFast (COPTER_LED_4, COPTER_LED_OFF);
@@ -292,30 +322,29 @@ static void copter_leds_oscillate(void) {
292
322
digitalWriteFast (COPTER_LED_7, COPTER_LED_OFF);
293
323
digitalWriteFast (COPTER_LED_8, COPTER_LED_OFF);
294
324
}else if (2 < copter_leds_motor_blink && copter_leds_motor_blink < 5 ) {
295
- if ( !bitRead (g.copter_leds_mode , 2 ) ) {
325
+ if ( !(g.copter_leds_mode & COPTER_LEDS_BITMASK_AUX) ) {
296
326
digitalWriteFast (COPTER_LED_1, COPTER_LED_OFF);
297
327
}
298
328
#if CONFIG_HAL_BOARD == HAL_BOARD_APM2
299
- if ( !bitRead (g.copter_leds_mode , 3 ) ) {
329
+ if ( !(g.copter_leds_mode & COPTER_LEDS_BITMASK_BEEPER) ) {
300
330
digitalWriteFast (COPTER_LED_2, COPTER_LED_OFF);
301
331
}
302
332
#else
303
333
digitalWriteFast (COPTER_LED_2, COPTER_LED_OFF);
304
334
#endif
305
- if ( !bitRead (g.copter_leds_mode , 1 ) ) {
335
+ if ( !(g.copter_leds_mode & COPTER_LEDS_BITMASK_GPS ) ) {
306
336
digitalWriteFast (COPTER_LED_3, COPTER_LED_ON);
307
337
}
308
338
digitalWriteFast (COPTER_LED_4, COPTER_LED_ON);
309
339
digitalWriteFast (COPTER_LED_5, COPTER_LED_OFF);
310
340
digitalWriteFast (COPTER_LED_6, COPTER_LED_OFF);
311
341
digitalWriteFast (COPTER_LED_7, COPTER_LED_ON);
312
342
digitalWriteFast (COPTER_LED_8, COPTER_LED_ON);
343
+ }else {
344
+ copter_leds_motor_blink = 0 ; // start blink cycle again
313
345
}
314
- else copter_leds_motor_blink = 0 ; // start blink cycle again
315
346
}
316
347
317
-
318
-
319
348
static void copter_leds_GPS_on (void ) {
320
349
digitalWriteFast (COPTER_LED_3, COPTER_LED_ON);
321
350
}
@@ -328,7 +357,7 @@ static void copter_leds_GPS_slow_blink(void) {
328
357
copter_leds_GPS_blink++; // this increments once every 1/10 second because it is in the 10hz loop
329
358
if ( 0 < copter_leds_GPS_blink && copter_leds_GPS_blink < 6 ) { // when the counter reaches 5 (1/2 sec), then toggle the leds
330
359
copter_leds_GPS_off ();
331
- if ( bitRead (g.copter_leds_mode , 6 ) && copter_leds_nav_blink >0 ) { // if blinking is called by the Nav Blinker...
360
+ if ((g.copter_leds_mode & COPTER_LEDS_BITMASK_GPS_NAV_BLINK ) && copter_leds_nav_blink >0 ) { // if blinking is called by the Nav Blinker...
332
361
copter_leds_nav_blink--; // decrement the Nav Blink counter
333
362
}
334
363
}else if (5 < copter_leds_GPS_blink && copter_leds_GPS_blink < 11 ) {
@@ -356,17 +385,31 @@ static void copter_leds_aux_on(void){
356
385
}
357
386
358
387
void piezo_on (){
359
- digitalWriteFast (PIEZO_PIN,HIGH);
388
+ if (g.copter_leds_mode & COPTER_LEDS_BITMASK_BEEPER) {
389
+ digitalWriteFast (PIEZO_PIN,HIGH);
390
+ }
360
391
}
361
392
362
393
void piezo_off (){
363
- digitalWriteFast (PIEZO_PIN,LOW);
394
+ if (g.copter_leds_mode & COPTER_LEDS_BITMASK_BEEPER) {
395
+ digitalWriteFast (PIEZO_PIN,LOW);
396
+ }
364
397
}
365
398
366
399
void piezo_beep (){ // Note! This command should not be used in time sensitive loops
367
- piezo_on ();
368
- delay (100 );
369
- piezo_off ();
400
+ if (g.copter_leds_mode & COPTER_LEDS_BITMASK_BEEPER) {
401
+ piezo_on ();
402
+ delay (100 );
403
+ piezo_off ();
404
+ }
405
+ }
406
+
407
+ void piezo_beep_twice (){ // Note! This command should not be used in time sensitive loops
408
+ if (g.copter_leds_mode & COPTER_LEDS_BITMASK_BEEPER) {
409
+ piezo_beep ();
410
+ delay (50 );
411
+ piezo_beep ();
412
+ }
370
413
}
371
414
372
415
#endif // COPTER_LEDS
0 commit comments