From 72272169cc492e1c8ee13dc047e075137e2bad6c Mon Sep 17 00:00:00 2001 From: Hugo Pezziardi Date: Wed, 20 Mar 2024 13:06:36 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20(RobotKit):=20Add=20Behaviors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sources/Views/RobotControlView.swift | 28 +++++++++++ .../RobotKit/Sources/Robot+Behaviors.swift | 48 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 Modules/RobotKit/Sources/Robot+Behaviors.swift diff --git a/Modules/RobotKit/Examples/RobotKitExample/Sources/Views/RobotControlView.swift b/Modules/RobotKit/Examples/RobotKitExample/Sources/Views/RobotControlView.swift index 93a3383b55..afdeb52b76 100644 --- a/Modules/RobotKit/Examples/RobotKitExample/Sources/Views/RobotControlView.swift +++ b/Modules/RobotKit/Examples/RobotKitExample/Sources/Views/RobotControlView.swift @@ -90,6 +90,34 @@ struct RobotControlView: View { } } + VStack(alignment: .leading, spacing: 5) { + Text("Behaviors") + .font(.title) + HStack { + RobotControlActionButton(title: "Magic Card detected", image: "figure.wave", tint: .orange) { + self.robot.run(.magicCardDetected) + } + RobotControlActionButton(title: "BLE Connection without video", image: "figure.wave", tint: .green) { + self.robot.run(.bleConnectionWithoutVideo) + } + RobotControlActionButton(title: "Launching", image: "figure.wave", tint: .indigo) { + self.robot.run(.launching) + } + RobotControlActionButton(title: "Low battery", image: "figure.wave", tint: .teal) { + self.robot.run(.lowBattery) + } + RobotControlActionButton(title: "Charging full", image: "figure.wave", tint: .orange) { + self.robot.run(.chargingFull) + } + RobotControlActionButton(title: "Medium low battery", image: "figure.wave", tint: .green) { + self.robot.run(.mediumLowBattery) + } + RobotControlActionButton(title: "BLE Connection with video", image: "figure.wave", tint: .indigo) { + self.robot.run(.bleConnectionWithVideo) + } + } + } + VStack(alignment: .leading, spacing: 5) { Text("Magic Cards") .font(.title) diff --git a/Modules/RobotKit/Sources/Robot+Behaviors.swift b/Modules/RobotKit/Sources/Robot+Behaviors.swift new file mode 100644 index 0000000000..7cae388a69 --- /dev/null +++ b/Modules/RobotKit/Sources/Robot+Behaviors.swift @@ -0,0 +1,48 @@ +// Leka - iOS Monorepo +// Copyright APF France handicap +// SPDX-License-Identifier: Apache-2.0 + +public extension Robot { + enum Behavior: UInt8, CaseIterable { + case stop = 0x00 + case launching = 0x01 + case sleeping = 0x02 + case waiting = 0x03 + case blinkOnCharge = 0x04 + case lowBattery = 0x05 + case chargingEmpty = 0x06 + case chargingLow = 0x07 + case chargingMedium = 0x08 + case chargingHigh = 0x09 + case chargingFull = 0x0A + case bleConnectionWithoutVideo = 0x0B + case bleConnectionWithVideo = 0x0C + case working = 0x0D + case fileExchange = 0x0E + case magicCardDetected = 0x0F + case mediumLowBattery = 0x10 + + // MARK: Internal + + static let id: UInt8 = 0x60 + + var cmd: [UInt8] { + let output: [UInt8] = [ + Self.id, + rawValue, + [rawValue].checksum8, + ] + + return output + } + } + + func run(_ behavior: Behavior) { + log.trace("🤖 RUN reinforcer \(behavior)") + + let output = Self.commandGenerator(commands: behavior.cmd) + + connectedPeripheral? + .sendCommand(output) + } +}