Skip to content

Support 8-bit expanders like the PCF8574 👾 #4

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

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 12 additions & 8 deletions adafruit_pcf8575.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,39 +48,43 @@ class PCF8575:

:param ~busio.I2C i2c_bus: The I2C bus the PCF8575 is connected to.
:param int address: The I2C device address. Default is :const:`0x20`
:param int gpios: The number of GPIO pins (8 or 16). Default is 16.
"""

def __init__(
self, i2c_bus: busio.I2C, address: int = PCF8575_I2CADDR_DEFAULT
self, i2c_bus: busio.I2C, address: int = PCF8575_I2CADDR_DEFAULT,
gpios: int = 16
) -> None:
self.i2c_device = I2CDevice(i2c_bus, address)
self._gpios = gpios
assert self._gpios == 8 or self._gpios == 16
self._writebuf = bytearray([0, 0])
self._readbuf = bytearray([0, 0])

def get_pin(self, pin: int) -> "DigitalInOut":
"""Convenience function to create an instance of the DigitalInOut class
pointing at the specified pin of this PCF8575 device.

:param int pin: pin to use for digital IO, 0 to 15
:param int pin: pin to use for digital IO
"""
assert 0 <= pin <= 15
assert 0 <= pin <= self._gpios
return DigitalInOut(pin, self)

def write_gpio(self, val: int) -> None:
"""Write a full 16-bit value to the GPIO register
"""Write a full value to the GPIO register

:param int val: The value to write to the register
"""

self._writebuf[0] = val & 0xFF
self._writebuf[1] = (val >> 8) & 0xFF
with self.i2c_device as i2c:
i2c.write(self._writebuf)
i2c.write(self._writebuf, end=self._gpios >> 3)

def read_gpio(self) -> int:
"""Read the full 16-bits of data from the GPIO register"""
"""Read the full data from the GPIO register"""
with self.i2c_device as i2c:
i2c.readinto(self._readbuf)
i2c.readinto(self._readbuf, end=self._gpios >> 3)
return self._readbuf[0] | (self._readbuf[1] << 8)

def write_pin(self, pin: int, val: bool) -> None:
Expand Down Expand Up @@ -128,7 +132,7 @@ class DigitalInOut:
"""

def __init__(self, pin_number: int, pcf: PCF8575) -> None:
"""Specify the pin number of the PCF8575 0..15, and instance.
"""Specify the pin number of the PCF8575, and instance.

:param int pin_number: The pin number
:param PCF8575 pfc: The associated PCF8575 instance
Expand Down