Skip to content

Commit

Permalink
Merge dc28ebc into e3f2f6b
Browse files Browse the repository at this point in the history
  • Loading branch information
HPezz authored Sep 22, 2022
2 parents e3f2f6b + dc28ebc commit 97c3d69
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/os/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "EventLoopKit.h"
#include "FATFileSystem.h"
#include "FirmwareKit.h"
#include "FlashNumberCounting.h"
#include "FoodRecognition.h"
#include "HelloWorld.h"
#include "LedColorRecognition.h"
Expand Down Expand Up @@ -347,6 +348,8 @@ namespace activities {
auto food_recognition = leka::activity::FoodRecognition(rfidkit, display::videokit, reinforcerkit);
auto led_number_counting =
leka::activity::LedNumberCounting(rfidkit, display::videokit, reinforcerkit, leds::belt);
auto flash_number_counting =
leka::activity::FlashNumberCounting(rfidkit, display::videokit, reinforcerkit, leds::belt);

} // namespace internal

Expand All @@ -359,6 +362,7 @@ namespace activities {
{MagicCard::number_4, &internal::emotion_recognition},
{MagicCard::number_5, &internal::food_recognition},
{MagicCard::number_6, &internal::led_number_counting},
{MagicCard::number_7, &internal::flash_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 @@ -22,6 +22,7 @@ target_sources(ActivityKit
source/activities/EmotionRecognition.cpp
source/activities/FoodRecognition.cpp
source/activities/LedNumberCounting.cpp
source/activities/FlashNumberCounting.cpp
)

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

#pragma once

// LCOV_EXCL_START

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

namespace leka::activity {

class FlashNumberCounting : public interface::Activity
{
public:
explicit FlashNumberCounting(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 = 5 * 2;
static constexpr uint8_t kSizeOfFlashNumberTable = 12;

static constexpr std::array<RGB, kSizeOfColorsTable> _colors = {
RGB::pure_blue, RGB::pure_green, RGB::cyan, RGB::yellow, RGB::magenta,
RGB::pure_blue, RGB::pure_green, RGB::cyan, RGB::yellow, RGB::magenta};

uint8_t _current_round = 0;
uint8_t _current_flashes_number = 0;
MagicCard _expected_tag_number = MagicCard::none;
std::function<void(const MagicCard &)> _backup_callback {};
std::array<uint8_t, kSizeOfFlashNumberTable> _flash_numbers = {1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6};
};

} // namespace leka::activity

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

// LCOV_EXCL_START

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

namespace leka::activity {
using namespace std::chrono;

void FlashNumberCounting::start()
{
_current_round = 0;
_current_flashes_number = 0;
_expected_tag_number = MagicCard::none;

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

_backup_callback = _rfidkit.getCallback();
std::shuffle(_flash_numbers.begin(), _flash_numbers.end(), std::mt19937(static_cast<unsigned int>(time(nullptr))));
rtos::ThisThread::sleep_for(2s);

launchNextRound();

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

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

void FlashNumberCounting::processCard(const MagicCard &card)
{
if (card == _expected_tag_number) {
_reinforcerkit.playDefault();
rtos::ThisThread::sleep_for(1s);

++_current_round;

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

void FlashNumberCounting::launchNextRound()
{
_current_flashes_number = _flash_numbers.at(_current_round);
_expected_tag_number = MagicCard(MagicCard::number_0.getId() + _current_flashes_number);

for (auto i = 0; i < _current_flashes_number; ++i) {
_led.setColor(_colors.at(_current_round));
_led.show();
rtos::ThisThread::sleep_for(400ms);
_led.setColor(RGB::black);
_led.show();
rtos::ThisThread::sleep_for(500ms);
}
}

} // namespace leka::activity

// LCOV_EXCL_STOP

0 comments on commit 97c3d69

Please sign in to comment.