Skip to content

Commit b944544

Browse files
authored
Add buffer write function for SPI (#7)
Signed-off-by: Andy Liu <andy@madmachine.io>
1 parent 38e4adc commit b944544

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

Sources/SwiftIO/SPI.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,5 +183,33 @@ import CSwiftIO
183183
print("SPI\(id) write error!")
184184
}
185185
}
186+
187+
/**
188+
Write an buffer of data to the slave device.
189+
- Parameter data: A UInt8 buffer to be sent to the slave device.
190+
*/
191+
@inline(__always)
192+
public func write(_ data: UnsafeBufferPointer<UInt8>, count: Int? = nil) {
193+
let ret: Int32
194+
let byteCount: Int
195+
196+
if let count = count {
197+
byteCount = min(data.count, count)
198+
} else {
199+
byteCount = data.count
200+
}
201+
202+
if byteCount <= 0 {
203+
return
204+
}
205+
206+
csEnable()
207+
ret = swifthal_spi_write(obj, data.baseAddress, Int32(byteCount))
208+
csDisable()
209+
210+
if ret != 0 {
211+
print("SPI\(id) write error!")
212+
}
213+
}
186214
}
187215

0 commit comments

Comments
 (0)