2011-07-17 07:32:00 -03:00
|
|
|
static void update_lights()
|
2011-06-16 14:03:26 -03:00
|
|
|
{
|
|
|
|
switch(led_mode){
|
|
|
|
case NORMAL_LEDS:
|
|
|
|
update_motor_light();
|
|
|
|
update_GPS_light();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AUTO_TRIM_LEDS:
|
|
|
|
dancing_light();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static void update_GPS_light(void)
|
2011-06-16 14:03:26 -03:00
|
|
|
{
|
|
|
|
// GPS LED on if we have a fix or Blink GPS LED if we are receiving data
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
switch (g_gps->status()){
|
|
|
|
|
|
|
|
case(2):
|
2012-01-23 18:10:03 -04:00
|
|
|
if(home_is_set) { // JLN update
|
|
|
|
digitalWrite(C_LED_PIN, LED_ON); //Turn LED C on when gps has valid fix AND home is set.
|
|
|
|
} else {
|
|
|
|
digitalWrite(C_LED_PIN, LED_OFF);
|
|
|
|
}
|
2011-06-16 14:03:26 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case(1):
|
|
|
|
if (g_gps->valid_read == true){
|
|
|
|
GPS_light = !GPS_light; // Toggle light on and off to indicate gps messages being received, but no GPS fix lock
|
|
|
|
if (GPS_light){
|
2011-11-12 23:44:36 -04:00
|
|
|
digitalWrite(C_LED_PIN, LED_OFF);
|
2011-06-16 14:03:26 -03:00
|
|
|
}else{
|
2011-11-12 23:44:36 -04:00
|
|
|
digitalWrite(C_LED_PIN, LED_ON);
|
2011-06-16 14:03:26 -03:00
|
|
|
}
|
|
|
|
g_gps->valid_read = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-11-12 23:44:36 -04:00
|
|
|
digitalWrite(C_LED_PIN, LED_OFF);
|
2011-06-16 14:03:26 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static void update_motor_light(void)
|
2011-06-16 14:03:26 -03:00
|
|
|
{
|
2012-04-04 10:57:50 -03:00
|
|
|
if(motors.armed() == false){
|
2011-06-16 14:03:26 -03:00
|
|
|
motor_light = !motor_light;
|
|
|
|
|
|
|
|
// blink
|
|
|
|
if(motor_light){
|
2011-11-12 23:44:36 -04:00
|
|
|
digitalWrite(A_LED_PIN, LED_ON);
|
2011-06-16 14:03:26 -03:00
|
|
|
}else{
|
2011-11-12 23:44:36 -04:00
|
|
|
digitalWrite(A_LED_PIN, LED_OFF);
|
2011-06-16 14:03:26 -03:00
|
|
|
}
|
|
|
|
}else{
|
|
|
|
if(!motor_light){
|
|
|
|
motor_light = true;
|
2011-11-12 23:44:36 -04:00
|
|
|
digitalWrite(A_LED_PIN, LED_ON);
|
2011-06-16 14:03:26 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static void dancing_light()
|
2011-06-16 14:03:26 -03:00
|
|
|
{
|
|
|
|
static byte step;
|
|
|
|
|
|
|
|
if (step++ == 3)
|
|
|
|
step = 0;
|
|
|
|
|
|
|
|
switch(step)
|
|
|
|
{
|
|
|
|
case 0:
|
2011-11-12 23:44:36 -04:00
|
|
|
digitalWrite(C_LED_PIN, LED_OFF);
|
|
|
|
digitalWrite(A_LED_PIN, LED_ON);
|
2011-06-16 14:03:26 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2011-11-12 23:44:36 -04:00
|
|
|
digitalWrite(A_LED_PIN, LED_OFF);
|
|
|
|
digitalWrite(B_LED_PIN, LED_ON);
|
2011-06-16 14:03:26 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2011-11-12 23:44:36 -04:00
|
|
|
digitalWrite(B_LED_PIN, LED_OFF);
|
|
|
|
digitalWrite(C_LED_PIN, LED_ON);
|
2011-06-16 14:03:26 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-07-17 07:32:00 -03:00
|
|
|
static void clear_leds()
|
2011-06-16 14:03:26 -03:00
|
|
|
{
|
2011-11-12 23:44:36 -04:00
|
|
|
digitalWrite(A_LED_PIN, LED_OFF);
|
|
|
|
digitalWrite(B_LED_PIN, LED_OFF);
|
|
|
|
digitalWrite(C_LED_PIN, LED_OFF);
|
2011-09-04 21:15:36 -03:00
|
|
|
motor_light = false;
|
2011-08-14 15:56:31 -03:00
|
|
|
led_mode = NORMAL_LEDS;
|
2011-06-16 14:03:26 -03:00
|
|
|
}
|
|
|
|
|
2012-04-12 10:54:14 -03:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copter LEDS by Robert Lefebvre
|
2012-05-15 13:00:21 -03:00
|
|
|
// Based on the work of U4eake, Bill Sanford, Max Levine, and Oliver
|
2012-04-12 10:54:14 -03:00
|
|
|
// g.copter_leds_mode controls the copter leds function via bitmath
|
2012-05-15 13:00:21 -03:00
|
|
|
// Zeroeth bit turns motor leds on and off: 00000001
|
2012-04-12 10:54:14 -03:00
|
|
|
// First bit turns GPS function on and off: 00000010
|
|
|
|
// Second bit turns Aux function on and off: 00000100
|
2012-05-15 13:00:21 -03:00
|
|
|
// Third bit turns on Beeper (legacy Piezo) function: 00001000
|
|
|
|
// Fourth bit toggles between Fast Flash or Oscillate on Low Battery: 000100000 (0) does Fast Flash, (1) does Oscillate
|
2012-04-12 10:54:14 -03:00
|
|
|
// This code is written in order to be backwards compatible with the old Motor_LEDS code
|
|
|
|
// I hope to include at least some of the Show_LEDS code in the future
|
|
|
|
// copter_leds_GPS_blink controls the blinking of the GPS LEDS
|
|
|
|
// copter_leds_motor_blink controls the blinking of the motor LEDS
|
2012-05-15 13:00:21 -03:00
|
|
|
// Piezo Code and beeps once on Startup to verify operation
|
|
|
|
// Piezo Enables Tone on reaching low battery or current alert
|
2012-04-12 10:54:14 -03:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
#if COPTER_LEDS == ENABLED
|
|
|
|
static void update_copter_leds(void)
|
2011-06-16 14:03:26 -03:00
|
|
|
{
|
2012-05-15 13:00:21 -03:00
|
|
|
if (g.copter_leds_mode == 0) {
|
|
|
|
copter_leds_reset(); //method of reintializing LED state
|
2012-04-12 10:54:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( bitRead(g.copter_leds_mode, 0) ) {
|
|
|
|
if (motors.armed() == true) {
|
|
|
|
if (low_batt == true) {
|
2012-05-15 13:00:21 -03:00
|
|
|
if ( bitRead(g.copter_leds_mode, 4 )) {
|
|
|
|
copter_leds_oscillate(); //if motors are armed, but battery level is low, motor leds fast blink
|
|
|
|
}else{
|
|
|
|
copter_leds_fast_blink(); //if motors are armed, but battery level is low, motor leds oscillate
|
|
|
|
}
|
2012-04-12 10:54:14 -03:00
|
|
|
} else {
|
2012-05-15 13:00:21 -03:00
|
|
|
copter_leds_on(); //if motors are armed, battery level OK, all motor leds ON
|
2011-09-16 22:56:51 -03:00
|
|
|
}
|
2012-04-12 10:54:14 -03:00
|
|
|
} else {
|
2012-05-15 13:00:21 -03:00
|
|
|
copter_leds_slow_blink(); //if motors are not armed, blink motor leds
|
2012-04-12 10:54:14 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( bitRead(g.copter_leds_mode, 1) ){
|
|
|
|
|
|
|
|
// GPS LED on if we have a fix or Blink GPS LED if we are receiving data
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
switch (g_gps->status()){
|
|
|
|
|
|
|
|
case(2):
|
|
|
|
if(home_is_set) {
|
2012-05-15 13:00:21 -03:00
|
|
|
copter_leds_GPS_on(); //Turn GPS LEDs on when gps has valid fix AND home is set
|
2012-04-12 10:54:14 -03:00
|
|
|
} else {
|
2012-05-15 13:00:21 -03:00
|
|
|
copter_leds_GPS_fast_blink(); //if GPS has fix, but home is not set, blink GPS LED fast
|
2012-04-12 10:54:14 -03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case(1):
|
|
|
|
|
2012-05-15 13:00:21 -03:00
|
|
|
copter_leds_GPS_slow_blink(); //if GPS has valid reads, but no fix, blink GPS LED slow
|
2012-04-12 10:54:14 -03:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2012-05-15 13:00:21 -03:00
|
|
|
copter_leds_GPS_off(); //if no valid GPS signal, turn GPS LED off
|
2012-04-12 10:54:14 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( bitRead(g.copter_leds_mode, 2) ) {
|
|
|
|
if (200 <= g.rc_7.control_in && g.rc_7.control_in < 400) {
|
2012-05-15 13:00:21 -03:00
|
|
|
copter_leds_aux_on(); //if sub-control of Ch7 is high, turn Aux LED on
|
2012-04-12 10:54:14 -03:00
|
|
|
} else if (g.rc_7.control_in < 200) {
|
2012-05-15 13:00:21 -03:00
|
|
|
copter_leds_aux_off(); //if sub-control of Ch7 is low, turn Aux LED off
|
2012-04-12 10:54:14 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void copter_leds_reset(void) {
|
2012-05-15 11:07:03 -03:00
|
|
|
digitalWrite(COPTER_LED_1, COPTER_LED_OFF);
|
2012-05-15 13:00:21 -03:00
|
|
|
digitalWrite(COPTER_LED_2, COPTER_LED_OFF);
|
2012-05-15 11:07:03 -03:00
|
|
|
digitalWrite(COPTER_LED_3, COPTER_LED_OFF);
|
|
|
|
digitalWrite(COPTER_LED_4, COPTER_LED_OFF);
|
|
|
|
digitalWrite(COPTER_LED_5, COPTER_LED_OFF);
|
|
|
|
digitalWrite(COPTER_LED_6, COPTER_LED_OFF);
|
|
|
|
digitalWrite(COPTER_LED_7, COPTER_LED_OFF);
|
|
|
|
digitalWrite(COPTER_LED_8, COPTER_LED_OFF);
|
2012-04-12 10:54:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void copter_leds_on(void) {
|
2012-05-15 13:00:21 -03:00
|
|
|
if ( !bitRead(g.copter_leds_mode, 2) ){
|
|
|
|
digitalWrite(COPTER_LED_1, COPTER_LED_ON);
|
|
|
|
}
|
|
|
|
#if CONFIG_APM_HARDWARE == APM_HARDWARE_APM2
|
|
|
|
if ( !bitRead(g.copter_leds_mode, 3) ){
|
|
|
|
digitalWrite(COPTER_LED_2, COPTER_LED_ON);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
digitalWrite(COPTER_LED_2, COPTER_LED_ON);
|
|
|
|
#endif
|
|
|
|
if ( !bitRead(g.copter_leds_mode, 1) ){
|
|
|
|
digitalWrite(COPTER_LED_3, COPTER_LED_ON);
|
|
|
|
}
|
|
|
|
digitalWrite(COPTER_LED_4, COPTER_LED_ON);
|
2012-05-15 11:07:03 -03:00
|
|
|
digitalWrite(COPTER_LED_5, COPTER_LED_ON);
|
|
|
|
digitalWrite(COPTER_LED_6, COPTER_LED_ON);
|
|
|
|
digitalWrite(COPTER_LED_7, COPTER_LED_ON);
|
|
|
|
digitalWrite(COPTER_LED_8, COPTER_LED_ON);
|
2012-04-12 10:54:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void copter_leds_off(void) {
|
2012-05-15 13:00:21 -03:00
|
|
|
if ( !bitRead(g.copter_leds_mode, 2) ){
|
|
|
|
digitalWrite(COPTER_LED_1, COPTER_LED_OFF);
|
|
|
|
}
|
|
|
|
#if CONFIG_APM_HARDWARE == APM_HARDWARE_APM2
|
|
|
|
if ( !bitRead(g.copter_leds_mode, 3) ){
|
2012-05-15 11:07:03 -03:00
|
|
|
digitalWrite(COPTER_LED_2, COPTER_LED_OFF);
|
|
|
|
}
|
2012-05-15 13:00:21 -03:00
|
|
|
#else
|
|
|
|
digitalWrite(COPTER_LED_2, COPTER_LED_OFF);
|
|
|
|
#endif
|
2012-04-12 10:54:14 -03:00
|
|
|
if ( !bitRead(g.copter_leds_mode, 1) ){
|
2012-05-15 13:00:21 -03:00
|
|
|
digitalWrite(COPTER_LED_3, COPTER_LED_OFF);
|
2012-04-12 10:54:14 -03:00
|
|
|
}
|
2012-05-15 13:00:21 -03:00
|
|
|
digitalWrite(COPTER_LED_4, COPTER_LED_OFF);
|
|
|
|
digitalWrite(COPTER_LED_5, COPTER_LED_OFF);
|
|
|
|
digitalWrite(COPTER_LED_6, COPTER_LED_OFF);
|
|
|
|
digitalWrite(COPTER_LED_7, COPTER_LED_OFF);
|
|
|
|
digitalWrite(COPTER_LED_8, COPTER_LED_OFF);
|
2012-04-12 10:54:14 -03:00
|
|
|
}
|
|
|
|
|
2012-05-15 13:00:21 -03:00
|
|
|
static void copter_leds_slow_blink(void) {
|
2012-04-12 10:54:14 -03:00
|
|
|
copter_leds_motor_blink++; // this increments once every 1/10 second because it is in the 10hz loop
|
|
|
|
if ( 0 < copter_leds_motor_blink && copter_leds_motor_blink < 6 ){ // when the counter reaches 5 (1/2 sec), then toggle the leds
|
|
|
|
copter_leds_on();
|
|
|
|
}else if (5 < copter_leds_motor_blink && copter_leds_motor_blink < 11){
|
|
|
|
copter_leds_off();
|
|
|
|
}
|
|
|
|
else copter_leds_motor_blink = 0; // start blink cycle again
|
|
|
|
}
|
|
|
|
|
2012-05-15 13:00:21 -03:00
|
|
|
static void copter_leds_fast_blink(void) {
|
|
|
|
copter_leds_motor_blink++; // this increments once every 1/10 second because it is in the 10hz loop
|
|
|
|
if ( 0 < copter_leds_motor_blink && copter_leds_motor_blink < 3 ){ // when the counter reaches 3 (1/5 sec), then toggle the leds
|
|
|
|
copter_leds_on();
|
|
|
|
}else if (2 < copter_leds_motor_blink && copter_leds_motor_blink < 5){
|
|
|
|
copter_leds_off();
|
|
|
|
}
|
|
|
|
else copter_leds_motor_blink = 0; // start blink cycle again
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-12 10:54:14 -03:00
|
|
|
static void copter_leds_oscillate(void) {
|
|
|
|
copter_leds_motor_blink++; // this increments once every 1/10 second because it is in the 10hz loop
|
|
|
|
if ( 0 < copter_leds_motor_blink && copter_leds_motor_blink < 3 ) { // when the counter reaches 3 (1/5 sec), then toggle the leds
|
2012-05-15 13:00:21 -03:00
|
|
|
if ( !bitRead(g.copter_leds_mode, 2) ){
|
|
|
|
digitalWrite(COPTER_LED_1, COPTER_LED_ON);
|
|
|
|
}
|
|
|
|
#if CONFIG_APM_HARDWARE == APM_HARDWARE_APM2
|
|
|
|
if ( !bitRead(g.copter_leds_mode, 3) ){
|
|
|
|
digitalWrite(COPTER_LED_2, COPTER_LED_ON);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
digitalWrite(COPTER_LED_2, COPTER_LED_ON);
|
|
|
|
#endif
|
|
|
|
if ( !bitRead(g.copter_leds_mode, 1) ){
|
|
|
|
digitalWrite(COPTER_LED_3, COPTER_LED_OFF);
|
|
|
|
}
|
|
|
|
digitalWrite(COPTER_LED_4, COPTER_LED_OFF);
|
2012-05-15 11:07:03 -03:00
|
|
|
digitalWrite(COPTER_LED_5, COPTER_LED_ON);
|
|
|
|
digitalWrite(COPTER_LED_6, COPTER_LED_ON);
|
|
|
|
digitalWrite(COPTER_LED_7, COPTER_LED_OFF);
|
|
|
|
digitalWrite(COPTER_LED_8, COPTER_LED_OFF);
|
2012-05-15 13:00:21 -03:00
|
|
|
}else if (2 < copter_leds_motor_blink && copter_leds_motor_blink < 5) {
|
|
|
|
if ( !bitRead(g.copter_leds_mode, 2) ){
|
|
|
|
digitalWrite(COPTER_LED_1, COPTER_LED_OFF);
|
2012-04-12 10:54:14 -03:00
|
|
|
}
|
2012-05-15 13:00:21 -03:00
|
|
|
#if CONFIG_APM_HARDWARE == APM_HARDWARE_APM2
|
|
|
|
if ( !bitRead(g.copter_leds_mode, 3) ){
|
|
|
|
digitalWrite(COPTER_LED_2, COPTER_LED_OFF);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
digitalWrite(COPTER_LED_2, COPTER_LED_OFF);
|
|
|
|
#endif
|
|
|
|
if ( !bitRead(g.copter_leds_mode, 1) ){
|
|
|
|
digitalWrite(COPTER_LED_3, COPTER_LED_ON);
|
2012-04-12 10:54:14 -03:00
|
|
|
}
|
2012-05-15 13:00:21 -03:00
|
|
|
digitalWrite(COPTER_LED_4, COPTER_LED_ON);
|
2012-05-15 11:07:03 -03:00
|
|
|
digitalWrite(COPTER_LED_5, COPTER_LED_OFF);
|
|
|
|
digitalWrite(COPTER_LED_6, COPTER_LED_OFF);
|
|
|
|
digitalWrite(COPTER_LED_7, COPTER_LED_ON);
|
|
|
|
digitalWrite(COPTER_LED_8, COPTER_LED_ON);
|
2011-06-16 14:03:26 -03:00
|
|
|
}
|
2012-04-12 10:54:14 -03:00
|
|
|
else copter_leds_motor_blink = 0; // start blink cycle again
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void copter_leds_GPS_on(void) {
|
2012-05-15 13:00:21 -03:00
|
|
|
digitalWrite(COPTER_LED_3, COPTER_LED_ON);
|
2011-06-16 14:03:26 -03:00
|
|
|
}
|
|
|
|
|
2012-04-12 10:54:14 -03:00
|
|
|
static void copter_leds_GPS_off(void) {
|
2012-05-15 13:00:21 -03:00
|
|
|
digitalWrite(COPTER_LED_3, COPTER_LED_OFF);
|
2012-04-12 10:54:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void copter_leds_GPS_slow_blink(void) {
|
|
|
|
copter_leds_GPS_blink++; // this increments once every 1/10 second because it is in the 10hz loop
|
|
|
|
if ( 0 < copter_leds_GPS_blink && copter_leds_GPS_blink < 6 ){ // when the counter reaches 5 (1/2 sec), then toggle the leds
|
|
|
|
copter_leds_GPS_off();
|
|
|
|
}else if (5 < copter_leds_GPS_blink && copter_leds_GPS_blink < 11){
|
|
|
|
copter_leds_GPS_on();
|
|
|
|
}
|
|
|
|
else copter_leds_GPS_blink = 0; // start blink cycle again
|
|
|
|
}
|
|
|
|
|
|
|
|
static void copter_leds_GPS_fast_blink(void) {
|
|
|
|
copter_leds_GPS_blink++; // this increments once every 1/10 second because it is in the 10hz loop
|
|
|
|
if ( 0 < copter_leds_GPS_blink && copter_leds_GPS_blink < 3 ) { // when the counter reaches 3 (1/5 sec), then toggle the leds
|
|
|
|
copter_leds_GPS_off();
|
|
|
|
}else if (2 < copter_leds_GPS_blink && copter_leds_GPS_blink < 5){
|
|
|
|
copter_leds_GPS_on();
|
|
|
|
}
|
|
|
|
else copter_leds_GPS_blink = 0; // start blink cycle again
|
|
|
|
}
|
|
|
|
|
2012-05-15 11:07:03 -03:00
|
|
|
static void copter_leds_aux_off(void){
|
2012-05-15 13:00:21 -03:00
|
|
|
digitalWrite(COPTER_LED_1, COPTER_LED_OFF);
|
2012-04-12 10:54:14 -03:00
|
|
|
}
|
|
|
|
|
2012-05-15 11:07:03 -03:00
|
|
|
static void copter_leds_aux_on(void){
|
2012-05-15 13:00:21 -03:00
|
|
|
digitalWrite(COPTER_LED_1, COPTER_LED_ON);
|
|
|
|
}
|
|
|
|
|
|
|
|
void piezo_on(){
|
|
|
|
digitalWrite(PIEZO_PIN,HIGH);
|
|
|
|
}
|
|
|
|
|
|
|
|
void piezo_off(){
|
|
|
|
digitalWrite(PIEZO_PIN,LOW);
|
|
|
|
}
|
|
|
|
|
|
|
|
void piezo_beep(){ // Note! This command should not be used in time sensitive loops
|
|
|
|
piezo_on();
|
|
|
|
delay(100);
|
|
|
|
piezo_off();
|
2012-04-12 10:54:14 -03:00
|
|
|
}
|
2011-07-10 21:47:08 -03:00
|
|
|
|
2012-04-12 10:54:14 -03:00
|
|
|
#endif //COPTER_LEDS
|