PX4 Firmware
PX4 Autopilot Software http://px4.io
indication_controller.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2014 PX4 Development Team. All rights reserved.
4  * Author: Pavel Kirienko <pavel.kirienko@gmail.com>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * 3. Neither the name PX4 nor the names of its contributors may be
17  * used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  ****************************************************************************/
34 
36 #include <uavcan/equipment/indication/LightsCommand.hpp>
37 #include "led.hpp"
38 
39 namespace
40 {
41 unsigned self_light_index = 0;
42 
43 void cb_light_command(const uavcan::ReceivedDataStructure<uavcan::equipment::indication::LightsCommand> &msg)
44 {
45  uavcan::uint32_t red = 0;
46  uavcan::uint32_t green = 0;
47  uavcan::uint32_t blue = 0;
48 
49  for (auto &cmd : msg.commands) {
50  if (cmd.light_id == self_light_index) {
51  using uavcan::equipment::indication::RGB565;
52 
53  red = uavcan::uint32_t(float(cmd.color.red) *
54  (255.0F / float(RGB565::FieldTypes::red::max())) + 0.5F);
55 
56  green = uavcan::uint32_t(float(cmd.color.green) *
57  (255.0F / float(RGB565::FieldTypes::green::max())) + 0.5F);
58 
59  blue = uavcan::uint32_t(float(cmd.color.blue) *
60  (255.0F / float(RGB565::FieldTypes::blue::max())) + 0.5F);
61 
62  red = uavcan::min<uavcan::uint32_t>(red, 0xFFU);
63  green = uavcan::min<uavcan::uint32_t>(green, 0xFFU);
64  blue = uavcan::min<uavcan::uint32_t>(blue, 0xFFU);
65  }
66 
67  if (cmd.light_id == self_light_index + 1) {
68  static int c = 0;
69 
70  if (c++ % 100 == 0) {
71  ::syslog(LOG_INFO, "rgb:%d %d %d hz %d\n", red, green, blue, int(cmd.color.red));
72  }
73 
74  rgb_led(red, green, blue, int(cmd.color.red));
75  break;
76  }
77  }
78 }
79 }
80 
81 int init_indication_controller(uavcan::INode &node)
82 {
83  static uavcan::Subscriber<uavcan::equipment::indication::LightsCommand> sub_light(node);
84 
85  self_light_index = 0;
86 
87  int res = 0;
88 
89  res = sub_light.start(cb_light_command);
90 
91  if (res != 0) {
92  return res;
93  }
94 
95  return 0;
96 }
static char msg[NUM_MSG][CONFIG_USART1_TXBUFSIZE]
Definition: px4io.c:89
int init_indication_controller(uavcan::INode &node)
constexpr _Tp max(_Tp a, _Tp b)
Definition: Limits.hpp:60
void rgb_led(int r, int g, int b, int freqs)
Definition: led.cpp:44