-
Notifications
You must be signed in to change notification settings - Fork 103
/
mcp23017.py
63 lines (47 loc) · 2.14 KB
/
mcp23017.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
"""
Low-level register access and a high-level application-programming
interface for the MCP23017 I2C GPIO expander.
"""
from quick2wire.i2c import writing_bytes, reading
import quick2wire.parts.mcp23x17 as mcp23x17
from quick2wire.parts.mcp23x17 import deferred_read, immediate_read, deferred_write, immediate_write, In, Out
class MCP23017(mcp23x17.PinBanks):
"""Application programming interface to the MCP23017 GPIO extender"""
def __init__(self, master, address=0x20):
"""Initialise to control an MCP23017 at the specified address via the given I2CMaster.
Parameters:
master -- the quick2wire.i2c.I2CMaster used to communicate with the chip.
address -- the address of the chip on the I2C bus (defaults to 0x20).
"""
super().__init__(Registers(master, address))
class Registers(mcp23x17.Registers):
"""Low level access to the MCP23017 registers
The MCP23017 has two register addressing modes, depending on the
value of bit7 of IOCON. We assume bank=0 addressing (which is the
POR default value).
"""
def __init__(self, master, address):
"""Initialise to control an MCP23017 at the specified address via the given I2CMaster.
Parameters:
master -- the quick2wire.i2c.I2CMaster used to communicate with the chip.
address -- the address of the chip on the I2C bus (defaults to 0x20).
"""
self.master = master
self.address = address
def write_register(self, register_id, byte):
"""Write the value of a register.
Parameters:
reg -- the register address
value -- the new value of the register
"""
self.master.transaction(
writing_bytes(self.address, register_id, byte))
def read_register(self, register_id):
"""Read the value of a register.
Parameters:
reg -- the register address
Returns: the value of the register.
"""
return self.master.transaction(
writing_bytes(self.address, register_id),
reading(self.address, 1))[0][0]