Skip to content

Commit

Permalink
Example API code
Browse files Browse the repository at this point in the history
  • Loading branch information
FDelporte committed Jun 10, 2024
1 parent 23d7927 commit 8b9b36b
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
}

0 comments on commit 8b9b36b

Please sign in to comment.