ardupilot/libraries/AP_Notify/ToshibaLED.cpp

305 lines
9.6 KiB
C++
Raw Normal View History

2013-08-09 00:17:29 -03:00
/*
ToshibaLED driver
*/
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
2013-08-09 00:17:29 -03:00
*/
#include <AP_HAL.h>
#include <AP_GPS.h>
2013-08-09 00:17:29 -03:00
#include "ToshibaLED.h"
#include "AP_Notify.h"
2013-08-09 00:17:29 -03:00
extern const AP_HAL::HAL& hal;
void ToshibaLED::init()
{
_healthy = hw_init();
2013-08-09 00:17:29 -03:00
}
// set_rgb - set color as a combination of red, green and blue values
void ToshibaLED::set_rgb(uint8_t red, uint8_t green, uint8_t blue)
{
// return immediately if not enabled
if (!_healthy) {
2013-08-09 00:17:29 -03:00
return;
}
if (red != _red_curr ||
green != _green_curr ||
blue != _blue_curr) {
// call the hardware update routine
if (hw_set_rgb(red, green, blue)) {
2013-08-13 23:50:52 -03:00
_red_curr = red;
2013-08-09 00:17:29 -03:00
_green_curr = green;
_blue_curr = blue;
}
}
}
// _scheduled_update - updates _red, _green, _blue according to notify flags
void ToshibaLED::update_colours(void)
2013-08-09 00:17:29 -03:00
{
uint8_t brightness = TOSHIBA_LED_BRIGHT;
// slow rate from 50Hz to 10hz
2013-08-13 23:50:52 -03:00
counter++;
if (counter < 5) {
2013-08-09 00:17:29 -03:00
return;
}
2013-08-13 23:50:52 -03:00
// reset counter
counter = 0;
// move forward one step
step++;
if (step >= 10) {
2013-08-13 23:50:52 -03:00
step = 0;
}
2013-08-09 00:17:29 -03:00
// use dim light when connected through USB
if (hal.gpio->usb_connected()) {
brightness = TOSHIBA_LED_DIM;
}
2013-08-09 00:17:29 -03:00
// initialising pattern
2013-08-13 23:50:52 -03:00
if (AP_Notify::flags.initialising) {
if (step & 1) {
// odd steps display red light
_red_des = brightness;
2013-08-13 23:50:52 -03:00
_blue_des = TOSHIBA_LED_OFF;
_green_des = TOSHIBA_LED_OFF;
} else {
2013-08-13 23:50:52 -03:00
// even display blue light
_red_des = TOSHIBA_LED_OFF;
_blue_des = brightness;
2013-08-13 23:50:52 -03:00
_green_des = TOSHIBA_LED_OFF;
2013-08-09 00:17:29 -03:00
}
2013-08-13 23:50:52 -03:00
// exit so no other status modify this pattern
2013-08-09 00:17:29 -03:00
return;
}
2013-08-13 23:50:52 -03:00
// save trim and esc calibration pattern
if (AP_Notify::flags.save_trim || AP_Notify::flags.esc_calibration) {
switch(step) {
2013-08-09 00:17:29 -03:00
case 0:
2013-08-13 23:50:52 -03:00
case 3:
case 6:
// red on
_red_des = brightness;
2013-08-09 00:17:29 -03:00
_blue_des = TOSHIBA_LED_OFF;
_green_des = TOSHIBA_LED_OFF;
break;
case 1:
2013-08-13 23:50:52 -03:00
case 4:
case 7:
// blue on
2013-08-09 00:17:29 -03:00
_red_des = TOSHIBA_LED_OFF;
_blue_des = brightness;
2013-08-09 00:17:29 -03:00
_green_des = TOSHIBA_LED_OFF;
break;
case 2:
2013-08-13 23:50:52 -03:00
case 5:
case 8:
// green on
2013-08-09 00:17:29 -03:00
_red_des = TOSHIBA_LED_OFF;
_blue_des = TOSHIBA_LED_OFF;
_green_des = brightness;
2013-08-09 00:17:29 -03:00
break;
2013-08-13 23:50:52 -03:00
case 9:
// all off
_red_des = TOSHIBA_LED_OFF;
_blue_des = TOSHIBA_LED_OFF;
_green_des = TOSHIBA_LED_OFF;
break;
}
// exit so no other status modify this pattern
return;
}
2014-07-21 07:12:53 -03:00
// radio and battery failsafe patter: flash yellow
// gps failsafe pattern : flashing yellow and blue
// baro glitching pattern : flashing yellow and purple
2014-07-21 07:12:53 -03:00
// ekf_bad pattern : flashing yellow and red
if (AP_Notify::flags.failsafe_radio || AP_Notify::flags.failsafe_battery ||
AP_Notify::flags.failsafe_gps || AP_Notify::flags.gps_glitching ||
AP_Notify::flags.baro_glitching ||
2014-07-21 07:12:53 -03:00
AP_Notify::flags.ekf_bad) {
switch(step) {
case 0:
case 1:
case 2:
case 3:
case 4:
// yellow on
_red_des = brightness;
_blue_des = TOSHIBA_LED_OFF;
_green_des = brightness;
break;
case 5:
case 6:
case 7:
case 8:
case 9:
if (AP_Notify::flags.failsafe_gps || AP_Notify::flags.gps_glitching) {
2014-07-21 07:12:53 -03:00
// blue on for gps failsafe or glitching
_red_des = TOSHIBA_LED_OFF;
_blue_des = brightness;
2014-07-21 07:12:53 -03:00
_green_des = TOSHIBA_LED_OFF;
} else if (AP_Notify::flags.baro_glitching) {
// purple on if baro glitching
_red_des = brightness;
_blue_des = brightness;
_green_des = TOSHIBA_LED_OFF;
2014-07-21 07:12:53 -03:00
} else if (AP_Notify::flags.ekf_bad) {
// red on if ekf bad
_red_des = brightness;
_blue_des = TOSHIBA_LED_OFF;
_green_des = TOSHIBA_LED_OFF;
}else{
2014-07-21 07:12:53 -03:00
// all off for radio or battery failsafe
_red_des = TOSHIBA_LED_OFF;
_blue_des = TOSHIBA_LED_OFF;
2014-07-21 07:12:53 -03:00
_green_des = TOSHIBA_LED_OFF;
}
2013-08-13 23:50:52 -03:00
break;
2013-08-09 00:17:29 -03:00
}
2013-08-13 23:50:52 -03:00
// exit so no other status modify this pattern
2013-08-09 00:17:29 -03:00
return;
}
2013-08-13 23:50:52 -03:00
// solid green or flashing green if armed
if (AP_Notify::flags.armed) {
// solid green if armed with GPS 3d lock
if (AP_Notify::flags.gps_status >= AP_GPS::GPS_OK_FIX_3D) {
2013-08-13 23:50:52 -03:00
_red_des = TOSHIBA_LED_OFF;
_blue_des = TOSHIBA_LED_OFF;
_green_des = brightness;
2013-08-09 00:17:29 -03:00
}else{
// solid blue if armed with no GPS lock
_red_des = TOSHIBA_LED_OFF;
_blue_des = brightness;
_green_des = TOSHIBA_LED_OFF;
}
return;
}else{
// double flash yellow if failing pre-arm checks
if (!AP_Notify::flags.pre_arm_check) {
2013-08-13 23:50:52 -03:00
switch(step) {
case 0:
case 1:
case 4:
case 5:
// yellow on
_red_des = brightness;
2013-08-13 23:50:52 -03:00
_blue_des = TOSHIBA_LED_OFF;
_green_des = brightness;
2013-08-13 23:50:52 -03:00
break;
case 2:
case 3:
2013-08-13 23:50:52 -03:00
case 6:
case 7:
case 8:
case 9:
// all off
2013-08-13 23:50:52 -03:00
_red_des = TOSHIBA_LED_OFF;
_blue_des = TOSHIBA_LED_OFF;
_green_des = TOSHIBA_LED_OFF;
break;
2013-08-09 00:17:29 -03:00
}
}else{
// flashing green if disarmed with GPS 3d lock
// flashing blue if disarmed with no gps lock
2013-08-13 23:50:52 -03:00
switch(step) {
case 0:
if (AP_Notify::flags.gps_status >= AP_GPS::GPS_OK_FIX_3D_DGPS) {
_green_des = brightness;
}
break;
2013-08-13 23:50:52 -03:00
case 1:
if (AP_Notify::flags.gps_status >= AP_GPS::GPS_OK_FIX_3D_DGPS) {
_green_des = TOSHIBA_LED_OFF;
}
break;
case 2:
if (AP_Notify::flags.gps_status >= AP_GPS::GPS_OK_FIX_3D_DGPS) {
_green_des = brightness;
}
break;
case 3:
if (AP_Notify::flags.gps_status >= AP_GPS::GPS_OK_FIX_3D_DGPS) {
_green_des = TOSHIBA_LED_OFF;
}
break;
case 4:
_red_des = TOSHIBA_LED_OFF;
if (AP_Notify::flags.gps_status >= AP_GPS::GPS_OK_FIX_3D) {
// flashing green if disarmed with GPS 3d lock
_blue_des = TOSHIBA_LED_OFF;
_green_des = brightness;
}else{
// flashing blue if disarmed with no gps lock
_blue_des = brightness;
_green_des = TOSHIBA_LED_OFF;
}
break;
case 5:
if (AP_Notify::flags.gps_status >= AP_GPS::GPS_OK_FIX_3D_DGPS) {
_green_des = TOSHIBA_LED_OFF;
}
break;
2013-08-13 23:50:52 -03:00
case 6:
if (AP_Notify::flags.gps_status >= AP_GPS::GPS_OK_FIX_3D_DGPS) {
_green_des = brightness;
}
break;
2013-08-13 23:50:52 -03:00
case 7:
if (AP_Notify::flags.gps_status >= AP_GPS::GPS_OK_FIX_3D_DGPS) {
_green_des = TOSHIBA_LED_OFF;
}
break;
2013-08-13 23:50:52 -03:00
case 8:
if (AP_Notify::flags.gps_status >= AP_GPS::GPS_OK_FIX_3D_DGPS) {
_green_des = brightness;
}
break;
2013-08-13 23:50:52 -03:00
case 9:
// all off
2013-08-13 23:50:52 -03:00
_red_des = TOSHIBA_LED_OFF;
_blue_des = TOSHIBA_LED_OFF;
_green_des = TOSHIBA_LED_OFF;
break;
}
2013-08-09 00:17:29 -03:00
}
}
}
// update - updates led according to timed_updated. Should be called
// at 50Hz
2013-08-09 00:17:29 -03:00
void ToshibaLED::update()
{
// return immediately if not enabled
if (!_healthy) {
2013-08-09 00:17:29 -03:00
return;
}
update_colours();
set_rgb(_red_des, _green_des, _blue_des);
}