Add factory to initialise vehicles

This commit is contained in:
Piv
2020-05-20 19:32:01 +09:30
parent 4ad8342033
commit 59834d2afc
2 changed files with 28 additions and 5 deletions

View 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()
}

View File

@@ -6,7 +6,6 @@
// //
import NIO import NIO
import SwiftyGPIO
import GRPC import GRPC
func doServer() throws { func doServer() throws {
@@ -17,12 +16,9 @@ func doServer() throws {
try! group.syncShutdownGracefully() 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. // 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. // Start the server and print its address once it has started.
let server = Server.insecure(group: group) let server = Server.insecure(group: group)