-
Notifications
You must be signed in to change notification settings - Fork 5
/
__init__.py
68 lines (52 loc) · 2.43 KB
/
__init__.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
""" Provides an SMBus class for use on micropython """
try:
from machine import I2C
except ImportError:
raise ImportError("Can't find the micropython machine.I2C class: "
"perhaps you don't need this adapter?")
class SMBus(I2C):
""" Provides an 'SMBus' module which supports some of the py-smbus
i2c methods, as well as being a subclass of machine.I2C
Hopefully this will allow you to run code that was targeted at
py-smbus unmodified on micropython.
Use it like you would the machine.I2C class:
import usmbus.SMBus
bus = SMBus(1, pins=('G15','G10'), baudrate=100000)
bus.read_byte_data(addr, register)
... etc
"""
def read_byte_data(self, addr, register):
""" Read a single byte from register of device at addr
Returns a single byte """
return self.readfrom_mem(addr, register, 1)[0]
def read_i2c_block_data(self, addr, register, length):
""" Read a block of length from register of device at addr
Returns a bytes object filled with whatever was read """
return self.readfrom_mem(addr, register, length)
def write_byte_data(self, addr, register, data):
""" Write a single byte from buffer `data` to register of device at addr
Returns None """
# writeto_mem() expects something it can treat as a buffer
if isinstance(data, int):
data = bytes([data])
return self.writeto_mem(addr, register, data)
def write_i2c_block_data(self, addr, register, data):
""" Write multiple bytes of data to register of device at addr
Returns None """
# writeto_mem() expects something it can treat as a buffer
if isinstance(data, int):
data = bytes([data])
return self.writeto_mem(addr, register, data)
# The follwing haven't been implemented, but could be.
def read_byte(self, *args, **kwargs):
""" Not yet implemented """
raise RuntimeError("Not yet implemented")
def write_byte(self, *args, **kwargs):
""" Not yet implemented """
raise RuntimeError("Not yet implemented")
def read_word_data(self, *args, **kwargs):
""" Not yet implemented """
raise RuntimeError("Not yet implemented")
def write_word_data(self, *args, **kwargs):
""" Not yet implemented """
raise RuntimeError("Not yet implemented")