-
Notifications
You must be signed in to change notification settings - Fork 2
/
Pi4JMinimalExample.java
78 lines (65 loc) · 2.49 KB
/
Pi4JMinimalExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS org.slf4j:slf4j-api:2.0.12
//DEPS org.slf4j:slf4j-simple:2.0.12
//DEPS com.pi4j:pi4j-core:2.7.0
//DEPS com.pi4j:pi4j-plugin-raspberrypi:2.7.0
//DEPS com.pi4j:pi4j-plugin-gpiod:2.7.0
import com.pi4j.Pi4J;
import com.pi4j.io.gpio.digital.DigitalInput;
import com.pi4j.io.gpio.digital.DigitalOutput;
import com.pi4j.io.gpio.digital.DigitalState;
import com.pi4j.io.gpio.digital.PullResistance;
import com.pi4j.util.Console;
/**
* Example code to blink a LED (DigitalOutput) and use a button (DigitalInput).
* Make sure to follow the README of this project to learn more about JBang and how to install it.
*
* This example must be executed with sudo as it uses PiGpio with:
* sudo `which jbang` Pi4JMinimalExample.java
*
* More information and a video explaining this example is available on:
* https://pi4j.com/documentation/building/jbang/
*/
public class Pi4JMinimalExample {
// Wiring see: https://pi4j.com/getting-started/minimal-example-application/
// Connect a button to PIN 18 = BCM 24
private static final int PIN_BUTTON = 24;
// Connect a LED to PIN 15 = BCM 22
private static final int PIN_LED = 22;
private static int pressCount = 0;
public static void main(String[] args) throws Exception {
final var console = new Console();
var pi4j = Pi4J.newAutoContext();
var ledConfig = DigitalOutput.newConfigBuilder(pi4j)
.id("led")
.name("LED Flasher")
.address(PIN_LED)
.shutdown(DigitalState.LOW)
.initial(DigitalState.LOW);
var led = pi4j.create(ledConfig);
var buttonConfig = DigitalInput.newConfigBuilder(pi4j)
.id("button")
.name("Press button")
.address(PIN_BUTTON)
.pull(PullResistance.PULL_DOWN)
.debounce(3000L);
var button = pi4j.create(buttonConfig);
button.addListener(e -> {
if (e.state() == DigitalState.LOW) {
pressCount++;
console.println("Button was pressed for the " + pressCount + "th time");
}
});
while (pressCount < 5) {
if (led.equals(DigitalState.HIGH)) {
console.println("LED low");
led.low();
} else {
console.println("LED high");
led.high();
}
Thread.sleep(500 / (pressCount + 1));
}
pi4j.shutdown();
}
}