Move settings/open of port into convenience initialiser.
This avoids attempting to open a serial port multiple times, which can be problematic if a blocking connection is used. Instead settings and port connection should be handled prior to designated inititializer.
This commit is contained in:
@@ -30,14 +30,15 @@ public class Esp32ServoOutput : PWMOutput {
|
||||
self.serialPort = port
|
||||
}
|
||||
|
||||
public convenience init?(forChannel channel: UInt8, forPin pin: UInt8, onPort port: String) {
|
||||
self.init(forChannel: channel, forPin: pin, onPort: SerialPort(path: port))
|
||||
public convenience init?(forChannel channel: UInt8, forPin pin: UInt8, onPort port: String) throws {
|
||||
let initPort = SerialPort(path: port)
|
||||
try initPort.openPort()
|
||||
initPort.setSettings(receiveRate: .baud115200, transmitRate: .baud115200, minimumBytesToRead: 1)
|
||||
self.init(forChannel: channel, forPin: pin, onPort: initPort)
|
||||
}
|
||||
|
||||
public func initPWM() {
|
||||
do {
|
||||
try serialPort.openPort()
|
||||
serialPort.setSettings(receiveRate: .baud115200, transmitRate: .baud115200, minimumBytesToRead: 1)
|
||||
let bytesWritten = try serialPort.writeData(Data([0, 1, channel, pin]))
|
||||
if bytesWritten != 4 {
|
||||
print("Wrote %d bytes, but should have written 4", bytesWritten)
|
||||
|
||||
Reference in New Issue
Block a user