3535except ImportError :
3636 pass
3737
38+ from micropython import const
3839from adafruit_mcp230xx .mcp23008 import MCP23008
40+ from adafruit_pcf8574 import PCF8574
3941from 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+
4557class 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 ()
0 commit comments