APM:Libraries
DiscoLED.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 Mathieu Othacehe. All rights reserved.
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #include <AP_HAL/AP_HAL.h>
18 
19 #if CONFIG_HAL_BOARD == HAL_BOARD_LINUX
20 #include "DiscoLED.h"
21 
22 #include <AP_HAL_Linux/Led_Sysfs.h>
23 #include <AP_HAL_Linux/PWM_Sysfs.h>
24 
25 #define RED_PWM_INDEX 9
26 #define GREEN_PWM_INDEX 8
27 #define BLUE_PWM_INDEX 15
28 
29 #define DISCO_LED_LOW 0x33
30 #define DISCO_LED_MEDIUM 0x7F
31 #define DISCO_LED_HIGH 0xFF
32 #define DISCO_LED_OFF 0x00
33 
34 #define DISCO_LED_RED_NAME "evinrude:red"
35 #define DISCO_LED_GREEN_NAME "evinrude:green"
36 #define DISCO_LED_BLUE_NAME "evinrude:blue"
37 
40  red_pwm(RED_PWM_INDEX),
41  green_pwm(GREEN_PWM_INDEX),
42  blue_pwm(BLUE_PWM_INDEX),
43 
44  red_led(DISCO_LED_RED_NAME),
45  green_led(DISCO_LED_GREEN_NAME),
46  blue_led(DISCO_LED_BLUE_NAME)
47 {
48 }
49 
51 {
52  /* If led sysfs api is present, use it, else use pwm sysfs api to
53  drive Disco leds */
54  if (red_led.init() && green_led.init() && blue_led.init()) {
56  return true;
57  }
58 
60 
64 
65  red_pwm.init();
66  green_pwm.init();
67  blue_pwm.init();
68 
69  red_pwm.enable(true);
70  green_pwm.enable(true);
71  blue_pwm.enable(true);
72 
73  return true;
74 }
75 
76 bool DiscoLED::hw_set_rgb(uint8_t red, uint8_t green, uint8_t blue)
77 {
78  switch (backend) {
79  case PWM_SYSFS:
80  red_pwm.set_duty_cycle(red / UINT8_MAX * red_pwm_period);
81  green_pwm.set_duty_cycle(green / UINT8_MAX * green_pwm_period);
82  blue_pwm.set_duty_cycle(blue / UINT8_MAX * blue_pwm_period);
83  break;
84  case LED_SYSFS:
88  break;
89  default:
90  return false;
91  }
92 
93  return true;
94 }
95 #endif
#define DISCO_LED_MEDIUM
Definition: DiscoLED.cpp:30
Definition: RGBLed.h:25
Linux::PWM_Sysfs_Bebop green_pwm
Definition: DiscoLED.h:38
bool set_brightness(uint8_t brightness)
Definition: Led_Sysfs.cpp:76
bool set_duty_cycle(uint32_t nsec_duty_cycle)
Definition: PWM_Sysfs.cpp:127
#define DISCO_LED_LOW
Definition: DiscoLED.cpp:29
Linux::Led_Sysfs blue_led
Definition: DiscoLED.h:43
#define DISCO_LED_RED_NAME
Definition: DiscoLED.cpp:34
Linux::PWM_Sysfs_Bebop blue_pwm
Definition: DiscoLED.h:39
#define DISCO_LED_HIGH
Definition: DiscoLED.cpp:31
uint32_t blue_pwm_period
Definition: DiscoLED.h:47
Linux::PWM_Sysfs_Bebop red_pwm
Definition: DiscoLED.h:37
#define RED_PWM_INDEX
Definition: DiscoLED.cpp:25
bool hw_init(void) override
Definition: DiscoLED.cpp:50
#define DISCO_LED_BLUE_NAME
Definition: DiscoLED.cpp:36
DiscoLED()
Definition: DiscoLED.cpp:38
uint32_t red_pwm_period
Definition: DiscoLED.h:45
#define DISCO_LED_OFF
Definition: DiscoLED.cpp:32
enum led_backend backend
Definition: DiscoLED.h:54
#define BLUE_PWM_INDEX
Definition: DiscoLED.cpp:27
Linux::Led_Sysfs green_led
Definition: DiscoLED.h:42
void enable(bool value)
Definition: PWM_Sysfs.cpp:76
bool hw_set_rgb(uint8_t r, uint8_t g, uint8_t b) override
Definition: DiscoLED.cpp:76
uint32_t green_pwm_period
Definition: DiscoLED.h:46
#define GREEN_PWM_INDEX
Definition: DiscoLED.cpp:26
Linux::Led_Sysfs red_led
Definition: DiscoLED.h:41
#define DISCO_LED_GREEN_NAME
Definition: DiscoLED.cpp:35