@@ -132,7 +132,7 @@ public final class SysFSI2C: I2CInterface {
132132
133133 let r = i2c_smbus_read_byte ( )
134134
135- if r < 0 { return nil }
135+ guard r >= 0 else { return nil }
136136
137137 #if swift(>=4.0)
138138 return UInt8 ( truncatingIfNeeded: r)
@@ -162,7 +162,7 @@ public final class SysFSI2C: I2CInterface {
162162
163163 let r = i2c_smbus_read_byte_data ( command: command)
164164
165- if r < 0 { return nil ; }
165+ guard r >= 0 else { return nil }
166166
167167 #if swift(>=4.0)
168168 return UInt8 ( truncatingIfNeeded: r)
@@ -192,7 +192,7 @@ public final class SysFSI2C: I2CInterface {
192192
193193 let r = i2c_smbus_read_word_data ( command: command)
194194
195- if r < 0 { return nil }
195+ guard r >= 0 else { return nil }
196196
197197 #if swift(>=4.0)
198198 return UInt16 ( truncatingIfNeeded: r)
@@ -220,9 +220,9 @@ public final class SysFSI2C: I2CInterface {
220220
221221 setSlaveAddress ( address)
222222
223- let r = i2c_smbus_read_block_data ( command: command, values: & buf)
223+ let r = i2c_smbus_read_block_data ( command: command, values: & buf)
224224
225- if r < 0 { return nil }
225+ guard r >= 0 else { return nil }
226226
227227 return buf
228228 }
@@ -247,17 +247,22 @@ public final class SysFSI2C: I2CInterface {
247247
248248 setSlaveAddress ( address)
249249
250- let r = i2c_smbus_read_i2c_block_data ( command: command, values: & buf)
250+ let r = i2c_smbus_read_i2c_block_data ( command: command, values: & buf)
251251
252- if r < 0 { return nil }
252+ guard r >= 0 else { return nil }
253253
254254 return buf
255255 }
256256
257257 public func readRaw( _ address: Int , length: Int ) -> [ UInt8 ] {
258258 var buf : [ UInt8 ] = [ UInt8] ( repeating: 0 , count: length)
259259
260- setSlaveAddress ( address)
260+ let i2cStatus = ioctl ( file, I2C_SLAVE, CInt ( slave ) )
261+
262+ if i2cStatus != 0 {
263+ print ( " ioctl failed " )
264+ abort ( ) ;
265+ }
261266
262267 let r = read ( Int32 ( i2cId ) , & buf, length )
263268
@@ -273,9 +278,13 @@ public final class SysFSI2C: I2CInterface {
273278
274279 setSlaveAddress ( address)
275280
281+ let i2cStatus = ioctl ( file, I2C_SLAVE, CInt ( slave ) )
282+
283+ guard i2cStatus == 0 else { return nil }
284+
276285 let r = read ( Int32 ( i2cId ) , & buf, length )
277286
278- if r < 0 { return nil }
287+ guard r >= 0 else { return nil }
279288
280289 return buf
281290 }
0 commit comments