Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hugo/feature/Add Sleeping animation #504

Merged
merged 2 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libs/LedKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ target_sources(LedKit
source/LedKit.cpp
source/LedKit.cpp
source/Singing.cpp
source/Sleeping.cpp
)

target_link_libraries(LedKit
Expand Down
67 changes: 67 additions & 0 deletions libs/LedKit/include/internal/Sleeping.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Leka - LekaOS
// Copyright 2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

// Stage 1 + Stage 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 Sleeping : public interface::LEDAnimation
{
public:
explicit Sleeping(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) const -> float;

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
143 changes: 143 additions & 0 deletions libs/LedKit/source/Sleeping.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Leka - LekaOS
// Copyright 2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

// ? LCOV_EXCL_START - Exclude from coverage report

#include "Sleeping.h"

#include "MathUtils.h"

namespace leka::led::animation {

void Sleeping::start()
{
turnLedBlack();
}

void Sleeping::stop()
{
turnLedBlack();
}

void Sleeping::run()
{
switch (_stage) {
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;
}
_belt.show();
}

auto Sleeping::mapStep(uint8_t step) const -> float
{
constexpr auto kMaxInputValue = uint8_t {45};
return utils::math::map(step, uint8_t {0}, kMaxInputValue, 0.F, 1.F);
}

void Sleeping::stage1()
{
if (auto pos = mapStep(_step); pos != 1.F) {
RGB color = ColorKit::colorGradient(RGB::black, RGB::white, pos);
_belt.setColor(color);
_step++;
} else {
_step = 0;
HPezz marked this conversation as resolved.
Show resolved Hide resolved
_stage++;
}
}

void Sleeping::stage2()
{
if (auto pos = mapStep(_step); pos != 1.F) {
RGB color = RGB::white;
_belt.setColor(color);
_step++;
} else {
_stage++;
}
}

void Sleeping::stage3()
{
static constexpr auto kTreshold1 = 0.45F;
decreaseBrightness(kTreshold1);
}

void Sleeping::stage4()
{
static constexpr auto kTreshold2 = 0.9F;
increaseBrightness(kTreshold2);
}

void Sleeping::stage5()
{
static constexpr auto kTreshold3 = 0.45F;
decreaseBrightness(kTreshold3);
}

void Sleeping::stage6()
{
static constexpr auto kTreshold4 = 0.80F;
increaseBrightness(kTreshold4);
}

void Sleeping::stage7()
{
decreaseBrightness(0.F);
}

void Sleeping::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 Sleeping::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 Sleeping::turnLedBlack()
{
_ears.setColor(RGB::black);
_belt.setColor(RGB::black);
_ears.show();
_belt.show();
}

} // namespace leka::led::animation

// ? LCOV_EXCL_STOP - Exclude from coverage report
13 changes: 10 additions & 3 deletions spikes/lk_led_kit/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "LedKit.h"
#include "LogKit.h"
#include "Singing.h"
#include "Sleeping.h"

using namespace leka;
using namespace std::chrono;
Expand All @@ -31,12 +32,13 @@ auto animation_thread = rtos::Thread {};
auto animation_event_queue = events::EventQueue {};

auto ledkit = LedKit {animation_thread, animation_event_queue, ears, belt};
led::animation::Happy animation_happy(ears, belt);
led::animation::Disgusted animation_disgusted(ears, belt);

led::animation::Bubbles animation_bubbles(ears, belt);
led::animation::Singing animation_singing(ears, belt);
led::animation::Disgusted animation_disgusted(ears, belt);
led::animation::Fly animation_fly {ears, belt};
led::animation::Happy animation_happy(ears, belt);
led::animation::Singing animation_singing(ears, belt);
led::animation::Sleeping animation_sleeping(ears, belt);

HelloWorld hello;

Expand All @@ -51,11 +53,16 @@ auto main() -> int
while (true) {
ledkit.start(animation_singing);
rtos::ThisThread::sleep_for(10s);

ledkit.start(animation_fly);
rtos::ThisThread::sleep_for(10s);

ledkit.start(animation_happy);
rtos::ThisThread::sleep_for(10s);

ledkit.start(animation_sleeping);
rtos::ThisThread::sleep_for(10s);

ledkit.stop();
rtos::ThisThread::sleep_for(1s);
}
Expand Down