Skip to content

Commit 9ac8577

Browse files
committed
Fix pre-commit formatting issues and includes
1 parent ee19760 commit 9ac8577

File tree

7 files changed

+416
-308
lines changed

7 files changed

+416
-308
lines changed

i2c_expanders/PCA9554.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@
77
# pylint: disable=too-many-public-methods
88

99

10-
#TODO: mostly a copy/paste from the PCA9555, make sure this stuff is still true here.
10+
# TODO: mostly a copy/paste from the PCA9555, make sure this stuff is still true here.
1111
"""
1212
`pca9554`
1313
====================================================
1414
1515
CircuitPython module for the PCA9554 and compatible expanders.
1616
The PCA9554 is a basic 8 pin I2C expander.
1717
*Configurable pins as input or output
18-
*Per pin polarity inversion. This inverts the value that is returned when an input port
18+
*Per pin polarity inversion. This inverts the value that is returned when an input port
1919
is read. Does not affect the pins set as outputs.
20-
*Pin change interrupts. An interrupt is generated on any pin change for a pin configured
21-
as an input. The interrupt signal is cleared by a change back to the original value of
20+
*Pin change interrupts. An interrupt is generated on any pin change for a pin configured
21+
as an input. The interrupt signal is cleared by a change back to the original value of
2222
the input pin or a read to the GPIO register.This will have to be detected and tracked in
2323
user code. There is no way to tell from the device what pin caused the interrupt.
24-
24+
2525
Use this class if you are using a PCA9554 or compatible expander. This class is also used
2626
as the base class for the PCAL9554 expander.
2727
@@ -33,27 +33,30 @@
3333
*PCA9554
3434
*PCA9538
3535
*TODO
36-
36+
3737
Heavily based on the code written by Tony DiCola for the MCP230xx library.
3838
3939
* Author(s): Pat Satyshur
4040
"""
4141

42-
from micropython import const
43-
from i2c_expander import I2c_Expander
44-
from lib.digital_inout import DigitalInOut
45-
#from mcp230xx import MCP230XX #removed relative import, might need this again if this is a module??
46-
#from digital_inout import DigitalInOut #removed relative import, might need this again if this is a module??
42+
# TODO: Fix these imports.
43+
from micropython import (
44+
const,
45+
) # TODO: What does const get me in this situation? Can I remove it?
46+
from i2c_expanders.i2c_expander import I2c_Expander
47+
from i2c_expanders.helpers import _enable_bit, Capability
4748

4849
__version__ = "0.0.0+auto.0"
4950
__repo__ = "https://github.com/ilikecake/CircuitPython_I2C_Expanders.git"
5051

51-
_PCA9554_ADDRESS = const(0x27) #TODO: this will probably change based on the device used. Not sure how to deal with this. Maybe remove the default and force the user to specify the address?
52+
# TODO: probably don't want this here.
53+
_PCA9554_ADDRESS = const(0x27)
54+
55+
_PCA9554_INPUT = const(0x00) # Input register
56+
_PCA9554_OUTPUT = const(0x01) # Output register
57+
_PCA9554_IPOL = const(0x02) # Polarity inversion register
58+
_PCA9554_IODIR = const(0x03) # Configuration (direction) register
5259

53-
_PCA9554_INPUT = const(0x00) #Input register
54-
_PCA9554_OUTPUT = const(0x01) #Output register
55-
_PCA9554_IPOL = const(0x02) #Polarity inversion register
56-
_PCA9554_IODIR = const(0x03) #Configuration (direction) register
5760

