Skip to content

Commit

Permalink
✨ (reinforcer): Add Rainbow reinforcer
Browse files Browse the repository at this point in the history
  • Loading branch information
HPezz committed Mar 15, 2022
1 parent dc38f98 commit 4abfdcc
Show file tree
Hide file tree
Showing 4 changed files with 135 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 @@ -29,6 +29,7 @@ target_sources(LedKit
source/LoadingOrange.cpp
source/LoadingRed.cpp
source/LoadingYellow.cpp
source/Rainbow.cpp
source/Sad.cpp
source/SadCry.cpp
source/Sick.cpp
Expand Down
38 changes: 38 additions & 0 deletions libs/LedKit/include/internal/Rainbow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Leka - LekaOS
// Copyright 2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#pragma once

// ? LCOV_EXCL_START - Exclude from coverage report

#include "LEDAnimation.h"

namespace leka::led::animation {

class Rainbow : public interface::LEDAnimation
{
public:
explicit Rainbow(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;
uint8_t _rainbow_tail_index = 0;

void stagesRainbow();
void moveRainbow();
[[nodiscard]] auto getRainbowColor(uint8_t index) const -> RGB;

void turnLedBlack();
};

} // namespace leka::led::animation

// ? LCOV_EXCL_STOP - Exclude from coverage report
88 changes: 88 additions & 0 deletions libs/LedKit/source/Rainbow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Leka - LekaOS
// Copyright 2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

// ? LCOV_EXCL_START - Exclude from coverage report

#include "Rainbow.h"

#include "MathUtils.h"

namespace leka::led::animation {

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

void Rainbow::stop()
{
turnLedBlack();
_step = 0;
_stage = 1;
_rainbow_tail_index = 0;
}

void Rainbow::run()
{
if (auto kLastStage = uint8_t {255}; _stage <= kLastStage) {
stagesRainbow();
} else {
_belt.setColor(RGB::black);
}
_belt.show();
}

void Rainbow::stagesRainbow()
{
_belt.setColor(RGB::pure_red);
moveRainbow();
_stage++;
}

void Rainbow::moveRainbow()
{
static constexpr auto kNumberOfLedsBelt = uint8_t {20};
if (_rainbow_tail_index == kNumberOfLedsBelt) {
_rainbow_tail_index = 0;
}
for (auto i = _rainbow_tail_index; i < _rainbow_tail_index + kNumberOfLedsBelt; i++) {
if (i < kNumberOfLedsBelt) {
RGB color = getRainbowColor(i - _rainbow_tail_index);
_belt.setColorAtIndex(i, color);
} else {
RGB color = getRainbowColor(i - _rainbow_tail_index);
_belt.setColorAtIndex(i - kNumberOfLedsBelt, color);
}
}
++_rainbow_tail_index;
}

auto Rainbow::getRainbowColor(uint8_t index) const -> RGB
{
static constexpr auto kRainbowColorNumber = 7;
static constexpr std::array<RGB, kRainbowColorNumber> rainbowColor = {
RGB::pure_red, RGB {180, 50, 0}, RGB {100, 100, 0}, RGB::pure_green,
RGB::pure_blue, RGB {0x4b, 0, 0x82}, RGB::magenta};

auto part_of_rainbow = index / 3;
auto position = static_cast<float>(index % 3) / 3;
if (part_of_rainbow < kRainbowColorNumber - 1) {
RGB color =
ColorKit::colorGradient(rainbowColor.at(part_of_rainbow), rainbowColor.at(part_of_rainbow + 1), position);
return color;
}
return rainbowColor.at(kRainbowColorNumber - 1);
}

void Rainbow::turnLedBlack()
{
_ears.setColor(RGB::black);
_belt.setColor(RGB::black);
_ears.show();
_belt.show();
}

} // namespace leka::led::animation

// ? LCOV_EXCL_STOP - Exclude from coverage report
8 changes: 8 additions & 0 deletions spikes/lk_reinforcer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "HelloWorld.h"
#include "LedKit.h"
#include "LogKit.h"
#include "Rainbow.h"
#include "SpinBlink.h"
#include "Sprinkles.h"

Expand All @@ -34,6 +35,7 @@ led::animation::BlinkGreen animation_blink_green(ears, belt);
led::animation::SpinBlink animation_spin_blink(ears, belt);
led::animation::Sprinkles animation_sprinkles(ears, belt);
led::animation::Fire animation_fire(ears, belt);
led::animation::Rainbow animation_rainbow(ears, belt);

HelloWorld hello;

Expand All @@ -46,6 +48,12 @@ auto main() -> int
hello.start();

while (true) {
ledkit.start(animation_rainbow);
rtos::ThisThread::sleep_for(10s);

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

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

Expand Down

0 comments on commit 4abfdcc

Please sign in to comment.