Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up I2C by not setting the MCP23008 bits one at a time. #80

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/pycqa/pylint
rev: v2.17.4
rev: v3.3.1
hooks:
- id: pylint
name: pylint (library code)
Expand Down
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ valid-metaclass-classmethod-first-arg=mcs
[DESIGN]

# Maximum number of arguments for function / method
max-args=5
max-args=13

# Maximum number of attributes for a class (see R0902).
# max-attributes=7
Expand Down
28 changes: 13 additions & 15 deletions adafruit_character_lcd/character_lcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def _map(
return ret


# pylint: disable-msg=too-many-instance-attributes
# pylint: disable=too-many-instance-attributes
class Character_LCD:
"""Base class for character LCD.

Expand All @@ -121,7 +121,7 @@ class Character_LCD:
LEFT_TO_RIGHT = const(0)
RIGHT_TO_LEFT = const(1)

# pylint: disable-msg=too-many-arguments
# pylint: disable=too-many-positional-arguments
def __init__(
self,
reset_dio: digitalio.DigitalInOut,
Expand Down Expand Up @@ -163,9 +163,10 @@ def __init__(
# Set entry mode
self._write8(_LCD_ENTRYMODESET | self.displaymode)
self.clear()
self._message = None
self._enable = None
self._direction = None

self._message = ""
self._backlight_on = False
self._direction = self.LEFT_TO_RIGHT
# track row and column used in cursor_position
# initialize to 0,0
self.row = 0
Expand Down Expand Up @@ -535,7 +536,7 @@ def _pulse_enable(self) -> None:
# pylint: enable-msg=too-many-instance-attributes


# pylint: disable-msg=too-many-instance-attributes
# pylint: disable=too-many-instance-attributes
class Character_LCD_Mono(Character_LCD):
"""Interfaces with monochromatic character LCDs.

Expand All @@ -554,7 +555,7 @@ class Character_LCD_Mono(Character_LCD):

"""

# pylint: disable-msg=too-many-arguments
# pylint: disable=too-many-positional-arguments
def __init__(
self,
reset_dio: digitalio.DigitalInOut,
Expand Down Expand Up @@ -607,15 +608,12 @@ def backlight(self) -> Optional[bool]:
time.sleep(5)

"""
return self._enable
return self._backlight_on

@backlight.setter
def backlight(self, enable: bool) -> None:
self._enable = enable
if enable:
self.backlight_pin.value = not self.backlight_inverted
else:
self.backlight_pin.value = self.backlight_inverted
def backlight(self, enable):
self._backlight_on = enable
self.backlight_pin.value = enable ^ self.backlight_inverted


class Character_LCD_RGB(Character_LCD):
Expand All @@ -637,7 +635,7 @@ class Character_LCD_RGB(Character_LCD):

"""

# pylint: disable-msg=too-many-arguments
# pylint: disable=too-many-positional-arguments
def __init__(
self,
reset_dio: digitalio.DigitalInOut,
Expand Down
56 changes: 46 additions & 10 deletions adafruit_character_lcd/character_lcd_i2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
https://github.com/adafruit/Adafruit_CircuitPython_BusDevice

"""
import time

try:
from typing import Optional
import busio
Expand All @@ -41,7 +43,7 @@


class Character_LCD_I2C(Character_LCD_Mono):
# pylint: disable=too-few-public-methods, too-many-arguments
# pylint: disable=too-few-public-methods
"""Character LCD connected to I2C/SPI backpack using its I2C connection.
This is a subclass of `Character_LCD_Mono` and implements all the same
functions and functionality.
Expand All @@ -57,6 +59,7 @@ class Character_LCD_I2C(Character_LCD_Mono):
lcd = Character_LCD_I2C(i2c, 16, 2)
"""

# pylint: disable=too-many-positional-arguments
def __init__(
self,
i2c: busio.I2C,
Expand All @@ -71,18 +74,51 @@ def __init__(
"""

if address:
mcp = MCP23008(i2c, address=address)
self.mcp = MCP23008(i2c, address=address)
else:
mcp = MCP23008(i2c)
self.mcp = MCP23008(i2c)
super().__init__(
mcp.get_pin(1),
mcp.get_pin(2),
mcp.get_pin(3),
mcp.get_pin(4),
mcp.get_pin(5),
mcp.get_pin(6),
self.mcp.get_pin(1), # reset
self.mcp.get_pin(2), # enable
self.mcp.get_pin(3), # data line 4
self.mcp.get_pin(4), # data line 5
self.mcp.get_pin(5), # data line 6
self.mcp.get_pin(6), # data line 7
columns,
lines,
backlight_pin=mcp.get_pin(7),
backlight_pin=self.mcp.get_pin(7),
backlight_inverted=backlight_inverted,
)

def _write8(self, value: int, char_mode: bool = False) -> None:
# Sends 8b ``value`` in ``char_mode``.
# :param value: bytes
# :param char_mode: character/data mode selector. False (default) for
# data only, True for character bits.
# one ms delay to prevent writing too quickly.
time.sleep(0.001)

# bits are, MSB (7) to LSB (0)
# backlight: bit 7
# data line 7: bit 6
# data line 6: bit 5
# data line 5: bit 4
# data line 4: bit 3
# enable: bit 2
# reset: bit 1
# (unused): bit 0

reset_bit = int(char_mode) << 1
backlight_bit = int(self.backlight ^ self.backlight_inverted) << 7

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

# do command
self._pulse_enable()

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

# do command
self._pulse_enable()
3 changes: 2 additions & 1 deletion adafruit_character_lcd/character_lcd_spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ class Character_LCD_SPI(Character_LCD_Mono): # pylint: disable=too-few-public-m
lcd = character_lcd.Character_LCD_SPI(spi, latch, 16, 2)
"""

def __init__( # pylint: disable=too-many-arguments
# pylint: disable=too-many-positional-arguments
def __init__(
self,
spi: busio.SPI,
latch: digitalio.DigitalInOut,
Expand Down
Loading