From 8b9b36ba54b1c88a3556477f041309af17dd67f9 Mon Sep 17 00:00:00 2001 From: Frank Delporte Date: Mon, 10 Jun 2024 08:48:08 +0200 Subject: [PATCH] Example API code --- .../sample/app/controller/Pi4JController.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 pi4j-spring-boot-starter-sample-app/src/main/java/com/pi4j/spring/boot/sample/app/controller/Pi4JController.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 new file mode 100644 index 0000000..4fea663 --- /dev/null +++ b/pi4j-spring-boot-starter-sample-app/src/main/java/com/pi4j/spring/boot/sample/app/controller/Pi4JController.java @@ -0,0 +1,33 @@ +package com.pi4j.spring.boot.sample.app.controller; + +import com.pi4j.context.Context; +import com.pi4j.io.gpio.digital.DigitalOutput; +import com.pi4j.io.gpio.digital.DigitalState; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api/pi4j/") +public class Pi4JController { + + private static final int PIN_LED = 22; // PIN 15 = BCM 22 + private DigitalOutput led; + + public Pi4JController(@Autowired Context pi4j) { + // LED example is based on https://www.pi4j.com/getting-started/minimal-example-application/ + pi4j.digitalOutput().create(PIN_LED); + } + + @GetMapping("/led/{status}") + public String setLedStatus(@PathVariable Boolean status) { + try { + led.state(status ? DigitalState.HIGH : DigitalState.LOW); + return "LED status set to " + (status ? "ON" : "OFF"); + } catch (Exception e) { + return "Error while changing the LED status: " + e.getMessage(); + } + } +}