|
1 | 1 | # SerialSwift
|
2 |
| -MacOS Serial solution (Observable & 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). |
0 commit comments