Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.
public class Hardware
- Object
- Hardware
Known subtypesBoardHardware
Entry point to the buses and pins of the device the application runs on: GPIO lines, I2C, SPI, serial ports, PWM, CAN, the hardware watchdog and the display backlight.
This is the API for boards such as the Raspberry Pi, i.MX, Rockchip and
other embedded Linux devices. Every method is available on every platform;
a port without the hardware reports it through isSupported(int) and its
open methods throw HardwareException, so the same application runs on a
phone, a desktop or the simulator.
Hardware hw = Hardware.getInstance();
try {
if (hw.isSupported(Hardware.FEATURE_GPIO)) {
final GpioPin led = hw.openGpioOutput("gpiochip0", 17, false);
led.setHigh(true);
GpioPin button = hw.openGpioInput("gpiochip0", 27, GpioPin.BIAS_PULL_UP, GpioPin.EDGE_FALLING);
button.setListener(new GpioListener() {
public void edgeDetected(GpioPin pin, boolean rising, long timestampNanos) {
try {
led.setHigh(!led.isHigh());
} catch (HardwareException e) {
Log.e(e);
}
}
});
}
} catch (HardwareException e) {
Log.e(e);
}
Threading
The transfer methods block for the duration of the bus transaction, which is microseconds for GPIO, I2C and SPI. Serial and CAN reads can wait for data; call them off the event dispatch thread, or use their timeout arguments. Listener callbacks always arrive on the event dispatch thread.
For port implementors
A port subclasses this class and overrides the protected primitives, which
take and return integer handles. The device classes (GpioPin,
I2cDevice, …) are written once over those primitives, so a port never
subclasses them. Every primitive’s default throws HardwareException.
Fields
public static final int FEATURE_GPIO = 1 | GPIO lines through the GPIO character device. |
public static final int FEATURE_I2C = 2 | I2C buses. |
public static final int FEATURE_SPI = 4 | SPI buses. |
public static final int FEATURE_SERIAL = 8 | Serial ports (UART, USB serial adapters). |
public static final int FEATURE_PWM = 16 | Hardware PWM channels. |
public static final int FEATURE_CAN = 32 | CAN buses. |
public static final int FEATURE_WATCHDOG = 64 | The hardware watchdog. |
public static final int FEATURE_BACKLIGHT = 128 | Display backlight brightness control. |
protected static final int EDGE_TIMEOUT = -1 | Edge-wait result: the wait timed out. |
protected static final int EDGE_RESULT_FALLING = 0 | Edge-wait result: a falling edge. |
protected static final int EDGE_RESULT_RISING = 1 | Edge-wait result: a rising edge. |
Constructors
protected Hardware() | Ports construct subclasses; applications call getInstance(). |
Methods
Inherited methods
Field details
FEATURE_GPIO
public static final int FEATURE_GPIO = 1FEATURE_I2C
public static final int FEATURE_I2C = 2FEATURE_SPI
public static final int FEATURE_SPI = 4FEATURE_SERIAL
public static final int FEATURE_SERIAL = 8FEATURE_PWM
public static final int FEATURE_PWM = 16FEATURE_CAN
public static final int FEATURE_CAN = 32FEATURE_WATCHDOG
public static final int FEATURE_WATCHDOG = 64FEATURE_BACKLIGHT
public static final int FEATURE_BACKLIGHT = 128EDGE_TIMEOUT
protected static final int EDGE_TIMEOUT = -1EDGE_RESULT_FALLING
protected static final int EDGE_RESULT_FALLING = 0EDGE_RESULT_RISING
protected static final int EDGE_RESULT_RISING = 1Constructor details
Hardware
protected Hardware()getInstance().Method details
getInstance
public static Hardware getInstance()null check.install
public static void install(Hardware hardware)hardware the instance getInstance() returns; null restores
the unsupported default. Display installs the port’s instance when it
starts, and the backend runtime installs its own, which is why this
class never names Display itself.deliver
protected void deliver(Runnable callback)isSupported
public boolean isSupported()true when the port supports at least one hardware feature.isSupported
public boolean isSupported(int feature)true when the port supports the given feature, one of the FEATURE_
constants. Support means the port can drive the bus; whether the board
exposes one is answered by the matching list method.getSupportedFeatures
protected int getSupportedFeatures()FEATURE_ constants this port implements. Ports
override this; the default supports nothing.listGpioChips
public String[] listGpioChips()gpiochip0.
Empty when there are none or GPIO is unsupported.listI2cBuses
public int[] listI2cBuses()/dev/i2c-N on Linux).listSpiDevices
public String[] listSpiDevices()bus.chipSelect strings, for example 0.1.listSerialPorts
public String[] listSerialPorts()/dev/ttyAMA0 or /dev/ttyUSB0.listCanInterfaces
public String[] listCanInterfaces()can0.openGpioOutput
public GpioPin openGpioOutput(String chip, int line, boolean initialHigh)
throws HardwareExceptionParameters
chipString- the controller name from
listGpioChips() lineint- the line offset within the controller (the BCM number on a
Raspberry Pi’s
gpiochip0) initialHighboolean- the level the line is driven to when claimed
Throws
HardwareException- when the line does not exist, is claimed by another consumer, or GPIO is unsupported
openGpioInput
public GpioPin openGpioInput(String chip, int line, int bias, int edges)
throws HardwareExceptionParameters
chipString- the controller name from
listGpioChips() lineint- the line offset within the controller
biasint- one of the
GpioPin.BIAS_constants edgesint- which edges
GpioPin.setListener(GpioListener)reports, one of theGpioPin.EDGE_constants
Throws
HardwareException- when the line cannot be claimed
openI2c
public I2cDevice openI2c(int bus, int address)
throws HardwareExceptionThrows
HardwareException- when the bus does not exist or I2C is unsupported
openSpi
public SpiDevice openSpi(int bus, int chipSelect, int mode, int speedHz, int bitsPerWord)
throws HardwareExceptionParameters
busint- the SPI controller number
chipSelectint- the chip-select line
modeint- SPI mode 0 to 3 (clock polarity and phase)
speedHzint- the clock rate
bitsPerWordint- the word size, usually 8
Throws
HardwareException- when the device does not exist or rejects the configuration
openSerial
public SerialPort openSerial(String device, int baud, int dataBits, int parity, int stopBits)
throws HardwareExceptionParameters
deviceString- the port, from
listSerialPorts() baudint- the baud rate, for example 9600 or 115200
dataBitsint- 5 to 8
parityint- one of the
SerialPort.PARITY_constants stopBitsint- 1 or 2
Throws
HardwareException- when the port cannot be opened or the rate is not supported
openPwm
public PwmChannel openPwm(int chip, int channel)
throws HardwareExceptionThrows
HardwareException- when the channel does not exist
openCan
public CanBus openCan(String iface)
throws HardwareExceptionip link set can0 up type can bitrate 500000), which needs privileges an application normally lacks.Throws
HardwareException- when the interface does not exist
openWatchdog
public Watchdog openWatchdog(int timeoutSeconds)
throws HardwareExceptionWatchdog.kick() is called within the timeout.Throws
HardwareException- when there is no watchdog or it is in use
getBacklightBrightness
public int getBacklightBrightness()getMaxBacklightBrightness
public int getMaxBacklightBrightness()-1 when not supported.setBacklightBrightness
public void setBacklightBrightness(int value)
throws HardwareExceptionThrows
HardwareException- when not supported or not permitted
unsupported
protected static HardwareException unsupported(String what)gpioOpen
protected int gpioOpen(String chip, int line, boolean output, boolean initialHigh, int bias, int edges)
throws HardwareExceptionThrows
gpioRead
protected boolean gpioRead(int handle)
throws HardwareExceptiontrue for high.Throws
gpioWrite
protected void gpioWrite(int handle, boolean high)
throws HardwareExceptionThrows
gpioWaitEdge
protected int gpioWaitEdge(int handle, int timeoutMs, long[] timestampOut)
throws HardwareExceptiontimeoutMs for an edge on an input line. Returns one of
EDGE_TIMEOUT, EDGE_RESULT_FALLING or EDGE_RESULT_RISING and
stores the edge’s timestamp in nanoseconds in timestampOut[0].
Called from a background thread, never the event dispatch thread.Throws
i2cOpen
protected int i2cOpen(int bus, int address)
throws HardwareExceptionThrows
i2cTransfer
protected void i2cTransfer(int handle, byte[] write, int writeLen, byte[] read, int readLen)
throws HardwareExceptionwriteLen bytes from
write, then, with a repeated start and no stop between, reads
readLen bytes into read. Either length may be zero.Throws
spiOpen
protected int spiOpen(int bus, int chipSelect, int mode, int speedHz, int bitsPerWord)
throws HardwareExceptionThrows
spiTransfer
protected void spiTransfer(int handle, byte[] tx, byte[] rx, int len)
throws HardwareExceptionlen bytes. rx may be the
same array as tx.Throws
serialOpen
protected int serialOpen(String device, int baud, int dataBits, int parity, int stopBits)
throws HardwareExceptionThrows
serialRead
protected int serialRead(int handle, byte[] buf, int off, int len, int timeoutMs)
throws HardwareExceptionlen bytes, waiting at most timeoutMs (negative waits
forever) for the first one. Returns the count read, 0 on timeout.Throws
serialWrite
protected void serialWrite(int handle, byte[] buf, int off, int len)
throws HardwareExceptionlen bytes.Throws
pwmOpen
protected int pwmOpen(int chip, int channel)
throws HardwareExceptionThrows
pwmConfigure
protected void pwmConfigure(int handle, long periodNanos, long dutyNanos, boolean enabled)
throws HardwareExceptionThrows
canOpen
protected int canOpen(String iface)
throws HardwareExceptionThrows
canSend
protected void canSend(int handle, int id, boolean extended, byte[] data, int len)
throws HardwareExceptionThrows
canReceive
protected int canReceive(int handle, int[] idOut, byte[] data, int timeoutMs)
throws HardwareExceptiontimeoutMs for a CAN frame. Returns the payload length,
or -1 on timeout; the identifier goes into idOut[0] and idOut[1]
is 1 for an extended identifier.Throws
watchdogOpen
protected int watchdogOpen(int timeoutSeconds)
throws HardwareExceptionThrows
watchdogKick
protected void watchdogKick(int handle)
throws HardwareExceptionThrows
close
protected void close(int handle, boolean disarm)disarm applies to a watchdog only: true stops it
where the driver allows, false leaves it running so the device resets.