Skip to content

Commit

Permalink
🔀️ Merge branch 'yann/feature/ble/display-image' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
ladislas committed Jun 26, 2024
2 parents 35bb911 + 17b9c58 commit 8fe199a
Show file tree
Hide file tree
Showing 5 changed files with 129 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 @@ -69,6 +69,7 @@
#include "commands/LedSingleCommand.h"
#include "commands/MotorsCommand.h"
#include "commands/ReinforcerCommand.h"
#include "commands/VideoCommand.h"

using namespace leka;
using namespace std::chrono;
Expand Down Expand Up @@ -290,6 +291,7 @@ namespace command {
auto led_range = LedRangeCommand {leds::ears, leds::belt};
auto motors = MotorsCommand {motors::left::motor, motors::right::motor};
auto reinforcer = ReinforcerCommand {reinforcerkit};
auto video = VideoCommand {videokit};

} // namespace internal

Expand All @@ -299,6 +301,7 @@ namespace command {
&internal::led_range,
&internal::motors,
&internal::reinforcer,
&internal::video,
});

} // namespace command
Expand Down
1 change: 1 addition & 0 deletions libs/CommandKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ if(${CMAKE_PROJECT_NAME} STREQUAL "LekaOSUnitTests")
tests/CommandKit_test_push.cpp
tests/CommandKit_test_register.cpp
tests/CommandRunner_test.cpp
tests/Command_Video_test.cpp
)
endif()
57 changes: 57 additions & 0 deletions libs/CommandKit/include/commands/VideoCommand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Leka - LekaOS
// Copyright 2024 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <span>

#include "Utils.h"
#include "interface/Command.h"
#include "interface/libs/VideoKit.h"

namespace leka {

struct VideoCommand : interface::Command {
explicit VideoCommand(interface::VideoKit &kit) : _videokit(kit) {}

auto id() -> uint8_t override { return cmd::id; }

auto data() -> uint8_t * override
{
args = {};
return args.data();
}

[[nodiscard]] auto size() const -> std::size_t override { return std::size(args); }

auto execute() -> bool override
{
auto [id_high, id_low, chcksm] = std::tuple_cat(args);

auto expected = [&] { return utils::math::checksum8(std::span {args.data(), args.size() - 1}); };

if (chcksm != expected()) {
return false;
}

auto id = (id_high << 8) | id_low;
std::array<char, 32> image_path = {};
snprintf(image_path.data(), image_path.size(), "/fs/home/img/id/%.4X.jpg", id);

_videokit.fillWhiteBackgroundAndDisplayImage(std::filesystem::path {image_path.data()});

return true;
}

private:
struct cmd {
static constexpr auto id = uint8_t {0x80};
static constexpr auto size = uint8_t {2 + 1}; // image_id + Checksum
};

std::array<uint8_t, cmd::size> args {};
interface::VideoKit &_videokit;
};

} // namespace leka
65 changes: 65 additions & 0 deletions libs/CommandKit/tests/Command_Video_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Leka - LekaOS
// Copyright 2024 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#include "CommandKit.h"
#include "commands/VideoCommand.h"
#include "gtest/gtest.h"
#include "mocks/leka/VideoKit.h"

using namespace leka;

class VideoCommandTest : public testing::Test
{
protected:
// void SetUp() override {}
// void TearDown() override {}

mock::VideoKit mock_videokit {};

VideoCommand video_command {mock_videokit};
};

TEST_F(VideoCommandTest, initialization)
{
EXPECT_NE(&video_command, nullptr);
}

TEST_F(VideoCommandTest, commandID)
{
auto expected_cmd_id = 0x80;
auto actual_cmd_id = video_command.id();

EXPECT_EQ(actual_cmd_id, expected_cmd_id);
}

TEST_F(VideoCommandTest, commandSize)
{
auto expected_cmd_size = uint8_t {2 + 1}; // image_id + Checksum
auto actual_cmd_size = video_command.size();

EXPECT_EQ(actual_cmd_size, expected_cmd_size);
}

TEST_F(VideoCommandTest, execute)
{
auto command = std::to_array({0x00, 0x7C, 0x7C});
auto expected_path = std::filesystem::path("/fs/home/img/id/007C.jpg");

auto *command_data = video_command.data();
std::copy(command.begin(), command.end(), command_data);

EXPECT_CALL(mock_videokit, fillWhiteBackgroundAndDisplayImage(expected_path));
video_command.execute();
}

TEST_F(VideoCommandTest, executeIncorrectChecksum)
{
auto command = std::to_array({0x00, 0x7C, 0x22});

auto *command_data = video_command.data();
std::copy(command.begin(), command.end(), command_data);

EXPECT_CALL(mock_videokit, fillWhiteBackgroundAndDisplayImage).Times(0);
video_command.execute();
}
3 changes: 3 additions & 0 deletions spikes/lk_command_kit/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "commands/MotorsCommand.h"
#include "commands/ReinforcerCommand.h"
#include "commands/TestCommand.h"
#include "commands/VideoCommand.h"

using namespace leka;
using namespace std::chrono;
Expand Down Expand Up @@ -161,6 +162,7 @@ namespace internal {
auto led_range = LedRangeCommand {leds::ears, leds::belt};
auto motors = MotorsCommand {motor::left, motor::right};
auto reinforcer = ReinforcerCommand {reinforcerkit};
auto video = VideoCommand {display::videokit};

} // namespace internal

Expand Down Expand Up @@ -211,6 +213,7 @@ auto list = std::to_array<interface::Command *>({
&internal::led_full,
&internal::led_range,
&internal::reinforcer,
&internal::video,
});

} // namespace command
Expand Down

0 comments on commit 8fe199a

Please sign in to comment.