From 6e1661e1ff696ee20a037a644351494b14ab1288 Mon Sep 17 00:00:00 2001 From: Frank Delporte Date: Mon, 10 Jun 2024 09:03:32 +0200 Subject: [PATCH] Example button handler code --- .../sample/app/controller/Pi4JController.java | 6 +++- .../boot/sample/app/service/Pi4JService.java | 34 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 pi4j-spring-boot-starter-sample-app/src/main/java/com/pi4j/spring/boot/sample/app/service/Pi4JService.java diff --git a/pi4j-spring-boot-starter-sample-app/src/main/java/com/pi4j/spring/boot/sample/app/controller/Pi4JController.java b/pi4j-spring-boot-starter-sample-app/src/main/java/com/pi4j/spring/boot/sample/app/controller/Pi4JController.java index 177ceab..122707b 100644 --- a/pi4j-spring-boot-starter-sample-app/src/main/java/com/pi4j/spring/boot/sample/app/controller/Pi4JController.java +++ b/pi4j-spring-boot-starter-sample-app/src/main/java/com/pi4j/spring/boot/sample/app/controller/Pi4JController.java @@ -3,6 +3,8 @@ import com.pi4j.context.Context; import com.pi4j.io.gpio.digital.DigitalOutput; import com.pi4j.io.gpio.digital.DigitalState; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -13,12 +15,14 @@ @RequestMapping("/api/pi4j/") public class Pi4JController { + private static final Logger logger = LoggerFactory.getLogger(Pi4JController.class); private static final int PIN_LED = 22; // PIN 15 = BCM 22 - private DigitalOutput led; + private final DigitalOutput led; public Pi4JController(@Autowired Context pi4j) { // LED example is based on https://www.pi4j.com/getting-started/minimal-example-application/ led = pi4j.digitalOutput().create(PIN_LED); + logger.info("LED initialized on pin {}", PIN_LED); } @GetMapping("/led/{status}") diff --git a/pi4j-spring-boot-starter-sample-app/src/main/java/com/pi4j/spring/boot/sample/app/service/Pi4JService.java b/pi4j-spring-boot-starter-sample-app/src/main/java/com/pi4j/spring/boot/sample/app/service/Pi4JService.java new file mode 100644 index 0000000..e7055a9 --- /dev/null +++ b/pi4j-spring-boot-starter-sample-app/src/main/java/com/pi4j/spring/boot/sample/app/service/Pi4JService.java @@ -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()); + } +}