Class ShiftRegister74HC595Helper

java.lang.Object
com.opensourcewithslu.outputdevices.ShiftRegister74HC595Helper

public class ShiftRegister74HC595Helper extends Object
Helper for controlling a 74HC595 shift register over SPI (Pi4J). Provides simple operations to write a full byte to the shift register, set or clear individual output bits, clear the register, and read the last written state. All SPI communication is performed using the provided Spi instance. The helper maintains an internal state byte reflecting the last value written to the device.
  • Constructor Summary

    Constructors
    Constructor
    Description
    ShiftRegister74HC595Helper(com.pi4j.io.spi.Spi spi)
    Constructs a new ShiftRegister74HC595Helper with the specified SPI interface.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clears the shift register by setting all outputs to LOW (0x00).
    void
    clearBit(int bitIndex)
    Clears (sets to LOW) a specific bit in the shift register.
    byte
    Returns the current state of the shift register.
    void
    setBit(int bitIndex, boolean value)
    Sets or clears a specific bit in the shift register.
    void
    shiftOut(byte value)
    Shifts out a byte value to the 74HC595 shift register via SPI.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ShiftRegister74HC595Helper

      public ShiftRegister74HC595Helper(com.pi4j.io.spi.Spi spi)
      Constructs a new ShiftRegister74HC595Helper with the specified SPI interface. Initializes the helper with a clean state (0x00) and sets up logging.
      Parameters:
      spi - the SPI interface to use for communication with the shift register
      Throws:
      NullPointerException - if spi is null
  • Method Details

    • shiftOut

      public void shiftOut(byte value) throws IOException
      Shifts out a byte value to the 74HC595 shift register via SPI. This method writes the specified byte value to the shift register and updates the internal state tracking. The operation is logged for debugging.
      Parameters:
      value - the byte value to shift out to the register
      Throws:
      IOException - if the SPI write operation fails
    • clear

      public void clear() throws IOException
      Clears the shift register by setting all outputs to LOW (0x00). This is equivalent to calling shiftOut((byte) 0x00).
      Throws:
      IOException - if the SPI write operation fails
    • setBit

      public void setBit(int bitIndex, boolean value) throws IOException
      Sets or clears a specific bit in the shift register. This method modifies the internal state by setting or clearing the specified bit index (0-7), then shifts out the updated state to the register.
      Parameters:
      bitIndex - the index of the bit to modify (0-7, where 0 is LSB)
      value - true to set the bit to HIGH, false to set to LOW
      Throws:
      IllegalArgumentException - if bitIndex is not in the range 0-7
      IOException - if the SPI write operation fails
    • clearBit

      public void clearBit(int bitIndex) throws IOException
      Clears (sets to LOW) a specific bit in the shift register. This is a convenience method equivalent to calling setBit(bitIndex, false).
      Parameters:
      bitIndex - the index of the bit to clear (0-7, where 0 is LSB)
      Throws:
      IllegalArgumentException - if bitIndex is not in the range 0-7
      IOException - if the SPI write operation fails
    • getState

      public byte getState()
      Returns the current state of the shift register. This method returns the internally tracked state without performing any SPI operations. The state reflects the last value written to the shift register.
      Returns:
      the current state as a byte value