1
1
import time
2
2
import smbus
3
- from I2C import Adafruit_I2C
4
3
5
4
__all__ = ['ADS1115' ]
6
5
@@ -175,3 +174,150 @@ def readADCSingleEnded(self, channel=0, pga=6144, sps=250):
175
174
return (val - 0xFFFF )* pga / 32768.0
176
175
else :
177
176
return ( (result [0 ] << 8 ) | (result [1 ]) )* pga / 32768.0
177
+
178
+
179
+ # ===========================================================================
180
+ # Adafruit_I2C Class
181
+ # ===========================================================================
182
+
183
+ class Adafruit_I2C (object ):
184
+
185
+ @staticmethod
186
+ def getPiRevision ():
187
+ "Gets the version number of the Raspberry Pi board"
188
+ # Revision list available at: http://elinux.org/RPi_HardwareHistory#Board_Revision_History
189
+ try :
190
+ with open ('/proc/cpuinfo' , 'r' ) as infile :
191
+ for line in infile :
192
+ # Match a line of the form "Revision : 0002" while ignoring extra
193
+ # info in front of the revsion (like 1000 when the Pi was over-volted).
194
+ match = re .match ('Revision\s+:\s+.*(\w{4})$' , line )
195
+ if match and match .group (1 ) in ['0000' , '0002' , '0003' ]:
196
+ # Return revision 1 if revision ends with 0000, 0002 or 0003.
197
+ return 1
198
+ elif match :
199
+ # Assume revision 2 if revision ends with any other 4 chars.
200
+ return 2
201
+ # Couldn't find the revision, assume revision 0 like older code for compatibility.
202
+ return 0
203
+ except :
204
+ return 0
205
+
206
+ @staticmethod
207
+ def getPiI2CBusNumber ():
208
+ # Gets the I2C bus number /dev/i2c#
209
+ return 1 if Adafruit_I2C .getPiRevision () > 1 else 0
210
+
211
+ def __init__ (self , address , busnum = - 1 , debug = False ):
212
+ self .address = address
213
+ # By default, the correct I2C bus is auto-detected using /proc/cpuinfo
214
+ # Alternatively, you can hard-code the bus version below:
215
+ # self.bus = smbus.SMBus(0); # Force I2C0 (early 256MB Pi's)
216
+ # self.bus = smbus.SMBus(1); # Force I2C1 (512MB Pi's)
217
+ self .bus = smbus .SMBus (busnum if busnum >= 0 else Adafruit_I2C .getPiI2CBusNumber ())
218
+ self .debug = debug
219
+
220
+ def reverseByteOrder (self , data ):
221
+ ''' Reverses the byte order of an int (16-bit) or long (32-bit) value '''
222
+ byteCount = len (hex (data )[2 :].replace ('L' ,'' )[::2 ])
223
+ val = 0
224
+ for i in range (byteCount ):
225
+ val = (val << 8 ) | (data & 0xff )
226
+ data >>= 8
227
+ return val
228
+
229
+ def errMsg (self ):
230
+ print ("Error accessing 0x%02X: Check your I2C address" % self .address )
231
+ return - 1
232
+
233
+ def write8 (self , reg , value ):
234
+ "Writes an 8-bit value to the specified register/address"
235
+ try :
236
+ self .bus .write_byte_data (self .address , reg , value )
237
+ if self .debug :
238
+ print ("I2C: Wrote 0x%02X to register 0x%02X" % (value , reg ))
239
+ except IOError :
240
+ return self .errMsg ()
241
+
242
+ def write16 (self , reg , value ):
243
+ "Writes a 16-bit value to the specified register/address pair"
244
+ try :
245
+ self .bus .write_word_data (self .address , reg , value )
246
+ if self .debug :
247
+ print ("I2C: Wrote 0x%02X to register pair 0x%02X,0x%02X" % (value , reg , reg + 1 ))
248
+ except IOError :
249
+ return self .errMsg ()
250
+
251
+ def writeRaw8 (self , value ):
252
+ "Writes an 8-bit value on the bus"
253
+ try :
254
+ self .bus .write_byte (self .address , value )
255
+ if self .debug :
256
+ print ("I2C: Wrote 0x%02X" % value )
257
+ except IOError :
258
+ return self .errMsg ()
259
+
260
+ def writeList (self , reg , list ):
261
+ "Writes an array of bytes using I2C format"
262
+ try :
263
+ if self .debug :
264
+ print ("I2C: Writing list to register 0x%02X:" % reg )
265
+ print (list )
266
+ self .bus .write_i2c_block_data (self .address , reg , list )
267
+ except IOError :
268
+ return self .errMsg ()
269
+
270
+ def readList (self , reg , length ):
271
+ "Read a list of bytes from the I2C device"
272
+ try :
273
+ results = self .bus .read_i2c_block_data (self .address , reg , length )
274
+ if self .debug :
275
+ print ("I2C: Device 0x%02X returned the following from reg 0x%02X" % (self .address , reg ))
276
+ print (results )
277
+ return results
278
+ except IOError :
279
+ return self .errMsg ()
280
+
281
+ def readU8 (self , reg ):
282
+ "Read an unsigned byte from the I2C device"
283
+ try :
284
+ result = self .bus .read_byte_data (self .address , reg )
285
+ if self .debug :
286
+ print ("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self .address , result & 0xFF , reg ))
287
+ return (result )
288
+ except IOError :
289
+ return self .errMsg ()
290
+
291
+ def readS8 (self , reg ):
292
+ "Reads a signed byte from the I2C device"
293
+ try :
294
+ result = self .bus .read_byte_data (self .address , reg )
295
+ if result > 127 : result -= 256
296
+ if self .debug :
297
+ print ("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self .address , result & 0xFF , reg ))
298
+ return result
299
+ except IOError :
300
+ return self .errMsg ()
301
+
302
+ def readU16 (self , reg , little_endian = True ):
303
+ "Reads an unsigned 16-bit value from the I2C device"
304
+ try :
305
+ result = self .bus .read_word_data (self .address ,reg )
306
+ # Swap bytes if using big endian because read_word_data assumes little
307
+ # endian on ARM (little endian) systems.
308
+ if not little_endian :
309
+ result = ((result << 8 ) & 0xFF00 ) + (result >> 8 )
310
+ if (self .debug ):
311
+ print ("I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self .address , result & 0xFFFF , reg ))
312
+ return result
313
+ except IOError :
314
+ return self .errMsg ()
315
+
316
+ def readS16 (self , reg , little_endian = True ):
317
+ "Reads a signed 16-bit value from the I2C device"
318
+ try :
319
+ result = self .readU16 (reg ,little_endian )
320
+ if result > 32767 : result -= 65536
321
+ return result
322
+ except IOError :
323
+ return self .errMsg ()
0 commit comments