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 index fa0ff24..4853575 100644 --- 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 @@ -14,9 +14,9 @@ public class Pi4JService { private static final Logger logger = LoggerFactory.getLogger(Pi4JService.class); private static final int PIN_LED = 22; // PIN 15 = BCM 22 private static final int PIN_BUTTON = 24; // PIN 18 = BCM 24 - private final DigitalOutput led; - private final DigitalInput button; - private final LcdDisplay lcd; + private DigitalOutput led = null; + private DigitalInput button = null; + private LcdDisplay lcd = null; public Pi4JService(@Autowired Context pi4j) { try { @@ -56,6 +56,11 @@ private void handleButtonChange(DigitalStateChangeEvent e) { } public boolean setLedState(Boolean state) { + if (led == null) { + logger.error("LED is not initialized"); + return false; + } + try { led.state(state ? DigitalState.HIGH : DigitalState.LOW); logger.info("LED state is set to {}", state); @@ -66,7 +71,21 @@ public boolean setLedState(Boolean state) { return false; } + public DigitalState getButtonState() { + if (button == null) { + logger.error("Button is not initialized"); + return DigitalState.LOW; + } + + return button.state(); + } + public boolean setLcdText(Integer line, String text) { + if (lcd == null) { + logger.error("LCD is not initialized"); + return false; + } + try { lcd.displayLineOfText(text, line); logger.info("LCD text on line {} is set to {}", line, text); @@ -76,8 +95,4 @@ public boolean setLcdText(Integer line, String text) { } return false; } - - public DigitalState getButtonState() { - return button.state(); - } }