RCOutputRGBLed.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #include "RCOutputRGBLed.h"
  18. #include <AP_Math/AP_Math.h>
  19. #include <SRV_Channel/SRV_Channel.h>
  20. static const AP_HAL::HAL& hal = AP_HAL::get_HAL();
  21. #define LED_OFF 0
  22. #define LED_FULL_BRIGHT 255
  23. #define LED_MEDIUM ((LED_FULL_BRIGHT / 5) * 4)
  24. #define LED_DIM ((LED_FULL_BRIGHT / 5) * 2)
  25. RCOutputRGBLed::RCOutputRGBLed(uint8_t red_channel, uint8_t green_channel, uint8_t blue_channel)
  26. : RCOutputRGBLed(red_channel, green_channel, blue_channel, LED_OFF,
  27. LED_FULL_BRIGHT, LED_MEDIUM, LED_DIM)
  28. {
  29. }
  30. RCOutputRGBLed::RCOutputRGBLed(uint8_t red_channel, uint8_t green_channel,
  31. uint8_t blue_channel, uint8_t led_off,
  32. uint8_t led_full, uint8_t led_medium,
  33. uint8_t led_dim)
  34. : RGBLed(led_off, led_full, led_medium, led_dim)
  35. , _red_channel(red_channel)
  36. , _green_channel(green_channel)
  37. , _blue_channel(blue_channel)
  38. {
  39. }
  40. bool RCOutputRGBLed::hw_init()
  41. {
  42. hal.rcout->enable_ch(_red_channel);
  43. hal.rcout->enable_ch(_green_channel);
  44. hal.rcout->enable_ch(_blue_channel);
  45. return true;
  46. }
  47. uint16_t RCOutputRGBLed::get_duty_cycle_for_color(const uint8_t color, const uint16_t usec_period) const
  48. {
  49. return usec_period * color / _led_bright;
  50. }
  51. uint16_t RCOutputRGBLedInverted::get_duty_cycle_for_color(const uint8_t color, const uint16_t usec_period) const
  52. {
  53. return usec_period * (255 - color) / _led_bright;
  54. }
  55. bool RCOutputRGBLed::hw_set_rgb(uint8_t red, uint8_t green, uint8_t blue)
  56. {
  57. const uint16_t freq_motor = hal.rcout->get_freq(0);
  58. const uint16_t freq = hal.rcout->get_freq(_red_channel);
  59. const uint16_t usec_period = hz_to_usec(freq);
  60. if (freq_motor != freq) {
  61. /*
  62. * keep at same frequency as the first RCOutput channel, some RCOutput
  63. * drivers can not operate in different frequency between channels
  64. */
  65. const uint32_t mask = 1 << _red_channel | 1 << _green_channel
  66. | 1 << _blue_channel;
  67. hal.rcout->set_freq(mask, freq_motor);
  68. }
  69. uint16_t usec_duty = get_duty_cycle_for_color(red, usec_period);
  70. SRV_Channels::set_output_pwm_chan(_red_channel, usec_duty);
  71. usec_duty = get_duty_cycle_for_color(green, usec_period);
  72. SRV_Channels::set_output_pwm_chan(_green_channel, usec_duty);
  73. usec_duty = get_duty_cycle_for_color(blue, usec_period);
  74. SRV_Channels::set_output_pwm_chan(_blue_channel, usec_duty);
  75. return true;
  76. }