Skip to content

Commit 220ff84

Browse files
committed
Version 1.0.0
Initial release commit
1 parent 952aad6 commit 220ff84

16 files changed

+706
-1
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,8 @@ fastlane/test_output
8888
# https://github.com/johnno1962/injectionforxcode
8989

9090
iOSInjectionProject/
91+
92+
*.DS_Store
93+
/.build
94+
/Packages
95+
*.xcodeproj

.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

Package.resolved

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// swift-tools-version: 5.7
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "SerialSwift",
8+
platforms: [
9+
.macOS(.v10_15)
10+
],
11+
products: [
12+
// Products define the executables and libraries a package produces, and make them visible to other packages.
13+
.library(
14+
name: "SerialSwift",
15+
targets: ["SerialSwift"]),
16+
],
17+
dependencies: [
18+
// Dependencies declare other packages that this package depends on.
19+
.package(url: "https://github.com/Flowduino/ThreadSafeSwift.git", .upToNextMajor(from: "1.1.0")),
20+
.package(url: "https://github.com/Flowduino/Observable.git", .upToNextMajor(from: "2.0.0")),
21+
.package(url: "https://github.com/Flowduino/EventDrivenSwift.git", .upToNextMajor(from: "4.0.0")),
22+
.package(url: "https://github.com/armadsen/ORSSerialPort.git", .upToNextMajor(from: "2.1.0"))
23+
],
24+
targets: [
25+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
26+
// Targets can depend on other targets in this package, and on products in packages this package depends on.
27+
.target(
28+
name: "SerialSwift",
29+
dependencies: [
30+
.product(name: "ThreadSafeSwift", package: "ThreadSafeSwift"),
31+
.product(name: "Observable", package: "Observable"),
32+
.product(name: "EventDrivenSwift", package: "EventDrivenSwift"),
33+
.product(name: "ORSSerial", package: "ORSSerialPort")
34+
]
35+
),
36+
.testTarget(
37+
name: "SerialSwiftTests",
38+
dependencies: ["SerialSwift",
39+
.product(name: "ThreadSafeSwift", package: "ThreadSafeSwift"),
40+
.product(name: "Observable", package: "Observable"),
41+
.product(name: "EventDrivenSwift", package: "EventDrivenSwift"),
42+
.product(name: "ORSSerial", package: "ORSSerialPort")
43+
]
44+
),
45+
]
46+
)

README.md

