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 LedNumberCounting activity #1011

Merged
merged 2 commits into from
Sep 22, 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
4 changes: 4 additions & 0 deletions app/os/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "FoodRecognition.h"
#include "HelloWorld.h"
#include "LedColorRecognition.h"
#include "LedNumberCounting.h"
#include "LogKit.h"
#include "NumberRecognition.h"
#include "PictoColorRecognition.h"
Expand Down Expand Up @@ -344,6 +345,8 @@ namespace activities {
leka::activity::LedColorRecognition(rfidkit, display::videokit, reinforcerkit, leds::belt);
auto emotion_recognition = leka::activity::EmotionRecognition(rfidkit, display::videokit, reinforcerkit);
auto food_recognition = leka::activity::FoodRecognition(rfidkit, display::videokit, reinforcerkit);
auto led_number_counting =
leka::activity::LedNumberCounting(rfidkit, display::videokit, reinforcerkit, leds::belt);

} // namespace internal

Expand All @@ -355,6 +358,7 @@ namespace activities {
{MagicCard::number_3, &internal::led_color_recognition},
{MagicCard::number_4, &internal::emotion_recognition},
{MagicCard::number_5, &internal::food_recognition},
{MagicCard::number_6, &internal::led_number_counting},
};

} // namespace activities
Expand Down
1 change: 1 addition & 0 deletions libs/ActivityKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ target_sources(ActivityKit
source/activities/ChooseReinforcer.cpp
source/activities/EmotionRecognition.cpp
source/activities/FoodRecognition.cpp
source/activities/LedNumberCounting.cpp
)

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

#pragma once

// LCOV_EXCL_START

#include "MathUtils.h"
#include "RFIDKit.h"
#include "ReinforcerKit.h"
#include "interface/Activity.h"
#include "interface/libs/VideoKit.h"

namespace leka::activity {

class LedNumberCounting : public interface::Activity
{
public:
explicit LedNumberCounting(RFIDKit &rfidkit, interface::VideoKit &videokit, ReinforcerKit &reinforcerkit,
interface::LED &led)
: _rfidkit(rfidkit), _videokit(videokit), _reinforcerkit(reinforcerkit), _led(led) {};

void start() final;
void stop() final;

private:
void processCard(const MagicCard &card);
void launchNextRound();

RFIDKit &_rfidkit;
interface::VideoKit &_videokit;
ReinforcerKit &_reinforcerkit;
interface::LED &_led;

static constexpr uint8_t kRoundsNumber = 10;
static constexpr uint8_t kSizeOfColorsTable = 6;
static constexpr uint8_t kSizeOfLedNumberTable = 12;
static constexpr uint8_t kSizeOfLedIndexTable = 7;

static constexpr std::array<RGB, kSizeOfColorsTable> colors_table = {RGB::orange, RGB::pure_green, RGB::cyan,
RGB::yellow, RGB::magenta, RGB::pure_red};

uint8_t _current_round = 0;
uint8_t _current_leds_number = 0;
MagicCard _expected_tag_number = MagicCard::none;

std::function<void(const MagicCard &)> _backup_callback {};
std::array<uint8_t, kSizeOfLedNumberTable> _led_numbers = {1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6};
std::array<uint8_t, kSizeOfLedIndexTable> _led_indexes = {0, 3, 6, 9, 12, 15, 19};
};

} // namespace leka::activity

// LCOV_EXCL_STOP
64 changes: 64 additions & 0 deletions libs/ActivityKit/source/activities/LedNumberCounting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Leka - LekaOS
// Copyright 2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

// LCOV_EXCL_START

#include "LedNumberCounting.h"
#include <random>

namespace leka::activity {

void LedNumberCounting::start()
{
_current_round = 0;
_current_leds_number = 0;
_expected_tag_number = MagicCard::none;

_videokit.displayImage("fs/home/img/system/robot-face-smiling-slightly.jpg");

_backup_callback = _rfidkit.getCallback();
std::shuffle(_led_indexes.begin(), _led_indexes.end(), std::mt19937(static_cast<unsigned int>(time(nullptr))));
std::shuffle(_led_numbers.begin(), _led_numbers.end(), std::mt19937(static_cast<unsigned int>(time(nullptr))));
launchNextRound();

_rfidkit.onTagActivated([this](const MagicCard &card) { processCard(card); });
}

void LedNumberCounting::stop()
{
_rfidkit.onTagActivated(_backup_callback);
}

void LedNumberCounting::processCard(const MagicCard &card)
{
if (card == _expected_tag_number) {
_reinforcerkit.playDefault();
++_current_round;

if (_current_round == kRoundsNumber) {
_backup_callback(MagicCard::dice_roll);
return;
}

std::shuffle(_led_indexes.begin(), _led_indexes.end(), std::mt19937(static_cast<unsigned int>(time(nullptr))));
launchNextRound();
} else {
_backup_callback(card);
}
}

void LedNumberCounting::launchNextRound()
{
_current_leds_number = _led_numbers.at(_current_round);
_expected_tag_number = MagicCard(MagicCard::number_0.getId() + _current_leds_number);

for (auto i = 0; i < _current_leds_number; ++i) {
_led.setColorAtIndex(_led_indexes.at(i), colors_table.at(i));
}
_led.show();
}

} // namespace leka::activity

// LCOV_EXCL_STOP