Skip to content

Commit

Permalink
♻️ (activity): Refactor EmotionRecognition
Browse files Browse the repository at this point in the history
  • Loading branch information
HPezz committed Sep 20, 2022
1 parent e5fdb0f commit c26729a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 33 deletions.
18 changes: 8 additions & 10 deletions libs/ActivityKit/include/activities/EmotionRecognition.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// LCOV_EXCL_START

#include <optional>
#include <random>

#include "RFIDKit.h"
#include "ReinforcerKit.h"
Expand All @@ -27,23 +26,22 @@ class EmotionRecognition : public interface::Activity
void stop() final;

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

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

static constexpr uint8_t kScoreToWin = 5;
static constexpr uint8_t kSizeOfEmotionsTable = 5;

uint8_t _score = 0;
std::optional<Emotion> _emotion {};
static constexpr uint8_t kRoundsNumber = 5;
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, 2 *kSizeOfEmotionsTable> emotions_table = {
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};
std::span<Emotion> emotions_span {emotions_table};

void setNextDisplay();
};

} // namespace leka::activity
Expand Down
50 changes: 27 additions & 23 deletions libs/ActivityKit/source/activities/EmotionRecognition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,49 @@
// LCOV_EXCL_START

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

namespace leka::activity {

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

_backup_callback = _rfidkit.getCallback();
std::shuffle(emotions_span.begin(), emotions_span.end(), std::mt19937(time(nullptr)));
setNextDisplay();
auto on_tag_detected_callback = [this](const MagicCard &card) {
if (card == std::get<0>(_emotion->cards) || card == std::get<1>(_emotion->cards)) {
_reinforcerkit.playDefault();
++_score;

if (_score == kScoreToWin) {
_backup_callback(MagicCard::dice_roll);
return;
}

setNextDisplay();
} else {
_backup_callback(card);
}
};
_rfidkit.onTagActivated(on_tag_detected_callback);
std::shuffle(_emotions.begin(), _emotions.end(), std::mt19937(time(nullptr)));
launchNextRound();

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

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

void EmotionRecognition::setNextDisplay()
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()
{
_emotion = emotions_table.at(_score);
_current_emotion = _emotions.at(_current_round);

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

Expand Down

0 comments on commit c26729a

Please sign in to comment.