Skip to content

Commit

Permalink
✨ (CommandKit): Add BehaviorCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLocatelli committed Mar 18, 2024
1 parent 617322f commit eb77bc6
Show file tree
Hide file tree
Showing 4 changed files with 128 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 @@ -65,6 +65,7 @@
#include "SuperSimon.h"
#include "VideoKit.h"
#include "bootutil/bootutil.h"
#include "commands/BehaviorCommand.h"
#include "commands/LedFullCommand.h"
#include "commands/LedRangeCommand.h"
#include "commands/LedSingleCommand.h"
Expand Down Expand Up @@ -292,6 +293,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 behavior = BehaviorCommand {behaviorkit};

} // namespace internal

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

} // 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 @@ -19,6 +19,7 @@ target_link_libraries(CommandKit
CoreLED
CoreMotor
ReinforcerKit
BehaviorKit
EventLoopKit
mbed-os
)
Expand Down
120 changes: 120 additions & 0 deletions libs/CommandKit/include/commands/BehaviorCommand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Leka - LekaOS
// Copyright 2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <span>

#include "BehaviorKit.h"
#include "Utils.h"
#include "interface/Command.h"

namespace leka {

struct BehaviorCommand : interface::Command {
explicit BehaviorCommand(BehaviorKit &kit) : _behaviorkit(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, chcksm] = std::tuple_cat(args);

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

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

switch (id) {
case cmd::behavior::stop:
_behaviorkit.stop();
break;
case cmd::behavior::launching:
_behaviorkit.launching();
break;
case cmd::behavior::sleeping:
_behaviorkit.sleeping();
break;
case cmd::behavior::waiting:
_behaviorkit.waiting();
break;
case cmd::behavior::blink_on_charge:
_behaviorkit.blinkOnCharge();
break;
case cmd::behavior::low_battery:
_behaviorkit.lowBattery();
break;
case cmd::behavior::charging_empty:
_behaviorkit.chargingEmpty();
break;
case cmd::behavior::charging_low:
_behaviorkit.chargingLow();
break;
case cmd::behavior::charging_medium:
_behaviorkit.chargingMedium();
break;
case cmd::behavior::charging_high:
_behaviorkit.chargingHigh();
break;
case cmd::behavior::charging_full:
_behaviorkit.chargingFull();
break;
case cmd::behavior::ble_connection_without_video:
_behaviorkit.bleConnectionWithoutVideo();
break;
case cmd::behavior::ble_connection_with_video:
_behaviorkit.bleConnectionWithVideo();
break;
case cmd::behavior::working:
_behaviorkit.working();
break;
case cmd::behavior::file_exchange:
_behaviorkit.fileExchange();
break;
default:
_behaviorkit.stop();
break;
}

return true;
}

private:
struct cmd {
static constexpr auto id = uint8_t {0x60};
static constexpr auto size = uint8_t {1 + 1}; // id + page + Checksum

struct behavior {
static constexpr auto stop = uint8_t {0x00};
static constexpr auto launching = uint8_t {0x01};
static constexpr auto sleeping = uint8_t {0x02};
static constexpr auto waiting = uint8_t {0x03};
static constexpr auto blink_on_charge = uint8_t {0x04};
static constexpr auto low_battery = uint8_t {0x05};
static constexpr auto charging_empty = uint8_t {0x06};
static constexpr auto charging_low = uint8_t {0x07};
static constexpr auto charging_medium = uint8_t {0x08};
static constexpr auto charging_high = uint8_t {0x09};
static constexpr auto charging_full = uint8_t {0x0A};
static constexpr auto ble_connection_without_video = uint8_t {0x0B};
static constexpr auto ble_connection_with_video = uint8_t {0x0C};
static constexpr auto working = uint8_t {0x0D};
static constexpr auto file_exchange = uint8_t {0x0E};
};
};

std::array<uint8_t, cmd::size> args {};
BehaviorKit &_behaviorkit;
};

} // namespace leka
4 changes: 4 additions & 0 deletions spikes/lk_command_kit/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "ReinforcerKit.h"
#include "SDBlockDevice.h"
#include "VideoKit.h"
#include "commands/BehaviorCommand.h"
#include "commands/LedFullCommand.h"
#include "commands/LedRangeCommand.h"
#include "commands/LedSingleCommand.h"
Expand Down Expand Up @@ -147,6 +148,7 @@ auto videokit = VideoKit {internal::event_loop, internal::corevideo};

} // namespace display

auto behaviorkit = BehaviorKit {display::videokit, ledkit, motor::left, motor::right};
auto reinforcerkit = ReinforcerKit {display::videokit, ledkit, motionkit};

namespace command {
Expand All @@ -161,6 +163,7 @@ namespace internal {
auto led_range = LedRangeCommand {leds::ears, leds::belt};
auto motors = MotorsCommand {motor::left, motor::right};
auto reinforcer = ReinforcerCommand {reinforcerkit};
auto behavior = BehaviorCommand {behaviorkit};

} // namespace internal

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

} // namespace command
Expand Down

0 comments on commit eb77bc6

Please sign in to comment.