Line data Source code
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 : /* 16 : AP_SerialLED for controlling serial connected LEDs using WS2812B protocol 17 : */ 18 : 19 : #include "AP_SerialLED.h" 20 : 21 : #if AP_SERIALLED_ENABLED 22 : 23 : #include <AP_Math/AP_Math.h> 24 : #include "SRV_Channel/SRV_Channel.h" 25 : 26 : extern const AP_HAL::HAL& hal; 27 : 28 : AP_SerialLED AP_SerialLED::singleton; 29 : 30 : // set number of NeoPixels per pin 31 0 : bool AP_SerialLED::set_num_neopixel(uint8_t chan, uint8_t num_leds) 32 : { 33 0 : if (chan >= 1 && chan <= 16 && num_leds <= AP_SERIALLED_MAX_LEDS) { 34 0 : return hal.rcout->set_serial_led_num_LEDs(chan-1, num_leds, AP_HAL::RCOutput::MODE_NEOPIXEL); 35 : } 36 0 : return false; 37 : } 38 : 39 : // set number of NeoPixels per pin in RGB mode 40 0 : bool AP_SerialLED::set_num_neopixel_rgb(uint8_t chan, uint8_t num_leds) 41 : { 42 0 : if (chan >= 1 && chan <= 16 && num_leds <= AP_SERIALLED_MAX_LEDS) { 43 0 : return hal.rcout->set_serial_led_num_LEDs(chan-1, num_leds, AP_HAL::RCOutput::MODE_NEOPIXELRGB); 44 : } 45 0 : return false; 46 : } 47 : 48 : // set number of ProfiLEDs per pin 49 0 : bool AP_SerialLED::set_num_profiled(uint8_t chan, uint8_t num_leds) 50 : { 51 0 : if (chan >= 1 && chan <= 16 && num_leds <= AP_SERIALLED_MAX_LEDS - 2) { 52 : // must have a clock 53 0 : uint32_t Clock_mask = 0; 54 0 : if (!SRV_Channels::function_assigned((SRV_Channel::Function)((uint8_t)SRV_Channel::k_ProfiLED_Clock))) { 55 0 : return false; 56 : } 57 0 : Clock_mask = SRV_Channels::get_output_channel_mask((SRV_Channel::Function)((uint8_t)SRV_Channel::k_ProfiLED_Clock)); 58 : 59 0 : return hal.rcout->set_serial_led_num_LEDs(chan-1, num_leds, AP_HAL::RCOutput::MODE_PROFILED, Clock_mask); 60 : } 61 0 : return false; 62 : } 63 : 64 : // set RGB value on LED number. LED number -1 is all LEDs. First LED is 0. chan is PWM output, 1..16 65 0 : bool AP_SerialLED::set_RGB(uint8_t chan, int8_t led, uint8_t red, uint8_t green, uint8_t blue) 66 : { 67 0 : if (chan >= 1 && chan <= 16) { 68 0 : return hal.rcout->set_serial_led_rgb_data(chan-1, led, red, green, blue); 69 : } 70 0 : return false; 71 : } 72 : 73 : // trigger sending of LED changes to LEDs 74 0 : bool AP_SerialLED::send(uint8_t chan) 75 : { 76 0 : if (chan >= 1 && chan <= 16) { 77 0 : return hal.rcout->serial_led_send(chan-1); 78 : } 79 0 : return false; 80 : } 81 : 82 : #endif // AP_SERIALLED_ENABLED