@@ -226,65 +226,6 @@ def __exit__(self, exc_type, exc_value, exc_tb):
226226 LH .debug ("[%d] Releasing the lock on the I²C bus." , id (self ))
227227 self ._i2c_bus .unlock ()
228228
229- def read_register (self , register : int , byte_count : int = 1 , max_tries : int = 5 ) -> int :
230- """
231- read a single register from I²C device identified by `i2c_adr` and
232- return its contents as an integer value
233- - may raise a RuntimeError if it was not possible to acquire
234- the bus within allowed time
235- - may raise a RuntimeError if there were too many errors
236- """
237- _validate_inputs (register = register , value = 0 , byte_count = byte_count , max_tries = max_tries )
238- if byte_count > 1 :
239- LH .warning ("Multi byte reads are not implemented yet! Returning a single byte instead." )
240- byte_count = 1
241- for cur_try in range (1 , 1 + max_tries ):
242- try :
243- buf_r = bytearray (1 )
244- buf_r [0 ] = register
245- buf_w = bytearray (byte_count )
246- self ._i2c_bus .writeto_then_readfrom (address = self ._i2c_adr , buffer_out = buf_r , buffer_in = buf_w )
247- # TODO properly handle multi byte reads
248- return buf_w [0 ]
249- except OSError as e :
250- # [Errno 121] Remote I/O error
251- LH .warning ("[%s] Failed to read register 0x%02X (%i/%i): %s" , __name__ , register , cur_try , max_tries , e )
252- time .sleep (0.001 )
253- except RuntimeError as e :
254- LH .warning ("[%s] Unable to read register 0x%02X (%i/%i): %s" , __name__ , register , cur_try , max_tries , e )
255- time .sleep (0.001 )
256- else :
257- raise RuntimeError (f"Unable to read register 0x{ register :02X} after { cur_try } attempts. Giving up." )
258-
259- def write_register (self , register : int , value : int , byte_count : int = 1 , max_tries : int = 3 ):
260- """
261- write a single register to I²C device identified by `i2c_adr`
262- - may raise a RuntimeError if it was not possible to acquire
263- the bus within allowed time
264- - may raise a RuntimeError if there were too many errors
265- """
266- _validate_inputs (register = register , value = value , byte_count = byte_count , max_tries = max_tries )
267- if byte_count > 1 :
268- LH .warning ("Multi byte writes are not implemented yet! Returning a single byte instead." )
269- byte_count = 1
270- for cur_try in range (1 , 1 + max_tries ):
271- try :
272- buf = bytearray (1 + byte_count )
273- buf [0 ] = register
274- buf [1 ] = value & 0xFF
275- # TODO properly handle multi byte reads
276- self ._i2c_bus .writeto (address = self ._i2c_adr , buffer = buf )
277- return
278- except OSError as e :
279- # [Errno 121] Remote I/O error
280- LH .warning ("[%s] Failed to read register 0x%02X (%i/%i): %s" , __name__ , register , cur_try , max_tries , e )
281- time .sleep (0.1 )
282- except RuntimeError as e :
283- LH .warning ("[%s] Unable to read register 0x%02X (%i/%i): %s" , __name__ , register , cur_try , max_tries , e )
284- time .sleep (0.1 )
285- else :
286- raise RuntimeError (f"Unable to read register 0x{ register :02X} after { cur_try } attempts. Giving up." )
287-
288229
289230def _validate_inputs (register : int , value : int , byte_count : int = 1 , max_tries : int = 3 ):
290231 if register < 0 or register > 255 :
0 commit comments