-
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
5 changed files
with
603 additions
and
20 deletions.
There are no files selected for viewing
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
74 changes: 74 additions & 0 deletions
74
...-boot-starter-sample-app/src/main/java/com/pi4j/spring/boot/sample/app/lcd/Component.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,74 @@ | ||
package com.pi4j.spring.boot.sample.app.lcd; | ||
|
||
import java.time.Duration; | ||
import java.util.logging.ConsoleHandler; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
public abstract class Component { | ||
/** | ||
* Logger instance | ||
*/ | ||
private static final Logger logger = Logger.getLogger("Pi4J Components"); | ||
|
||
static { | ||
Level appropriateLevel = Level.INFO; | ||
//Level appropriateLevel = Level.FINE; //use if 'debug' | ||
|
||
System.setProperty("java.util.logging.SimpleFormatter.format", | ||
"%4$s: %5$s [%1$tl:%1$tM:%1$tS %1$Tp]%n"); | ||
|
||
logger.setLevel(appropriateLevel); | ||
logger.setUseParentHandlers(false); | ||
ConsoleHandler handler = new ConsoleHandler(); | ||
handler.setLevel(appropriateLevel); | ||
logger.addHandler(handler); | ||
} | ||
|
||
protected Component() { | ||
} | ||
|
||
/** | ||
* Override this method to clean up all used resources | ||
*/ | ||
public void reset() { | ||
//nothing to do by default | ||
} | ||
|
||
protected void logInfo(String msg, Object... args) { | ||
logger.info(() -> String.format(msg, args)); | ||
} | ||
|
||
protected void logError(String msg, Object... args) { | ||
logger.severe(() -> String.format(msg, args)); | ||
} | ||
|
||
protected void logDebug(String msg, Object... args) { | ||
logger.fine(() -> String.format(msg, args)); | ||
} | ||
|
||
protected void logException(String msg, Throwable exception) { | ||
logger.log(Level.SEVERE, msg, exception); | ||
} | ||
|
||
/** | ||
* Utility function to sleep for the specified amount of milliseconds. | ||
* An {@link InterruptedException} will be caught and ignored while setting the interrupt flag again. | ||
* | ||
* @param duration Time to sleep | ||
*/ | ||
protected void delay(Duration duration) { | ||
try { | ||
long nanos = duration.toNanos(); | ||
long millis = nanos / 1_000_000; | ||
int remainingNanos = (int) (nanos % 1_000_000); | ||
Thread.sleep(millis, remainingNanos); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
|
||
protected <T> T asMock(Class<T> type, Object instance) { | ||
return type.cast(instance); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...-boot-starter-sample-app/src/main/java/com/pi4j/spring/boot/sample/app/lcd/I2CDevice.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,83 @@ | ||
package com.pi4j.spring.boot.sample.app.lcd; | ||
|
||
import com.pi4j.context.Context; | ||
import com.pi4j.io.i2c.I2C; | ||
import com.pi4j.plugin.mock.provider.i2c.MockI2C; | ||
|
||
import java.time.Duration; | ||
|
||
public abstract class I2CDevice extends Component { | ||
|
||
/** | ||
* The Default BUS and Device Address. | ||
* On the PI, you can look it up with the Command 'sudo i2cdetect -y 1' | ||
*/ | ||
protected static final int DEFAULT_BUS = 0x01; | ||
|
||
/** | ||
* The PI4J I2C component | ||
*/ | ||
private final I2C i2c; | ||
|
||
|
||
protected I2CDevice(Context pi4j, int device, String name) { | ||
i2c = pi4j.create(I2C.newConfigBuilder(pi4j) | ||
.id("I2C-" + DEFAULT_BUS + "@" + device) | ||
.name(name + "@" + device) | ||
.bus(DEFAULT_BUS) | ||
.device(device) | ||
.build()); | ||
init(i2c); | ||
logDebug("I2C device %s initialized", name); | ||
} | ||
|
||
|
||
/** | ||
* send a single command to device | ||
*/ | ||
protected void sendCommand(byte cmd) { | ||
i2c.write(cmd); | ||
delay(Duration.ofNanos(100_000)); | ||
} | ||
|
||
protected int readRegister(int register) { | ||
return i2c.readRegisterWord(register); | ||
} | ||
|
||
/** | ||
* send custom configuration to device | ||
* | ||
* @param config custom configuration | ||
*/ | ||
protected void writeRegister(int register, int config) { | ||
i2c.writeRegisterWord(register, config); | ||
} | ||
|
||
/** | ||
* send some data to device | ||
* | ||
* @param data | ||
*/ | ||
protected void write(byte data) { | ||
i2c.write(data); | ||
} | ||
|
||
/** | ||
* Execute Display commands | ||
* | ||
* @param command Select the LCD Command | ||
* @param data Setup command data | ||
*/ | ||
protected void sendCommand(byte command, byte data) { | ||
sendCommand((byte) (command | data)); | ||
} | ||
|
||
protected abstract void init(I2C i2c); | ||
|
||
// --------------- for testing -------------------- | ||
|
||
public MockI2C mock() { | ||
return asMock(MockI2C.class, i2c); | ||
} | ||
|
||
} |
Oops, something went wrong.