mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-15 15:23:35 +08:00
led drivers and controller move to uORB::Subscription
This commit is contained in:
+8
-15
@@ -38,24 +38,13 @@
|
||||
|
||||
#include "led.h"
|
||||
|
||||
int LedController::init(int led_control_sub)
|
||||
{
|
||||
_led_control_sub = led_control_sub;
|
||||
_last_update_call = hrt_absolute_time();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int LedController::update(LedControlData &control_data)
|
||||
{
|
||||
bool updated = false;
|
||||
|
||||
orb_check(_led_control_sub, &updated);
|
||||
|
||||
while (updated || _force_update) {
|
||||
while (_led_control_sub.updated() || _force_update) {
|
||||
// handle new state
|
||||
led_control_s led_control;
|
||||
|
||||
if (orb_copy(ORB_ID(led_control), _led_control_sub, &led_control) == 0) {
|
||||
if (_led_control_sub.copy(&led_control)) {
|
||||
|
||||
// don't apply the new state just yet to avoid interrupting an ongoing blinking state
|
||||
for (int i = 0; i < BOARD_MAX_LEDS; ++i) {
|
||||
@@ -80,14 +69,18 @@ int LedController::update(LedControlData &control_data)
|
||||
}
|
||||
|
||||
_force_update = false;
|
||||
|
||||
orb_check(_led_control_sub, &updated);
|
||||
}
|
||||
|
||||
bool had_changes = false; // did one of the outputs change?
|
||||
|
||||
// handle state updates
|
||||
hrt_abstime now = hrt_absolute_time();
|
||||
|
||||
if (_last_update_call == 0) {
|
||||
_last_update_call = now;
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint16_t blink_delta_t = (uint16_t)((now - _last_update_call) / 100); // Note: this is in 0.1ms
|
||||
constexpr uint16_t breathe_duration = BREATHE_INTERVAL * BREATHE_STEPS / 100;
|
||||
|
||||
|
||||
+11
-23
@@ -44,6 +44,8 @@
|
||||
#include <drivers/drv_hrt.h>
|
||||
#include <drivers/drv_led.h>
|
||||
|
||||
#include <uORB/Subscription.hpp>
|
||||
#include <uORB/topics/led_control.h>
|
||||
|
||||
struct LedControlDataSingle {
|
||||
uint8_t color; ///< one of led_control_s::COLOR_*
|
||||
@@ -64,18 +66,6 @@ public:
|
||||
LedController() = default;
|
||||
~LedController() = default;
|
||||
|
||||
/**
|
||||
* initialize. Call this once before using the object
|
||||
* @param led_control_sub uorb subscription for led_control
|
||||
* @return 0 on success, <0 on error otherwise
|
||||
*/
|
||||
int init(int led_control_sub);
|
||||
|
||||
/**
|
||||
* check if already initialized
|
||||
*/
|
||||
bool is_init() const { return _led_control_sub >= 0; }
|
||||
|
||||
/**
|
||||
* get maxium time between two consecutive calls to update() in us.
|
||||
*/
|
||||
@@ -92,18 +82,16 @@ public:
|
||||
*/
|
||||
int update(LedControlData &control_data);
|
||||
|
||||
static const int BREATHE_INTERVAL = 25 * 1000; /**< single step when in breathe mode */
|
||||
static const int BREATHE_STEPS = 64; /**< number of steps in breathe mode for a full on-off cycle */
|
||||
static constexpr int BREATHE_INTERVAL = 25 * 1000; /**< single step when in breathe mode */
|
||||
static constexpr int BREATHE_STEPS = 64; /**< number of steps in breathe mode for a full on-off cycle */
|
||||
|
||||
static const int BLINK_FAST_DURATION = 100 * 1000; /**< duration of half a blinking cycle
|
||||
static constexpr int BLINK_FAST_DURATION = 100 * 1000; /**< duration of half a blinking cycle
|
||||
(on-to-off and off-to-on) in us */
|
||||
static const int BLINK_NORMAL_DURATION = 500 * 1000; /**< duration of half a blinking cycle
|
||||
static constexpr int BLINK_NORMAL_DURATION = 500 * 1000; /**< duration of half a blinking cycle
|
||||
(on-to-off and off-to-on) in us */
|
||||
static const int BLINK_SLOW_DURATION = 2000 * 1000; /**< duration of half a blinking cycle
|
||||
static constexpr int BLINK_SLOW_DURATION = 2000 * 1000; /**< duration of half a blinking cycle
|
||||
(on-to-off and off-to-on) in us */
|
||||
|
||||
int led_control_subscription() const { return _led_control_sub; }
|
||||
|
||||
private:
|
||||
|
||||
/** set control_data based on current Led states */
|
||||
@@ -186,8 +174,8 @@ private:
|
||||
|
||||
PerLedData _states[BOARD_MAX_LEDS]; ///< keep current LED states
|
||||
|
||||
int _led_control_sub = -1; ///< uorb subscription
|
||||
hrt_abstime _last_update_call;
|
||||
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
|
||||
uORB::Subscription _led_control_sub{ORB_ID(led_control)}; ///< uorb subscription
|
||||
hrt_abstime _last_update_call{0};
|
||||
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
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user