lib/led: move drivers/lights/rgbled LED_RGB_MAXBRT -> SYS_RGB_MAXBRT

This commit is contained in:
Daniel Agar
2021-09-28 10:09:55 -04:00
parent b24e5fc0af
commit 03bd5e0f48
7 changed files with 75 additions and 169 deletions
+27 -4
View File
@@ -40,6 +40,31 @@
int LedController::update(LedControlData &control_data)
{
bool had_changes = false; // did one of the outputs change?
// check for parameter updates
if (_parameter_update_sub.updated()) {
// clear update
parameter_update_s pupdate;
_parameter_update_sub.copy(&pupdate);
updateParams();
const uint8_t max_brightness_prev = _max_brightness;
// set maximum brightness (0-255) from percentage
_max_brightness = roundf(math::constrain(_param_sys_rgb_maxbrt.get(), 0.f, 1.f) * UINT8_MAX);
// update existing
for (int i = 0; i < BOARD_MAX_LEDS; ++i) {
control_data.leds[i].brightness = math::min(_max_brightness, control_data.leds[i].brightness);
}
if (_max_brightness != max_brightness_prev) {
had_changes = true;
}
}
while (_led_control_sub.updated() || _force_update) {
const unsigned last_generation = _led_control_sub.get_last_generation();
@@ -78,8 +103,6 @@ int LedController::update(LedControlData &control_data)
_force_update = false;
}
bool had_changes = false; // did one of the outputs change?
// handle state updates
hrt_abstime now = hrt_absolute_time();
@@ -207,7 +230,7 @@ void LedController::get_control_data(LedControlData &control_data)
for (int i = 0; i < BOARD_MAX_LEDS; ++i) {
control_data.leds[i].color = led_control_s::COLOR_OFF; // set output to a defined state
control_data.leds[i].brightness = 255;
control_data.leds[i].brightness = _max_brightness;
for (int priority = led_control_s::MAX_PRIORITY; priority >= 0; --priority) {
bool flash_output_active = true;
@@ -226,7 +249,7 @@ void LedController::get_control_data(LedControlData &control_data)
// fade on and off
int counter = _states[i].current_blinking_time / (BREATHE_INTERVAL / 100);
int n = counter >= (BREATHE_STEPS / 2) ? BREATHE_STEPS - counter : counter;
control_data.leds[i].brightness = (n * n) * 255 / (BREATHE_STEPS * BREATHE_STEPS / 4); // (n/(steps/2))^2
control_data.leds[i].brightness = (n * n) * _max_brightness / (BREATHE_STEPS * BREATHE_STEPS / 4); // (n/(steps/2))^2
control_data.leds[i].color = cur_data.color;
_breathe_enabled = true;
break;
+23 -8
View File
@@ -44,15 +44,23 @@
#include <drivers/drv_hrt.h>
#include <drivers/drv_led.h>
#include <px4_platform_common/module_params.h>
#include <uORB/Subscription.hpp>
#include <uORB/SubscriptionInterval.hpp>
#include <uORB/topics/led_control.h>
#include <uORB/topics/parameter_update.h>
#include <lib/parameters/param.h>
using namespace time_literals;
struct LedControlDataSingle {
uint8_t color; ///< one of led_control_s::COLOR_*
uint8_t brightness; ///< brightness in [0, 255]
uint8_t color{led_control_s::COLOR_OFF}; ///< one of led_control_s::COLOR_*
uint8_t brightness{0}; ///< brightness in [0, 255]
};
struct LedControlData {
LedControlDataSingle leds[BOARD_MAX_LEDS];
LedControlDataSingle leds[BOARD_MAX_LEDS] {};
};
@@ -60,11 +68,11 @@ struct LedControlData {
** class LedController
* Handles the led_control topic: blinking, priorities and state updates.
*/
class LedController
class LedController : public ModuleParams
{
public:
LedController() = default;
~LedController() = default;
LedController() : ModuleParams(nullptr) {}
~LedController() override = default;
/**
* get maxium time between two consecutive calls to update() in us.
@@ -148,8 +156,6 @@ private:
// handle infinite case
priority[next_priority].blink_times_left = 246;
}
}
void apply_next_state()
@@ -175,7 +181,16 @@ private:
PerLedData _states[BOARD_MAX_LEDS]; ///< keep current LED states
uORB::Subscription _led_control_sub{ORB_ID(led_control)}; ///< uorb subscription
uORB::SubscriptionInterval _parameter_update_sub{ORB_ID(parameter_update), 1_s};
hrt_abstime _last_update_call{0};
uint8_t _max_brightness{UINT8_MAX};
bool _force_update{true}; ///< force an orb_copy in the beginning
bool _breathe_enabled{false}; ///< true if at least one of the led's is currently in breathe mode
DEFINE_PARAMETERS(
(ParamFloat<px4::params::SYS_RGB_MAXBRT>) _param_sys_rgb_maxbrt
)
};
+42
View File
@@ -0,0 +1,42 @@
/****************************************************************************
*
* Copyright (c) 2015-2021 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.
*
****************************************************************************/
/**
* RGB Led brightness limit
*
* Set to 0 to disable, 1 for maximum brightness
*
* @unit %
* @group System
*/
PARAM_DEFINE_FLOAT(SYS_RGB_MAXBRT, 1.f);
+13
View File
@@ -38,6 +38,7 @@
#include <lib/drivers/device/Device.hpp>
#include <drivers/drv_sensor.h>
#include <lib/parameters/param.h>
#include <lib/mathlib/mathlib.h>
bool param_modify_on_import(bson_node_t node)
{
@@ -91,6 +92,18 @@ bool param_modify_on_import(bson_node_t node)
}
}
// 2021-08-27: translate LED_RGB_MAXBRT (0-15) to SYS_RGB_MAXBRT(0.f-1.f)
if (node->type == BSON_INT32) {
if (strcmp("LED_RGB_MAXBRT", node->name) == 0) {
// convert integer (0-15) to float percentage
node->d = math::constrain(static_cast<double>(node->i) / 15., 0., 1.);
node->type = BSON_DOUBLE;
strcpy(node->name, "SYS_RGB_MAXBRT");
PX4_INFO("param migrating LED_RGB_MAXBRT (removed) -> SYS_RGB_MAXBRT: value=%.3f", node->d);
return true;
}
}
// 2020-06-29 (v1.11 beta): translate CAL_ACCx_EN/CAL_GYROx_EN/CAL_MAGx_EN -> CAL_ACCx_PRIO/CAL_GYROx_PRIO/CAL_MAGx_PRIO
if (node->type == BSON_INT32) {