Skip to content

Commit eb77bc6

Browse files
committed
✨ (CommandKit): Add BehaviorCommand
1 parent 617322f commit eb77bc6

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

app/os/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include "SuperSimon.h"
6666
#include "VideoKit.h"
6767
#include "bootutil/bootutil.h"
68+
#include "commands/BehaviorCommand.h"
6869
#include "commands/LedFullCommand.h"
6970
#include "commands/LedRangeCommand.h"
7071
#include "commands/LedSingleCommand.h"
@@ -292,6 +293,7 @@ namespace command {
292293
auto led_range = LedRangeCommand {leds::ears, leds::belt};
293294
auto motors = MotorsCommand {motors::left::motor, motors::right::motor};
294295
auto reinforcer = ReinforcerCommand {reinforcerkit};
296+
auto behavior = BehaviorCommand {behaviorkit};
295297

296298
} // namespace internal
297299

@@ -301,6 +303,7 @@ namespace command {
301303
&internal::led_range,
302304
&internal::motors,
303305
&internal::reinforcer,
306+
&internal::behavior,
304307
});
305308

306309
} // namespace command

libs/CommandKit/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ target_link_libraries(CommandKit
1919
CoreLED
2020
CoreMotor
2121
ReinforcerKit
22+
BehaviorKit
2223
EventLoopKit
2324
mbed-os
2425
)
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Leka - LekaOS
2+
// Copyright 2022 APF France handicap
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
#pragma once
6+
7+
#include <span>
8+
9+
#include "BehaviorKit.h"
10+
#include "Utils.h"
11+
#include "interface/Command.h"
12+
13+
namespace leka {
14+
15+
struct BehaviorCommand : interface::Command {
16+
explicit BehaviorCommand(BehaviorKit &kit) : _behaviorkit(kit) {}
17+
18+
auto id() -> uint8_t override { return cmd::id; }
19+
20+
auto data() -> uint8_t * override
21+
{
22+
args = {};
23+
return args.data();
24+
}
25+
26+
[[nodiscard]] auto size() const -> std::size_t override { return std::size(args); }
27+
28+
auto execute() -> bool override
29+
{
30+
auto [id, chcksm] = std::tuple_cat(args);
31+
32+
auto expected = [&] { return utils::math::checksum8(std::span {args.data(), args.size() - 1}); };
33+
34+
if (chcksm != expected()) {
35+
return false;
36+
}
37+
38+
switch (id) {
39+
case cmd::behavior::stop:
40+
_behaviorkit.stop();
41+
break;
42+
case cmd::behavior::launching:
43+
_behaviorkit.launching();
44+
break;
45+
case cmd::behavior::sleeping:
46+
_behaviorkit.sleeping();
47+
break;
48+
case cmd::behavior::waiting:
49+
_behaviorkit.waiting();
50+
break;
51+
case cmd::behavior::blink_on_charge:
52+
_behaviorkit.blinkOnCharge();
53+
break;
54+
case cmd::behavior::low_battery:
55+
_behaviorkit.lowBattery();
56+
break;
57+
case cmd::behavior::charging_empty:
58+
_behaviorkit.chargingEmpty();
59+
break;
60+
case cmd::behavior::charging_low:
61+
_behaviorkit.chargingLow();
62+
break;
63+
case cmd::behavior::charging_medium:
64+
_behaviorkit.chargingMedium();
65+
break;
66+
case cmd::behavior::charging_high:
67+
_behaviorkit.chargingHigh();
68+
break;
69+
case cmd::behavior::charging_full:
70+
_behaviorkit.chargingFull();
71+
break;
72+
case cmd::behavior::ble_connection_without_video:
73+
_behaviorkit.bleConnectionWithoutVideo();
74+
break;
75+
case cmd::behavior::ble_connection_with_video:
76+
_behaviorkit.bleConnectionWithVideo();
77+
break;
78+
case cmd::behavior::working:
79+
_behaviorkit.working();
80+
break;
81+
case cmd::behavior::file_exchange:
82+
_behaviorkit.fileExchange();
83+
break;
84+
default:
85+
_behaviorkit.stop();
86+
break;
87+
}
88+
89+
return true;
90+
}
91+
92+
private:
93+
struct cmd {
94+
static constexpr auto id = uint8_t {0x60};
95+
static constexpr auto size = uint8_t {1 + 1}; // id + page + Checksum
96+
97+
struct behavior {
98+
static constexpr auto stop = uint8_t {0x00};
99+
static constexpr auto launching = uint8_t {0x01};
100+
static constexpr auto sleeping = uint8_t {0x02};
101+
static constexpr auto waiting = uint8_t {0x03};
102+
static constexpr auto blink_on_charge = uint8_t {0x04};
103+
static constexpr auto low_battery = uint8_t {0x05};
104+
static constexpr auto charging_empty = uint8_t {0x06};
105+
static constexpr auto charging_low = uint8_t {0x07};
106+
static constexpr auto charging_medium = uint8_t {0x08};
107+
static constexpr auto charging_high = uint8_t {0x09};
108+
static constexpr auto charging_full = uint8_t {0x0A};
109+
static constexpr auto ble_connection_without_video = uint8_t {0x0B};
110+
static constexpr auto ble_connection_with_video = uint8_t {0x0C};
111+
static constexpr auto working = uint8_t {0x0D};
112+
static constexpr auto file_exchange = uint8_t {0x0E};
113+
};
114+
};
115+
116+
std::array<uint8_t, cmd::size> args {};
117+
BehaviorKit &_behaviorkit;
118+
};
119+
120+
} // namespace leka

spikes/lk_command_kit/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "ReinforcerKit.h"
3939
#include "SDBlockDevice.h"
4040
#include "VideoKit.h"
41+
#include "commands/BehaviorCommand.h"
4142
#include "commands/LedFullCommand.h"
4243
#include "commands/LedRangeCommand.h"
4344
#include "commands/LedSingleCommand.h"
@@ -147,6 +148,7 @@ auto videokit = VideoKit {internal::event_loop, internal::corevideo};
147148

148149
} // namespace display
149150

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

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

165168
} // namespace internal
166169

@@ -211,6 +214,7 @@ auto list = std::to_array<interface::Command *>({
211214
&internal::led_full,
212215
&internal::led_range,
213216
&internal::reinforcer,
217+
&internal::behavior,
214218
});
215219

216220
} // namespace command

0 commit comments

Comments
 (0)