Pi4J / pi4j-v1

DEPRECATED Java I/O library for Raspberry Pi (GPIO, I2C, SPI, UART)
http://www.pi4j.com
Apache License 2.0
1.31k stars 448 forks source link

How to use GpioController #506

Closed onlinelei closed 3 years ago

onlinelei commented 3 years ago

hello I have a question: GpioController is Singleton Pattern ? can I create two or more GpioController class operating pi I/O at the same time

eitch commented 3 years ago

Hi, no you can't have two GpioControllers at the same time. There is also no need for this. Am i right in thinking you want to access multiple pins at the same time? Then just get the singleton instance of the GpioController and then provision your pins:

GpioFactory.setExecutorServiceFactory(new SingleThreadGpioExecutorServiceFactory());
GpioFactory.setDefaultProvider(new RaspiGpioProvider(RaspiPinNumberingScheme.BROADCOM_PIN_NUMBERING));
GpioController controller = GpioFactory.getInstance();

Pin interruptPin = RaspiBcmPin.getPinByAddress(...);
GpioPinDigitalInput interruptGpioPin = gpioController.provisionDigitalInputPin(interruptPin, PinPullResistance.PULL_DOWN);
interruptGpioPin.addListener((GpioPinListenerDigital) this::handleInterrupt);

GpioPinDigitalOutput outputPin = gpioController.provisionDigitalOutputPin(...);
outputPin.setState(PinState.HIGH);
onlinelei commented 3 years ago

thanks