Skip to content

Commit

Permalink
🔀 Merge branch 'ladislas/feature/robotkit+send_commands'
Browse files Browse the repository at this point in the history
  • Loading branch information
ladislas committed Oct 12, 2023
2 parents dee843c + a9b3945 commit 458e6a0
Show file tree
Hide file tree
Showing 11 changed files with 660 additions and 100 deletions.
16 changes: 9 additions & 7 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ identifier_name:
min_length: # only min_length
error: 4 # only error
excluded:
- id
- rx
- tx
- URL
- ble
- BLE
- new
- cmd
- i
- id
- img
- lhs
- rhs
- i
- new
- red
- rhs
- rx
- tx
- URL

line_length:
warning: 130
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ struct RobotControlView: View {
.font(.title)
HStack {
RobotControlActionButton(title: "Move forward", image: "arrow.up", tint: .orange) {
robot.move(.forward, speed: 1.0)
robot.move(.forward(speed: 1.0))
}
RobotControlActionButton(title: "Move backward", image: "arrow.down", tint: .green) {
robot.move(.backward, speed: 0.5)
robot.move(.backward(speed: 0.5))
}
RobotControlActionButton(title: "Spin clockwise", image: "arrow.clockwise", tint: .indigo) {
robot.spin(.clockwise, speed: 0.7)
robot.move(.spin(.clockwise, speed: 0.7))
}
RobotControlActionButton(
title: "Spin counterclockwise", image: "arrow.counterclockwise", tint: .teal
) {
robot.spin(.counterclockwise, speed: 1.0)
robot.move(.spin(.counterclockwise, speed: 0.7))
}
RobotControlActionButton(title: "Stop motion", image: "xmark", tint: .red) {
robot.stopMotion()
Expand All @@ -48,16 +48,21 @@ struct RobotControlView: View {
.font(.title)
HStack {
RobotControlActionButton(title: "Individual LEDs", image: "light.max", tint: .orange) {
robot.shine(.spot(ids: [0, 4, 8, 10, 12]), color: .red)
robot.shine(.spot(.belt, ids: [0, 4, 8, 10, 12], in: .red))
}
RobotControlActionButton(title: "Quarters", image: "light.max", tint: .green) {
robot.shine(.quarterBackLeft, color: .blue)
robot.shine(.quarterFrontLeft(in: .blue))
robot.shine(.quarterFrontRight(in: .red))
robot.shine(.quarterBackLeft(in: .red))
robot.shine(.quarterBackRight(in: .blue))
}
RobotControlActionButton(title: "Halves", image: "light.max", tint: .indigo) {
robot.shine(.halfRight, color: .green)
robot.shine(.halfRight(in: .green))
robot.shine(.halfLeft(in: .red))
}
RobotControlActionButton(title: "Full", image: "light.max", tint: .teal) {
robot.shine(.full, color: .blue)
RobotControlActionButton(title: "Full belt + ears", image: "light.max", tint: .teal) {
robot.shine(.full(.belt, in: .blue))
robot.shine(.full(.ears, in: .green))
}
RobotControlActionButton(title: "Turn off lights", image: "xmark", tint: .red) {
robot.stopLights()
Expand Down
17 changes: 17 additions & 0 deletions Modules/RobotKit/Sources/Extensions/Array+checksum8.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Leka - iOS Monorepo
// Copyright 2023 APF France handicap
// SPDX-License-Identifier: Apache-2.0

extension Array where Element == UInt8 {

public var checksum8: UInt8 {
var checksum: Int = 0

for value in self {
checksum = (Int(value) + checksum) % 256
}

return UInt8(checksum)
}

}
45 changes: 45 additions & 0 deletions Modules/RobotKit/Sources/Robot+Colors.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Leka - iOS Monorepo
// Copyright 2023 APF France handicap
// SPDX-License-Identifier: Apache-2.0

import Foundation

extension Robot {

public struct Color {

public var data: [UInt8]

init(_ values: UInt8...) {
guard values.count == 3 else { fatalError() }
data = values
}

public var red: UInt8 {
data[0]
}

public var green: UInt8 {
data[1]
}

public var blue: UInt8 {
data[2]
}

public static let black: Color = Color(0, 0, 0)
public static let white: Color = Color(255, 255, 255)

public static let red: Color = Color(255, 0, 0)
public static let green: Color = Color(0, 150, 0)
public static let blue: Color = Color(0, 0, 255)

public static let orange: Color = Color(248, 100, 0)
public static let yellow: Color = Color(255, 255, 0)
public static let lightBlue: Color = Color(0, 121, 255)
public static let purple: Color = Color(20, 0, 80)
public static let pink: Color = Color(255, 0, 127)

}

}
29 changes: 29 additions & 0 deletions Modules/RobotKit/Sources/Robot+Commands.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Leka - iOS Monorepo
// Copyright 2023 APF France handicap
// SPDX-License-Identifier: Apache-2.0

import Foundation

extension Robot {

static let kHeaderPattern: [UInt8] = [0x2A, 0x2A, 0x2A, 0x2A]

func commandGenerator(commands: [UInt8]...) -> Data {
commandGenerator(commands: commands)
}

func commandGenerator(commands: [[UInt8]]) -> Data {
let commands = commands.filter { $0 != [] }
var output: [UInt8] = []

output.append(contentsOf: Robot.kHeaderPattern)
output.append(UInt8(commands.count))

for command in commands {
output.append(contentsOf: command)
}

return Data(output)
}

}
Loading

0 comments on commit 458e6a0

Please sign in to comment.