@@ -66,19 +66,18 @@ def read_register(self, register: int, byte_count: int = 1, max_tries: int = 5)
6666 buf_r = bytearray (byte_count )
6767 buf_r [0 ] = register
6868 buf_w = bytearray (byte_count )
69+ cur_try = 0
6970 for cur_try in range (1 , 1 + max_tries ):
7071 try :
7172 self ._i2c_bus .writeto_then_readfrom (address = self ._i2c_adr , buffer_out = buf_r , buffer_in = buf_w )
7273 return convert_bytearry_to_uint (buf_w )
73- except OSError as e :
74+ # protect against sporadic errors on actual devices
75+ # (maybe we can do something to prevent these errors?)
76+ except (OSError , RuntimeError ) as e :
7477 # [Errno 121] Remote I/O error
75- LH .warning ("[%s] Failed to read register 0x%02X (%i/%i): %s" , __name__ , register , cur_try , max_tries , e )
76- time .sleep (0.001 )
77- except RuntimeError as e :
7878 LH .warning ("[%s] Unable to read register 0x%02X (%i/%i): %s" , __name__ , register , cur_try , max_tries , e )
7979 time .sleep (0.001 )
80- else :
81- raise RuntimeError (f"Unable to read register 0x{ register :02X} after { cur_try } attempts. Giving up." )
80+ raise RuntimeError (f"Unable to read register 0x{ register :02X} after { cur_try } attempts. Giving up." )
8281
8382 def write_register (self , register : int , value : int , byte_count : int = 1 , max_tries : int = 3 ):
8483 """
@@ -91,23 +90,21 @@ def write_register(self, register: int, value: int, byte_count: int = 1, max_tri
9190 """
9291 _validate_register_address (register )
9392 ba = convert_uint_to_bytearry (value , byte_count )
94- buf = bytearray (1 + len ( ba ) )
95- buf [ 0 ] = register
96- for i in range ( len ( ba )):
97- buf [ 1 + i ] = ba [ i ]
93+ buf = bytearray ([ register ] )
94+ for byte in ba :
95+ buf . append ( byte )
96+ cur_try = 0
9897 for cur_try in range (1 , 1 + max_tries ):
9998 try :
10099 self ._i2c_bus .writeto (address = self ._i2c_adr , buffer = buf )
101100 return
102- except OSError as e :
101+ # protect against sporadic errors on actual devices
102+ # (maybe we can do something to prevent these errors?)
103+ except (OSError , RuntimeError ) as e :
103104 # [Errno 121] Remote I/O error
104- LH .warning ("[%s] Failed to read register 0x%02X (%i/%i): %s" , __name__ , register , cur_try , max_tries , e )
105+ LH .warning ("[%s] Unable to write register 0x%02X (%i/%i): %s" , __name__ , register , cur_try , max_tries , e )
105106 time .sleep (0.1 )
106- except RuntimeError as e :
107- LH .warning ("[%s] Unable to read register 0x%02X (%i/%i): %s" , __name__ , register , cur_try , max_tries , e )
108- time .sleep (0.1 )
109- else :
110- raise RuntimeError (f"Unable to read register 0x{ register :02X} after { cur_try } attempts. Giving up." )
107+ raise RuntimeError (f"Unable to read register 0x{ register :02X} after { cur_try } attempts. Giving up." )
111108
112109 # it is unclear if it's possible to have a multi-byte state registers
113110 # (a register write looks exactly like a multi-byte state write)
@@ -124,19 +121,18 @@ def get_state(self, byte_count: int = 1, max_tries: int = 5) -> int:
124121 LH .warning ("Multi byte reads are not implemented yet! Returning a single byte instead." )
125122 byte_count = 1
126123 buf = bytearray (byte_count )
124+ cur_try = 0
127125 for cur_try in range (1 , 1 + max_tries ):
128126 try :
129127 self ._i2c_bus .readfrom_into (address = self ._i2c_adr , buffer = buf )
130128 return buf [0 ]
131- except OSError as e :
129+ # protect against sporadic errors on actual devices
130+ # (maybe we can do something to prevent these errors?)
131+ except (OSError , RuntimeError ) as e :
132132 # [Errno 121] Remote I/O error
133- LH .warning ("[%s] Failed to read state (%i/%i): %s" , __name__ , cur_try , max_tries , e )
134- time .sleep (0.001 )
135- except RuntimeError as e :
136133 LH .warning ("[%s] Unable to read state (%i/%i): %s" , __name__ , cur_try , max_tries , e )
137134 time .sleep (0.001 )
138- else :
139- raise RuntimeError (f"Unable to read state after { cur_try } attempts. Giving up." )
135+ raise RuntimeError (f"Unable to read state after { cur_try } attempts. Giving up." )
140136
141137 def set_state (self , value : int , byte_count : int = 1 , max_tries : int = 3 ):
142138 """
@@ -149,19 +145,18 @@ def set_state(self, value: int, byte_count: int = 1, max_tries: int = 3):
149145 LH .warning ("Multi byte writes are not implemented yet! Returning a single byte instead." )
150146 byte_count = 1
151147 buf = convert_uint_to_bytearry (value , byte_count )
148+ cur_try = 0
152149 for cur_try in range (1 , 1 + max_tries ):
153150 try :
154151 self ._i2c_bus .writeto (address = self ._i2c_adr , buffer = buf )
155152 return
156- except OSError as e :
153+ # protect against sporadic errors on actual devices
154+ # (maybe we can do something to prevent these errors?)
155+ except (OSError , RuntimeError ) as e :
157156 # [Errno 121] Remote I/O error
158- LH .warning ("[%s] Failed to write state (%i/%i): %s" , __name__ , cur_try , max_tries , e )
157+ LH .warning ("[%s] Unable to write state (%i/%i): %s" , __name__ , cur_try , max_tries , e )
159158 time .sleep (0.1 )
160- except RuntimeError as e :
161- LH .warning ("[%s] Unable to write state 0x%02X (%i/%i): %s" , __name__ , cur_try , max_tries , e )
162- time .sleep (0.1 )
163- else :
164- raise RuntimeError (f"Unable to write state after { cur_try } attempts. Giving up." )
159+ raise RuntimeError (f"Unable to write state after { cur_try } attempts. Giving up." )
165160
166161
167162class BurstHandler :
@@ -182,6 +177,8 @@ def __init__(self, i2c_bus: busio.I2C, i2c_adr: int, timeout_ms: int | None = 50
182177 self ._timeout_ms = timeout_ms
183178 else :
184179 raise ValueError ("Provided timeout is not a positive integer or 'None'!" )
180+ # register '_timestart_ns' - we will populate it later on
181+ self ._timestart_ns = 0
185182
186183 def __enter__ (self ) -> BurstHandle :
187184 """
0 commit comments