PX4 Firmware
PX4 Autopilot Software http://px4.io
led.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2013 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file led.cpp
36  *
37  * LED driver to control the onboard LED(s) via ioctl interface.
38  */
39 
40 #include <px4_platform_common/px4_config.h>
41 #include <lib/cdev/CDev.hpp>
42 #include <drivers/drv_board_led.h>
43 #include <stdio.h>
44 
45 /*
46  * Ideally we'd be able to get these from up_internal.h,
47  * but since we want to be able to disable the NuttX use
48  * of leds for system indication at will and there is no
49  * separate switch, we need to build independent of the
50  * CONFIG_ARCH_LEDS configuration switch.
51  */
53 extern void led_init();
54 extern void led_on(int led);
55 extern void led_off(int led);
56 extern void led_toggle(int led);
58 
59 class LED : cdev::CDev
60 {
61 public:
62  LED();
63  ~LED() override = default;
64 
65  int init() override;
66  int ioctl(cdev::file_t *filp, int cmd, unsigned long arg) override;
67 };
68 
70 {
71  // force immediate init/device registration
72  init();
73 }
74 
75 int
77 {
78  PX4_DEBUG("LED::init");
79 
80  CDev::init();
81 
82  led_init();
83 
84  return 0;
85 }
86 
87 int
88 LED::ioctl(cdev::file_t *filp, int cmd, unsigned long arg)
89 {
90  int result = OK;
91 
92  switch (cmd) {
93  case LED_ON:
94  led_on(arg);
95  break;
96 
97  case LED_OFF:
98  led_off(arg);
99  break;
100 
101  case LED_TOGGLE:
102  led_toggle(arg);
103  break;
104 
105  default:
106  result = CDev::ioctl(filp, cmd, arg);
107  }
108 
109  return result;
110 }
111 
112 namespace
113 {
114 LED *gLED;
115 }
116 
117 void
119 {
120  if (gLED == nullptr) {
121  gLED = new LED;
122  }
123 }
#define __END_DECLS
Definition: visibility.h:59
int ioctl(cdev::file_t *filp, int cmd, unsigned long arg) override
Perform an ioctl operation on the device.
Definition: led.cpp:88
LED()
Definition: led.cpp:69
Definition: led.cpp:59
void led_off(int led)
LED driver API to control the onboard LED(s) via ioctl() interface.
__BEGIN_DECLS void led_init()
Abstract class for any character device.
Definition: CDev.hpp:58
void init()
Activates/configures the hardware registers.
#define LED_ON
Definition: drv_board_led.h:59
~LED() override=default
#define LED0_DEVICE_PATH
Definition: drv_board_led.h:47
#define __BEGIN_DECLS
Definition: visibility.h:58
#define LED_OFF
Definition: drv_board_led.h:60
void led_on(int led)
#define LED_TOGGLE
Definition: drv_board_led.h:61
CDev(const char *devname)
Constructor.
Definition: CDev.cpp:50
#define OK
Definition: uavcan_main.cpp:71
int init() override
Definition: led.cpp:76
void led_toggle(int led)
void drv_led_start(void)
Definition: led.cpp:118