Skip to content

Instantly share code, notes, and snippets.

@ppazos
Created May 4, 2018 23:16
Show Gist options
  • Save ppazos/ac307e73f1f4fb27502d272590329f2e to your computer and use it in GitHub Desktop.
Save ppazos/ac307e73f1f4fb27502d272590329f2e to your computer and use it in GitHub Desktop.
import java.util.concurrent.Callable;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.trigger.GpioCallbackTrigger;
import com.pi4j.io.gpio.trigger.GpioPulseStateTrigger;
import com.pi4j.io.gpio.trigger.GpioSetStateTrigger;
import com.pi4j.io.gpio.trigger.GpioSyncStateTrigger;
import java.util.HashMap;
import java.util.Map;
import com.pi4j.io.gpio.Pin;
/**
* This example code demonstrates how to setup simple triggers for GPIO pins on the Raspberry Pi.
*
* @author Robert Savage
*/
public class TriggerGpioExamplePablo {
public static void main(String[] args) throws InterruptedException {
// in-out mapping
Map<Pin, Pin> map = new HashMap<Pin, Pin>();
map.put(RaspiPin.GPIO_02, RaspiPin.GPIO_03);
// TODO: more mappings
System.out.println("<--Pi4J--> GPIO Trigger ... started.");
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
// TODO: loop over the map, create all the pins in the keys, store those pins in a map
// provision gpio pin #02 as an input pin with its internal pull down resistor enabled
final GpioPinDigitalInput in1 = gpio.provisionDigitalInputPin(RaspiPin.GPIO_02,
PinPullResistance.PULL_DOWN);
System.out.println(" ... complete the GPIO #02 circuit and see the triggers take effect.");
/* TODO: create each ping for the values in the map
// setup gpio pins #04, #05, #06 as an output pins and make sure they are all LOW at startup
GpioPinDigitalOutput myLed[] = {
gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04, "LED #1", PinState.LOW),
gpio.provisionDigitalOutputPin(RaspiPin.GPIO_05, "LED #2", PinState.LOW),
gpio.provisionDigitalOutputPin(RaspiPin.GPIO_06, "LED #3", PinState.LOW)
};
*/
GpioPinDigitalOutput out1 = gpio.provisionDigitalOutputPin(map.get(RaspiPin.GPIO_02), "LED #1", PinState.LOW);
// create a gpio callback trigger on gpio pin#4; when #4 changes state, perform a callback
// invocation on the user defined 'Callable' class instance
// only triggers when the sensor is HIGH, if no state is specified the trigger is fired
// on high and low state changes.
in1.addTrigger(new GpioCallbackTrigger(PinState.HIGH, new Callable<Void>() {
@Override
public Void call() throws Exception {
System.out.println(" --> GPIO TRIGGER CALLBACK RECEIVED ");
out1.toggle();
return null;
}
}));
// keep program running until user aborts (CTRL-C)
while (true) {
Thread.sleep(200);
}
// 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(); <--- implement this method call if you wish to terminate the Pi4J GPIO controller
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment