Better way to create objects #219
-
Hi! 1, What exactly do the parametres do, like i know .address tells what pin it is,
var ledAconf = DigitalOutput.newConfigBuilder(pi4j) And that really stacks up Edit; And is it possible to create the objects as "universal", meaning they could be used outside the mainclass, and in methods Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
One of the Pi4J V2 goals was to have default providers based on the current "platform". However, this is not currently implemented. This would avoid having to explicitly specify which provider to use and could result in GPIO instance creation as simple as:
We are working on this "default" behaviour now. Another option is to reuse the
You could also create I/O instances via a set of "Properties" like in this examples code: |
Beta Was this translation helpful? Give feedback.
-
Yes. the // three ways to get existing LED output instance
DigitalOutput led = pi4j.io("my-led");
var led = (DigitalOutput)pi4j.io("my-led");
var led = pi4j.io("my-led", DigitalOutput.class); Internally the I/O instances are maintained by the // get the Pi4J I/O registry
Registry registry = pi4j.registry();
// check to see if the LED output already exists (by id)
boolean myLedAlreadyExists = registry.exists("my-led");
// get all digital output instances from the Pi4J I/O registry
var outputs = registry.allByIoType(IOType.DIGITAL_OUTPUT); Fields like // create digital output I/O configuration
var config = DigitalOutput.newConfigBuilder(pi4j)
.id("my-dout")
.name("My Digital Output")
.address(GPIO_PIN)
.shutdown(DigitalState.LOW)
.initial(DigitalState.HIGH)
.provider("linuxfs-digital-output");
// create digital output I/O instance using configuration
var output = pi4j.create(config);
// print digital output object to system out
output.describe().print(System.out);
----
// ... CONSOLE OUTPUT
// > IO: "My Digital Output" {my-dout} <com.pi4j.plugin.linuxfs.provider.gpio.digital.LinuxFsDigitalOutput> {DOUT-26} |
Beta Was this translation helpful? Give feedback.
-
Thanks @Brosbud for the question and @savageautomate for the clear answers. This was exactly the info that was still missing on our website as there was only "TODO" on this page... https://pi4j.com/documentation/build-io/ I used the info from this discussion to fill that page... ;-) |
Beta Was this translation helpful? Give feedback.
@Brosbud
One of the Pi4J V2 goals was to have default providers based on the current "platform". However, this is not currently implemented. This would avoid having to explicitly specify which provider to use and could result in GPIO instance creation as simple as:
We are working on this "default" behaviour now.
Another option is to reuse the
config
object that you create overriding the 'address' (and 'id' if used) for each I/O instance: