Skip to content

Commit

Permalink
Added readRaw
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig Altrenburg committed Dec 6, 2021
1 parent bbc5a07 commit 2d46c99
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion Sources/I2C.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 2d46c99

Please sign in to comment.