1
+ # ===========================================================================
2
+ # Adafruit_I2C Class
3
+ # ===========================================================================
4
+ import time
5
+ import smbus
6
+
7
+ class Adafruit_I2C (object ):
8
+
9
+ @staticmethod
10
+ def getPiRevision ():
11
+ "Gets the version number of the Raspberry Pi board"
12
+ # Revision list available at: http://elinux.org/RPi_HardwareHistory#Board_Revision_History
13
+ try :
14
+ with open ('/proc/cpuinfo' , 'r' ) as infile :
15
+ for line in infile :
16
+ # Match a line of the form "Revision : 0002" while ignoring extra
17
+ # info in front of the revsion (like 1000 when the Pi was over-volted).
18
+ match = re .match ('Revision\s+:\s+.*(\w{4})$' , line )
19
+ if match and match .group (1 ) in ['0000' , '0002' , '0003' ]:
20
+ # Return revision 1 if revision ends with 0000, 0002 or 0003.
21
+ return 1
22
+ elif match :
23
+ # Assume revision 2 if revision ends with any other 4 chars.
24
+ return 2
25
+ # Couldn't find the revision, assume revision 0 like older code for compatibility.
26
+ return 0
27
+ except :
28
+ return 0
29
+
30
+ @staticmethod
31
+ def getPiI2CBusNumber ():
32
+ # Gets the I2C bus number /dev/i2c#
33
+ return 1 if Adafruit_I2C .getPiRevision () > 1 else 0
34
+
35
+ def __init__ (self , address , busnum = - 1 , debug = False ):
36
+ self .address = address
37
+ # By default, the correct I2C bus is auto-detected using /proc/cpuinfo
38
+ # Alternatively, you can hard-code the bus version below:
39
+ # self.bus = smbus.SMBus(0); # Force I2C0 (early 256MB Pi's)
40
+ # self.bus = smbus.SMBus(1); # Force I2C1 (512MB Pi's)
41
+ self .bus = smbus .SMBus (busnum if busnum >= 0 else Adafruit_I2C .getPiI2CBusNumber ())
42
+ self .debug = debug
43
+
44
+ def reverseByteOrder (self , data ):
45
+ ''' Reverses the byte order of an int (16-bit) or long (32-bit) value '''
46
+ byteCount = len (hex (data )[2 :].replace ('L' ,'' )[::2 ])
47
+ val = 0
48
+ for i in range (byteCount ):
49
+ val = (val << 8 ) | (data & 0xff )
50
+ data >>= 8
51
+ return val
52
+
53
+ def errMsg (self ):
54
+ print ("Error accessing 0x%02X: Check your I2C address" % self .address )
55
+ return - 1
56
+
57
+ def write8 (self , reg , value ):
58
+ "Writes an 8-bit value to the specified register/address"
59
+ try :
60
+ self .bus .write_byte_data (self .address , reg , value )
61
+ if self .debug :
62
+ print ("I2C: Wrote 0x%02X to register 0x%02X" % (value , reg ))
63
+ except IOError :
64
+ return self .errMsg ()
65
+
66
+ def write16 (self , reg , value ):
67
+ "Writes a 16-bit value to the specified register/address pair"
68
+ try :
69
+ self .bus .write_word_data (self .address , reg , value )
70
+ if self .debug :
71
+ print ("I2C: Wrote 0x%02X to register pair 0x%02X,0x%02X" % (value , reg , reg + 1 ))
72
+ except IOError :
73
+ return self .errMsg ()
74
+
75
+ def writeRaw8 (self , value ):
76
+ "Writes an 8-bit value on the bus"
77
+ try :
78
+ self .bus .write_byte (self .address , value )
79
+ if self .debug :
80
+ print ("I2C: Wrote 0x%02X" % value )
81
+ except IOError :
82
+ return self .errMsg ()
83
+
84
+ def writeList (self , reg , list ):
85
+ "Writes an array of bytes using I2C format"
86
+ try :
87
+ if self .debug :
88
+ print ("I2C: Writing list to register 0x%02X:" % reg )
89
+ print (list )
90
+ self .bus .write_i2c_block_data (self .address , reg , list )
91
+ except IOError :
92
+ return self .errMsg ()
93
+
94
+ def readList (self , reg , length ):
95
+ "Read a list of bytes from the I2C device"
96
+ try :
97
+ results = self .bus .read_i2c_block_data (self .address , reg , length )
98
+ if self .debug :
99
+ print ("I2C: Device 0x%02X returned the following from reg 0x%02X" % (self .address , reg ))
100
+ print (results )
101
+ return results
102
+ except IOError :
103
+ return self .errMsg ()
104
+
105
+ def readU8 (self , reg ):
106
+ "Read an unsigned byte from the I2C device"
107
+ try :
108
+ result = self .bus .read_byte_data (self .address , reg )
109
+ if self .debug :
110
+ print ("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self .address , result & 0xFF , reg ))
111
+ return (result )
112
+ except IOError :
113
+ return self .errMsg ()
114
+
115
+ def readS8 (self , reg ):
116
+ "Reads a signed byte from the I2C device"
117
+ try :
118
+ result = self .bus .read_byte_data (self .address , reg )
119
+ if result > 127 : result -= 256
120
+ if self .debug :
121
+ print ("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self .address , result & 0xFF , reg ))
122
+ return result
123
+ except IOError :
124
+ return self .errMsg ()
125
+
126
+ def readU16 (self , reg , little_endian = True ):
127
+ "Reads an unsigned 16-bit value from the I2C device"
128
+ try :
129
+ result = self .bus .read_word_data (self .address ,reg )
130
+ # Swap bytes if using big endian because read_word_data assumes little
131
+ # endian on ARM (little endian) systems.
132
+ if not little_endian :
133
+ result = ((result << 8 ) & 0xFF00 ) + (result >> 8 )
134
+ if (self .debug ):
135
+ print ("I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self .address , result & 0xFFFF , reg ))
136
+ return result
137
+ except IOError :
138
+ return self .errMsg ()
139
+
140
+ def readS16 (self , reg , little_endian = True ):
141
+ "Reads a signed 16-bit value from the I2C device"
142
+ try :
143
+ result = self .readU16 (reg ,little_endian )
144
+ if result > 32767 : result -= 65536
145
+ return result
146
+ except IOError :
147
+ return self .errMsg ()
0 commit comments