-
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
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
...r-sample-app/src/main/java/com/pi4j/spring/boot/sample/app/controller/Pi4JController.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,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(); | ||
} | ||
} | ||
} |