diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-05-29 16:46:19 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-05-29 18:49:15 +0200 |
commit | 3af13f1bc5842c6bc7404579ae8d0ad6b7d0f7b6 (patch) | |
tree | cf4f863f2bdc1dbc29714350a19a5db140efb813 /tracker-trackhat/led.cpp | |
parent | 0b2a330e0edb4cd1fecdd59d5ff86998544c2b94 (diff) |
tracker/trackhat: implement LED tracking indicator
Diffstat (limited to 'tracker-trackhat/led.cpp')
-rw-r--r-- | tracker-trackhat/led.cpp | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/tracker-trackhat/led.cpp b/tracker-trackhat/led.cpp new file mode 100644 index 00000000..db5ab6c2 --- /dev/null +++ b/tracker-trackhat/led.cpp @@ -0,0 +1,66 @@ +#include "trackhat.hpp" + +namespace trackhat_impl { + +void led_updater::update_(trackHat_Device_t* device, trackHat_SetLeds_t leds) +{ + if (leds_.ledRedState == leds.ledRedState && + leds_.ledGreenState == leds.ledGreenState && + leds_.ledBlueState == leds.ledBlueState) + return; + (void)trackHat_SetLeds(device, &leds); + leds_ = leds; +} + +trackHat_SetLeds_t led_updater::next_state(led_mode mode, led_state new_state) +{ + switch (mode) + { + case led_mode::off: + state_ = led_state::stopped; + timer_ = std::nullopt; + return LED_off; + default: + case led_mode::constant: + state_ = led_state::stopped; + timer_ = std::nullopt; + return LED_idle; + case led_mode::dynamic: + break; + } + + if (new_state <= led_state::stopped) + { + state_ = new_state; + timer_ = std::nullopt; + return LED_idle; + } + else if (new_state == state_) + { + timer_ = std::nullopt; + return leds_; + } + else if (!timer_) + { + timer_ = Timer{}; + return leds_; + } + else if (timer_->elapsed_ms() > SWITCH_TIME_MS) + { + state_ = new_state; + timer_ = std::nullopt; + return new_state == led_state::not_tracking + ? LED_not_tracking + : LED_tracking; + } + else + return leds_; +} + +void led_updater::update(trackHat_Device_t* device, led_mode mode, led_state new_state) +{ + auto leds = next_state(mode, new_state); + update_(device, leds); +} + +} // namespace trackhat_impl |