-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCA9534.py
More file actions
147 lines (118 loc) · 5.36 KB
/
TCA9534.py
File metadata and controls
147 lines (118 loc) · 5.36 KB
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
145
146
147
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
# SPDX-FileCopyrightText: Copyright (c) 2023 Chris Burton
#
# SPDX-License-Identifier: MIT
"""
`TCA9534`
=======
CircuitPython library for Texas Instrument PCA9534 and TCA9534 ICs.
* Author(s): Chris Burton
Usage Notes
-----------
Inversion only applies when reading a pin.
"""
try:
# This is only needed for typing
from typing import Optional
import busio
except ImportError:
pass
from adafruit_bus_device.i2c_device import I2CDevice
from micropython import const
import digitalio
_TCA9534_DEFAULT_I2C_ADDR = const(0x20) # Default I2C address
_TCA9534_REGISTER_INPUT_PORT = const(0x00) # Default XXXX XXXX
_TCA9534_REGISTER_OUTPUT_PORT = const(0x01) # Default 1111 1111
_TCA9534_REGISTER_INVERSION = const(0x02) # Default 0000 0000 (No inversion)
_TCA9534_REGISTER_CONFIGURATION = const(0x03) # Default 1111 1111 (All Inputs)
class TCA9534:
def __init__(self, i2c: I2C, address: int = _TCA9534_DEFAULT_I2C_ADDR, reset: bool = True) -> None:
self.i2c_device = I2CDevice(i2c, address)
self._output = bytearray([0])
self._inversion = bytearray([0])
self._configuration = bytearray([0])
if reset:
# Reset to all inputs, disable inversion, set outputs to 1
with self.i2c_device as i2c:
i2c.write( bytearray([_TCA9534_REGISTER_CONFIGURATION, 0xFF]) )
i2c.write( bytearray([_TCA9534_REGISTER_INVERSION, 0x00]) )
i2c.write( bytearray([_TCA9534_REGISTER_OUTPUT_PORT, 0xFF]) )
with self.i2c_device as i2c:
i2c.write_then_readinto( bytearray([_TCA9534_REGISTER_OUTPUT_PORT, ]), self._output )
i2c.write_then_readinto( bytearray([_TCA9534_REGISTER_INVERSION, ]), self._inversion )
i2c.write_then_readinto( bytearray([_TCA9534_REGISTER_CONFIGURATION, ]), self._configuration )
def read_gpio(self) -> int:
buf = bytearray([0])
with self.i2c_device as i2c:
i2c.write_then_readinto( bytearray([_TCA9534_REGISTER_INPUT_PORT, ]), buf )
return buf[0]
def write_gpio(self, val: int) -> None:
self._output[0] = val & 0xFF
with self.i2c_device as i2c:
i2c.write( bytearray([_TCA9534_REGISTER_OUTPUT_PORT, self._output[0]]) )
def set_iodir(self, val: int) -> None:
self._configuration[0] = (val & 0xFF)
with self.i2c_device as i2c:
i2c.write( bytearray([_TCA9534_REGISTER_CONFIGURATION, self._configuration[0]]) )
def get_iodir(self) -> int:
return self._configuration[0]
def get_inv(self) -> int:
return self._inversion[0]
def set_inv(self, val: int) -> None: # Inversion only applies to inputs
self._inversion[0] = val & 0xFF
with self.i2c_device as i2c:
i2c.write( bytearray([_TCA9534_REGISTER_INVERSION, self._inversion[0]]) )
def get_pin(self, pin: int) -> "DigitalInOut":
assert 0<= pin <= 7
return DigitalInOut(pin, self)
def write_pin(self, pin: int, val: bool) -> None:
if val:
self.write_gpio(self.output | (1<<pin))
else:
self.write_gpio(self.output & ~(1<<pin))
def read_pin(self, pin: int) -> bool:
return (self.read_gpio() >> pin) & 0x1
class DigitalInOut:
def __init__(self, pin_number: int, tca: TCA9534) -> None:
self._pin = pin_number
self._tca = tca
def switch_to_output(self, value: bool = False, **kwargs) -> None:
if value:
self._tca.write_gpio( self._tca._output[0] | ( 1<<self._pin ) )
else:
self._tca.write_gpio( self._tca._output[0] & ~( 1<<self._pin ) )
self._tca.set_iodir( ((self._tca._configuration[0] & (1<<self._pin)) >> self._pin) & ~( 1<<self._pin ) )
def switch_to_input(self, **kwargs) -> None:
self._tca.set_iodir( ((self._tca._configuration[0] & (1<<self._pin)) >> self._pin) | ( 1<<self._pin ) )
@property
def value(self) -> bool:
return (self._tca.read_gpio() & (1<<self._pin)) >> self._pin
@value.setter
def value(self, val: bool) -> None:
if val:
self._tca.write_gpio( self._tca._output[0] | ( 1<<self._pin ) )
else:
self._tca.write_gpio( self._tca._output[0] & ~( 1<<self._pin ) )
@property
def direction(self) -> digitalio.Direction:
if ((self._tca._configuration[0] & (1<<self._pin)) >> self._pin):
return digitalio.Direction.INPUT
else:
return digitalio.Direction.OUTPUT
@direction.setter
def direction(self,val: digitalio.Direction) -> None:
if val == digitalio.Direction.INPUT:
self._tca.set_iodir( ((self._tca._configuration[0] & (1<<self._pin)) >> self._pin) | ( 1<<self._pin ) )
elif val == digitalio.Direction.OUTPUT:
self._tca.set_iodir( ((self._tca._configuration[0] & (1<<self._pin)) >> self._pin) & ~( 1<<self._pin ) )
else:
raise ValueError("Expected INPUT or OUTPUT direction!")
@property
def invert_polarity(self) -> bool:
return self._tca._inversion[0] & (1<<self._pin) >> self._pin
@invert_polarity.setter
def invert_polarity(self, val: bool) -> None:
if val:
self._tca.set_inv( self._tca.get_inv() | (1<<self._pin) )
else:
self._tca.set_inv( self._tca.get_inv() & ~(1<<self._pin) )