Skip to content

Commit 9cecf0c

Browse files
committed
Character_LCD_I2C: speed up PCF8574 by setting all GPIO bits at once
Signed-off-by: Roman Ondráček <[email protected]>
1 parent daa56fb commit 9cecf0c

File tree

1 file changed

+43
-27
lines changed

1 file changed

+43
-27
lines changed

adafruit_character_lcd/character_lcd_i2c.py

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -124,38 +124,54 @@ def __init__(
124124
)
125125

126126
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-
131127
# Sends 8b ``value`` in ``char_mode``.
132128
# :param value: bytes
133129
# :param char_mode: character/data mode selector. False (default) for
134130
# data only, True for character bits.
135131
# one ms delay to prevent writing too quickly.
136132
time.sleep(0.001)
137133

138-
# bits are, MSB (7) to LSB (0)
139-
# backlight: bit 7
140-
# data line 7: bit 6
141-
# data line 6: bit 5
142-
# data line 5: bit 4
143-
# data line 4: bit 3
144-
# enable: bit 2
145-
# reset: bit 1
146-
# (unused): bit 0
147-
148-
reset_bit = int(char_mode) << 1
149-
backlight_bit = int(self.backlight ^ self.backlight_inverted) << 7
150-
151-
# Write char_mode and upper 4 bits of data, shifted to the correct position.
152-
self.expander.gpio = reset_bit | backlight_bit | ((value & 0xF0) >> 1)
153-
154-
# do command
155-
self._pulse_enable()
156-
157-
# Write char_mode and lower 4 bits of data, shifted to the correct position.
158-
self.expander.gpio = reset_bit | backlight_bit | ((value & 0x0F) << 3)
159-
160-
# do command
134+
if isinstance(self.expander, MCP23008):
135+
# bits are, MSB (7) to LSB (0)
136+
# backlight: bit 7
137+
# data line 7: bit 6
138+
# data line 6: bit 5
139+
# data line 5: bit 4
140+
# data line 4: bit 3
141+
# enable: bit 2
142+
# reset: bit 1
143+
# (unused): bit 0
144+
145+
reset_bit = int(char_mode) << 1
146+
backlight_bit = int(self.backlight ^ self.backlight_inverted) << 7
147+
148+
# Write char_mode and upper 4 bits of data, shifted to the correct position.
149+
self.__write_command(reset_bit | backlight_bit | ((value & 0xF0) >> 1))
150+
self.__write_command(reset_bit | backlight_bit | ((value & 0x0F) << 3))
151+
152+
elif isinstance(self.expander, PCF8574):
153+
# bits are, MSB (7) to LSB (0)
154+
# data line 7: bit 7
155+
# data line 6: bit 6
156+
# data line 5: bit 5
157+
# data line 4: bit 4
158+
# backlight: bit 3
159+
# enable: bit 2
160+
# write/read: bit 1
161+
# reset: bit 0
162+
reset_bit = int(char_mode)
163+
backlight_bit = int(self.backlight ^ self.backlight_inverted) << 3
164+
165+
# Write char_mode and upper 4 bits of data, shifted to the correct position.
166+
self.__write_command(reset_bit | backlight_bit | (value & 0xF0))
167+
self.__write_command(reset_bit | backlight_bit | (value << 4))
168+
169+
def __write_command(self, value: int) -> None:
170+
# Write command bits to expander.
171+
if isinstance(self.expander, MCP23008):
172+
self.expander.gpio = value
173+
elif isinstance(self.expander, PCF8574):
174+
self.expander.write_gpio(value)
175+
176+
# execute command
161177
self._pulse_enable()

0 commit comments

Comments
 (0)