Add factory to initialise vehicles
This commit is contained in:
27
SwiftyCar/Sources/SwiftyCar/VehicleFactory.swift
Normal file
27
SwiftyCar/Sources/SwiftyCar/VehicleFactory.swift
Normal file
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// File.swift
|
||||
//
|
||||
//
|
||||
// Created by Michael Pivato on 20/5/20.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftyGPIO
|
||||
|
||||
|
||||
|
||||
func getVehicle2D() throws -> Vehicle2D {
|
||||
if let value = ProcessInfo.processInfo.environment["CAR_VEHICLE"] {
|
||||
switch value{
|
||||
case "CAR_2D":
|
||||
// Get car for rpi.
|
||||
let pwms = SwiftyGPIO.hardwarePWMs(for:.RaspberryPi3)!
|
||||
|
||||
// Read the feature database.
|
||||
return try RPiVehicle2D(withThrottlePin: Servo(forPin: (pwms[0]?[.P18])!)!, withSteeringPin:Servo(forPin: (pwms[1]?[.P19])!)!)
|
||||
default:
|
||||
return MockVehicle()
|
||||
}
|
||||
}
|
||||
return MockVehicle()
|
||||
}
|
||||
@@ -6,7 +6,6 @@
|
||||
//
|
||||
|
||||
import NIO
|
||||
import SwiftyGPIO
|
||||
import GRPC
|
||||
|
||||
func doServer() throws {
|
||||
@@ -17,12 +16,9 @@ func doServer() throws {
|
||||
try! group.syncShutdownGracefully()
|
||||
}
|
||||
|
||||
let pwms = SwiftyGPIO.hardwarePWMs(for:.RaspberryPi3)!
|
||||
|
||||
// Read the feature database.
|
||||
let vehicle = try RPiVehicle2D(withThrottlePin: Servo(forPin: (pwms[0]?[.P18])!)!, withSteeringPin:Servo(forPin: (pwms[1]?[.P19])!)!)
|
||||
// Create a provider using the features we read.
|
||||
let provider = MotorProvider(vehicle: vehicle)
|
||||
let provider = try MotorProvider(vehicle: getVehicle2D())
|
||||
|
||||
// Start the server and print its address once it has started.
|
||||
let server = Server.insecure(group: group)
|
||||
|
||||
Reference in New Issue
Block a user