APM:Libraries
RCOutputRGBLed.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Intel Corporation. All rights reserved.
3  *
4  * This file is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This file is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  * See the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "RCOutputRGBLed.h"
19 
20 #include <AP_Math/AP_Math.h>
22 
23 static const AP_HAL::HAL& hal = AP_HAL::get_HAL();
24 
25 #define LED_OFF 0
26 #define LED_FULL_BRIGHT 255
27 #define LED_MEDIUM ((LED_FULL_BRIGHT / 5) * 4)
28 #define LED_DIM ((LED_FULL_BRIGHT / 5) * 2)
29 
30 RCOutputRGBLed::RCOutputRGBLed(uint8_t red_channel, uint8_t green_channel, uint8_t blue_channel)
31  : RCOutputRGBLed(red_channel, green_channel, blue_channel, LED_OFF,
33 {
34 }
35 
36 RCOutputRGBLed::RCOutputRGBLed(uint8_t red_channel, uint8_t green_channel,
37  uint8_t blue_channel, uint8_t led_off,
38  uint8_t led_full, uint8_t led_medium,
39  uint8_t led_dim)
40  : RGBLed(led_off, led_full, led_medium, led_dim)
41  , _red_channel(red_channel)
42  , _green_channel(green_channel)
43  , _blue_channel(blue_channel)
44 {
45 }
46 
48 {
52 
53  return true;
54 }
55 
56 uint16_t RCOutputRGBLed::get_duty_cycle_for_color(const uint8_t color, const uint16_t usec_period) const
57 {
58  return usec_period * color / _led_bright;
59 }
60 
61 uint16_t RCOutputRGBLedInverted::get_duty_cycle_for_color(const uint8_t color, const uint16_t usec_period) const
62 {
63  return usec_period * (255 - color) / _led_bright;
64 }
65 
66 
67 bool RCOutputRGBLed::hw_set_rgb(uint8_t red, uint8_t green, uint8_t blue)
68 {
69  const uint16_t freq_motor = hal.rcout->get_freq(0);
70  const uint16_t freq = hal.rcout->get_freq(_red_channel);
71  const uint16_t usec_period = hz_to_usec(freq);
72 
73  if (freq_motor != freq) {
74  /*
75  * keep at same frequency as the first RCOutput channel, some RCOutput
76  * drivers can not operate in different frequency between channels
77  */
78  const uint32_t mask = 1 << _red_channel | 1 << _green_channel
79  | 1 << _blue_channel;
80  hal.rcout->set_freq(mask, freq_motor);
81  }
82 
83  uint16_t usec_duty = get_duty_cycle_for_color(red, usec_period);
85 
86  usec_duty = get_duty_cycle_for_color(green, usec_period);
88 
89  usec_duty = get_duty_cycle_for_color(blue, usec_period);
91 
92  return true;
93 }
uint8_t _red_channel
Definition: RGBLed.h:25
uint8_t _green_channel
#define LED_FULL_BRIGHT
virtual void set_freq(uint32_t chmask, uint16_t freq_hz)=0
static const AP_HAL::HAL & hal
virtual uint16_t get_freq(uint8_t ch)=0
virtual uint16_t get_duty_cycle_for_color(const uint8_t color, const uint16_t usec_period) const
virtual uint16_t get_duty_cycle_for_color(const uint8_t color, const uint16_t usec_period) const override
uint8_t _led_bright
Definition: RGBLed.h:62
virtual void enable_ch(uint8_t ch)=0
static void set_output_pwm_chan(uint8_t chan, uint16_t value)
#define LED_MEDIUM
uint8_t _blue_channel
bool hw_init() override
virtual bool hw_set_rgb(uint8_t red, uint8_t green, uint8_t blue) override
const HAL & get_HAL()
#define LED_DIM
AP_HAL::RCOutput * rcout
Definition: HAL.h:113
RCOutputRGBLed(uint8_t red_channel, uint8_t green_channel, uint8_t blue_channel, uint8_t led_off, uint8_t led_full, uint8_t led_medium, uint8_t led_dim)
#define LED_OFF
uint32_t hz_to_usec(uint32_t freq)
Definition: AP_Math.h:227