Skip to content

Commit

Permalink
Simplified Pi4J context thanks to bump to 2.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
FDelporte committed Oct 3, 2024
1 parent 9d3e9a7 commit 68f35a9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 35 deletions.
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ This project provides a Spring Boot Starter to use the Pi4J V2 library in your S
* After the application has launched, open a browser and check:
* http://localhost:8080/actuator/health
* http://localhost:8080/actuator/info
* Change the connected components:
* LED on: http://localhost:8080/api/pi4j/led/true
* LED off: http://localhost:8080/api/pi4j/led/false
* Get the button state: http://localhost:8080/api/pi4j/button
* Set text on the LCD: http://localhost:8080/api/pi4j/lcd/0/test

## Running the sample application on a Raspberry Pi

Expand Down Expand Up @@ -67,6 +72,26 @@ $ sudo ~/.sdkman/candidates/java/19.0.2-zulu/bin/java -jar pi4j-spring-boot-star

```shell

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.2.5)
[pi4j-spring-boot-sample-app] [ main] c.p.spring.boot.sample.app.Application : Starting Application using Java 22.0.2 with PID 62220 (/Users/frank/Documents/GitHub/pi4j-springboot/pi4j-spring-boot-starter-sample-app/target/classes started by frank in /Users/frank/Documents/GitHub/pi4j-springboot)
[pi4j-spring-boot-sample-app] [ main] c.p.spring.boot.sample.app.Application : No active profile set, falling back to 1 default profile: "default"
[pi4j-spring-boot-sample-app] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)
[pi4j-spring-boot-sample-app] [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
[pi4j-spring-boot-sample-app] [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.20]
[pi4j-spring-boot-sample-app] [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
[pi4j-spring-boot-sample-app] [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 375 ms
[pi4j-spring-boot-sample-app] [ main] com.pi4j.Pi4J : New auto context
[pi4j-spring-boot-sample-app] [ main] com.pi4j.Pi4J : New context builder
[pi4j-spring-boot-sample-app] [ main] c.pi4j.boardinfo.util.BoardInfoHelper : Detected OS: Name: Mac OS X, version: 15.0, architecture: aarch64
[pi4j-spring-boot-sample-app] [ main] c.pi4j.boardinfo.util.BoardInfoHelper : Detected Java: Version: 22.0.2, runtime: 22.0.2+9, vendor: Azul Systems, Inc., vendor version: Zulu22.32+15-CA
...
```
* Open browser with http://{RASPBERRY_PI_IP_ADDRESS}:8080/actuator/info
Expand Down Expand Up @@ -133,6 +158,5 @@ $ sudo ~/.sdkman/candidates/java/19.0.2-zulu/bin/java -jar pi4j-spring-boot-star
This project was started by Daniel Frey and got further improved by DaShaun Carter during a few live-coding sessions:
1. TODO link to first Twitch
2. [Working on Pi4J Spring Boot](https://www.twitch.tv/videos/1828406758)
3. [Spring Boot Pi4J Starter :: Testing my PR](https://www.twitch.tv/videos/1829189803)
1. [Working on Pi4J Spring Boot](https://www.twitch.tv/videos/1828406758)
1. [Spring Boot Pi4J Starter :: Testing my PR](https://www.twitch.tv/videos/1829189803)
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
package com.pi4j.spring.boot;

import com.pi4j.Pi4J;
import com.pi4j.boardinfo.definition.BoardModel;
import com.pi4j.boardinfo.util.BoardInfoHelper;
import com.pi4j.context.Context;
import com.pi4j.plugin.mock.platform.MockPlatform;
import com.pi4j.plugin.mock.provider.gpio.analog.MockAnalogInputProvider;
import com.pi4j.plugin.mock.provider.gpio.analog.MockAnalogOutputProvider;
import com.pi4j.plugin.mock.provider.gpio.digital.MockDigitalInputProvider;
import com.pi4j.plugin.mock.provider.gpio.digital.MockDigitalOutputProvider;
import com.pi4j.plugin.mock.provider.i2c.MockI2CProvider;
import com.pi4j.plugin.mock.provider.pwm.MockPwmProvider;
import com.pi4j.plugin.mock.provider.serial.MockSerialProvider;
import com.pi4j.plugin.mock.provider.spi.MockSpiProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
Expand All @@ -28,26 +17,9 @@ public class ContextConfiguration {

public ContextConfiguration() {
try {
if (BoardInfoHelper.current().getBoardModel() == BoardModel.UNKNOWN) {
logger.warn("The Pi4J library could not detect that this system is a Raspberry Pi board.");
logger.warn("For this reason, Mock implementations will be loaded for all I/O.");
logger.warn("This means, you can test most functionality of the Pi4J library, but it will not try to interact with I/Os.");
this.pi4j = Pi4J.newContextBuilder()
.add(new MockPlatform())
.add(MockAnalogInputProvider.newInstance(),
MockAnalogOutputProvider.newInstance(),
MockSpiProvider.newInstance(),
MockPwmProvider.newInstance(),
MockSerialProvider.newInstance(),
MockI2CProvider.newInstance(),
MockDigitalInputProvider.newInstance(),
MockDigitalOutputProvider.newInstance())
.build();
} else {
this.pi4j = Pi4J.newAutoContext();
}
} catch (Error e) {
logger.error("Pi4J library failed to load: {}", e.getMessage());
this.pi4j = Pi4J.newAutoContext();
} catch (Exception e) {
logger.error("Pi4J library failed to initialize: {}", e.getMessage());
}
}

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<properties>
<java.version>17</java.version>
<spring-boot.version>3.2.5</spring-boot.version>
<pi4j.version>2.6.1</pi4j.version>
<pi4j.version>2.7.0</pi4j.version>

<!-- Plugins -->
<jacoco-maven-plugin.version>0.8.10</jacoco-maven-plugin.version>
Expand Down

0 comments on commit 68f35a9

Please sign in to comment.