5861
class PCA9554(I2c_Expander):
5962
"""Supports PCA9554 instance on specified I2C bus and optionally
@@ -62,10 +65,12 @@ class PCA9554(I2c_Expander):
6265

6366
def __init__(self, i2c, address=_PCA9554_ADDRESS, reset=True):
6467
super().__init__(i2c, address)
68+
self.maxpins = 7
69+
self.capability = _enable_bit(0x00, Capability.INVERT_POL)
6570
if reset:
6671
# Reset to all inputs with no pull-ups and no inverted polarity.
67-
self.iodir = 0xFF #Set all IOs to inputs
68-
self.ipol = 0x00 #Set polatiry inversion off for all pins
72+
self.iodir = 0xFF # Set all IOs to inputs
73+
self.ipol = 0x00 # Set polatiry inversion off for all pins
6974

7075
@property
7176
def gpio(self):

i2c_expanders/PCA9555.py

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
CircuitPython module for the PCA9555 and compatible expanders.
1414
The PCA9555 is a basic 16 pin I2C expander.
1515
*Configurable pins as input or output
16-
*Per pin polarity inversion. This inverts the value that is returned when an input port
16+
*Per pin polarity inversion. This inverts the value that is returned when an input port
1717
is read. Does not affect the pins set as outputs.
18-
*Pin change interrupts. An interrupt is generated on any pin change for a pin configured
19-
as an input. The interrupt signal is cleared by a change back to the original value of
18+
*Pin change interrupts. An interrupt is generated on any pin change for a pin configured
19+
as an input. The interrupt signal is cleared by a change back to the original value of
2020
the input pin or a read to the GPIO register.This will have to be detected and tracked in
2121
user code. There is no way to tell from the device what pin caused the interrupt.
22-
22+
2323
Use this class if you are using a PCA9555 or compatible expander. This class is also used
2424
as the base class for the PCAL9555 expander.
2525
@@ -36,39 +36,45 @@
3636
* Author(s): Pat Satyshur
3737
"""
3838

39-
#DriveMode: PUSH_PULL vs OPEN_DRAIN
40-
#Pull: Pull.Up vs Pull.DOWN vs None
41-
#TODO: Handle interrupts in here somewhere
39+
# DriveMode: PUSH_PULL vs OPEN_DRAIN
40+
# Pull: Pull.Up vs Pull.DOWN vs None
41+
# TODO: Handle interrupts in here somewhere
42+
43+
from micropython import (
44+
const,
45+
) # TODO: What does const get me in this situation? Can I remove it?
46+
from i2c_expanders.i2c_expander import I2c_Expander
47+
from i2c_expanders.helpers import _enable_bit, Capability
4248

43-
from micropython import const #TODO: What does const get me in this situation? Can I remove it?
44-
import i2c_expander
45-
from lib.digital_inout import DigitalInOut, _enable_bit
46-
#from mcp230xx import MCP230XX #TODO: removed relative import, might need this again if this is a module??
47-
#from digital_inout import DigitalInOut #removed relative import, might need this again if this is a module??
49+
# from i2c_expanders.digital_inout import _enable_bit
4850

4951
__version__ = "0.0.0+auto.0"
5052
__repo__ = "https://github.com/ilikecake/CircuitPython_I2C_Expanders.git"
5153

52-
_PCA9555_ADDRESS = const(0x20) #TODO: this will probably change based on the device used. Not sure how to deal with this. Maybe remove the default and force the user to specify the address?
54+
# TODO: this will probably change based on the device used.
55+
# Not sure how to deal with this. Maybe remove the default and
56+
# force the user to specify the address?
57+
58+
_PCA9555_ADDRESS = const(0x20)
59+
_PCA9555_INPUT0 = const(0x00) # Input register 0
60+
_PCA9555_INPUT1 = const(0x01) # Input register 1
61+
_PCA9555_OUTPUT0 = const(0x02) # Output register 0
62+
_PCA9555_OUTPUT1 = const(0x03) # Output register 1
63+
_PCA9555_IPOL0 = const(0x04) # Polarity inversion register 0
64+
_PCA9555_IPOL1 = const(0x05) # Polarity inversion register 1
65+
_PCA9555_IODIR0 = const(0x06) # Configuration (direction) register 0
66+
_PCA9555_IODIR1 = const(0x07) # Configuration (direction) register 1
5367

54-
_PCA9555_INPUT0 = const(0x00) #Input register 0
55-
_PCA9555_INPUT1 = const(0x01) #Input register 1
56-
_PCA9555_OUTPUT0 = const(0x02) #Output register 0
57-
_PCA9555_OUTPUT1 = const(0x03) #Output register 1
58-
_PCA9555_IPOL0 = const(0x04) #Polarity inversion register 0
59-
_PCA9555_IPOL1 = const(0x05) #Polarity inversion register 1
60-
_PCA9555_IODIR0 = const(0x06) #Configuration (direction) register 0
61-
_PCA9555_IODIR1 = const(0x07) #Configuration (direction) register 1
6268

