From 202cad3cc48ee04bad6f9a51bb5884fe3b40a2f4 Mon Sep 17 00:00:00 2001 From: Hugo Pezziardi Date: Tue, 8 Mar 2022 12:40:28 +0100 Subject: [PATCH] :sparkles: (animation): Add Wake Up animation --- libs/LedKit/CMakeLists.txt | 1 + libs/LedKit/include/internal/WakeUp.h | 69 +++++++++++ libs/LedKit/source/WakeUp.cpp | 160 ++++++++++++++++++++++++++ 3 files changed, 230 insertions(+) create mode 100644 libs/LedKit/include/internal/WakeUp.h create mode 100644 libs/LedKit/source/WakeUp.cpp diff --git a/libs/LedKit/CMakeLists.txt b/libs/LedKit/CMakeLists.txt index 8f2549dbac..da58a5c901 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/WakeUp.cpp ) target_link_libraries(LedKit diff --git a/libs/LedKit/include/internal/WakeUp.h b/libs/LedKit/include/internal/WakeUp.h new file mode 100644 index 0000000000..4b905ebad6 --- /dev/null +++ b/libs/LedKit/include/internal/WakeUp.h @@ -0,0 +1,69 @@ +// Leka - LekaOS +// Copyright 2022 APF France handicap +// SPDX-License-Identifier: Apache-2.0 + +// + Stage 0+ 1 + 2 + 3 + 4 + 5 + 6 + 7 + +// | | | | | | | | | +// | | | | | | | | | +// | | | | | | | | | +// | | | | | | |------------| | --- White +// | | | | | | /| |\ | +// | | | | | | / | | \ | +// | | | | | | / | | \ | +// | | | | |\ | / | | \ | +// | | | | /| \ | / | | \ | +// | | | | / | \ | / | | \ | +// | | | | / | \| / | | \ | +// | | |\ | / | |/ | | \ | +// | | /| \ | / | | | | \ | +// | | / | \| / | | | | \ | +// | | / | |/ | | | | \ | +// | | / | | | | | | \ | +// | | / | | | | | | \ | +// | |/ | | | | | | \| +// |--------| | | | | | | | --- Black +// | | | | | | | | | + +#pragma once + +// ? LCOV_EXCL_START - Exclude from coverage report + +#include "LEDAnimation.h" + +namespace leka::led::animation { + +class WakeUp : public interface::LEDAnimation +{ + public: + explicit WakeUp(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 = 0; + + [[nodiscard]] auto mapStep(uint8_t step) const -> float; + + void stage0(); + void stage1(); + void stage2(); + void stage3(); + void stage4(); + void stage5(); + void stage6(); + void stage7(); + + void increaseBrightness(float treshold); + void decreaseBrightness(float treshold); + + void turnLedBlack(); +}; + +} // namespace leka::led::animation + +// ? LCOV_EXCL_STOP - Exclude from coverage report diff --git a/libs/LedKit/source/WakeUp.cpp b/libs/LedKit/source/WakeUp.cpp new file mode 100644 index 0000000000..0477efbf17 --- /dev/null +++ b/libs/LedKit/source/WakeUp.cpp @@ -0,0 +1,160 @@ +// Leka - LekaOS +// Copyright 2022 APF France handicap +// SPDX-License-Identifier: Apache-2.0 + +// ? LCOV_EXCL_START - Exclude from coverage report + +#include "WakeUp.h" + +#include "LogKit.h" +#include "MathUtils.h" + +namespace leka::led::animation { + +static constexpr auto kTreshold1 = 0.80F; +static constexpr auto kTreshold2 = 0.45F; +static constexpr auto kTreshold3 = 0.9F; +static constexpr auto kTreshold4 = 0.5F; + +void WakeUp::start() +{ + turnLedBlack(); +} + +void WakeUp::stop() +{ + turnLedBlack(); +} + +void WakeUp::run() +{ + switch (_stage) { + case 0: + stage0(); + break; + case 1: + stage1(); + break; + case 2: + stage2(); + break; + case 3: + stage3(); + break; + case 4: + stage4(); + break; + case 5: + stage5(); + break; + case 6: + stage6(); + break; + case 7: + stage7(); + break; + default: + break; + } + log_debug("step = %i", _step); + log_debug("stage = %i", _stage); + _belt.show(); +} + +auto WakeUp::mapStep(uint8_t step) const -> float +{ + constexpr auto kMaxInputValue = uint8_t {25}; + return utils::math::map(step, uint8_t {0}, kMaxInputValue, 0.F, 1.F); +} + +void WakeUp::stage0() +{ + if (auto pos = mapStep(_step); pos != 1.F) { + _step++; + } else { + _step = 0; + _stage++; + } +} + +void WakeUp::stage1() +{ + increaseBrightness(kTreshold1); +} + +void WakeUp::stage2() +{ + decreaseBrightness(kTreshold2); +} + +void WakeUp::stage3() +{ + increaseBrightness(kTreshold3); +} + +void WakeUp::stage4() +{ + decreaseBrightness(kTreshold4); +} + +void WakeUp::stage5() +{ + if (auto pos = mapStep(_step); pos != 1.F) { + RGB color = ColorKit::colorGradient(RGB::black, RGB::white, pos); + _belt.setColor(color); + _step++; + } else { + _step = 0; + _stage++; + } +} + +void WakeUp::stage6() +{ + if (auto pos = mapStep(_step); pos != 1.F) { + RGB color = RGB::white; + _belt.setColor(color); + _step++; + } else { + _stage++; + } +} + +void WakeUp::stage7() +{ + decreaseBrightness(0.F); +} + +void WakeUp::increaseBrightness(float treshold) +{ + if (auto pos = mapStep(_step); pos < treshold) { + RGB color = ColorKit::colorGradient(RGB::black, RGB::white, pos); + _belt.setColor(color); + _step++; + } else { + _stage++; + } +} + +void WakeUp::decreaseBrightness(float treshold) +{ + if (auto pos = mapStep(_step); pos > treshold) { + RGB color = ColorKit::colorGradient(RGB::black, RGB::white, pos); + _belt.setColor(color); + _step--; + } else { + _stage++; + } +} + +void WakeUp::turnLedBlack() +{ + _ears.setColor(RGB::black); + _belt.setColor(RGB::black); + _ears.show(); + _belt.show(); +} + +} // namespace leka::led::animation + +// ? LCOV_EXCL_STOP - Exclude from coverage report