-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdafruit_I2C.py
144 lines (118 loc) · 4.39 KB
/
Adafruit_I2C.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/python
# NOTE!! This code is from the Adafruit Learning System articles on the Raspberry Pi (http://learn.adafruit.com/)
# The original version of the code can be found in the Adafruit Raspberry Pi Python Library on Github at https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code
# ===========================================================================
# Adafruit_I2C Base Class
# ===========================================================================
class Adafruit_I2C :
def __init__(self, address, busnum = 3, debug=False):
try:
import smbus
self.bus = smbus.SMBus(busnum)
except ImportError:
self.bus=None
self.address = address
self.debug = debug
def reverseByteOrder(self, data):
"Reverses the byte order of an int (16-bit) or long (32-bit) value"
# Courtesy Vishal Sapre
dstr = hex(data)[2:].replace('L','')
byteCount = len(dstr[::2])
val = 0
for i, n in enumerate(range(byteCount)):
d = data & 0xFF
val |= (d << (8 * (byteCount - i - 1)))
data >>= 8
return val
def write8(self, reg, value):
"Writes an 8-bit value to the specified register/address"
if self.bus is None:
return -1
try:
self.bus.write_byte_data(self.address, reg, value)
if (self.debug):
print "I2C: Wrote 0x%02X to register 0x%02X" % (value, reg)
except IOError, err:
print "Error accessing 0x%02X: Check your I2C address" % self.address
return -1
def writeList(self, reg, list):
"Writes an array of bytes using I2C format"
if self.bus is None:
return -1
try:
if (self.debug):
print "I2C: Writing list to register 0x%02X:" % reg
print list
self.bus.write_i2c_block_data(self.address, reg, list)
except IOError, err:
print "Error accessing 0x%02X: Check your I2C address" % self.address
return -1
def readList(self, reg, length):
"Read a list of bytes from the I2C device"
if self.bus is None:
return -1
results = []
try:
results = self.bus.read_i2c_block_data(self.address, reg, length)
if (self.debug):
print "I2C: Device 0x%02X returned the following from reg 0x%02X" % (self.address, reg)
print results
return results
except IOError, err:
print "Error accessing 09x%02X: Check your I2C address" % self.address
return -1
def readU8(self, reg):
"Read an unsigned byte from the I2C device"
if self.bus is None:
return -1
try:
result = self.bus.read_byte_data(self.address, reg)
if (self.debug):
print "I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self.address, result & 0xFF, reg)
return result
except IOError, err:
print "Error accessing 0x%02X: Check your I2C address" % self.address
return -1
def readS8(self, reg):
"Reads a signed byte from the I2C device"
if self.bus is None:
return -1
try:
result = self.bus.read_byte_data(self.address, reg)
if (self.debug):
print "I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self.address, result & 0xFF, reg)
if (result > 127):
return result - 256
else:
return result
except IOError, err:
print "Error accessing 0x%02X: Check your I2C address" % self.address
return -1
def readU16(self, reg):
"Reads an unsigned 16-bit value from the I2C device"
if self.bus is None:
return -1
try:
hibyte = self.bus.read_byte_data(self.address, reg)
result = (hibyte << 8) + self.bus.read_byte_data(self.address, reg+1)
if (self.debug):
print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg)
return result
except IOError, err:
print "Error accessing 0x%02X: Check your I2C address" % self.address
return -1
def readS16(self, reg):
"Reads a signed 16-bit value from the I2C device"
if self.bus is None:
return -1
try:
hibyte = self.bus.read_byte_data(self.address, reg)
if (hibyte > 127):
hibyte -= 256
result = (hibyte << 8) + self.bus.read_byte_data(self.address, reg+1)
if (self.debug):
print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg)
return result
except IOError, err:
print "Error accessing 0x%02X: Check your I2C address" % self.address
return -1