From bd3f48b4630db7e23105703d5c4303c6e82f3a34 Mon Sep 17 00:00:00 2001 From: crane-hiromu Date: Sun, 25 Sep 2022 06:37:44 +0900 Subject: [PATCH] update actions types and update test --- Sources/DroidKit/Core/DroidOperator.swift | 19 +++++--- Sources/DroidKit/Model/DroidWheel.swift | 26 +++++++++-- Sources/DroidKit/View/DroidKitDebugView.swift | 6 +-- .../Core/DroidOperatorTest.swift | 2 +- .../Model/DroidWheelActionTest.swift | 44 +++++++++++++++++++ .../Model/DroidWheelOptionTest.swift | 34 -------------- 6 files changed, 83 insertions(+), 48 deletions(-) create mode 100644 Tests/DroidKitTests/Model/DroidWheelActionTest.swift delete mode 100644 Tests/DroidKitTests/Model/DroidWheelOptionTest.swift diff --git a/Sources/DroidKit/Core/DroidOperator.swift b/Sources/DroidKit/Core/DroidOperator.swift index fc0e5d1..4e0b1e9 100644 --- a/Sources/DroidKit/Core/DroidOperator.swift +++ b/Sources/DroidKit/Core/DroidOperator.swift @@ -23,7 +23,8 @@ public protocol DroidOperatorProtocol: AnyObject { func go(at speed: Double) async throws func back(at speed: Double) async throws func turn(by degree: Double) async throws - func stop(_ type: DroidWheel) async throws + func stop() async throws + func endTurn() async throws func changeLEDColor(to color: UIColor) async throws func playSound(_ type: DroidSound) async throws func wait(for seconds: Double) async throws @@ -80,13 +81,13 @@ extension DroidOperator: DroidOperatorProtocol { /// move forward public func go(at speed: Double) async throws { - let payload = [DroidWheel.move.rawValue, DroidWheelOption.go(speed: speed).value] + let payload = [DroidWheel.move.rawValue, DroidWheelMovementAction.go(speed: speed).value] try await action(command: .moveWheel, payload: payload) } /// move back public func back(at speed: Double) async throws { - let payload = [DroidWheel.move.rawValue, DroidWheelOption.back(speed: speed).value] + let payload = [DroidWheel.move.rawValue, DroidWheelMovementAction.back(speed: speed).value] try await action(command: .moveWheel, payload: payload) } @@ -94,13 +95,19 @@ extension DroidOperator: DroidOperatorProtocol { /// - right: 0~90 /// - left: 90~180 public func turn(by degree: Double) async throws { - let payload = [DroidWheel.turn.rawValue, DroidWheelOption.turn(degree: degree).value] + let payload = [DroidWheel.turn.rawValue, DroidWheelTurnAction.turn(degree: degree).value] try await action(command: .moveWheel, payload: payload) } /// stop moving - public func stop(_ type: DroidWheel) async throws { - let payload = [type.rawValue, DroidWheelOption.end.value] + public func stop() async throws { + let payload = [DroidWheel.move.rawValue, DroidWheelMovementAction.end.value] + try await action(command: .moveWheel, payload: payload) + } + + /// reset wheel degree + public func endTurn() async throws { + let payload = [DroidWheel.turn.rawValue, DroidWheelTurnAction.end.value] try await action(command: .moveWheel, payload: payload) } diff --git a/Sources/DroidKit/Model/DroidWheel.swift b/Sources/DroidKit/Model/DroidWheel.swift index 923642e..8130f87 100644 --- a/Sources/DroidKit/Model/DroidWheel.swift +++ b/Sources/DroidKit/Model/DroidWheel.swift @@ -11,13 +11,18 @@ import Foundation public enum DroidWheel: UInt8 { case turn = 1 case move = 2 + static let endValue: UInt8 = 128 } -// MARK: - Wheel Option Type -public enum DroidWheelOption { +// MARK: - Action Protocol +protocol DroidWheelAction { + var value: UInt8 { get } +} + +// MARK: - Movement Action Type +public enum DroidWheelMovementAction: DroidWheelAction { case go(speed: Double) case back(speed: Double) - case turn(degree: Double) case end public var value: UInt8 { @@ -38,6 +43,19 @@ public enum DroidWheelOption { /// canvert value return UInt8(128 - round(Double(128) * result)) + case .end: + return DroidWheel.endValue + } + } +} + +// MARK: - Turn Action Type +public enum DroidWheelTurnAction: DroidWheelAction { + case turn(degree: Double) + case end + + public var value: UInt8 { + switch self { case .turn(let degree): /// if degree is less than 0, no calculation guard 0 < degree else { return 0 } @@ -47,7 +65,7 @@ public enum DroidWheelOption { return UInt8(round(result / Double(180) * Double(255))) case .end: - return 128 + return DroidWheel.endValue } } } diff --git a/Sources/DroidKit/View/DroidKitDebugView.swift b/Sources/DroidKit/View/DroidKitDebugView.swift index 1502604..9c6a66d 100644 --- a/Sources/DroidKit/View/DroidKitDebugView.swift +++ b/Sources/DroidKit/View/DroidKitDebugView.swift @@ -96,12 +96,12 @@ private extension DroidKitDebugView { DroidKitActionButton(title: "Go") { try await droidOperator.go(at: speed) try await droidOperator.wait(for: duration) - try await droidOperator.stop(.move) + try await droidOperator.stop() } DroidKitActionButton(title: "Back") { try await droidOperator.back(at: speed) try await droidOperator.wait(for: duration) - try await droidOperator.stop(.move) + try await droidOperator.stop() } } } @@ -121,7 +121,7 @@ private extension DroidKitDebugView { try await droidOperator.turn(by: degree) } DroidKitActionButton(title: "Reset") { - try await droidOperator.stop(.turn) + try await droidOperator.endTurn() } } .padding(.bottom, 16) diff --git a/Tests/DroidKitTests/Core/DroidOperatorTest.swift b/Tests/DroidKitTests/Core/DroidOperatorTest.swift index d8cf834..69f8d45 100644 --- a/Tests/DroidKitTests/Core/DroidOperatorTest.swift +++ b/Tests/DroidKitTests/Core/DroidOperatorTest.swift @@ -69,7 +69,7 @@ final class DroidOperatorTest: XCTestCase { func testStop() async throws { let droidConnectorMock = DroidConnectorMock() let droidOperator = DroidOperator(connector: droidConnectorMock) - try await droidOperator.stop(.move) + try await droidOperator.stop() XCTAssertTrue(droidConnectorMock.writeValueCalled) } diff --git a/Tests/DroidKitTests/Model/DroidWheelActionTest.swift b/Tests/DroidKitTests/Model/DroidWheelActionTest.swift new file mode 100644 index 0000000..eadc44b --- /dev/null +++ b/Tests/DroidKitTests/Model/DroidWheelActionTest.swift @@ -0,0 +1,44 @@ +// +// DroidWheelActionTest.swift +// +// +// Created by h.crane on 2022/09/18. +// + +import Foundation +import XCTest +@testable import DroidKit + +// MARK: - Test +final class DroidWheelActionTest: XCTestCase { + + func testMovementActionValue() { + XCTContext.runActivity(named: "case .go") { _ in + XCTAssertEqual(DroidWheelMovementAction.go(speed: -1).value, 0) + XCTAssertEqual(DroidWheelMovementAction.go(speed: 0.5).value, 191) + XCTAssertEqual(DroidWheelMovementAction.go(speed: 2).value, 255) + } + + XCTContext.runActivity(named: "case .back") { _ in + XCTAssertEqual(DroidWheelMovementAction.back(speed: -1).value, 0) + XCTAssertEqual(DroidWheelMovementAction.back(speed: 0.5).value, 64) + XCTAssertEqual(DroidWheelMovementAction.back(speed: 2).value, 0) + } + + XCTContext.runActivity(named: "case .end") { _ in + XCTAssertEqual(DroidWheelMovementAction.end.value, 128) + } + } + + func testTurnActionValue() { + XCTContext.runActivity(named: "case .turn") { _ in + XCTAssertEqual(DroidWheelTurnAction.turn(degree: -1).value, 0) + XCTAssertEqual(DroidWheelTurnAction.turn(degree: 90).value, 128) + XCTAssertEqual(DroidWheelTurnAction.turn(degree: 181).value, 255) + } + + XCTContext.runActivity(named: "case .end") { _ in + XCTAssertEqual(DroidWheelTurnAction.end.value, 128) + } + } +} diff --git a/Tests/DroidKitTests/Model/DroidWheelOptionTest.swift b/Tests/DroidKitTests/Model/DroidWheelOptionTest.swift deleted file mode 100644 index 1a6c611..0000000 --- a/Tests/DroidKitTests/Model/DroidWheelOptionTest.swift +++ /dev/null @@ -1,34 +0,0 @@ -// -// DroidWheelOptionTest.swift -// -// -// Created by h.crane on 2022/09/18. -// - -import Foundation -import XCTest -@testable import DroidKit - -// MARK: - Test -final class DroidWheelOptionTest: XCTestCase { - - func testValue() { - XCTContext.runActivity(named: "case .go") { _ in - XCTAssertEqual(DroidWheelOption.go(speed: -1).value, 0) - XCTAssertEqual(DroidWheelOption.go(speed: 0.5).value, 191) - XCTAssertEqual(DroidWheelOption.go(speed: 2).value, 255) - } - - XCTContext.runActivity(named: "case .back") { _ in - XCTAssertEqual(DroidWheelOption.back(speed: -1).value, 0) - XCTAssertEqual(DroidWheelOption.back(speed: 0.5).value, 64) - XCTAssertEqual(DroidWheelOption.back(speed: 2).value, 0) - } - - XCTContext.runActivity(named: "case .turn") { _ in - XCTAssertEqual(DroidWheelOption.turn(degree: -1).value, 0) - XCTAssertEqual(DroidWheelOption.turn(degree: 90).value, 128) - XCTAssertEqual(DroidWheelOption.turn(degree: 181).value, 255) - } - } -}