Skip to content

Commit

Permalink
🔀 Merge branch 'hugo/feature/Add-PictoEmotionRecognition-activity' in…
Browse files Browse the repository at this point in the history
…to develop
  • Loading branch information
ladislas committed Sep 22, 2022
2 parents 279f8aa + c9072b1 commit 575f0d1
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/os/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "CoreTimeout.h"
#include "CoreVideo.hpp"
#include "DisplayTags.h"
#include "EmotionRecognition.h"
#include "EventLoopKit.h"
#include "FATFileSystem.h"
#include "FirmwareKit.h"
Expand Down Expand Up @@ -340,6 +341,7 @@ namespace activities {
auto picto_color_recognition = leka::activity::PictoColorRecognition(rfidkit, display::videokit, reinforcerkit);
auto led_color_recognition =
leka::activity::LedColorRecognition(rfidkit, display::videokit, reinforcerkit, leds::belt);
auto emotion_recognition = leka::activity::EmotionRecognition(rfidkit, display::videokit, reinforcerkit);

} // namespace internal

Expand All @@ -349,6 +351,7 @@ namespace activities {
{MagicCard::number_1, &internal::number_recognition},
{MagicCard::number_2, &internal::picto_color_recognition},
{MagicCard::number_3, &internal::led_color_recognition},
{MagicCard::number_4, &internal::emotion_recognition},
};

} // 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 @@ -19,6 +19,7 @@ target_sources(ActivityKit
source/activities/PictoColorRecognition.cpp
source/activities/LedColorRecognition.cpp
source/activities/ChooseReinforcer.cpp
source/activities/EmotionRecognition.cpp
)

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

#pragma once

// LCOV_EXCL_START

#include <optional>

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

namespace leka::activity {

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

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

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

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

static constexpr uint8_t kRoundsNumber = 10;
static constexpr uint8_t kSizeOfEmotionsTable = 5 * 2;

uint8_t _current_round = 0;
std::optional<Emotion> _current_emotion {};
std::function<void(const MagicCard &)> _backup_callback {};
std::array<Emotion, kSizeOfEmotionsTable> _emotions = {
Emotion::anger, Emotion::anger, Emotion::fear, Emotion::fear, Emotion::joy,
Emotion::joy, Emotion::sadness, Emotion::sadness, Emotion::disgust, Emotion::disgust};
};

} // namespace leka::activity

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

// LCOV_EXCL_START

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

namespace leka::activity {

void EmotionRecognition::start()
{
_current_round = 0;
_current_emotion = {};

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

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

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

void EmotionRecognition::processCard(const MagicCard &card)
{
if (card == std::get<0>(_current_emotion->cards) || card == std::get<1>(_current_emotion->cards)) {
_reinforcerkit.playDefault();
++_current_round;

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

launchNextRound();
} else {
_backup_callback(card);
}
}

void EmotionRecognition::launchNextRound()
{
_current_emotion = _emotions.at(_current_round);

auto full_path = "/fs/home/img/id/" + std::string(_current_emotion->id) + ".jpg";
_videokit.displayImage(full_path);
}

} // namespace leka::activity

// LCOV_EXCL_STOP

0 comments on commit 575f0d1

Please sign in to comment.