public abstract class Device extends Object implements Closeable
The device must be opened and configured
before being used (the init() method does both with default values).
The device must be closed when done, in order to clear the
display and release all resources. The method can
be called to enable a shutdown hook that will close the device automatically
when the JVM exits, which is convenient in most simple use cases.
The LED matrix data is written via the setDisplay(int, int) methods. Data is
expressed as a single byte per column, where each bit represents the state
of a single led on that column. Columns are indexed from left to right, and
the rows within a column are indexed from top (lowest bit) to bottom (highest bit).
The written matrix data is stored by the device in temporary registers, and
becomes visible only when the update() method is invoked to apply the
changes.
The LED brightness can be set as a value in the range 0 (off) to 128 (brightest). The brightness setting applies to the entire matrix and cannot be set for each LED individually.
This class is not thread-safe, and should only be accessed by one thread at a time.
| Modifier and Type | Field and Description |
|---|---|
static int |
ADDR_GND |
static int |
ADDR_SCL |
static int |
ADDR_SDA |
static int |
ADDR_VCC |
static byte |
CONFIG_AUDIO_INPUT |
static byte |
CONFIG_DISPLAY_MATRIX_1 |
static byte |
CONFIG_DISPLAY_MATRIX_1_AND_2 |
static byte |
CONFIG_DISPLAY_MATRIX_2 |
static byte |
CONFIG_MATRIX_5X11 |
static byte |
CONFIG_MATRIX_6X10 |
static byte |
CONFIG_MATRIX_7X9 |
static byte |
CONFIG_MATRIX_8X8 |
static byte |
CONFIG_SOFTWARE_SHUTDOWN |
protected int |
errors |
protected int |
height |
static byte |
REG_BRIGHTNESS_CONFIG |
static byte |
REG_BRIGHTNESS_PWM |
static byte |
REG_CONFIG |
static byte |
REG_MATRIX1_DATA_START |
static byte |
REG_MATRIX2_DATA_START |
static byte |
REG_RESET |
static byte |
REG_UPDATE |
protected Thread |
shutdownHook |
protected int |
throwErrorCount |
protected int |
warnErrorCount |
protected int |
width |
| Constructor and Description |
|---|
Device() |
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes the I2C bus and device.
|
protected abstract void |
closeImpl()
Closes the I2C bus and device.
|
Device |
closeOnShutdown(boolean enable)
Specifies whether to set a system shutdown
hook to close this device when the JVM exits.
|
Device |
configure(int config)
Configures the device.
|
void |
displayTestPatterns()
Displays test patterns.
|
int |
getHeight()
Returns the configured device height.
|
int |
getWidth()
Returns the configured device width.
|
protected void |
handleError(IOException ioe)
Handles errors that occur while writing data to the device.
|
Device |
init()
Initializes the device.
|
static void |
main(String[] args)
The main command-line utility entry point.
|
static Device |
newInstance()
Returns a new Device instance.
|
static Device |
newInstance(String impl)
Returns a new Device instance using the given implementation.
|
Device |
open(int address)
Opens the connection to I2C bus number 1
and to the device at the given address.
|
Device |
open(int busNumber,
int address)
Opens the connection to the I2C bus and
to the device at the given address.
|
protected abstract Device |
openImpl(int busNumber,
int address)
Opens the connection to the I2C bus and
to the device at the given address.
|
void |
reset()
Resets the device (by writing the reset command to it).
|
void |
setBrightness(int value)
Sets the display brightness.
|
void |
setDisplay(byte[] columns)
Sets the values of a consecutive sequence of columns,
starting at the first column.
|
void |
setDisplay(int startColumn,
byte[] columns)
Sets the values of a consecutive sequence of columns.
|
void |
setDisplay(int startColumn,
byte[] columns,
int offset,
int length)
Sets the values of a consecutive sequence of columns.
|
void |
setDisplay(int column,
int value)
Sets the value of the given column.
|
void |
update()
Updates the display.
|
void |
update(byte[] columns)
Writes the given display data and updates the display.
|
void |
write(byte register,
byte[] data,
int offset,
int length)
Writes a series of bytes of data starting at the given register.
|
void |
write(byte register,
int data)
Writes a single byte of data to the given register.
|
protected abstract void |
writeImpl(byte register,
byte[] data,
int offset,
int length)
Writes a series of bytes of data starting at the given register.
|
protected void |
writeImpl(byte register,
int data)
Writes a single byte of data to the given register.
|
public static final int ADDR_GND
public static final int ADDR_SCL
public static final int ADDR_SDA
public static final int ADDR_VCC
public static final byte REG_CONFIG
public static final byte REG_MATRIX1_DATA_START
public static final byte REG_MATRIX2_DATA_START
public static final byte REG_UPDATE
public static final byte REG_BRIGHTNESS_CONFIG
public static final byte REG_BRIGHTNESS_PWM
public static final byte REG_RESET
public static final byte CONFIG_MATRIX_8X8
public static final byte CONFIG_MATRIX_7X9
public static final byte CONFIG_MATRIX_6X10
public static final byte CONFIG_MATRIX_5X11
public static final byte CONFIG_AUDIO_INPUT
public static final byte CONFIG_DISPLAY_MATRIX_1
public static final byte CONFIG_DISPLAY_MATRIX_2
public static final byte CONFIG_DISPLAY_MATRIX_1_AND_2
public static final byte CONFIG_SOFTWARE_SHUTDOWN
protected int errors
protected int warnErrorCount
protected int throwErrorCount
protected int width
protected int height
protected Thread shutdownHook
public static Device newInstance()
The implementation name can be specified in the "scrollphat.impl" system property, and if none exists, a default is used.
IllegalArgumentException - if the implementation name is invalidpublic static Device newInstance(String impl)
The implementation name can be one of "pi4j", "jnadev" or "mock". If null, the implementation name is taken from the "scrollphat.impl" system property, and if none exists, a default is used.
impl - the implementation nameIllegalArgumentException - if the implementation name is invalidpublic int getWidth()
public int getHeight()
protected void handleError(IOException ioe) throws IOException
The error may be rethrown, swallowed, or a warning printed, depending on the error handling configuration and current accumulated error count.
ioe - the error that occurredIOException - if the error is to be propagatedpublic Device open(int address) throws IOException
address - the I2C device addressIOException - if an error occursIllegalArgumentException - if the address is invalidpublic Device open(int busNumber, int address) throws IOException
busNumber - the I2C bus number to useaddress - the I2C device addressIOException - if an error occursIllegalArgumentException - if the address is invalidprotected abstract Device openImpl(int busNumber, int address) throws IOException
busNumber - the I2C bus number to useaddress - the I2C device addressIOException - if an error occursIllegalArgumentException - if the address is invalidpublic void close()
throws IOException
This method may be called multiple times, or
without a successful call to open(int) before it.
close in interface Closeableclose in interface AutoCloseableIOException - if an error occursprotected abstract void closeImpl()
throws IOException
This method may be called multiple times, or
without a successful call to open(int) before it.
IOException - if an error occurspublic void write(byte register,
int data)
throws IOException
register - the register to write todata - the data to write (only the lower 8 bits are written)IOException - if an error occursprotected void writeImpl(byte register,
int data)
throws IOException
register - the register to write todata - the data to write (only the lower 8 bits are written)IOException - if an error occurspublic void write(byte register,
byte[] data,
int offset,
int length)
throws IOException
register - the register to write todata - an array containing the data to writeoffset - the offset within the array of the first byte to writelength - the number of bytes to writeIOException - if an error occursprotected abstract void writeImpl(byte register,
byte[] data,
int offset,
int length)
throws IOException
register - the register to write todata - an array containing the data to writeoffset - the offset within the array of the first byte to writelength - the number of bytes to writeIOException - if an error occurspublic Device closeOnShutdown(boolean enable)
If disabled, the caller is responsible for performing a graceful
shutdown, e.g. by calling close() in a finally block.
enable - specifies whether to enable or disable
close on shutdownpublic Device configure(int config) throws IOException
config - the bitmask of configuration optionsIOException - if an error occurspublic Device init() throws IOException
This is a convenience method that calls closeOnShutdown(boolean),
open(int) and configure(int) with default options.
IOException - if an error occurspublic void reset()
throws IOException
IOException - if an error occurspublic void setBrightness(int value)
throws IOException
value - a brightness value in the range 0-128IOException - if an error occurspublic void update()
throws IOException
IOException - if an error occurspublic void update(byte[] columns)
throws IOException
columns - the consecutive column values to writeIOException - if an error occurspublic void setDisplay(int column,
int value)
throws IOException
column - the column numbervalue - the column valueIOException - if an error occurspublic void setDisplay(int startColumn,
byte[] columns,
int offset,
int length)
throws IOException
startColumn - the column to start writing atcolumns - an array containing the consecutive column values to writeoffset - the offset within the array of the first column's valuelength - the number of consecutive column values to writeIOException - if an error occurspublic void setDisplay(int startColumn,
byte[] columns)
throws IOException
startColumn - the column to start writing atcolumns - the consecutive column values to writeIOException - if an error occurspublic void setDisplay(byte[] columns)
throws IOException
columns - the consecutive column values to writeIOException - if an error occurspublic void displayTestPatterns()
throws IOException,
InterruptedException
IOException - if an error occursInterruptedException - if the thread is interruptedpublic static void main(String[] args) throws IOException, InterruptedException
args - the argumentsIOException - if an error occursInterruptedException - if the thread is interruptedCopyright © 2017 freeutils.net. All rights reserved.