Skip to content

Commit

Permalink
✨ (animation): Add Wake Up animation
Browse files Browse the repository at this point in the history
  • Loading branch information
HPezz committed Mar 8, 2022
1 parent 633575a commit 202cad3
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 0 deletions.
1 change: 1 addition & 0 deletions libs/LedKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ target_include_directories(LedKit
target_sources(LedKit
PRIVATE
source/LedKit.cpp
source/WakeUp.cpp
)

target_link_libraries(LedKit
Expand Down
69 changes: 69 additions & 0 deletions libs/LedKit/include/internal/WakeUp.h
Original file line number Diff line number Diff line change
@@ -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
160 changes: 160 additions & 0 deletions libs/LedKit/source/WakeUp.cpp
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 202cad3

Please sign in to comment.