-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
39 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...starter-sample-app/src/main/java/com/pi4j/spring/boot/sample/app/service/Pi4JService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.pi4j.spring.boot.sample.app.service; | ||
|
||
import com.pi4j.context.Context; | ||
import com.pi4j.io.gpio.digital.DigitalInput; | ||
import com.pi4j.io.gpio.digital.DigitalStateChangeEvent; | ||
import com.pi4j.io.gpio.digital.PullResistance; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class Pi4JService { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(Pi4JService.class); | ||
private static final int PIN_BUTTON = 24; // PIN 18 = BCM 24 | ||
|
||
public Pi4JService(@Autowired Context pi4j) { | ||
// Button example is based on https://www.pi4j.com/getting-started/minimal-example-application/ | ||
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 -> handleButtonChange(e)); | ||
logger.info("Button initialized on pin {}", PIN_BUTTON); | ||
} | ||
|
||
private void handleButtonChange(DigitalStateChangeEvent e) { | ||
logger.info("Button state changed to {}", e.state()); | ||
} | ||
} |