/
i2c.py
74 lines (58 loc) · 1.44 KB
/
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
'''
Raspio32 I2C interface and helpers
Inspired from AdaFruit I2C && MCP23017 Python library
(https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/)
'''
import smbus
class RaspioI2C:
'''
Raspio I2C management class
'''
@staticmethod
def get_bus_number():
'''
Get PI bus number based on revision
'''
try:
with open('/proc/cpuinfo', 'r') as f:
for line in f:
if line.startswith('Revision'):
return 1
except:
return 1
def __init__(self, address):
# save address
self.address = address
# create bus
self.bus = smbus.SMBus(self.get_bus_number())
def write_byte(self, reg, value):
'''
Write an 8-bit value to the specified register/address"
'''
# Write data to bus
self.bus.write_byte_data(self.address, reg, value)
def write_word(self, reg, value):
'''
Write a 16-bit value to the specified register/address
'''
self.bus.write_word_data(self.address, reg, value)
def write_array(self, reg, array):
'''
Write an array of bytes
'''
self.bus.write_i2c_block_data(self.address, reg, array)
def read_array(self, reg, length):
'''
Read an array of bytes
'''
return self.bus.read_i2c_block_data(self.address, reg, length)
def read_byte(self, reg):
'''
Read an unsigned byte from the bus
'''
return self.bus.read_byte_data(self.address, reg)
def read_word (self, reg):
'''
Read a 16-bit value from the bus
'''
return self.bus.read_word_data(self.address, reg)