Controlling devices via a shift register #77
-
Is there any way to use the built in classes (i.e. /**
* A shift register chip designed for output, i.e. the 74HC595
*/
public class OutputShiftRegister implements DeviceInterface {
private final DigitalOutputDevice data;
private final DigitalOutputDevice dataWrite;
private final DigitalOutputDevice dataOut;
private boolean[] buf;
private boolean flushOnWrite;
public OutputShiftRegister(int width, int ser, int srclk, int rclk, boolean flushOnWrite) {
this.data = new DigitalOutputDevice(ser);
this.dataWrite = new DigitalOutputDevice(srclk);
this.dataOut = new DigitalOutputDevice(rclk);
this.buf = new boolean[width];
this.flushOnWrite = flushOnWrite;
}
public OutputShiftRegister(int width, int ser, int srclk, int rclk) {
this(width, ser, srclk, rclk, true);
}
/**
* Writes the value to the internal buffer. If {@link #flushOnWrite()} is
* {@code true}, the the buffer will then be written to the chip
*
* @throws IndexOutOfBoundsException if the index is bigger than what the chip can hold
*/
public void write(int index, boolean value) {
try {
buf[index] = value;
} catch (ArrayIndexOutOfBoundsException e) {
throw new IndexOutOfBoundsException(index);
}
if (flushOnWrite) {
flush();
}
}
/**
* Writes the value directly to the chip instead of the buffer
*/
public void writeValue(boolean value) {
data.setOn(value);
dataWrite.on();
SleepUtil.sleepNanos(0, 1000);
dataWrite.off();
}
/**
* Writes the contents of the buffer to the chip and outputs it
*/
public void flush() {
for (boolean b : buf) {
writeValue(b);
}
writeRegister();
}
/**
* Clears the chip and the internal buffer
*/
public void clear() {
buf = new boolean[buf.length];
for (int i = 0; i < buf.length; i++) {
writeValue(false);
}
writeRegister();
}
/**
* Writes the contents of the register in the chip to the chip's output
*/
public void writeRegister() {
dataOut.on();
SleepUtil.sleepNanos(0, 1000);
dataOut.off();
}
public boolean flushOnWrite() {
return flushOnWrite;
}
public void setFlushOnWrite(boolean flushOnWrite) {
this.flushOnWrite = flushOnWrite;
}
@Override
public void close() throws RuntimeIOException {
clear();
this.data.close();
this.dataWrite.close();
this.dataOut.close();
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
Apologies, I've not done anything with shift registers before. Can you explain a bit more about how this might be implemented please. I've just read this article https://www.arduino.cc/en/Tutorial/Foundations/ShiftOut and can sort of understand the GPIO digital output multiplexing. The way to handle that would be to create a shift register digital output device factory similar to the MCP23007 class. Let me have a think but I'd be interested in your thoughts. |
Beta Was this translation helpful? Give feedback.
-
As a side note, the |
Beta Was this translation helpful? Give feedback.
Apologies, I've not done anything with shift registers before. Can you explain a bit more about how this might be implemented please.
I've just read this article https://www.arduino.cc/en/Tutorial/Foundations/ShiftOut and can sort of understand the GPIO digital output multiplexing. The way to handle that would be to create a shift register digital output device factory similar to the MCP23007 class. Let me have a think but I'd be interested in your thoughts.