Lines changed: 157 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,158 @@
11
# SerialSwift
2-
MacOS Serial solution (Observable &amp; Event-Driven) to make integration of Serial peripherals trivial
2+
3+
<p>
4+
<img src="https://img.shields.io/badge/Swift-5.1%2B-yellowgreen.svg?style=flat" />
5+
<img src="https://img.shields.io/badge/macOS-10.15+-179AC8.svg" />
6+
<img src="https://img.shields.io/badge/License-MIT-blue.svg" />
7+
<a href="https://github.com/apple/swift-package-manager">
8+
<img src="https://img.shields.io/badge/spm-compatible-brightgreen.svg?style=flat" />
9+
</a>
10+
</p>
11+
12+
`SerialSwift` makes communicating with your Serial Peripherals on MacOS trivial.
13+
14+
Better still, `SerialSwift` is designed to be fundamnetally *Observable* and *Event-Driven*, making it easier than ever before to consume information coming into your application from your external Serial peripheral(s).
15+
16+
`SerialSwift` is built on top of a number of packages:
17+
- [`ThreadSafeSwift` from ourselves at Flowduino](https://github.com/Flowduino/ThreadSafeSwift) is used to ensure Thread-Safety throughout the library
18+
- [`Observable` from ourselves at Flowduino](https://github.com/Flowduino/Observable) is used to provide protocol-conformance Observer Pattern support throughout the library
19+
- [`EventDrivenSwift` from ourselves at Flowduino](https://github.com/Flowduino/EventDrivenSwift) is used to emit relevant and extremely high-performance *Events* for every Serial Event your Peripherals generate.
20+
- [`ORSSerialPort` from Armadsen](https://github.com/armadsen/ORSSerialPort) is used to actually interface with your Serial Peripherals.
21+
22+
In this way, `SerialSwift` can be integrated into your code in any way you prefer, making it extremely versatile.
23+
24+
## Installation
25+
### Xcode Projects
26+
Select `File` -> `Swift Packages` -> `Add Package Dependency` and enter `https://github.com/Flowduino/SerialSwift.git`
27+
28+
### Swift Package Manager Projects
29+
You can use `SerialSwift` as a Package Dependency in your own Packages' `Package.swift` file:
30+
```swift
31+
let package = Package(
32+
//...
33+
dependencies: [
34+
.package(
35+
url: "https://github.com/Flowduino/SerialSwift.git",
36+
.upToNextMajor(from: "1.0.0")
37+
),
38+
],
39+
//...
40+
)
41+
```
42+
43+
From there, refer to `SerialSwift` as a "target dependency" in any of _your_ package's targets that need it.
44+
45+
```swift
46+
targets: [
47+
.target(
48+
name: "YourLibrary",
49+
dependencies: [
50+
"SerialSwift",
51+
],
52+
//...
53+
),
54+
//...
55+
]
56+
```
57+
You can then do `import SerialSwift` in any code that requires it.
58+
59+
## Usage
60+
61+
Here are some quick and easy usage examples for the features provided by `SerialSwift`:
62+
63+
### Connecting to your Serial Peripheral
64+
You can create an instance of `SerialSwift` per Peripheral as easily as this:
65+
```swift
66+
var mySerialDevice = Serial["/dev/cu.myserialdevice", .baud9600]
67+
```
68+
You would, of course, substitute both parameters in the above example with (firstly) the path to your Serial device, followed by (secondly) the Baud Rate your Serial Device uses.
69+
70+
It is recommended that you retain a reference to your `Serialable` object somewhere globally in your application (such as on the `Environment` of your application, or as a *Singleton*). This is because, from the moment you connect to your Serial peripheral, it will begin emitting *Events* that you can consume anywhere in your application... as demonstrated below.
71+
72+
### Events your Serial device will emit throughout your Application
73+
Now that you have connected to your Serial device, the following *Events* will be emitted universally throughout your application, and can be consumed from *anywhere* in your code.
74+
75+
#### `SerialPortClosedEvent`
76+
If you need to perform a specific operation any time your connection to your Serial device closes, you can do so simply from *anywhere* in your code:
77+
```swift
78+
SerialPortClosedEvent.addListener(self) { (event: SerialPortClosedEvent, priority) in
79+
/**
80+
Your code goes in here!
81+
Properties available to you:
82+
`event.refTime` = the precise "Mach Time" at which the Serial Port closed
83+
`event.serial` = A reference to the Serial device which triggered the Event.
84+
85+
You can use `if ObjectIdentifier(event.serial) != ObjectIdentifier(mySerialDevice) { return }` to ensure you're only acting on Events emitted by a specific Serial device
86+
*/
87+
}
88+
```
89+
90+
#### `SerialPortDataReceivedEvent`
91+
If you need to perform a specific operation any time your Serial device sends Data to your computer, you can do so simply from *anywhere* in your code:
92+
```swift
93+
SerialPortDataReceivedEvent.addListener(self) { (event: SerialPortDataReceivedEvent, priority) in
94+
/**
95+
Your code goes in here!
96+
Properties available to you:
97+
`event.refTime` = the precise "Mach Time" at which the Serial Port closed
98+
`event.serial` = A reference to the Serial device which triggered the Event.
99+
`event.data` = The actual `Data` your Serial device sent to your computer.
100+
101+
You can use `if ObjectIdentifier(event.serial) != ObjectIdentifier(mySerialDevice) { return }` to ensure you're only acting on Events emitted by a specific Serial device
102+
*/
103+
}
104+
```
105+
106+
#### `SerialPortErrorEvent`
107+
If you need to perform a specific operation any time your Serial device encounters an Error, you can do so simply from *anywhere* in your code:
108+
```swift
109+
SerialPortErrorEvent.addListener(self) { (event: SerialPortErrorEvent, priority) in
110+
/**
111+
Your code goes in here!
112+
Properties available to you:
113+
`event.refTime` = the precise "Mach Time" at which the Serial Port closed
114+
`event.serial` = A reference to the Serial device which triggered the Event.
115+
`event.error` = The actual `Error` your Serial device encountered.
116+
117+
You can use `if ObjectIdentifier(event.serial) != ObjectIdentifier(mySerialDevice) { return }` to ensure you're only acting on Events emitted by a specific Serial device
118+
*/
119+
}
120+
```
121+
122+
#### `SerialPortOpenedEvent`
123+
If you need to perform a specific operation any time your Serial device establishes a connection with your Computer, you can do so simply from *anywhere* in your code:
124+
```swift
125+
SerialPortOpenedEvent.addListener(self) { (event: SerialPortOpenedEvent, priority) in
126+
/**
127+
Your code goes in here!
128+
Properties available to you:
129+
`event.refTime` = the precise "Mach Time" at which the Serial Port closed
130+
`event.serial` = A reference to the Serial device which triggered the Event.
131+
132+
You can use `if ObjectIdentifier(event.serial) != ObjectIdentifier(mySerialDevice) { return }` to ensure you're only acting on Events emitted by a specific Serial device
133+
*/
134+
}
135+
```
136+
137+
#### `SerialPortRemovedEvent`
138+
If you need to perform a specific operation any time your Serial device disconnects from your Computer, you can do so simply from *anywhere* in your code:
139+
```swift
140+
SerialPortRemovedEvent.addListener(self) { (event: SerialPortRemovedEvent, priority) in
141+
/**
142+
Your code goes in here!
143+
Properties available to you:
144+
`event.refTime` = the precise "Mach Time" at which the Serial Port closed
145+
`event.serial` = A reference to the Serial device which triggered the Event.
146+
147+
You can use `if ObjectIdentifier(event.serial) != ObjectIdentifier(mySerialDevice) { return }` to ensure you're only acting on Events emitted by a specific Serial device
148+
*/
149+
}
150+
```
151+
152+
## License
153+
154+
`SerialSwift` is available under the MIT license. See the [LICENSE file](./LICENSE) for more info.
155+
156+
## Join us on Discord
157+
158+
If you require additional support, or would like to discuss `SerialSwift`, Swift, or any other topics related to Flowduino, you can [join us on Discord](https://discord.com/invite/GdZZKFTQ2A).

Sources/SerialSwift/BaudRate.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// BaudRate.swift
3+
//
4+
//
5+
// Created by Simon Stuart on 18/08/2022.
6+
//
7+
8+
import Foundation
9+
10+
public enum BaudRate: NSNumber {
11+
case baud0 = 0
12+
case baud50 = 50
13+
case baud75 = 75
14+
case baud110 = 110
15+
case baud134 = 134
16+
case baud150 = 150
17+
case baud200 = 200
18+
case baud300 = 300
19+
case baud600 = 600
20+
case baud1200 = 1200
21+
case baud1800 = 1800
22+
case baud2400 = 2400
23+
case baud4800 = 4800
24+
case baud9600 = 9600
25+
case baud19200 = 19200
26+
case baud38400 = 38400
27+
case baud7200 = 7200
28+
case baud14400 = 14400
29+
case baud28800 = 28800
30+
case baud57600 = 57600
31+
case baud76800 = 76800
32+
case baud115200 = 115200
33+
case baud230400 = 230400
34+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// SerialPortClosedEvent.swift
3+
// SebaSwift
4+
//
5+
// Created by Simon Stuart on 18th August 2022
6+
// Copyright (c) 2022, Flowduino (Simon J. Stuart), All Rights Reserved
7+
//
8+
9+
import Foundation
10+
import EventDrivenSwift
11+
12+
/**
13+
Dispatched any time a Serial Interface opens
14+
*/
15+
public struct SerialPortClosedEvent: Eventable {
16+
var refTime: UInt64
17+
var serial: Serialable?
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// SerialPortDataReceivedEvent.swift
3+
// SebaSwift
4+
//
5+
// Created by Simon Stuart on 18th August 2022
6+
// Copyright (c) 2022, Flowduino (Simon J. Stuart), All Rights Reserved
7+
//
8+
9+
import Foundation
10+
import EventDrivenSwift
11+
12+
/**
13+
Dispatched any time a Serial Interface receives data from the peripheral device
14+
*/
15+
public struct SerialPortDataReceivedEvent: Eventable {
16+
var refTime: UInt64
17+
var serial: Serialable?
18+
var data: Data
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// SerialErrorEvent.swift
3+
// SebaSwift
4+
//
5+
// Created by Simon Stuart on 18th August 2022
6+
// Copyright (c) 2022, Flowduino (Simon J. Stuart), All Rights Reserved
7+
//
8+
9+
import Foundation
10+
import EventDrivenSwift
11+
12+
/**
13+
Dispatched any time a Serial Interface error occurs
14+
*/
15+
public struct SerialPortErrorEvent: Eventable {
16+
var refTime: UInt64
17+
var serial: Serialable?
18+
var error: Error
19+
}

0 commit comments

Comments
 (0)