Skip to content

Commit

Permalink
💫 (activity): Add EmotionRecognition activity
Browse files Browse the repository at this point in the history
  • Loading branch information
HPezz committed Sep 22, 2022
1 parent 6328fd7 commit 804a9f2
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
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 804a9f2

Please sign in to comment.