Skip to content

Adafruit_CircuitPython_74HC595: Added support for multiple shift registers #17

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

Merged
merged 4 commits into from
Apr 14, 2021
Merged
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
45 changes: 31 additions & 14 deletions adafruit_74hc595.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def __init__(self, pin_number, shift_register_74hc595):
ShiftRegister74HC595 instance.
"""
self._pin = pin_number
self._byte_pos = self._pin // 8
self._byte_pin = self._pin % 8
self._shift_register = shift_register_74hc595

# kwargs in switch functions below are _necessary_ for compatibility
Expand All @@ -65,16 +67,23 @@ def switch_to_input(self, **kwargs): # pylint: disable=no-self-use
@property
def value(self):
"""The value of the pin, either True for high or False for low."""
return self._shift_register.gpio & (1 << self._pin) == (1 << self._pin)
return self._shift_register.gpio[self._byte_pos] & (1 << self._byte_pin) == (
1 << self._byte_pin
)

@value.setter
def value(self, val):
gpio = self._shift_register.gpio
if val:
gpio |= 1 << self._pin
else:
gpio &= ~(1 << self._pin)
self._shift_register.gpio = gpio

if (
self._pin >= 0
and self._pin < self._shift_register.number_of_shift_registers * 8
):
gpio = self._shift_register.gpio
if val:
gpio[self._byte_pos] |= 1 << self._byte_pin
else:
gpio[self._byte_pos] &= ~(1 << self._byte_pin)
self._shift_register.gpio = gpio

@property
def direction(self):
Expand All @@ -100,23 +109,31 @@ def pull(self, val): # pylint: disable=no-self-use


class ShiftRegister74HC595:
"""Initialise the 74HC595 on specified SPI bus."""
"""Initialise the 74HC595 on specified SPI bus
and indicate the number of shift registers being used
"""

def __init__(self, spi, latch):
def __init__(self, spi, latch, number_of_shift_registers=1):
self._device = spi_device.SPIDevice(spi, latch, baudrate=1000000)
self._gpio = bytearray(1)
self._gpio[0] = 0x00
self._number_of_shift_registers = number_of_shift_registers
self._gpio = bytearray(self._number_of_shift_registers)

@property
def number_of_shift_registers(self):
"""The number of shift register chips """
return self._number_of_shift_registers

@property
def gpio(self):
"""The raw GPIO output register. Each bit represents the
output value of the associated pin (0 = low, 1 = high).
"""
return self._gpio[0]
return self._gpio

@gpio.setter
def gpio(self, val):
self._gpio[0] = val & 0xFF
self._gpio = val

with self._device as spi:
# pylint: disable=no-member
spi.write(self._gpio)
Expand All @@ -125,5 +142,5 @@ def get_pin(self, pin):
"""Convenience function to create an instance of the DigitalInOut class
pointing at the specified pin of this 74HC595 device .
"""
assert 0 <= pin <= 7
assert 0 <= pin <= (self._number_of_shift_registers * 8) - 1
return DigitalInOut(pin, self)