@@ -112,6 +112,8 @@ def __init__(self, sda=None, scl=None, freq=100000, *args, **argk):
112112 self ._scl = scl
113113 self ._freq = freq
114114
115+ self ._i2cbus = _connectToI2CBus (sda = self ._sda , scl = self ._scl , freq = self ._freq )
116+
115117 # Okay, are we running on a circuit py system?
116118 @classmethod
117119 def isPlatform (cls ):
@@ -133,8 +135,6 @@ def is_platform(cls):
133135 def __getattr__ (self , name ):
134136
135137 if (name == "i2cbus" ):
136- if ( self ._i2cbus == None ):
137- self ._i2cbus = _connectToI2CBus (sda = self ._sda , scl = self ._scl , freq = self ._freq )
138138 return self ._i2cbus
139139
140140 else :
@@ -156,18 +156,18 @@ def __setattr__(self, name, value):
156156 # read Data Command
157157
158158 def readWord (self , address , commandCode ):
159- if not self .i2cbus .try_lock ():
159+ if not self ._i2cbus .try_lock ():
160160 raise Exception ("Unable to lock I2C bus" )
161161
162162 buffer = bytearray (2 )
163163
164164 try :
165- self .i2cbus .writeto_then_readfrom (address , bytes ([commandCode ]), buffer )
165+ self ._i2cbus .writeto_then_readfrom (address , bytes ([commandCode ]), buffer )
166166 except Exception as e :
167- self .i2cbus .unlock ()
167+ self ._i2cbus .unlock ()
168168 raise e
169169 else :
170- self .i2cbus .unlock ()
170+ self ._i2cbus .unlock ()
171171
172172 # build and return a word
173173 return (buffer [1 ] << 8 ) | buffer [0 ]
@@ -177,18 +177,18 @@ def read_word(self, address, commandCode):
177177
178178 #----------------------------------------------------------
179179 def readByte (self , address , commandCode ):
180- if not self .i2cbus .try_lock ():
180+ if not self ._i2cbus .try_lock ():
181181 raise Exception ("Unable to lock I2C bus" )
182182
183183 buffer = bytearray (1 )
184184
185185 try :
186- self .i2cbus .writeto_then_readfrom (address , bytes ([commandCode ]), buffer )
186+ self ._i2cbus .writeto_then_readfrom (address , bytes ([commandCode ]), buffer )
187187 except Exception as e :
188- self .i2cbus .unlock ()
188+ self ._i2cbus .unlock ()
189189 raise e
190190 else :
191- self .i2cbus .unlock ()
191+ self ._i2cbus .unlock ()
192192
193193 return buffer [0 ]
194194
@@ -197,18 +197,18 @@ def read_byte(self, address, commandCode = None):
197197
198198 #----------------------------------------------------------
199199 def readBlock (self , address , commandCode , nBytes ):
200- if not self .i2cbus .try_lock ():
200+ if not self ._i2cbus .try_lock ():
201201 raise Exception ("Unable to lock I2C bus" )
202202
203203 buffer = bytearray (nBytes )
204204
205205 try :
206- self .i2cbus .writeto_then_readfrom (address , bytes ([commandCode ]), buffer )
206+ self ._i2cbus .writeto_then_readfrom (address , bytes ([commandCode ]), buffer )
207207 except Exception as e :
208- self .i2cbus .unlock ()
208+ self ._i2cbus .unlock ()
209209 raise e
210210 else :
211- self .i2cbus .unlock ()
211+ self ._i2cbus .unlock ()
212212
213213 return list (buffer )
214214
@@ -224,86 +224,86 @@ def read_block(self, address, commandCode, nBytes):
224224 #
225225
226226 def writeCommand (self , address , commandCode ):
227- if not self .i2cbus .try_lock ():
227+ if not self ._i2cbus .try_lock ():
228228 raise Exception ("Unable to lock I2C bus" )
229229
230230 try :
231- self .i2cbus .writeto (address , bytes ([commandCode ]))
231+ self ._i2cbus .writeto (address , bytes ([commandCode ]))
232232 except Exception as e :
233- self .i2cbus .unlock ()
233+ self ._i2cbus .unlock ()
234234 raise e
235235 else :
236- self .i2cbus .unlock ()
236+ self ._i2cbus .unlock ()
237237
238238 def write_command (self , address , commandCode ):
239239 return self .writeCommand (address , commandCode )
240240
241241 #----------------------------------------------------------
242242 def writeWord (self , address , commandCode , value ):
243- if not self .i2cbus .try_lock ():
243+ if not self ._i2cbus .try_lock ():
244244 raise Exception ("Unable to lock I2C bus" )
245245
246246 buffer = [0 , 0 ]
247247 buffer [0 ] = value & 0xFF
248248 buffer [1 ] = (value >> 8 ) & 0xFF
249249
250250 try :
251- self .i2cbus .writeto (address , bytes ([commandCode ] + buffer ))
251+ self ._i2cbus .writeto (address , bytes ([commandCode ] + buffer ))
252252 except Exception as e :
253- self .i2cbus .unlock ()
253+ self ._i2cbus .unlock ()
254254 raise e
255255 else :
256- self .i2cbus .unlock ()
256+ self ._i2cbus .unlock ()
257257
258258 def write_word (self , address , commandCode , value ):
259259 return self .writeWord (address , commandCode , value )
260260
261261 #----------------------------------------------------------
262262 def writeByte (self , address , commandCode , value ):
263- if not self .i2cbus .try_lock ():
263+ if not self ._i2cbus .try_lock ():
264264 raise Exception ("Unable to lock I2C bus" )
265265
266266 try :
267- self .i2cbus .writeto (address , bytes ([commandCode ] + [value ]))
267+ self ._i2cbus .writeto (address , bytes ([commandCode ] + [value ]))
268268 except Exception as e :
269- self .i2cbus .unlock ()
269+ self ._i2cbus .unlock ()
270270 raise e
271271 else :
272- self .i2cbus .unlock ()
272+ self ._i2cbus .unlock ()
273273
274274 def write_byte (self , address , commandCode , value ):
275275 return self .writeByte (address , commandCode , value )
276276
277277 #----------------------------------------------------------
278278 def writeBlock (self , address , commandCode , value ):
279- if not self .i2cbus .try_lock ():
279+ if not self ._i2cbus .try_lock ():
280280 raise Exception ("Unable to lock I2C bus" )
281281
282282 try :
283- self .i2cbus .writeto (address , bytes ([commandCode ] + value ))
283+ self ._i2cbus .writeto (address , bytes ([commandCode ] + value ))
284284 except Exception as e :
285- self .i2cbus .unlock ()
285+ self ._i2cbus .unlock ()
286286 raise e
287287 else :
288- self .i2cbus .unlock ()
288+ self ._i2cbus .unlock ()
289289
290290 def write_block (self , address , commandCode , value ):
291291 return self .writeBlock (address , commandCode , value )
292292
293293 def isDeviceConnected (self , devAddress ):
294- if not self .i2cbus .try_lock ():
294+ if not self ._i2cbus .try_lock ():
295295 raise Exception ("Unable to lock I2C bus" )
296296
297297 isConnected = False
298298 try :
299299 # Try to write nothing to the device
300300 # If it throws an I/O error - the device isn't connected
301- self .i2cbus .writeto (devAddress , bytearray ())
301+ self ._i2cbus .writeto (devAddress , bytearray ())
302302 isConnected = True
303303 except :
304304 pass
305305 finally :
306- self .i2cbus .unlock ()
306+ self ._i2cbus .unlock ()
307307
308308 return isConnected
309309
@@ -320,15 +320,15 @@ def ping(self, devAddress):
320320 #
321321 def scan (self ):
322322 """ Returns a list of addresses for the devices connected to the I2C bus."""
323- if not self .i2cbus .try_lock ():
323+ if not self ._i2cbus .try_lock ():
324324 raise Exception ("Unable to lock I2C bus" )
325325
326326 try :
327- devices = self .i2cbus .scan ()
327+ devices = self ._i2cbus .scan ()
328328 except Exception as e :
329- self .i2cbus .unlock ()
329+ self ._i2cbus .unlock ()
330330 raise e
331331 else :
332- self .i2cbus .unlock ()
332+ self ._i2cbus .unlock ()
333333
334334 return devices
0 commit comments