Skip to content

Commit e2b618f

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 e2b618f

File tree

1 file changed

+50
-28
lines changed

1 file changed

+50
-28
lines changed

adafruit_character_lcd/character_lcd_i2c.py

+50-28
Original file line numberDiff line numberDiff line change
@@ -124,38 +124,60 @@ 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
161-
self._pulse_enable()
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.expander.gpio = reset_bit | backlight_bit | ((value & 0xF0) >> 1)
150+
151+
# do command
152+
self._pulse_enable()
153+
154+
# Write char_mode and lower 4 bits of data, shifted to the correct position.
155+
self.expander.gpio = reset_bit | backlight_bit | ((value & 0x0F) << 3)
156+
157+
# do command
158+
self._pulse_enable()
159+
160+
elif isinstance(self.expander, PCF8574):
161+
# bits are, MSB (7) to LSB (0)
162+
# data line 7: bit 7
163+
# data line 6: bit 6
164+
# data line 5: bit 5
165+
# data line 4: bit 4
166+
# backlight: bit 3
167+
# enable: bit 2
168+
# write/read: bit 1
169+
# reset: bit 0
170+
reset_bit = int(char_mode)
171+
backlight_bit = int(self.backlight ^ self.backlight_inverted) << 3
172+
173+
# Write char_mode and upper 4 bits of data, shifted to the correct position.
174+
self.expander.write_gpio(reset_bit | backlight_bit | (value & 0xF0))
175+
176+
# do command
177+
self._pulse_enable()
178+
179+
# Write char_mode and lower 4 bits of data, shifted to the correct position.
180+
self.expander.write_gpio(reset_bit | backlight_bit | (value << 4))
181+
182+
# do command
183+
self._pulse_enable()

0 commit comments

Comments
 (0)