Skip to content

Commit

Permalink
Merge pull request #4 from crane-hiromu/feature/refactor
Browse files Browse the repository at this point in the history
Add action types
  • Loading branch information
crane-hiromu authored Sep 24, 2022
2 parents b104fdb + bd3f48b commit 5c5eff3
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 48 deletions.
19 changes: 13 additions & 6 deletions Sources/DroidKit/Core/DroidOperator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -80,27 +81,33 @@ 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)
}

/// turn towards
/// - 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)
}

Expand Down
26 changes: 22 additions & 4 deletions Sources/DroidKit/Model/DroidWheel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 }
Expand All @@ -47,7 +65,7 @@ public enum DroidWheelOption {
return UInt8(round(result / Double(180) * Double(255)))

case .end:
return 128
return DroidWheel.endValue
}
}
}
6 changes: 3 additions & 3 deletions Sources/DroidKit/View/DroidKitDebugView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Tests/DroidKitTests/Core/DroidOperatorTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
44 changes: 44 additions & 0 deletions Tests/DroidKitTests/Model/DroidWheelActionTest.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
34 changes: 0 additions & 34 deletions Tests/DroidKitTests/Model/DroidWheelOptionTest.swift

This file was deleted.

0 comments on commit 5c5eff3

Please sign in to comment.