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.
38  */
39 
40 #include <px4_platform_common/px4_config.h>
41 #include <drivers/drv_board_led.h>
42 #include <stdio.h>
43 
44 #include "VirtDevObj.hpp"
45 
46 using namespace DriverFramework;
47 
48 /*
49  * Ideally we'd be able to get these from up_internal.h,
50  * but since we want to be able to disable the NuttX use
51  * of leds for system indication at will and there is no
52  * separate switch, we need to build independent of the
53  * CONFIG_ARCH_LEDS configuration switch.
54  */
56 extern void led_init();
57 extern void led_on(int led);
58 extern void led_off(int led);
59 extern void led_toggle(int led);
61 
62 class LED : public VirtDevObj
63 {
64 public:
65  LED();
66  ~LED() override = default;
67 
68  int init() override;
69  int devIOCTL(unsigned long cmd, unsigned long arg) override;
70 
71 protected:
72  void _measure() override {}
73 };
74 
75 LED::LED() :
76  VirtDevObj("led", "/dev/ledsim", LED_BASE_DEVICE_PATH, 0)
77 {
78  // force immediate init/device registration
79  init();
80 }
81 
82 int
83 LED::init()
84 {
85  int ret = VirtDevObj::init();
86 
87  if (ret == 0) {
88  led_init();
89  }
90 
91  return ret;
92 }
93 
94 int
95 LED::devIOCTL(unsigned long cmd, unsigned long arg)
96 {
97  int result = OK;
98 
99  switch (cmd) {
100  case LED_ON:
101  led_on(arg);
102  break;
103 
104  case LED_OFF:
105  led_off(arg);
106  break;
107 
108  case LED_TOGGLE:
109  led_toggle(arg);
110  break;
111 
112 
113  default:
114  result = VirtDevObj::devIOCTL(cmd, arg);
115  }
116 
117  return result;
118 }
119 
120 namespace
121 {
122 LED *gLED;
123 }
124 
125 void
127 {
128  if (gLED == nullptr) {
129  gLED = new LED;
130 
131  if (gLED != nullptr) {
132  gLED->init();
133  }
134  }
135 }
#define LED_BASE_DEVICE_PATH
Definition: drv_board_led.h:46
#define __END_DECLS
Definition: visibility.h:59
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.
void _measure() override
Definition: led.cpp:72
__BEGIN_DECLS void led_init()
int devIOCTL(unsigned long cmd, unsigned long arg) override
Definition: led.cpp:95
void init()
Activates/configures the hardware registers.
#define LED_ON
Definition: drv_board_led.h:59
#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
#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