From 0001d1db643ce61bb58f84d9756d7e159e0ae5cc Mon Sep 17 00:00:00 2001 From: Hugo Pezziardi Date: Tue, 1 Mar 2022 17:09:39 +0100 Subject: [PATCH] :sparkles: (animation): Add Waiting (by breathing) animation --- libs/LedKit/CMakeLists.txt | 1 + libs/LedKit/include/internal/Waiting.h | 60 +++++++++++++ libs/LedKit/source/Waiting.cpp | 112 +++++++++++++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 libs/LedKit/include/internal/Waiting.h create mode 100644 libs/LedKit/source/Waiting.cpp diff --git a/libs/LedKit/CMakeLists.txt b/libs/LedKit/CMakeLists.txt index 8f2549dbac..8fc3335572 100644 --- a/libs/LedKit/CMakeLists.txt +++ b/libs/LedKit/CMakeLists.txt @@ -13,6 +13,7 @@ target_include_directories(LedKit target_sources(LedKit PRIVATE source/LedKit.cpp + source/Waiting.cpp ) target_link_libraries(LedKit diff --git a/libs/LedKit/include/internal/Waiting.h b/libs/LedKit/include/internal/Waiting.h new file mode 100644 index 0000000000..1931fbb6d5 --- /dev/null +++ b/libs/LedKit/include/internal/Waiting.h @@ -0,0 +1,60 @@ +// Leka - LekaOS +// Copyright 2022 APF France handicap +// SPDX-License-Identifier: Apache-2.0 + +// + + + + + +// | Stage 1 | Stage 2 | Stage 3 | Stage 4 | +// | | | | | +// | |-\ | |-\ | --- White +// | -/| -\ | -/| -\ | +// | -/ | -\ | -/ | -\ | +// | -/ | -\ | -/ | -\ | +// | -/ | -\ | -/ | -\ | +// | -/ | -\ | -/ | -\ | +// | -/ | -\ | -/ | -\ | +// | -/ | -\| -/ | -\ | +// | -/ | |/ | -\ | +// | -/ | | | -\ | +// | -/ | | | -\ | +// | -/ | | | -\ | +// | -/ | | | -\ | +// | -/ | | | -\| --- Black +// |/ | | | | +// | | | | | + +#pragma once + +// ? LCOV_EXCL_START - Exclude from coverage report + +#include "LEDAnimation.h" + +namespace leka::led::animation { + +class Waiting : public interface::LEDAnimation +{ + public: + explicit Waiting(interface::LED &ears, interface::LED &belt) : _ears(ears), _belt(belt) {}; + + void start() final; + void run() final; + void stop() final; + + private: + interface::LED &_ears; + interface::LED &_belt; + uint8_t _step = 0; + uint8_t _stage = 1; + + [[nodiscard]] auto mapStep(uint8_t step, uint8_t max_input_value) const -> float; + + void stage1(); + void stage2(); + void stage3(); + void stage4(); + + void turnLedBlack(); +}; + +} // namespace leka::led::animation + +// ? LCOV_EXCL_STOP - Exclude from coverage report diff --git a/libs/LedKit/source/Waiting.cpp b/libs/LedKit/source/Waiting.cpp new file mode 100644 index 0000000000..14a2cf05ad --- /dev/null +++ b/libs/LedKit/source/Waiting.cpp @@ -0,0 +1,112 @@ +// Leka - LekaOS +// Copyright 2022 APF France handicap +// SPDX-License-Identifier: Apache-2.0 + +// ? LCOV_EXCL_START - Exclude from coverage report + +#include "Waiting.h" + +#include "MathUtils.h" + +namespace leka::led::animation { + +void Waiting::start() +{ + turnLedBlack(); +} + +void Waiting::stop() +{ + turnLedBlack(); +} + +void Waiting::run() +{ + switch (_stage) { + case 1: + stage1(); + break; + case 2: + stage2(); + break; + case 3: + stage3(); + break; + case 4: + stage4(); + break; + default: + break; + } + _belt.show(); +} + +auto Waiting::mapStep(uint8_t step, uint8_t max_input_value) const -> float +{ + return utils::math::map(step, uint8_t {0}, max_input_value, 0.F, 1.F); +} + +void Waiting::stage1() +{ + static constexpr auto kMaxInputValue1 = uint8_t {70}; + static constexpr auto kMaxInputValue2 = uint8_t {50}; + if (auto pos = mapStep(_step, kMaxInputValue1); pos != 1.F) { + RGB color = ColorKit::colorGradient(RGB::black, RGB::white, pos); + _belt.setColor(color); + _step++; + } else { + _step = kMaxInputValue2; + _stage++; + } +} + +void Waiting::stage2() +{ + static constexpr auto tresholdDown = 0.3F; + static constexpr auto kMaxInputValue2 = uint8_t {50}; + if (auto pos = mapStep(_step, kMaxInputValue2); pos >= tresholdDown) { + RGB color = ColorKit::colorGradient(RGB::black, RGB::white, pos); + _belt.setColor(color); + _step--; + } else { + _stage++; + } +} + +void Waiting::stage3() +{ + static constexpr auto kMaxInputValue1 = uint8_t {70}; + static constexpr auto kMaxInputValue2 = uint8_t {50}; + if (auto pos = mapStep(_step, kMaxInputValue2); pos != 1.F) { + RGB color = ColorKit::colorGradient(RGB::black, RGB::white, pos); + _belt.setColor(color); + _step++; + } else { + _step = kMaxInputValue1; + _stage++; + } +} + +void Waiting::stage4() +{ + static constexpr auto kMaxInputValue1 = uint8_t {70}; + if (auto pos = mapStep(_step, kMaxInputValue1); pos != 0.F) { + RGB color = ColorKit::colorGradient(RGB::black, RGB::white, pos); + _belt.setColor(color); + _step--; + } else { + _stage++; + } +} + +void Waiting::turnLedBlack() +{ + _ears.setColor(RGB::black); + _belt.setColor(RGB::black); + _ears.show(); + _belt.show(); +} + +} // namespace leka::led::animation + +// ? LCOV_EXCL_STOP - Exclude from coverage report