Skip to content

Commit

Permalink
♻️ (RobotKitExample): Add Robot to RobotControlView
Browse files Browse the repository at this point in the history
  • Loading branch information
ladislas committed Oct 9, 2023
1 parent fe3d6d8 commit 44a4f9e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright 2023 APF France handicap
// SPDX-License-Identifier: Apache-2.0

import RobotKit
import SwiftUI

struct MainView: View {
Expand All @@ -24,7 +25,7 @@ struct MainView: View {

NavigationLink(
destination: {
RobotControlView()
RobotControlView(viewModel: RobotControlViewModel(robot: Robot.shared))
},
label: {
Text("Go to robot control")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Leka - iOS Monorepo
// Copyright 2023 APF France handicap
// SPDX-License-Identifier: Apache-2.0

import Combine
import RobotKit
import SwiftUI

class RobotControlViewModel: ObservableObject {

@Published var magicCard: MagicCard = .none
@Published var magicCardImage: Image = Image(systemName: "photo")

private let robot: Robot
private var cancellables: Set<AnyCancellable> = []

init(robot: Robot) {
self.robot = robot
self.robot.onMagicCard()
.receive(on: DispatchQueue.main)
.sink {
self.magicCard = $0
self.magicCardImage = {
switch $0 {
case .none:
Image(systemName: "cross")
case .emergency_stop:
Image(systemName: "exclamationmark.octagon")
case .dice_roll:
Image(systemName: "dice")
default:
Image(systemName: "photo")
}
}($0)
}
.store(in: &cancellables)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@
// Copyright 2023 APF France handicap
// SPDX-License-Identifier: Apache-2.0

import Combine
import RobotKit
import SwiftUI

struct RobotControlView: View {

@StateObject var viewModel: RobotControlViewModel

private let robot = Robot.shared

init(viewModel: RobotControlViewModel) {
self._viewModel = StateObject(wrappedValue: viewModel)
}

var body: some View {
VStack {
VStack(alignment: .leading, spacing: 30) {
Expand All @@ -14,21 +24,21 @@ struct RobotControlView: View {
.font(.title)
HStack {
RobotControlActionButton(title: "Move forward", image: "arrow.up", tint: .orange) {
// TODO(@ladislas): Add command
robot.move(.forward, speed: 1.0)
}
RobotControlActionButton(title: "Move backward", image: "arrow.down", tint: .green) {
// TODO(@ladislas): Add command
robot.move(.backward, speed: 0.5)
}
RobotControlActionButton(title: "Spin clockwise", image: "arrow.clockwise", tint: .indigo) {
// TODO(@ladislas): Add command
robot.spin(.clockwise, speed: 0.7)
}
RobotControlActionButton(
title: "Spin counterclockwise", image: "arrow.counterclockwise", tint: .teal
) {
// TODO(@ladislas): Add command
robot.spin(.counterclockwise, speed: 1.0)
}
RobotControlActionButton(title: "Stop motion", image: "xmark", tint: .red) {
// TODO(@ladislas): Add command
robot.stopMotion()
}
}
}
Expand All @@ -38,19 +48,19 @@ struct RobotControlView: View {
.font(.title)
HStack {
RobotControlActionButton(title: "Individual LEDs", image: "light.max", tint: .orange) {
// TODO(@ladislas): Add command
robot.shine(.spot(ids: [0, 4, 8, 10, 12]), color: .red)
}
RobotControlActionButton(title: "Quarters", image: "light.max", tint: .green) {
// TODO(@ladislas): Add command
robot.shine(.quarterBackLeft, color: .blue)
}
RobotControlActionButton(title: "Halves", image: "light.max", tint: .indigo) {
// TODO(@ladislas): Add command
robot.shine(.halfRight, color: .green)
}
RobotControlActionButton(title: "Full counterclockwise", image: "light.max", tint: .teal) {
// TODO(@ladislas): Add command
RobotControlActionButton(title: "Full", image: "light.max", tint: .teal) {
robot.shine(.full, color: .blue)
}
RobotControlActionButton(title: "Turn off lights", image: "xmark", tint: .red) {
// TODO(@ladislas): Add command
robot.stopLights()
}
}
}
Expand All @@ -60,16 +70,16 @@ struct RobotControlView: View {
.font(.title)
HStack {
RobotControlActionButton(title: "Rainbow", image: "number.circle", tint: .orange) {
// TODO(@ladislas): Add command
robot.run(.rainbow)
}
RobotControlActionButton(title: "Fire", image: "number.circle", tint: .green) {
// TODO(@ladislas): Add command
robot.run(.fire)
}
RobotControlActionButton(title: "Spin 1", image: "number.circle", tint: .indigo) {
// TODO(@ladislas): Add command
robot.run(.spinBlinkGreenOff)
}
RobotControlActionButton(title: "Spin 2", image: "number.circle", tint: .teal) {
// TODO(@ladislas): Add command
robot.run(.spinBlinkBlueViolet)
}
}
}
Expand All @@ -78,10 +88,9 @@ struct RobotControlView: View {
Text("Magic Cards")
.font(.title)
HStack(alignment: .center, spacing: 30) {
Text("ID: 0x\(String(0xDEAD_BEEF, radix: 16, uppercase: true))")
Text("ID: 0x\(String(format: "%04X", viewModel.magicCard.id))")
.monospacedDigit()
// TODO(@ladislas): Display image of magic card read by the robot
Image(systemName: "photo")
viewModel.magicCardImage
.resizable()
.aspectRatio(contentMode: .fit)
.frame(height: 180)
Expand All @@ -100,8 +109,7 @@ struct RobotControlView: View {

var stopButton: some View {
Button {
print("STOP ROBOT")
// TODO(@ladislas): Add command
robot.stop()
} label: {
Image(systemName: "exclamationmark.octagon.fill")
Text("STOP")
Expand All @@ -113,7 +121,9 @@ struct RobotControlView: View {
}

#Preview {
NavigationStack {
RobotControlView()
let viewModel = RobotControlViewModel(robot: Robot.shared)

return NavigationStack {
RobotControlView(viewModel: viewModel)
}
}

0 comments on commit 44a4f9e

Please sign in to comment.