本文整理汇总了Java中com.pi4j.wiringpi.GpioUtil类的典型用法代码示例。如果您正苦于以下问题:Java GpioUtil类的具体用法?Java GpioUtil怎么用?Java GpioUtil使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
GpioUtil类属于com.pi4j.wiringpi包,在下文中一共展示了GpioUtil类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test
import com.pi4j.wiringpi.GpioUtil; //导入依赖的package包/类
public void test(int gpio) {
int status = Gpio.wiringPiSetupGpio();
if (status != 0) {
throw new RuntimeException("Error initialising wiringPi: " + status);
}
Gpio.pinMode(gpio, Gpio.INPUT);
Gpio.pullUpDnControl(gpio, Gpio.PUD_UP);
int delay = 20;
System.out.println("Waiting " + delay + "s for events..., thread name=" + Thread.currentThread().getName());
if (Gpio.wiringPiISR(gpio, Gpio.INT_EDGE_BOTH, this) != 1) {
System.out.println("Error in wiringPiISR");
} else {
System.out.println("Sleeping for " + delay + "s");
SleepUtil.sleepSeconds(delay);
}
GpioUtil.unexport(gpio);
}
示例2: main
import com.pi4j.wiringpi.GpioUtil; //导入依赖的package包/类
public static void main(String[] args) throws PlatformAlreadyAssignedException {
// Default platform is Raspberry -> Explicit assign the target platform
// TODO : Use PI4J_PLATFORM env variable ??
PlatformManager.setPlatform(Platform.ODROID);
// PI4J Init
if (Gpio.wiringPiSetup() == -1) {
log.error(" ==>> GPIO SETUP FAILED");
return;
}
// GPIO 1 init as Output
GpioUtil.export(1, GpioUtil.DIRECTION_OUT);
Gpio.pinMode (1, Gpio.OUTPUT) ;
// Force low state for GPIO 1
Gpio.digitalWrite(1, Gpio.LOW);
// Vertx event timer
Vertx.vertx().setPeriodic(1000, l -> {
// Blink led every seconds
if (Gpio.digitalRead(1) != Gpio.LOW) {
log.info("Switch off ...");
Gpio.digitalWrite(1, Gpio.LOW);
}
else {
log.info("Switch on ...");
Gpio.digitalWrite(1, Gpio.HIGH);
}
});
}
示例3: WiringPiDigitalOutputDevice
import com.pi4j.wiringpi.GpioUtil; //导入依赖的package包/类
WiringPiDigitalOutputDevice(String key, DeviceFactoryInterface deviceFactory, int gpio, boolean initialValue) throws RuntimeIOException {
super(key, deviceFactory);
this.gpio = gpio;
try {
if (GpioUtil.isExported(gpio)) {
GpioUtil.setDirection(gpio, initialValue ? GpioUtil.DIRECTION_HIGH : GpioUtil.DIRECTION_LOW);
} else {
GpioUtil.export(gpio, initialValue ? GpioUtil.DIRECTION_HIGH : GpioUtil.DIRECTION_LOW);
}
Gpio.pinMode(gpio, Gpio.OUTPUT);
} catch (RuntimeException re) {
throw new RuntimeIOException(re);
}
}
示例4: WiringPiPwmOutputDevice
import com.pi4j.wiringpi.GpioUtil; //导入依赖的package包/类
WiringPiPwmOutputDevice(String key, DeviceFactoryInterface deviceFactory, PwmType pwmType,
int range, int gpio, float initialValue) throws RuntimeIOException {
super(key, deviceFactory);
this.pwmType = pwmType;
this.gpio = gpio;
this.value = initialValue;
this.range = range;
switch (pwmType) {
case HARDWARE:
if (GpioUtil.isExported(gpio)) {
GpioUtil.setDirection(gpio, GpioUtil.DIRECTION_OUT);
} else {
GpioUtil.export(gpio, GpioUtil.DIRECTION_OUT);
}
Gpio.pinMode(gpio, Gpio.PWM_OUTPUT);
// Have to call this after setting the pin mode! Yuck
Gpio.pwmSetMode(Gpio.PWM_MODE_MS);
Gpio.pwmWrite(gpio, Math.round(initialValue * range));
break;
case SOFTWARE:
int status = SoftPwm.softPwmCreate(gpio, Math.round(initialValue * range), range);
if (status != 0) {
throw new RuntimeIOException("Error setting up software controlled PWM GPIO on BCM pin " +
gpio + ", status=" + status);
}
break;
}
}
示例5: closeDevice
import com.pi4j.wiringpi.GpioUtil; //导入依赖的package包/类
@Override
protected void closeDevice() throws RuntimeIOException {
Logger.debug("closeDevice()");
switch (pwmType) {
case HARDWARE:
GpioUtil.unexport(gpio);
case SOFTWARE:
SoftPwm.softPwmStop(gpio);
GpioUtil.unexport(gpio);
break;
default:
}
}
示例6: main
import com.pi4j.wiringpi.GpioUtil; //导入依赖的package包/类
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Usage: " + WiringPiRawPerfTest.class.getName() + " <pin-number> [<iterations>]");
System.exit(1);
}
final int pin = Integer.parseInt(args[0]);
final int iterations = args.length > 1 ? Integer.parseInt(args[1]) : DEFAULT_ITERATIONS;
Gpio.wiringPiSetupGpio();
if (GpioUtil.isExported(pin)) {
GpioUtil.setDirection(pin, GpioUtil.DIRECTION_OUT);
} else {
GpioUtil.export(pin, GpioUtil.DIRECTION_OUT);
}
Gpio.pinMode(pin, Gpio.OUTPUT);
for (int j=0; j<5; j++) {
long start_nano = System.nanoTime();
for (int i=0; i<iterations; i++) {
Gpio.digitalWrite(pin, true);
Gpio.digitalWrite(pin, false);
}
long duration_ns = (System.nanoTime() - start_nano);
System.out.format("Duration for %d iterations: %.4fs%n",
Integer.valueOf(iterations), Float.valueOf(((float)duration_ns) / 1000 / 1000 / 1000));
}
GpioUtil.unexport(pin);
}
示例7: closeDevice
import com.pi4j.wiringpi.GpioUtil; //导入依赖的package包/类
@Override
protected void closeDevice() {
Logger.debug("closeDevice()");
//GpioFactory.getInstance().unprovisionPin(pwmOutputPin);
switch (pwmType) {
case HARDWARE:
GpioUtil.unexport(gpio);
case SOFTWARE:
SoftPwm.softPwmStop(gpio);
GpioUtil.unexport(gpio);
break;
default:
}
}
示例8: test
import com.pi4j.wiringpi.GpioUtil; //导入依赖的package包/类
public void test(int gpio) {
GpioFactory.setDefaultProvider(new RaspiGpioProvider(RaspiPinNumberingScheme.BROADCOM_PIN_NUMBERING));
GpioController gpio_controller = GpioFactory.getInstance();
Pin pin = RaspiBcmPin.getPinByAddress(gpio);
GpioPinDigitalInput digitalInputPin = gpio_controller.provisionDigitalInputPin(pin,
"Digital Input for BCM GPIO " + gpio, PinPullResistance.PULL_UP);
GpioUtil.setEdgeDetection(pin.getAddress(), PinEdge.BOTH.getValue());
digitalInputPin.addListener(this);
System.out.println("Waiting 20s for events..., thread name=" + Thread.currentThread().getName());
SleepUtil.sleepSeconds(20);
gpio_controller.unprovisionPin(digitalInputPin);
gpio_controller.shutdown();
}
示例9: MCP23017Binding
import com.pi4j.wiringpi.GpioUtil; //导入依赖的package包/类
public MCP23017Binding() {
try {
// ask for non privileged access (run without root)
GpioUtil.enableNonPrivilegedAccess();
} catch (UnsatisfiedLinkError e) {
logger.error("MCP23017 Binding needs to be run a Raspberry Pi - deactivating it here.");
gpio = null;
return;
}
// now create a controller
gpio = GpioFactory.getInstance();
}
示例10: main
import com.pi4j.wiringpi.GpioUtil; //导入依赖的package包/类
public static void main(String args[]) throws InterruptedException {
int pin;
int dataPtr;
int l, s, d;
System.out.println("<--Pi4J--> GPIO test program");
// setup wiringPi
if (Gpio.wiringPiSetup() == -1) {
System.out.println(" ==>> GPIO SETUP FAILED");
return;
}
// set GPIO 4 as the input trigger
GpioUtil.export(7, GpioUtil.DIRECTION_IN);
GpioUtil.setEdgeDetection(7, GpioUtil.EDGE_BOTH);
Gpio.pinMode (7, Gpio.INPUT) ;
Gpio.pullUpDnControl(7, Gpio.PUD_DOWN);
// set all other GPIO as outputs
for (pin = 0; pin < 7; ++pin) {
// export all the GPIO pins that we will be using
GpioUtil.export(pin, GpioUtil.DIRECTION_OUT);
Gpio.pinMode(pin, Gpio.OUTPUT);
}
dataPtr = 0;
while (true) {
l = data[dataPtr++]; // LED
s = data[dataPtr++]; // State
d = data[dataPtr++]; // Duration (10ths)
if ((l + s + d) == 27) {
dataPtr = 0;
continue;
}
Gpio.digitalWrite(l, s);
if (Gpio.digitalRead(7) == 1) // Pressed as our switch shorts to ground
Gpio.delay(d * 10); // Faster!
else
Gpio.delay(d * 100);
}
}
示例11: main
import com.pi4j.wiringpi.GpioUtil; //导入依赖的package包/类
public static void main(String[] args) throws InterruptedException {
System.out.println("<--Pi4J--> Non-Privileged GPIO Example ... started.");
// we can use this utility method to pre-check to determine if
// privileged access is required on the running system
if(GpioUtil.isPrivilegedAccessRequired()){
System.err.println("*****************************************************************");
System.err.println("Privileged access is required on this system to access GPIO pins!");
System.err.println("*****************************************************************");
return;
}
// ----------------------
// ATTENTION
// ----------------------
// YOU CANNOT USE ANY HARDWARE PWM OR CLOCK FUNCTIONS WHILE ACCESSING NON-PRIVILEGED GPIO.
// THIS METHOD MUST BE INVOKED BEFORE CREATING A GPIO CONTROLLER INSTANCE.
GpioUtil.enableNonPrivilegedAccess();
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
// ------------
// OUTPUT PIN
// ------------
// provision gpio pin #01 as an output pin and blink it
final GpioPinDigitalOutput output = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01);
// set shutdown state for the output pin (and un-export the pin)
output.setShutdownOptions(true, PinState.LOW);
// blink output pin every one second
output.blink(1000);
// display info to user
System.out.println("Pin [" + output.getName() + "] should be blinking/toggling every 1 second.");
// ------------
// INPUT PIN
// ------------
// provision gpio pin #02 as an input pin with its internal pull down resistor enabled
final GpioPinDigitalInput input = gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, PinPullResistance.PULL_DOWN);
// set shutdown state for the input pin (and un-export the pin)
input.setShutdownOptions(true);
// create and register gpio pin listener
input.addListener(new GpioPinListenerDigital() {
@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
// display pin state on console
System.out.println(" --> GPIO PIN STATE CHANGE: " + event.getPin() + " = " + event.getState());
}
});
// display info to user
System.out.println("You can connect pin [" + input.getName() + "] to +3VDC to capture input state changes.");
// ----------------
// WAIT & SHUTDOWN
// ----------------
// sleep for 1 minute, then shutdown
Thread.sleep(60000);
// stop all GPIO activity/threads by shutting down the GPIO controller
// (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
gpio.shutdown();
System.out.println("Exiting NonPrivilegedGpioExample");
}
示例12: main
import com.pi4j.wiringpi.GpioUtil; //导入依赖的package包/类
public static void main(String args[]) throws InterruptedException {
System.out.println("<--Pi4J--> GPIO INTERRUPT test program");
// create and add GPIO listener
GpioInterrupt.addListener(new GpioInterruptListener() {
@Override
public void pinStateChange(GpioInterruptEvent event) {
System.out.println("Raspberry Pi PIN [" + event.getPin() +"] is in STATE [" + event.getState() + "]");
if(event.getPin() == 7) {
Gpio.digitalWrite(6, event.getStateValue());
}
if(event.getPin() == 0) {
Gpio.digitalWrite(5, event.getStateValue());
}
}
});
// setup wiring pi
if (Gpio.wiringPiSetup() == -1) {
System.out.println(" ==>> GPIO SETUP FAILED");
return;
}
// export all the GPIO pins that we will be using
GpioUtil.export(0, GpioUtil.DIRECTION_IN);
GpioUtil.export(7, GpioUtil.DIRECTION_IN);
GpioUtil.export(5, GpioUtil.DIRECTION_OUT);
GpioUtil.export(6, GpioUtil.DIRECTION_OUT);
// set the edge state on the pins we will be listening for
GpioUtil.setEdgeDetection(0, GpioUtil.EDGE_BOTH);
GpioUtil.setEdgeDetection(7, GpioUtil.EDGE_BOTH);
// configure GPIO pins 5, 6 as an OUTPUT;
Gpio.pinMode(5, Gpio.OUTPUT);
Gpio.pinMode(6, Gpio.OUTPUT);
// configure GPIO 0 as an INPUT pin; enable it for callbacks
Gpio.pinMode(0, Gpio.INPUT);
Gpio.pullUpDnControl(0, Gpio.PUD_DOWN);
GpioInterrupt.enablePinStateChangeCallback(0);
// configure GPIO 7 as an INPUT pin; enable it for callbacks
Gpio.pinMode(7, Gpio.INPUT);
Gpio.pullUpDnControl(7, Gpio.PUD_DOWN);
GpioInterrupt.enablePinStateChangeCallback(7);
// continuously loop to prevent program from exiting
while (true) {
Thread.sleep(5000);
}
}
示例13: closeDevice
import com.pi4j.wiringpi.GpioUtil; //导入依赖的package包/类
@Override
protected void closeDevice() {
Logger.debug("closeDevice()");
GpioUtil.unexport(gpio);
}
示例14: WiringPiDigitalInputDevice
import com.pi4j.wiringpi.GpioUtil; //导入依赖的package包/类
public WiringPiDigitalInputDevice(String key, DeviceFactoryInterface deviceFactory, int gpio,
GpioPullUpDown pud, GpioEventTrigger trigger) throws RuntimeIOException {
super(key, deviceFactory);
this.gpio = gpio;
switch (trigger) {
case RISING:
edge = Gpio.INT_EDGE_RISING;
break;
case FALLING:
edge = Gpio.INT_EDGE_FALLING;
break;
case BOTH:
default:
edge = Gpio.INT_EDGE_BOTH;
break;
}
try {
// Note calling this method will automatically export the pin and set the pin direction to INPUT
if (!GpioUtil.setEdgeDetection(gpio, edge)) {
throw new RuntimeIOException("Error setting edge detection (" + edge + ") for pin " + gpio);
}
} catch (RuntimeException re) {
throw new RuntimeIOException(re);
}
int wpi_pud;
switch (pud) {
case PULL_DOWN:
wpi_pud = Gpio.PUD_DOWN;
break;
case PULL_UP:
wpi_pud = Gpio.PUD_UP;
break;
case NONE:
default:
wpi_pud = Gpio.PUD_OFF;
break;
}
Gpio.pullUpDnControl(gpio, wpi_pud);
}
示例15: closeDevice
import com.pi4j.wiringpi.GpioUtil; //导入依赖的package包/类
@Override
protected void closeDevice() {
Logger.debug("closeDevice()");
removeListener();
GpioUtil.unexport(gpio);
}