63-
class PCA9555(i2c_expander.I2c_Expander):
69+
class PCA9555(I2c_Expander):
6470
"""Supports PCA9555 instance on specified I2C bus and optionally
6571
at the specified I2C address.
6672
"""
6773

6874
def __init__(self, i2c, address=_PCA9555_ADDRESS, reset=True):
6975
super().__init__(i2c, address)
70-
self._maxpins = 15 #how many pins does the IO expander have starts at 0 and goes up to this value. An 8 pin expander would set maxpins to 7 (0-7).
71-
self._capability = _enable_bit(0x00, i2c_expander.Capability.INVERT_POL)
76+
self.maxpins = 15
77+
self.capability = _enable_bit(0x00, Capability.INVERT_POL)
7278
if reset:
7379
self.reset_to_defaults()
7480

@@ -96,15 +102,17 @@ def iodir(self, val):
96102
self._write_u16le(_PCA9555_IODIR0, val)
97103

98104
def reset_to_defaults(self):
99-
''' Reset all registers to their default state. This is also done with a power cycle, but it can be called by software here.
100-
101-
:return: Nothing.
102-
'''
103-
#TODO: Should I make some sort of 'register' class to handle memory addresses and default states?
104-
#Input port register is read only.
105-
self.gpio = 0xFFFF
106-
self.ipol = 0x0000
107-
self.iodir = 0xFFFF
105+
"""Reset all registers to their default state. This is also
106+
done with a power cycle, but it can be called by software here.
107+
108+
:return: Nothing.
109+
"""
110+
# TODO: Should I make some sort of 'register' class to
111+
# handle memory addresses and default states?
112+
# Input port register is read only.
113+
self.gpio = 0xFFFF
114+
self.ipol = 0x0000
115+
self.iodir = 0xFFFF
108116

109117
@property
110118
def ipol(self):

i2c_expanders/PCAL9554.py

Lines changed: 64 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# pylint: disable=too-many-public-methods
88

9-
#TODO: mostly a copy/paste from the PCAL9555, make sure this stuff is still true here.
9+
# TODO: mostly a copy/paste from the PCAL9555, make sure this stuff is still true here.
1010
"""
1111
`pcal9554`
1212
====================================================
@@ -36,28 +36,25 @@
3636
* Author(s): Pat Satyshur
3737
"""
3838

39+
# TODO: Disable unused imports here. After I finish this code, turn that off and see if they
40+
# are still unused.
41+
# pylint: disable=unused-import
3942
from micropython import const
40-
from PCA9554 import PCA9554 #removed relative import, might need this again if this is a module??
41-
from digitalio import DigitalInOut #Do i need this??
42-
#import digitalio
43+
import digitalio # import DigitalInOut #TODO: Do i need this??
44+
45+
from i2c_expanders.PCA9554 import PCA9554
46+
from i2c_expanders.helpers import (
47+
Capability,
48+
_get_bit,
49+
_enable_bit,
50+
_clear_bit,
51+
)
4352

4453
__version__ = "0.0.0+auto.0"
4554
__repo__ = "https://github.com/ilikecake/CircuitPython_I2C_Expanders.git"
4655

47-
# Internal helpers to simplify setting and getting a bit inside an integer.
48-
#TODO: Can I include these from another file?
49-
def _get_bit(val, bit):
50-
return val & (1 << bit) > 0
51-
52-
53-
def _enable_bit(val, bit):
54-
return val | (1 << bit)
55-
56-
57-
def _clear_bit(val, bit):
58-
return val & ~(1 << bit)
59-
60-
_PCAL9554_ADDRESS = const(0x27) #TODO: this will probably change based on the device used. Not sure how to deal with this. Maybe remove the default and force the user to specify the address?
56+
# TODO: Probably don't want this here.
57+
_PCAL9554_ADDRESS = const(0x27)
6158

6259
_PCAL9554_OUTPUT_DRIVE_1 = const(0x40)
6360
_PCAL9554_OUTPUT_DRIVE_2 = const(0x41)
@@ -68,51 +65,80 @@ def _clear_bit(val, bit):
6865
_PCAL9554_IRQ_STATUS = const(0x46)
6966
_PCAL9554_OUTPUT_PORT_CONFIG = const(0x4F)
7067

68+
7169
class PCAL9554(PCA9554):
7270
"""Supports PCAL9554 instance on specified I2C bus and optionally
7371
at the specified I2C address.
7472
"""
7573

