88
99class Adafruit_I2C :
1010
11+ @staticmethod
1112 def getPiRevision ():
1213 "Gets the version number of the Raspberry Pi board"
1314 # Courtesy quick2wire-python-api
@@ -19,109 +20,110 @@ def getPiRevision():
1920 return 1 if line .rstrip ()[- 1 ] in ['1' ,'2' ] else 2
2021 except :
2122 return 0
23+
24+ @staticmethod
25+ def getPiI2CBusNumber ():
26+ # Gets the I2C bus number /dev/i2c#
27+ return 1 if Adafruit_I2C .getPiRevision () > 1 else 0
2228
23- def __init__ (self , address , bus = smbus . SMBus ( 1 if getPiRevision () > 1 else 0 ) , debug = False ):
29+ def __init__ (self , address , bus = - 1 , debug = False ):
2430 self .address = address
25- # By default, the correct I2C bus is auto-detected using /proc/cpuinfo
26- self .bus = bus
27- # Alternatively, you can hard-code the bus version below:
28- # self.bus = smbus.SMBus(0); # Force I2C0 (early 256MB Pi's)
31+ # By default, the correct I2C bus is auto-detected using /proc/cpuinfo
32+ # Alternatively, you can hard-code the bus version below:
33+ # self.bus = smbus.SMBus(0); # Force I2C0 (early 256MB Pi's)
2934 # self.bus = smbus.SMBus(1); # Force I2C1 (512MB Pi's)
35+ self .bus = smbus .SMBus (
36+ bus if bus >= 0 else Adafruit_I2C .getPiI2CBusNumber ())
3037 self .debug = debug
3138
3239 def reverseByteOrder (self , data ):
3340 "Reverses the byte order of an int (16-bit) or long (32-bit) value"
3441 # Courtesy Vishal Sapre
35- dstr = hex (data )[2 :].replace ('L' ,'' )
36- byteCount = len (dstr [::2 ])
37- val = 0
38- for i , n in enumerate (range (byteCount )):
39- d = data & 0xFF
40- val |= (d << (8 * (byteCount - i - 1 )))
42+ byteCount = len (hex (data )[2 :].replace ('L' ,'' )[::2 ])
43+ val = 0
44+ for i in range (byteCount ):
45+ val = (val << 8 ) | (data & 0xff )
4146 data >>= 8
4247 return val
4348
49+ def errMsg ():
50+ print "Error accessing 0x%02X: Check your I2C address" % self .address
51+ return - 1
52+
4453 def write8 (self , reg , value ):
4554 "Writes an 8-bit value to the specified register/address"
4655 try :
4756 self .bus .write_byte_data (self .address , reg , value )
48- if ( self .debug ) :
57+ if self .debug :
4958 print "I2C: Wrote 0x%02X to register 0x%02X" % (value , reg )
5059 except IOError , err :
51- print "Error accessing 0x%02X: Check your I2C address" % self .address
52- return - 1
60+ return errMsg ()
5361
5462 def writeList (self , reg , list ):
5563 "Writes an array of bytes using I2C format"
5664 try :
57- if ( self .debug ) :
65+ if self .debug :
5866 print "I2C: Writing list to register 0x%02X:" % reg
5967 print list
6068 self .bus .write_i2c_block_data (self .address , reg , list )
6169 except IOError , err :
62- print "Error accessing 0x%02X: Check your I2C address" % self .address
63- return - 1
70+ return errMsg ()
6471
6572 def readList (self , reg , length ):
6673 "Read a list of bytes from the I2C device"
6774 results = []
6875 try :
6976 results = self .bus .read_i2c_block_data (self .address , reg , length )
70- if (self .debug ):
71- print "I2C: Device 0x%02X returned the following from reg 0x%02X" % (self .address , reg )
77+ if self .debug :
78+ print ("I2C: Device 0x%02X returned the following from reg 0x%02X" %
79+ (self .address , reg ))
7280 print results
7381 return results
7482 except IOError , err :
75- print "Error accessing 0x%02X: Check your I2C address" % self .address
76- return - 1
83+ return errMsg ()
7784
7885 def readU8 (self , reg ):
7986 "Read an unsigned byte from the I2C device"
8087 try :
8188 result = self .bus .read_byte_data (self .address , reg )
82- if (self .debug ):
83- print "I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self .address , result & 0xFF , reg )
89+ if self .debug :
90+ print ("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" %
91+ (self .address , result & 0xFF , reg ))
8492 return result
8593 except IOError , err :
86- print "Error accessing 0x%02X: Check your I2C address" % self .address
87- return - 1
94+ return errMsg ()
8895
8996 def readS8 (self , reg ):
9097 "Reads a signed byte from the I2C device"
9198 try :
9299 result = self .bus .read_byte_data (self .address , reg )
93- if (self .debug ):
94- print "I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self .address , result & 0xFF , reg )
95- if (result > 127 ):
96- return result - 256
97- else :
98- return result
100+ if result > 127 : result -= 256
101+ if self .debug :
102+ print ("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" %
103+ (self .address , result & 0xFF , reg ))
104+ return result
99105 except IOError , err :
100- print "Error accessing 0x%02X: Check your I2C address" % self .address
101- return - 1
106+ return errMsg ()
102107
103108 def readU16 (self , reg ):
104109 "Reads an unsigned 16-bit value from the I2C device"
105110 try :
106- hibyte = self .bus .read_byte_data (self .address , reg )
107- result = ( hibyte << 8 ) + self .bus . read_byte_data ( self . address , reg + 1 )
108- if ( self . debug ):
109- print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self .address , result & 0xFFFF , reg )
111+ result = self .bus .read_word_data (self .address , reg )
112+ if self .debug :
113+ print ( "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" %
114+ (self .address , result & 0xFFFF , reg ) )
110115 return result
111116 except IOError , err :
112- print "Error accessing 0x%02X: Check your I2C address" % self .address
113- return - 1
117+ return errMsg ()
114118
115119 def readS16 (self , reg ):
116120 "Reads a signed 16-bit value from the I2C device"
117121 try :
118- hibyte = self .bus .read_byte_data (self .address , reg )
119- if (hibyte > 127 ):
120- hibyte -= 256
121- result = (hibyte << 8 ) + self .bus .read_byte_data (self .address , reg + 1 )
122- if (self .debug ):
123- print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self .address , result & 0xFFFF , reg )
122+ result = self .bus .read_word_data (self .address , reg )
123+ if result > 32767 : result -= 65536
124+ if self .debug :
125+ print ("I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" %
126+ (self .address , result & 0xFFFF , reg ))
124127 return result
125128 except IOError , err :
126- print "Error accessing 0x%02X: Check your I2C address" % self .address
127- return - 1
129+ return errMsg ()
0 commit comments