Add parameter to limit the maximum brightness of the RGB LED

This commit is contained in:
Nate Weibley
2015-09-18 10:21:50 -04:00
parent 1bf01a5a72
commit 04a7d19405
3 changed files with 96 additions and 5 deletions
+40 -4
View File
@@ -101,11 +101,13 @@ private:
uint8_t _g;
uint8_t _b;
float _brightness;
float _max_brightness;
bool _running;
int _led_interval;
bool _should_run;
int _counter;
int _param_sub;
void set_color(rgbled_color_t ledcolor);
void set_mode(rgbled_mode_t mode);
@@ -117,6 +119,7 @@ private:
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();
};
/* for now, we only support one RGBLED */
@@ -140,10 +143,12 @@ RGBLED::RGBLED(int bus, int rgbled) :
_g(0),
_b(0),
_brightness(1.0f),
_max_brightness(1.0f),
_running(false),
_led_interval(0),
_should_run(false),
_counter(0)
_counter(0),
_param_sub(-1)
{
memset(&_work, 0, sizeof(_work));
memset(&_pattern, 0, sizeof(_pattern));
@@ -285,6 +290,22 @@ RGBLED::led()
return;
}
if (_param_sub < 0) {
_param_sub = orb_subscribe(ORB_ID(parameter_update));
}
if (_param_sub >= 0) {
bool updated = false;
orb_check(_param_sub, &updated);
if (updated) {
parameter_update_s pupdate;
orb_copy(ORB_ID(parameter_update), _param_sub, &pupdate);
update_params();
// Immediately update to change brightness
send_led_rgb();
}
}
switch (_mode) {
case RGBLED_MODE_BLINK_SLOW:
case RGBLED_MODE_BLINK_NORMAL:
@@ -546,9 +567,9 @@ RGBLED::send_led_rgb()
{
/* To scale from 0..255 -> 0..15 shift right by 4 bits */
const uint8_t msg[6] = {
SUB_ADDR_PWM0, (uint8_t)((int)(_b * _brightness) >> 4),
SUB_ADDR_PWM1, (uint8_t)((int)(_g * _brightness) >> 4),
SUB_ADDR_PWM2, (uint8_t)((int)(_r * _brightness) >> 4)
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);
}
@@ -573,6 +594,21 @@ RGBLED::get(bool &on, bool &powersave, uint8_t &r, uint8_t &g, uint8_t &b)
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_usage()
{