7674
def __init__(self, i2c, address=_PCAL9554_ADDRESS, reset=True):
77-
super().__init__(i2c, address, False) #This initializes the PCA9554 compatible registers.
78-
self._capability = _enable_bit(0x00, i2c_expander.Capability.PULL_UP) | \
79-
_enable_bit(0x00, i2c_expander.Capability.PULL_DOWN) | \
80-
_enable_bit(0x00, i2c_expander.Capability.INVERT_POL) | \
81-
_enable_bit(0x00, i2c_expander.Capability.DRIVE_MODE) #TODO: This device does not really have a capability to set drive mode the way digitalio is expecting. I should probably not se this here.
75+
super().__init__(
76+
i2c, address, False
77+
) # This initializes the PCA9554 compatible registers.
78+
self._capability = (
79+
_enable_bit(0x00, Capability.PULL_UP)
80+
| _enable_bit(0x00, Capability.PULL_DOWN)
81+
| _enable_bit(0x00, Capability.INVERT_POL)
82+
| _enable_bit(0x00, Capability.DRIVE_MODE)
83+
) # TODO: This device does not really have a capability to set drive mode the way
84+
# digitalio is expecting. I should probably not set this here.
8285
if reset:
8386
self.reset_to_defaults()
8487

88+
def reset_to_defaults(self):
89+
"""Reset all registers to their default state. This is also
90+
done with a power cycle, but it can be called by software here.
91+
92+
:return: Nothing.
93+
"""
94+
# TODO: Should I make some sort of 'register' class to handle
95+
# memory addresses and default states?
96+
# Input port and interrupt status registers are read only.
97+
self.gpio = 0xFF
98+
self.ipol = 0x0000
99+
self.iodir = 0xFF
100+
101+
# self.out0_drive = 0xFFFF
102+
# self.out1_drive = 0xFFFF
103+
# self.input_latch = 0x0000
104+
# self.pupd_en = 0xFFFF
105+
# self.pupd_sel = 0xFFFF
106+
# self.irq_mask = 0xFFFF
107+
# self.out_port_config = 0x00
108+
85109
@property
86110
def pupd_en(self):
87-
"""reads the pull up/down status
88-
"""
111+
"""reads the pull up/down status"""
89112
return self._read_u8(_PCAL9554_PUPD_EN)
90113

91114
@pupd_en.setter
92115
def pupd_en(self, val):
93116
self._write_u8(_PCAL9554_PUPD_EN, val)
94-
117+
95118
@property
96119
def pupd_sel(self):
97-
"""reads the pull up/down status
98-
"""
120+
"""reads the pull up/down status"""
99121
return self._read_u8(_PCAL9554_PUPD_SEL)
100122

101123
@pupd_sel.setter
102124
def pupd_sel(self, val):
103125
self._write_u8(_PCAL9554_PUPD_SEL, val)
104-
105-
#Enable interrupt on a pin. Interrupts are triggered by any state change of the pin.
126+
127+
# Enable interrupt on a pin. Interrupts are triggered by any state change of the pin.
106128
def set_int_pin(self, pin):
107-
self._write_u8(_PCAL9554_IRQ_MASK, _clear_bit(self._read_u8(_PCAL9554_IRQ_MASK), pin))
108-
self.gpio() #Read from the input port register to clear interrupts
109-
110-
#Disable interrupts on a pin.
129+
"""Set interrupt pin"""
130+
self._write_u8(
131+
_PCAL9554_IRQ_MASK, _clear_bit(self._read_u8(_PCAL9554_IRQ_MASK), pin)
132+
)
133+
134+
# Disable interrupts on a pin.
111135
def clear_int_pin(self, pin):
112-
self._write_u8(_PCAL9554_IRQ_MASK, _set_bit(self._read_u8(_PCAL9554_IRQ_MASK), pin))
113-
self.gpio() #Read from the input port register to clear interrupts. Not sure if I need this here...
114-
136+
"""Clear interrupt pin"""
137+
self._write_u8(
138+
_PCAL9554_IRQ_MASK, _enable_bit(self._read_u8(_PCAL9554_IRQ_MASK), pin)
139+
)
140+
115141
@property
116142
def get_int_status(self):
143+
"""Get interrupt status"""
117144
return self._read_u8(_PCAL9554_IRQ_STATUS)
118-

0 commit comments

Comments
 (0)