|
| 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 |
0 commit comments