From 2d46c99971d14229649e677cf1305f47a8890946 Mon Sep 17 00:00:00 2001 From: Craig Altrenburg Date: Mon, 6 Dec 2021 17:00:15 -0500 Subject: [PATCH] Added readRaw --- Sources/I2C.swift | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/Sources/I2C.swift b/Sources/I2C.swift index 30de382..bc64d15 100644 --- a/Sources/I2C.swift +++ b/Sources/I2C.swift @@ -76,11 +76,15 @@ public protocol I2CInterface { func readWord(_ address: Int, command: UInt8) -> UInt16 func readData(_ address: Int, command: UInt8) -> [UInt8] func readI2CData(_ address: Int, command: UInt8) -> [UInt8] + func readRaw(_ address: Int, length: UInt) -> [UInt8] + func tryReadByte(_ address: Int) -> UInt8? func tryReadByte(_ address: Int, command: UInt8) -> UInt8? func tryReadWord(_ address: Int, command: UInt8) -> UInt16? func tryReadData(_ address: Int, command: UInt8) -> [UInt8]? func tryReadI2CData(_ address: Int, command: UInt8) -> [UInt8]? + func tryReadRaw(_ address: Int, length: UInt) -> [UInt8]? + @discardableResult func writeQuick(_ address: Int) -> Bool @discardableResult func writeByte(_ address: Int, value: UInt8) -> Bool @discardableResult func writeByte(_ address: Int, command: UInt8, value: UInt8) -> Bool @@ -249,7 +253,33 @@ public final class SysFSI2C: I2CInterface { return buf } - + + public func readRaw(_ address: Int, length: UInt) -> [UInt8] { + var buf: [UInt8] = [UInt8](repeating:0, count: length) + + setSlaveAddress(address) + + let r = read( i2cId, &buf, length ) + + if r < 0 { + perror("I2C read failed") + abort() + } + return buf + } + + public func tryReadRaw(_ address: Int, length: UInt) -> [UInt8]? { + var buf: [UInt8] = [UInt8](repeating:0, count: length) + + setSlaveAddress(address) + + let r = read( i2cId, &buf, length ) + + if r < 0 { return nil } + + return buf + } + public func writeQuick(_ address: Int) -> Bool { setSlaveAddress(address)