From 0026aade391b52e94de548384dc0e64d408a90ae Mon Sep 17 00:00:00 2001 From: Piv <18462828+Piv200@users.noreply.github.com> Date: Sun, 13 Sep 2020 21:36:08 +0930 Subject: [PATCH] Initialise serial port in factory --- SwiftyCar/Sources/SwiftyCar/VehicleFactory.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/SwiftyCar/Sources/SwiftyCar/VehicleFactory.swift b/SwiftyCar/Sources/SwiftyCar/VehicleFactory.swift index e0f78a8..df83c86 100644 --- a/SwiftyCar/Sources/SwiftyCar/VehicleFactory.swift +++ b/SwiftyCar/Sources/SwiftyCar/VehicleFactory.swift @@ -1,5 +1,5 @@ // -// File.swift +// VehicleFactory.swift // // // Created by Michael Pivato on 20/5/20. @@ -22,6 +22,10 @@ func getVehicle2D() throws -> Vehicle2D? { // TODO: Get from environment variable. tty won't work in macos anyway. // We share the serialport object, as cu will block on macOS (required by SwiftSerial), so can't open 2 of the same port. let serialPort = SerialPort(path: "/dev/ttyUSB0") + + // The port does not open/initialise inside of the ESP32ServoOutputs, as on macOS /dev/cu.* blocks. + try initPort.openPort() + initPort.setSettings(receiveRate: .baud115200, transmitRate: .baud115200, minimumBytesToRead: 1)x guard let throttlePin = Esp32ServoOutput(forChannel: 1, forPin: 14, onPort: serialPort) else { print("Failed to create throttle pin.") return nil