Skip to content

Commit 3f0a6c3

Browse files
committed
Character_LCD_I2C: add support for PCF8574 I2C expander backpack
Signed-off-by: Roman Ondráček <[email protected]>
1 parent 9c147b1 commit 3f0a6c3

File tree

3 files changed

+58
-18
lines changed

3 files changed

+58
-18
lines changed

README.rst

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ This driver depends on:
5555
* `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_
5656
* `Adafruit CircuitPython BusDevice <https://github.com/adafruit/Adafruit_CircuitPython_BusDevice>`_
5757
* `Adafruit CircuitPython MCP230xx <https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx>`_
58+
* `Adafruit CircuitPython PCF8574 <https://github.com/adafruit/Adafruit_CircuitPython_PCF8574>`_
5859
* `Adafruit CircuitPython 74HC595 <https://github.com/adafruit/Adafruit_CircuitPython_74HC595>`_
5960

6061
I2C & SPI displays also depend on:

adafruit_character_lcd/character_lcd_i2c.py

+56-18
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,25 @@
3535
except ImportError:
3636
pass
3737

38+
from micropython import const
3839
from adafruit_mcp230xx.mcp23008 import MCP23008
40+
from adafruit_pcf8574 import PCF8574
3941
from adafruit_character_lcd.character_lcd import Character_LCD_Mono
4042

4143
__version__ = "0.0.0+auto.0"
4244
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CharLCD.git"
4345

4446

47+
class I2C_Expander:
48+
# pylint: disable=too-few-public-methods
49+
"""
50+
I2C Expander ICs
51+
"""
52+
53+
MCP23008 = "MCP23008"
54+
PCF8574 = "PCF8574"
55+
56+
4557
class Character_LCD_I2C(Character_LCD_Mono):
4658
# pylint: disable=too-few-public-methods
4759
"""Character LCD connected to I2C/SPI backpack using its I2C connection.
@@ -67,30 +79,56 @@ def __init__(
6779
lines: int,
6880
address: Optional[int] = None,
6981
backlight_inverted: bool = False,
82+
expander: I2C_Expander = I2C_Expander.MCP23008,
7083
) -> None:
7184
"""Initialize character LCD connected to backpack using I2C connection
7285
on the specified I2C bus with the specified number of columns and
7386
lines on the display. Optionally specify if backlight is inverted.
7487
"""
7588

76-
if address:
77-
self.mcp = MCP23008(i2c, address=address)
78-
else:
79-
self.mcp = MCP23008(i2c)
80-
super().__init__(
81-
self.mcp.get_pin(1), # reset
82-
self.mcp.get_pin(2), # enable
83-
self.mcp.get_pin(3), # data line 4
84-
self.mcp.get_pin(4), # data line 5
85-
self.mcp.get_pin(5), # data line 6
86-
self.mcp.get_pin(6), # data line 7
87-
columns,
88-
lines,
89-
backlight_pin=self.mcp.get_pin(7),
90-
backlight_inverted=backlight_inverted,
91-
)
89+
if expander == I2C_Expander.MCP23008:
90+
if address:
91+
self.expander = MCP23008(i2c, address=address)
92+
else:
93+
self.expander = MCP23008(i2c)
94+
95+
super().__init__(
96+
self.expander.get_pin(1), # reset
97+
self.expander.get_pin(2), # enable
98+
self.expander.get_pin(3), # data line 4
99+
self.expander.get_pin(4), # data line 5
100+
self.expander.get_pin(5), # data line 6
101+
self.expander.get_pin(6), # data line 7
102+
columns,
103+
lines,
104+
backlight_pin=self.expander.get_pin(7),
105+
backlight_inverted=backlight_inverted,
106+
)
107+
108+
elif expander == I2C_Expander.PCF8574:
109+
if address:
110+
self.expander = PCF8574(i2c, address=address)
111+
else:
112+
self.expander = PCF8574(i2c)
113+
114+
super().__init__(
115+
self.expander.get_pin(0), # reset
116+
self.expander.get_pin(2), # enable
117+
self.expander.get_pin(4), # data line 4
118+
self.expander.get_pin(5), # data line 5
119+
self.expander.get_pin(6), # data line 6
120+
self.expander.get_pin(7), # data line 7
121+
columns,
122+
lines,
123+
backlight_pin=self.expander.get_pin(3),
124+
backlight_inverted=backlight_inverted,
125+
)
92126

93127
def _write8(self, value: int, char_mode: bool = False) -> None:
128+
if not isinstance(self.expander, MCP23008):
129+
super()._write8(value, char_mode)
130+
return
131+
94132
# Sends 8b ``value`` in ``char_mode``.
95133
# :param value: bytes
96134
# :param char_mode: character/data mode selector. False (default) for
@@ -112,13 +150,13 @@ def _write8(self, value: int, char_mode: bool = False) -> None:
112150
backlight_bit = int(self.backlight ^ self.backlight_inverted) << 7
113151

114152
# Write char_mode and upper 4 bits of data, shifted to the correct position.
115-
self.mcp.gpio = reset_bit | backlight_bit | ((value & 0xF0) >> 1)
153+
self.expander.gpio = reset_bit | backlight_bit | ((value & 0xF0) >> 1)
116154

117155
# do command
118156
self._pulse_enable()
119157

120158
# Write char_mode and lower 4 bits of data, shifted to the correct position.
121-
self.mcp.gpio = reset_bit | backlight_bit | ((value & 0x0F) << 3)
159+
self.expander.gpio = reset_bit | backlight_bit | ((value & 0x0F) << 3)
122160

123161
# do command
124162
self._pulse_enable()

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
Adafruit-Blinka
66
adafruit-circuitpython-mcp230xx
7+
adafruit-circuitpython-pcf8574
78
adafruit-circuitpython-busdevice
89
adafruit-circuitpython-74hc595
910
adafruit-circuitpython-typing~=1.5

0 commit comments

Comments
 (0)