2014-11-14 10:14:40 -04:00
|
|
|
/*
|
|
|
|
Generic RGBLed 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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2015-08-11 03:28:44 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_GPS/AP_GPS.h>
|
2014-12-26 00:05:15 -04:00
|
|
|
#include "RGBLed.h"
|
2014-11-14 10:14:40 -04:00
|
|
|
#include "AP_Notify.h"
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
RGBLed::RGBLed(uint8_t led_off, uint8_t led_bright, uint8_t led_medium, uint8_t led_dim):
|
|
|
|
_led_off(led_off),
|
|
|
|
_led_bright(led_bright),
|
|
|
|
_led_medium(led_medium),
|
|
|
|
_led_dim(led_dim)
|
|
|
|
{
|
|
|
|
|
2021-03-13 16:59:08 -04:00
|
|
|
}
|
2014-11-14 10:14:40 -04:00
|
|
|
|
|
|
|
// set_rgb - set color as a combination of red, green and blue values
|
2016-07-16 03:02:27 -03:00
|
|
|
void RGBLed::_set_rgb(uint8_t red, uint8_t green, uint8_t blue)
|
2014-11-14 10:14:40 -04:00
|
|
|
{
|
|
|
|
if (red != _red_curr ||
|
|
|
|
green != _green_curr ||
|
|
|
|
blue != _blue_curr) {
|
|
|
|
// call the hardware update routine
|
|
|
|
if (hw_set_rgb(red, green, blue)) {
|
|
|
|
_red_curr = red;
|
|
|
|
_green_curr = green;
|
|
|
|
_blue_curr = blue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:17:24 -03:00
|
|
|
RGBLed::rgb_source_t RGBLed::rgb_source() const
|
|
|
|
{
|
|
|
|
return rgb_source_t(pNotify->_rgb_led_override.get());
|
|
|
|
}
|
|
|
|
|
2016-07-16 03:02:27 -03:00
|
|
|
// set_rgb - set color as a combination of red, green and blue values
|
|
|
|
void RGBLed::set_rgb(uint8_t red, uint8_t green, uint8_t blue)
|
|
|
|
{
|
2018-08-13 23:17:24 -03:00
|
|
|
if (rgb_source() == mavlink) {
|
2016-07-16 03:02:27 -03:00
|
|
|
// don't set if in override mode
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_set_rgb(red, green, blue);
|
|
|
|
}
|
|
|
|
|
2018-08-13 08:42:28 -03:00
|
|
|
uint8_t RGBLed::get_brightness(void) const
|
2014-11-14 10:14:40 -04:00
|
|
|
{
|
|
|
|
uint8_t brightness = _led_bright;
|
2015-12-01 16:22:26 -04:00
|
|
|
|
|
|
|
switch (pNotify->_rgb_led_brightness) {
|
|
|
|
case RGB_LED_OFF:
|
|
|
|
brightness = _led_off;
|
|
|
|
break;
|
|
|
|
case RGB_LED_LOW:
|
|
|
|
brightness = _led_dim;
|
|
|
|
break;
|
|
|
|
case RGB_LED_MEDIUM:
|
|
|
|
brightness = _led_medium;
|
|
|
|
break;
|
|
|
|
case RGB_LED_HIGH:
|
|
|
|
brightness = _led_bright;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-11-14 10:14:40 -04:00
|
|
|
// use dim light when connected through USB
|
2015-12-01 16:22:26 -04:00
|
|
|
if (hal.gpio->usb_connected() && brightness > _led_dim) {
|
2014-11-14 10:14:40 -04:00
|
|
|
brightness = _led_dim;
|
|
|
|
}
|
2018-08-13 08:42:28 -03:00
|
|
|
return brightness;
|
|
|
|
}
|
2014-11-14 10:14:40 -04:00
|
|
|
|
2018-08-13 23:17:24 -03:00
|
|
|
uint32_t RGBLed::get_colour_sequence_obc(void) const
|
|
|
|
{
|
|
|
|
if (AP_Notify::flags.armed) {
|
|
|
|
return DEFINE_COLOUR_SEQUENCE_SOLID(RED);
|
|
|
|
}
|
|
|
|
return DEFINE_COLOUR_SEQUENCE_SOLID(GREEN);
|
|
|
|
}
|
|
|
|
|
2018-08-13 08:42:28 -03:00
|
|
|
// _scheduled_update - updates _red, _green, _blue according to notify flags
|
|
|
|
uint32_t RGBLed::get_colour_sequence(void) const
|
|
|
|
{
|
2014-11-14 10:14:40 -04:00
|
|
|
// initialising pattern
|
|
|
|
if (AP_Notify::flags.initialising) {
|
2018-08-13 08:42:28 -03:00
|
|
|
return sequence_initialising;
|
2014-11-14 10:14:40 -04:00
|
|
|
}
|
2018-08-13 08:42:28 -03:00
|
|
|
|
2021-03-13 16:59:08 -04:00
|
|
|
// save trim or any calibration pattern
|
|
|
|
if (AP_Notify::flags.save_trim ||
|
|
|
|
AP_Notify::flags.esc_calibration ||
|
|
|
|
AP_Notify::flags.compass_cal_running ||
|
|
|
|
AP_Notify::flags.temp_cal_running) {
|
2018-08-13 08:42:28 -03:00
|
|
|
return sequence_trim_or_esc;
|
2014-11-14 10:14:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// radio and battery failsafe patter: flash yellow
|
|
|
|
// gps failsafe pattern : flashing yellow and blue
|
|
|
|
// ekf_bad pattern : flashing yellow and red
|
2018-08-13 08:42:28 -03:00
|
|
|
if (AP_Notify::flags.failsafe_radio ||
|
2019-10-31 19:10:36 -03:00
|
|
|
AP_Notify::flags.failsafe_gcs ||
|
2018-08-13 08:42:28 -03:00
|
|
|
AP_Notify::flags.failsafe_battery ||
|
|
|
|
AP_Notify::flags.ekf_bad ||
|
|
|
|
AP_Notify::flags.gps_glitching ||
|
|
|
|
AP_Notify::flags.leak_detected) {
|
|
|
|
|
|
|
|
if (AP_Notify::flags.leak_detected) {
|
|
|
|
// purple if leak detected
|
|
|
|
return sequence_failsafe_leak;
|
|
|
|
} else if (AP_Notify::flags.ekf_bad) {
|
|
|
|
// red on if ekf bad
|
|
|
|
return sequence_failsafe_ekf;
|
|
|
|
} else if (AP_Notify::flags.gps_glitching) {
|
|
|
|
// blue on gps glitch
|
|
|
|
return sequence_failsafe_gps_glitching;
|
2014-11-14 10:14:40 -04:00
|
|
|
}
|
2018-08-13 08:42:28 -03:00
|
|
|
// all off for radio or battery failsafe
|
|
|
|
return sequence_failsafe_radio_or_battery;
|
2014-11-14 10:14:40 -04:00
|
|
|
}
|
|
|
|
|
2014-12-24 09:23:00 -04:00
|
|
|
// solid green or blue if armed
|
2014-11-14 10:14:40 -04:00
|
|
|
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) {
|
2018-08-13 08:42:28 -03:00
|
|
|
return sequence_armed;
|
2014-11-14 10:14:40 -04:00
|
|
|
}
|
2018-08-13 08:42:28 -03:00
|
|
|
// solid blue if armed with no GPS lock
|
|
|
|
return sequence_armed_nogps;
|
|
|
|
}
|
2014-11-14 10:14:40 -04:00
|
|
|
|
2018-08-13 08:42:28 -03:00
|
|
|
// double flash yellow if failing pre-arm checks
|
|
|
|
if (!AP_Notify::flags.pre_arm_check) {
|
|
|
|
return sequence_prearm_failing;
|
|
|
|
}
|
|
|
|
if (AP_Notify::flags.gps_status >= AP_GPS::GPS_OK_FIX_3D_DGPS && AP_Notify::flags.pre_arm_gps_check) {
|
|
|
|
return sequence_disarmed_good_dgps;
|
|
|
|
}
|
2014-11-14 10:14:40 -04:00
|
|
|
|
2018-08-13 08:42:28 -03:00
|
|
|
if (AP_Notify::flags.gps_status >= AP_GPS::GPS_OK_FIX_3D && AP_Notify::flags.pre_arm_gps_check) {
|
|
|
|
return sequence_disarmed_good_gps;
|
2014-11-14 10:14:40 -04:00
|
|
|
}
|
2018-08-13 08:42:28 -03:00
|
|
|
|
|
|
|
return sequence_disarmed_bad_gps;
|
|
|
|
}
|
|
|
|
|
2019-08-29 18:04:17 -03:00
|
|
|
uint32_t RGBLed::get_colour_sequence_traffic_light(void) const
|
|
|
|
{
|
|
|
|
if (AP_Notify::flags.initialising) {
|
2020-09-03 10:32:40 -03:00
|
|
|
return DEFINE_COLOUR_SEQUENCE(RED,GREEN,BLUE,RED,GREEN,BLUE,RED,GREEN,BLUE,BLACK);
|
2019-08-29 18:04:17 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (AP_Notify::flags.armed) {
|
|
|
|
return DEFINE_COLOUR_SEQUENCE_SLOW(RED);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hal.util->safety_switch_state() != AP_HAL::Util::SAFETY_DISARMED) {
|
|
|
|
if (!AP_Notify::flags.pre_arm_check) {
|
2020-09-03 10:32:40 -03:00
|
|
|
return DEFINE_COLOUR_SEQUENCE_ALTERNATE(YELLOW, BLACK);
|
2019-08-29 18:04:17 -03:00
|
|
|
} else {
|
|
|
|
return DEFINE_COLOUR_SEQUENCE_SLOW(YELLOW);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AP_Notify::flags.pre_arm_check) {
|
2020-09-03 10:32:40 -03:00
|
|
|
return DEFINE_COLOUR_SEQUENCE_ALTERNATE(GREEN, BLACK);
|
2019-08-29 18:04:17 -03:00
|
|
|
}
|
|
|
|
return DEFINE_COLOUR_SEQUENCE_SLOW(GREEN);
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:17:24 -03:00
|
|
|
// update - updates led according to timed_updated. Should be called
|
|
|
|
// at 50Hz
|
|
|
|
void RGBLed::update()
|
2018-08-13 08:42:28 -03:00
|
|
|
{
|
2018-08-13 23:17:24 -03:00
|
|
|
uint32_t current_colour_sequence = 0;
|
2018-08-13 08:42:28 -03:00
|
|
|
|
2018-08-13 23:17:24 -03:00
|
|
|
switch (rgb_source()) {
|
|
|
|
case mavlink:
|
|
|
|
update_override();
|
|
|
|
return; // note this is a return not a break!
|
|
|
|
case standard:
|
|
|
|
current_colour_sequence = get_colour_sequence();
|
|
|
|
break;
|
|
|
|
case obc:
|
|
|
|
current_colour_sequence = get_colour_sequence_obc();
|
|
|
|
break;
|
2019-08-29 18:04:17 -03:00
|
|
|
case traffic_light:
|
|
|
|
current_colour_sequence = get_colour_sequence_traffic_light();
|
|
|
|
break;
|
2018-08-13 23:17:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t brightness = get_brightness();
|
2018-08-13 08:42:28 -03:00
|
|
|
|
2018-10-07 02:47:05 -03:00
|
|
|
uint8_t step = (AP_HAL::millis()/100) % 10;
|
2018-08-13 08:42:28 -03:00
|
|
|
|
2018-10-07 02:47:05 -03:00
|
|
|
// ensure we can't skip a step even with awful timing
|
|
|
|
if (step != last_step) {
|
|
|
|
step = (last_step+1) % 10;
|
|
|
|
last_step = step;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t colour = (current_colour_sequence >> (step*3)) & 7;
|
2018-08-13 08:42:28 -03:00
|
|
|
|
2020-09-03 09:41:43 -03:00
|
|
|
uint8_t red_des = (colour & RED) ? brightness : _led_off;
|
|
|
|
uint8_t green_des = (colour & GREEN) ? brightness : _led_off;
|
|
|
|
uint8_t blue_des = (colour & BLUE) ? brightness : _led_off;
|
2014-11-14 10:14:40 -04:00
|
|
|
|
2020-09-03 09:41:43 -03:00
|
|
|
set_rgb(red_des, green_des, blue_des);
|
2016-07-16 03:02:27 -03:00
|
|
|
}
|
|
|
|
|
2022-10-21 08:48:51 -03:00
|
|
|
#if AP_NOTIFY_MAVLINK_LED_CONTROL_SUPPORT_ENABLED
|
2016-07-16 03:02:27 -03:00
|
|
|
/*
|
|
|
|
handle LED control, only used when LED_OVERRIDE=1
|
|
|
|
*/
|
2019-04-30 07:22:48 -03:00
|
|
|
void RGBLed::handle_led_control(const mavlink_message_t &msg)
|
2016-07-16 03:02:27 -03:00
|
|
|
{
|
2018-08-29 00:37:34 -03:00
|
|
|
if (rgb_source() != mavlink) {
|
2016-07-16 03:02:27 -03:00
|
|
|
// ignore LED_CONTROL commands if not in LED_OVERRIDE mode
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// decode mavlink message
|
|
|
|
mavlink_led_control_t packet;
|
2019-04-30 07:22:48 -03:00
|
|
|
mavlink_msg_led_control_decode(&msg, &packet);
|
2016-07-16 03:02:27 -03:00
|
|
|
|
|
|
|
_led_override.start_ms = AP_HAL::millis();
|
2021-03-13 16:59:08 -04:00
|
|
|
|
2022-10-20 05:28:01 -03:00
|
|
|
uint8_t rate_hz = 0;
|
2016-07-16 03:02:27 -03:00
|
|
|
switch (packet.custom_len) {
|
|
|
|
case 4:
|
2022-10-20 05:28:01 -03:00
|
|
|
rate_hz = packet.custom_bytes[3];
|
|
|
|
FALLTHROUGH;
|
|
|
|
case 3:
|
|
|
|
rgb_control(packet.custom_bytes[0], packet.custom_bytes[1], packet.custom_bytes[2], rate_hz);
|
2016-07-16 03:02:27 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-10-21 08:48:51 -03:00
|
|
|
#endif
|
2016-07-16 03:02:27 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
update LED when in override mode
|
|
|
|
*/
|
|
|
|
void RGBLed::update_override(void)
|
|
|
|
{
|
|
|
|
if (_led_override.rate_hz == 0) {
|
|
|
|
// solid colour
|
|
|
|
_set_rgb(_led_override.r, _led_override.g, _led_override.b);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// blinking
|
|
|
|
uint32_t ms_per_cycle = 1000 / _led_override.rate_hz;
|
|
|
|
uint32_t cycle = (AP_HAL::millis() - _led_override.start_ms) % ms_per_cycle;
|
|
|
|
if (cycle > ms_per_cycle / 2) {
|
|
|
|
// on
|
|
|
|
_set_rgb(_led_override.r, _led_override.g, _led_override.b);
|
|
|
|
} else {
|
|
|
|
_set_rgb(0, 0, 0);
|
|
|
|
}
|
2014-11-14 10:14:40 -04:00
|
|
|
}
|
2019-12-09 17:06:35 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
RGB control
|
|
|
|
give RGB and flash rate, used with scripting
|
|
|
|
*/
|
|
|
|
void RGBLed::rgb_control(uint8_t r, uint8_t g, uint8_t b, uint8_t rate_hz)
|
|
|
|
{
|
|
|
|
_led_override.rate_hz = rate_hz;
|
|
|
|
_led_override.r = r;
|
|
|
|
_led_override.g = g;
|
|
|
|
_led_override.b = b;
|
|
|
|
}
|