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 }
68 }
69 
70 int init_indication_controller(uavcan::INode &node)
71 {
72  static uavcan::Subscriber<uavcan::equipment::indication::LightsCommand> sub_light(node);
73 
74  self_light_index = 0;
75 
76  int res = 0;
77 
78  res = sub_light.start(cb_light_command);
79 
80  if (res != 0) {
81  return res;
82  }
83 
84  return 0;
85 }
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