3636 pass
3737
3838from adafruit_mcp230xx .mcp23008 import MCP23008
39+ from adafruit_pcf8574 import PCF8574
3940from adafruit_character_lcd .character_lcd import Character_LCD_Mono
4041
4142__version__ = "0.0.0+auto.0"
4243__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CharLCD.git"
4344
4445
46+ class I2C_Expander :
47+ # pylint: disable=too-few-public-methods
48+ """
49+ I2C Expander ICs
50+ """
51+
52+ MCP23008 = "MCP23008"
53+ PCF8574 = "PCF8574"
54+
55+
4556class Character_LCD_I2C (Character_LCD_Mono ):
4657 # pylint: disable=too-few-public-methods
4758 """Character LCD connected to I2C/SPI backpack using its I2C connection.
@@ -67,30 +78,56 @@ def __init__(
6778 lines : int ,
6879 address : Optional [int ] = None ,
6980 backlight_inverted : bool = False ,
81+ expander : I2C_Expander = I2C_Expander .MCP23008 ,
7082 ) -> None :
7183 """Initialize character LCD connected to backpack using I2C connection
7284 on the specified I2C bus with the specified number of columns and
7385 lines on the display. Optionally specify if backlight is inverted.
7486 """
7587
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- )
88+ if expander == I2C_Expander .MCP23008 :
89+ if address :
90+ self .expander = MCP23008 (i2c , address = address )
91+ else :
92+ self .expander = MCP23008 (i2c )
93+
94+ super ().__init__ (
95+ self .expander .get_pin (1 ), # reset
96+ self .expander .get_pin (2 ), # enable
97+ self .expander .get_pin (3 ), # data line 4
98+ self .expander .get_pin (4 ), # data line 5
99+ self .expander .get_pin (5 ), # data line 6
100+ self .expander .get_pin (6 ), # data line 7
101+ columns ,
102+ lines ,
103+ backlight_pin = self .expander .get_pin (7 ),
104+ backlight_inverted = backlight_inverted ,
105+ )
106+
107+ elif expander == I2C_Expander .PCF8574 :
108+ if address :
109+ self .expander = PCF8574 (i2c , address = address )
110+ else :
111+ self .expander = PCF8574 (i2c )
112+
113+ super ().__init__ (
114+ self .expander .get_pin (0 ), # reset
115+ self .expander .get_pin (2 ), # enable
116+ self .expander .get_pin (4 ), # data line 4
117+ self .expander .get_pin (5 ), # data line 5
118+ self .expander .get_pin (6 ), # data line 6
119+ self .expander .get_pin (7 ), # data line 7
120+ columns ,
121+ lines ,
122+ backlight_pin = self .expander .get_pin (3 ),
123+ backlight_inverted = backlight_inverted ,
124+ )
92125
93126 def _write8 (self , value : int , char_mode : bool = False ) -> None :
127+ if not isinstance (self .expander , MCP23008 ):
128+ super ()._write8 (value , char_mode )
129+ return
130+
94131 # Sends 8b ``value`` in ``char_mode``.
95132 # :param value: bytes
96133 # :param char_mode: character/data mode selector. False (default) for
@@ -112,13 +149,13 @@ def _write8(self, value: int, char_mode: bool = False) -> None:
112149 backlight_bit = int (self .backlight ^ self .backlight_inverted ) << 7
113150
114151 # 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 )
152+ self .expander .gpio = reset_bit | backlight_bit | ((value & 0xF0 ) >> 1 )
116153
117154 # do command
118155 self ._pulse_enable ()
119156
120157 # 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 )
158+ self .expander .gpio = reset_bit | backlight_bit | ((value & 0x0F ) << 3 )
122159
123160 # do command
124161 self ._pulse_enable ()
0 commit comments