rgbled.cpp
9.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/****************************************************************************
*
* Copyright (c) 2012-2019 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* @file rgbled.cpp
*
* Driver for the onboard RGB LED controller (TCA62724FMG) connected via I2C.
*
* @author Julian Oes <julian@px4.io>
* @author Anton Babushkin <anton.babushkin@me.com>
*/
#include <string.h>
#include <drivers/device/i2c.h>
#include <lib/led/led.h>
#include <lib/parameters/param.h>
#include <px4_platform_common/getopt.h>
#include <px4_platform_common/i2c_spi_buses.h>
#include <px4_platform_common/module.h>
#include <uORB/Subscription.hpp>
#include <uORB/SubscriptionInterval.hpp>
#include <uORB/topics/parameter_update.h>
using namespace time_literals;
#define ADDR 0x55 /**< I2C adress of TCA62724FMG */
#define SUB_ADDR_START 0x01 /**< write everything (with auto-increment) */
#define SUB_ADDR_PWM0 0x81 /**< blue (without auto-increment) */
#define SUB_ADDR_PWM1 0x82 /**< green (without auto-increment) */
#define SUB_ADDR_PWM2 0x83 /**< red (without auto-increment) */
#define SUB_ADDR_SETTINGS 0x84 /**< settings (without auto-increment)*/
#define SETTING_NOT_POWERSAVE 0x01 /**< power-save mode not off */
#define SETTING_ENABLE 0x02 /**< on */
class RGBLED : public device::I2C, public I2CSPIDriver<RGBLED>
{
public:
RGBLED(I2CSPIBusOption bus_option, const int bus, int bus_frequency, const int address);
virtual ~RGBLED() = default;
static I2CSPIDriverBase *instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
int runtime_instance);
static void print_usage();
int init() override;
int probe() override;
void RunImpl();
protected:
void print_status() override;
private:
float _brightness{1.0f};
float _max_brightness{1.0f};
uint8_t _r{0};
uint8_t _g{0};
uint8_t _b{0};
bool _leds_enabled{true};
uORB::SubscriptionInterval _parameter_update_sub{ORB_ID(parameter_update), 1_s};
LedController _led_controller;
int send_led_enable(bool enable);
int send_led_rgb();
int get(bool &on, bool &powersave, uint8_t &r, uint8_t &g, uint8_t &b);
void update_params();
};
RGBLED::RGBLED(I2CSPIBusOption bus_option, const int bus, int bus_frequency, const int address) :
I2C(DRV_LED_DEVTYPE_RGBLED, MODULE_NAME, bus, address, bus_frequency),
I2CSPIDriver(MODULE_NAME, px4::device_bus_to_wq(get_device_id()), bus_option, bus, address)
{
}
int
RGBLED::init()
{
int ret = I2C::init();
if (ret != OK) {
return ret;
}
/* switch off LED on start */
send_led_enable(false);
send_led_rgb();
update_params();
// kick off work queue
ScheduleNow();
return OK;
}
int
RGBLED::probe()
{
int ret;
bool on, powersave;
uint8_t r, g, b;
/**
this may look strange, but is needed. There is a serial
EEPROM (Microchip-24aa01) that responds to a bunch of I2C
addresses, including the 0x55 used by this LED device. So
we need to do enough operations to be sure we are talking
to the right device. These 3 operations seem to be enough,
as the 3rd one consistently fails if no RGBLED is on the bus.
*/
unsigned prevretries = _retries;
_retries = 4;
if ((ret = get(on, powersave, r, g, b)) != OK ||
(ret = send_led_enable(false) != OK) ||
(ret = send_led_enable(false) != OK)) {
return ret;
}
_retries = prevretries;
return ret;
}
void
RGBLED::print_status()
{
bool on, powersave;
uint8_t r, g, b;
int ret = get(on, powersave, r, g, b);
if (ret == OK) {
/* we don't care about power-save mode */
PX4_INFO("state: %s", on ? "ON" : "OFF");
PX4_INFO("red: %u, green: %u, blue: %u", (unsigned)r, (unsigned)g, (unsigned)b);
} else {
PX4_WARN("failed to read led");
}
}
void
RGBLED::RunImpl()
{
// check for parameter updates
if (_parameter_update_sub.updated()) {
// clear update
parameter_update_s pupdate;
_parameter_update_sub.copy(&pupdate);
// update parameters from storage
update_params();
// Immediately update to change brightness
send_led_rgb();
}
LedControlData led_control_data;
if (_led_controller.update(led_control_data) == 1) {
switch (led_control_data.leds[0].color) {
case led_control_s::COLOR_RED:
_r = 255; _g = 0; _b = 0;
send_led_enable(true);
break;
case led_control_s::COLOR_GREEN:
_r = 0; _g = 255; _b = 0;
send_led_enable(true);
break;
case led_control_s::COLOR_BLUE:
_r = 0; _g = 0; _b = 255;
send_led_enable(true);
break;
case led_control_s::COLOR_AMBER: //make it the same as yellow
case led_control_s::COLOR_YELLOW:
_r = 255; _g = 255; _b = 0;
send_led_enable(true);
break;
case led_control_s::COLOR_PURPLE:
_r = 255; _g = 0; _b = 255;
send_led_enable(true);
break;
case led_control_s::COLOR_CYAN:
_r = 0; _g = 255; _b = 255;
send_led_enable(true);
break;
case led_control_s::COLOR_WHITE:
_r = 255; _g = 255; _b = 255;
send_led_enable(true);
break;
default: // led_control_s::COLOR_OFF
_r = 0; _g = 0; _b = 0;
send_led_enable(false);
break;
}
_brightness = (float)led_control_data.leds[0].brightness / 255.f;
send_led_rgb();
}
/* re-queue ourselves to run again later */
ScheduleDelayed(_led_controller.maximum_update_interval());
}
/**
* Sent ENABLE flag to LED driver
*/
int
RGBLED::send_led_enable(bool enable)
{
if (_leds_enabled && enable) {
// already enabled
return 0;
}
_leds_enabled = enable;
uint8_t settings_byte = 0;
if (enable) {
settings_byte |= SETTING_ENABLE;
}
settings_byte |= SETTING_NOT_POWERSAVE;
const uint8_t msg[2] = { SUB_ADDR_SETTINGS, settings_byte};
return transfer(msg, sizeof(msg), nullptr, 0);
}
/**
* Send RGB PWM settings to LED driver according to current color and brightness
*/
int
RGBLED::send_led_rgb()
{
/* To scale from 0..255 -> 0..15 shift right by 4 bits */
const uint8_t msg[6] = {
SUB_ADDR_PWM0, static_cast<uint8_t>((_b >> 4) * _brightness * _max_brightness + 0.5f),
SUB_ADDR_PWM1, static_cast<uint8_t>((_g >> 4) * _brightness * _max_brightness + 0.5f),
SUB_ADDR_PWM2, static_cast<uint8_t>((_r >> 4) * _brightness * _max_brightness + 0.5f)
};
return transfer(msg, sizeof(msg), nullptr, 0);
}
int
RGBLED::get(bool &on, bool &powersave, uint8_t &r, uint8_t &g, uint8_t &b)
{
uint8_t result[2] = {0, 0};
int ret;
ret = transfer(nullptr, 0, &result[0], 2);
if (ret == OK) {
on = ((result[0] >> 4) & SETTING_ENABLE);
powersave = !((result[0] >> 4) & SETTING_NOT_POWERSAVE);
/* XXX check, looks wrong */
r = (result[0] & 0x0f) << 4;
g = (result[1] & 0xf0);
b = (result[1] & 0x0f) << 4;
}
return ret;
}
void
RGBLED::update_params()
{
int32_t maxbrt = 15;
param_get(param_find("LED_RGB_MAXBRT"), &maxbrt);
maxbrt = maxbrt > 15 ? 15 : maxbrt;
maxbrt = maxbrt < 0 ? 0 : maxbrt;
// A minimum of 2 "on" steps is required for breathe effect
if (maxbrt == 1) {
maxbrt = 2;
}
_max_brightness = maxbrt / 15.0f;
}
void
RGBLED::print_usage()
{
PRINT_MODULE_USAGE_NAME("rgbled", "driver");
PRINT_MODULE_USAGE_COMMAND("start");
PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(true, false);
PRINT_MODULE_USAGE_PARAMS_I2C_ADDRESS(0x55);
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
}
I2CSPIDriverBase *RGBLED::instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
int runtime_instance)
{
RGBLED *instance = new RGBLED(iterator.configuredBusOption(), iterator.bus(), cli.bus_frequency, cli.i2c_address);
if (instance == nullptr) {
PX4_ERR("alloc failed");
return nullptr;
}
if (instance->init() != PX4_OK) {
delete instance;
return nullptr;
}
return instance;
}
extern "C" __EXPORT int rgbled_main(int argc, char *argv[])
{
using ThisDriver = RGBLED;
BusCLIArguments cli{true, false};
cli.default_i2c_frequency = 100000;
cli.i2c_address = ADDR;
const char *verb = cli.parseDefaultArguments(argc, argv);
if (!verb) {
ThisDriver::print_usage();
return -1;
}
BusInstanceIterator iterator(MODULE_NAME, cli, DRV_LED_DEVTYPE_RGBLED);
if (!strcmp(verb, "start")) {
return ThisDriver::module_start(cli, iterator);
}
if (!strcmp(verb, "stop")) {
return ThisDriver::module_stop(iterator);
}
if (!strcmp(verb, "status")) {
return ThisDriver::module_status(iterator);
}
ThisDriver::print_usage();
return -1;
}