DiscoLED.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. Copyright (C) 2016 Mathieu Othacehe. All rights reserved.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <AP_HAL/AP_HAL.h>
  15. #if CONFIG_HAL_BOARD == HAL_BOARD_LINUX
  16. #include "DiscoLED.h"
  17. #include <AP_HAL_Linux/Led_Sysfs.h>
  18. #include <AP_HAL_Linux/PWM_Sysfs.h>
  19. #define RED_PWM_INDEX 9
  20. #define GREEN_PWM_INDEX 8
  21. #define BLUE_PWM_INDEX 15
  22. #define DISCO_LED_LOW 0x33
  23. #define DISCO_LED_MEDIUM 0x7F
  24. #define DISCO_LED_HIGH 0xFF
  25. #define DISCO_LED_OFF 0x00
  26. #define DISCO_LED_RED_NAME "evinrude:red"
  27. #define DISCO_LED_GREEN_NAME "evinrude:green"
  28. #define DISCO_LED_BLUE_NAME "evinrude:blue"
  29. DiscoLED::DiscoLED():
  30. RGBLed(DISCO_LED_OFF, DISCO_LED_HIGH, DISCO_LED_MEDIUM, DISCO_LED_LOW),
  31. red_pwm(RED_PWM_INDEX),
  32. green_pwm(GREEN_PWM_INDEX),
  33. blue_pwm(BLUE_PWM_INDEX),
  34. red_led(DISCO_LED_RED_NAME),
  35. green_led(DISCO_LED_GREEN_NAME),
  36. blue_led(DISCO_LED_BLUE_NAME)
  37. {
  38. }
  39. bool DiscoLED::hw_init()
  40. {
  41. /* If led sysfs api is present, use it, else use pwm sysfs api to
  42. drive Disco leds */
  43. if (red_led.init() && green_led.init() && blue_led.init()) {
  44. backend = LED_SYSFS;
  45. return true;
  46. }
  47. backend = PWM_SYSFS;
  48. red_pwm_period = red_pwm.get_period();
  49. green_pwm_period = green_pwm.get_period();
  50. blue_pwm_period = blue_pwm.get_period();
  51. red_pwm.init();
  52. green_pwm.init();
  53. blue_pwm.init();
  54. red_pwm.enable(true);
  55. green_pwm.enable(true);
  56. blue_pwm.enable(true);
  57. return true;
  58. }
  59. bool DiscoLED::hw_set_rgb(uint8_t red, uint8_t green, uint8_t blue)
  60. {
  61. switch (backend) {
  62. case PWM_SYSFS:
  63. red_pwm.set_duty_cycle(red / UINT8_MAX * red_pwm_period);
  64. green_pwm.set_duty_cycle(green / UINT8_MAX * green_pwm_period);
  65. blue_pwm.set_duty_cycle(blue / UINT8_MAX * blue_pwm_period);
  66. break;
  67. case LED_SYSFS:
  68. red_led.set_brightness(red);
  69. green_led.set_brightness(green);
  70. blue_led.set_brightness(blue);
  71. break;
  72. default:
  73. return false;
  74. }
  75. return true;
  76. }
  77. #endif