Explorar el Código

(feat) added SwiftTerminal

Michael Redig hace 2 años
padre
commit
955610aefc

+ 16 - 0
Package.resolved

@@ -0,0 +1,16 @@
+{
+  "object": {
+    "pins": [
+      {
+        "package": "swift-argument-parser",
+        "repositoryURL": "https://github.com/apple/swift-argument-parser.git",
+        "state": {
+          "branch": null,
+          "revision": "c8ed701b513cf5177118a175d85fbbbcd707ab41",
+          "version": "1.3.0"
+        }
+      }
+    ]
+  },
+  "version": 1
+}

+ 12 - 3
Package.swift

@@ -10,13 +10,22 @@ let package = Package(
 	],
 	products: [
 		.library(name: "SwiftSerial", targets: ["SwiftSerial"]),
+		.executable(name: "SerialTerminal", targets: ["SerialTerminal"])
+	],
+	dependencies: [
+		.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMinor(from: "1.3.0")),
 	],
-	dependencies: [],
 	targets: [
 		.target(
 			name: "SwiftSerial",
-			dependencies: [],
-			path: "Sources"
+			dependencies: []
 		),
+		.executableTarget(
+			name: "SerialTerminal",
+			dependencies: [
+				"SwiftSerial",
+				.product(name: "ArgumentParser", package: "swift-argument-parser"),
+			]
+		)
 	]
 )

+ 2 - 0
README.md

@@ -48,3 +48,5 @@ try serialPort.writeData(Data([1,2,3,4]))
 * Monitoring output and delivering via AsyncStream for reading instead of the old polling, or dare I say, omniscience, 
 method, where you need to know exactly how many bytes or lines to read.
 * Thread safety
+* BaudRate has UInt initializer
+* Added `SwiftTerminal` demo to connect and interface with a serial connection

+ 37 - 0
Sources/SerialTerminal/Terminal.swift

@@ -0,0 +1,37 @@
+import Foundation
+import SwiftSerial
+import ArgumentParser
+
+private var readTask: Task<Void, Error>?
+
+@main
+struct Terminal: AsyncParsableCommand {
+	@Argument(help: "Path to port (aka /dev/cu.serialsomethingorother)")
+	var path: String
+
+	@Argument(help: "Baud Rate", transform: {
+		let value = UInt($0) ?? 1
+		return try BaudRate(value)
+	})
+	var baudRate: BaudRate
+
+	private var writeBuffer: String = ""
+
+	func run() async throws {
+		let serialPort = SerialPort(path: path)
+		try serialPort.openPort()
+		try serialPort.setSettings(receiveRate: baudRate, transmitRate: baudRate, minimumBytesToRead: 1)
+
+		readTask = Task {
+			let lines = try serialPort.asyncLines()
+
+			for await line in lines {
+				print(line, terminator: "")
+			}
+		}
+
+		while let line = readLine(strippingNewline: false) {
+			_ = try serialPort.writeString(line)
+		}
+	}
+}

+ 69 - 0
Sources/SwiftSerial/BaudRate.swift

@@ -34,6 +34,75 @@ public enum BaudRate {
 	case baud4000000
 	#endif
 
+	public init(_ value: UInt) throws {
+		switch value {
+		case 0:
+			self = .baud0
+		case 50:
+			self = .baud50
+		case 75:
+			self = .baud75
+		case 110:
+			self = .baud110
+		case 134:
+			self = .baud134
+		case 150:
+			self = .baud150
+		case 200:
+			self = .baud200
+		case 300:
+			self = .baud300
+		case 600:
+			self = .baud600
+		case 1200:
+			self = .baud1200
+		case 1800:
+			self = .baud1800
+		case 2400:
+			self = .baud2400
+		case 4800:
+			self = .baud4800
+		case 9600:
+			self = .baud9600
+		case 19200:
+			self = .baud19200
+		case 38400:
+			self = .baud38400
+		case 57600:
+			self = .baud57600
+		case 115200:
+			self = .baud115200
+		case 230400:
+			self = .baud230400
+		#if os(Linux)
+		case 460800:
+			self = .baud460800
+		case 500000:
+			self = .baud500000
+		case 576000:
+			self = .baud576000
+		case 921600:
+			self = .baud921600
+		case 1000000:
+			self = .baud1000000
+		case 1152000:
+			self = .baud1152000
+		case 1500000:
+			self = .baud1500000
+		case 2000000:
+			self = .baud2000000
+		case 2500000:
+			self = .baud2500000
+		case 3500000:
+			self = .baud3500000
+		case 4000000:
+			self = .baud4000000
+		#endif
+		default:
+			throw PortError.invalidPort
+		}
+	}
+
 	var speedValue: speed_t {
 		switch self {
 		case .baud0:

+ 1 - 0
Sources/SwiftSerial/PortError.swift

@@ -9,4 +9,5 @@ public enum PortError: Int32, Error {
 	case unableToConvertByteToCharacter
 	case deviceNotConnected
 	case instanceAlreadyOpen
+	case invalidPort
 }