From 05ebdbd0a4716e5a1c821692720e0dc5ca758ef6 Mon Sep 17 00:00:00 2001 From: michaelpivato Date: Thu, 9 Jul 2020 12:22:53 +0930 Subject: [PATCH] Add protocol for serial library to implement. --- Sources/SwiftRPLidar/LidarSerial.swift | 8 ++++++++ Sources/SwiftRPLidar/SwiftRPLidar.swift | 6 +++--- 2 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 Sources/SwiftRPLidar/LidarSerial.swift diff --git a/Sources/SwiftRPLidar/LidarSerial.swift b/Sources/SwiftRPLidar/LidarSerial.swift new file mode 100644 index 0000000..e1b1c38 --- /dev/null +++ b/Sources/SwiftRPLidar/LidarSerial.swift @@ -0,0 +1,8 @@ +protocol LidarSerial { + var dtr: Bool {get set}; + var inWaiting: Int {get}; + func closePort() -> Void; + func openPort() -> Void; + readData(ofLength: Int) throws -> Void; + writeData(_ data: Data) throws -> Void; +} diff --git a/Sources/SwiftRPLidar/SwiftRPLidar.swift b/Sources/SwiftRPLidar/SwiftRPLidar.swift index 9c8ac86..4676ebb 100644 --- a/Sources/SwiftRPLidar/SwiftRPLidar.swift +++ b/Sources/SwiftRPLidar/SwiftRPLidar.swift @@ -1,5 +1,5 @@ import Foundation -import SwiftSerial +import LidarSerial struct Constants{ static let SYNC: UInt8 = 0xA5 @@ -64,11 +64,11 @@ typealias ScanHandler = (_ scans: [LidarScan]) -> Void class RPLidar{ private var motor: Bool = false - private var serialPort: SerialPort? = nil + private var serialPort: LidarSerial? = nil private var motorRunning = false - init(serialPort: SerialPort) throws { + init(serialPort: LidarSerial) throws { self.serialPort = serialPort }