Utility Classes
com.diozero.sbc
The com.diozero.sbc package contains classes for interfacing with Single Board Board Computers, most notably the DeviceFactoryHelper class which provides utility methods for accessing the automatically detected device factory for the local SBC on which your application is running.
The Diozero class contains utility methods for graceful shutdown.
The other key class in this package is BoardPinInfo which provides various methods for accessing PinInfo instances, including by physical pin, GPIO character device chip and line offset, as well as GPIO number. The BoardPinInfo instance for your board should always be accessed via the device factory’s getBoardPinInfo() method.
For example, get by GPIO character device chip and line offset:
// GPIO 18 on the Raspberry Pi
int chip = 0;
int line_offset = 18;
PinInfo pin_info = DeviceFactoryHelper.getNativeDeviceFactory().getBoardPinInfo()
.getByChipAndLineOffsetOrThrow(chip, line_offset);
try (LED led = new LED(pin_info, true, false)) {
led.on();
SleepUtil.sleepSeconds(1);
led.off()
}
Get by physical pin:
// GPIO 18 on the Raspberry Pi
String header = "J8";
int physical_pin = 12;
PinInfo pin_info = DeviceFactoryHelper.getNativeDeviceFactory().getBoardPinInfo()
.getByPhyscialPinOrThrow(header, physical_pin);
try (LED led = new LED(pin_info, true, false)) {
led.on();
SleepUtil.sleepSeconds(1);
led.off()
}
com.diozero.util
The com.diozero.util package contains general utility classes, including:
- BitManipulation
- Interacting with individual bits within a byte.
- ColourUtil
- Currently contains one method for generating a colour value using the 5-6-5 colourspace from individual RGB values.
- Crc
- CRC-8 and CRC-16 calculator - supports all common configurations.
- Diozero
- Utility methods for initialisation and graceful shutdown.
- DiozeroScheduler
- Wrapper around the Java ExecutorService and ScheduledExecutorService; provided to enable graceful shutdown when using non-daemon threads.
- Hex
- Utility methods for encoding and decoding hex strings.
- IOUtil
- Input / output utility methods.
- MmapIntBuffer
- Wrap an system file into a memory mapped java.nio.IntBuffer object for high performance direct memory mapped access. Used for interacting with GPIO registers in
/dev/gpiomem
. - MutableByte
- A mutable byte object with operations for getting / setting individual bit values.
- PropertyUtil
- Utility class for accessing system properties that are set either as environment variables or as
-D
command line flags. Options set via command line flags take precedence over environment variables. - RangeUtil
- Map and constrain values between different ranges. Useful when converting percentage values in the range 0..1 to the 16-bit digital equivalent for ADCs and DACs.
- ServoUtil
- Utility methods for calculating servo pulse values.
- SleepUtil
- Wrap the Java Thread.sleep method with explicit options for sleeping for seconds or milliseconds. Also contains a busySleep method for finer grained delays.
- StringUtil
- Helper methods for padding a string and repeating a character.
- UsbInfo
- Lookup USB vendor and product names from corresponding id values using
/var/lib/usbutils/usb.ids
.