Class ShiftRegister74HC595Helper
java.lang.Object
com.opensourcewithslu.outputdevices.ShiftRegister74HC595Helper
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
ConstructorsConstructorDescriptionShiftRegister74HC595Helper(com.pi4j.io.spi.Spi spi) Constructs a new ShiftRegister74HC595Helper with the specified SPI interface. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears the shift register by setting all outputs to LOW (0x00).voidclearBit(int bitIndex) Clears (sets to LOW) a specific bit in the shift register.bytegetState()Returns the current state of the shift register.voidsetBit(int bitIndex, boolean value) Sets or clears a specific bit in the shift register.voidshiftOut(byte value) Shifts out a byte value to the 74HC595 shift register via SPI.
-
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
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
Clears the shift register by setting all outputs to LOW (0x00). This is equivalent to callingshiftOut((byte) 0x00).- Throws:
IOException- if the SPI write operation fails
-
setBit
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-trueto set the bit to HIGH,falseto set to LOW- Throws:
IllegalArgumentException- if bitIndex is not in the range 0-7IOException- if the SPI write operation fails
-
clearBit
Clears (sets to LOW) a specific bit in the shift register. This is a convenience method equivalent to callingsetBit(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-7IOException- 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
-