Skip to content

Commit

Permalink
34: init migration to pi4jv2
Browse files Browse the repository at this point in the history
  • Loading branch information
mirage22 committed Nov 3, 2023
1 parent f5de3d3 commit 5ce6670
Show file tree
Hide file tree
Showing 30 changed files with 306 additions and 212 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ project("robo4j-hw-rpi") {
dependencies {
implementation(project(":robo4j-math"))
implementation "com.pi4j:pi4j-core:${pi4jVersion}"
implementation "com.pi4j:pi4j-plugin-pigpio:${pi4jVersion}"
implementation "com.pi4j:pi4j-plugin-raspberrypi:${pi4jVersion}"
}

Expand Down
7 changes: 6 additions & 1 deletion robo4j-hw-rpi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<artifactId>pi4j-plugin-raspberrypi</artifactId>
<version>${pi4j.version}</version>
</dependency>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-plugin-pigpio</artifactId>
<version>${pi4j.version}</version>
</dependency>

<!-- TEST -->
<dependency>
Expand All @@ -43,4 +48,4 @@
<scope>test</scope>
</dependency>
</dependencies>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
*/
package com.robo4j.hw.rpi.i2c.accelerometer;

import com.pi4j.io.i2c.I2CBus;
import java.io.IOException;

import com.robo4j.hw.rpi.i2c.AbstractI2CDevice;
import com.robo4j.hw.rpi.i2c.ReadableDevice;
import com.robo4j.hw.rpi.utils.I2cBus;
import com.robo4j.math.geometry.Tuple3f;

import java.io.IOException;

/**
* Abstraction for reading data from a LSM303 accelerometer, for example the one
* on the Adafruit IMU breakout board.
Expand Down Expand Up @@ -65,6 +65,7 @@ public int getCtrlCode() {

public enum PowerMode {
NORMAL(0x0), LOW_POWER(0x8);

private int ctrlCode;

PowerMode(int ctrlCode) {
Expand All @@ -78,6 +79,7 @@ public int getCtrlCode() {

public enum FullScale {
G_2(0x0, 1), G_4(0x10, 1), G_8(0x20, 4), G_16(0x30, 12);

private int ctrlCode;
private int sensitivity;

Expand All @@ -104,10 +106,10 @@ public AccelerometerLSM303Device() throws IOException {

public AccelerometerLSM303Device(PowerMode mode, DataRate rate, FullScale scale, boolean highres)
throws IOException {
this(I2CBus.BUS_1, DEFAULT_I2C_ADDRESS, mode, rate, AXIS_ENABLE_ALL, scale, highres);
this(I2cBus.BUS_1, DEFAULT_I2C_ADDRESS, mode, rate, AXIS_ENABLE_ALL, scale, highres);
}

public AccelerometerLSM303Device(int bus, int address, PowerMode mode, DataRate rate, int axisEnable,
public AccelerometerLSM303Device(I2cBus bus, int address, PowerMode mode, DataRate rate, int axisEnable,
FullScale scale, boolean highres) throws IOException {
super(bus, address);
this.scale = scale;
Expand All @@ -122,7 +124,8 @@ public AccelerometerLSM303Device(int bus, int address, PowerMode mode, DataRate
public synchronized Tuple3f read() throws IOException {
Tuple3f rawData = new Tuple3f();
byte[] data = new byte[6];
int n = i2CConfig.read(OUT_X_L_A | 0x80, data, 0, 6);
// int n = i2CConfig.read(OUT_X_L_A | 0x80, data, 0, 6);
int n = readBufferByAddress(OUT_X_L_A | 0x80, data, 0, 6);
if (n != 6) {
getLogger().warning("Failed to read all data from accelerometer. Should have read 6, could only read " + n);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

import java.io.IOException;

import com.pi4j.io.i2c.I2CBus;
import com.robo4j.hw.rpi.i2c.AbstractI2CDevice;
import com.robo4j.hw.rpi.utils.I2cBus;

/**
* AbstractBackpack is the abstraction for all Adafruit Backpack devices
Expand All @@ -33,6 +33,7 @@
public abstract class AbstractBackpack extends AbstractI2CDevice {

public static final int DEFAULT_BRIGHTNESS = 15;
private static final int DEFAULT_ADDRESS = 0x70;
private static final int OSCILLATOR_TURN_ON = 0x21;
private static final int HT16K33_BLINK_CMD = 0x80;
private static final int HT16K33_BLINK_DISPLAY_ON = 0x01;
Expand All @@ -41,10 +42,10 @@ public abstract class AbstractBackpack extends AbstractI2CDevice {
private final short[] buffer = new short[8]; // uint16_t

AbstractBackpack() throws IOException {
this(I2CBus.BUS_1, 0x70, DEFAULT_BRIGHTNESS);
this(I2cBus.BUS_1, DEFAULT_ADDRESS, DEFAULT_BRIGHTNESS);
}

AbstractBackpack(int bus, int address, int brightness) throws IOException {
AbstractBackpack(I2cBus bus, int address, int brightness) throws IOException {
super(bus, address);
initiate(brightness);
}
Expand All @@ -53,15 +54,15 @@ public void display() {
try {
writeDisplay();
} catch (IOException e) {
System.out.println(String.format("error display: %s", e.getMessage()));
System.out.printf("error display: %s%n", e.getMessage());
}
}

public void clear() {
try {
clearBuffer();
} catch (IOException e) {
System.out.println(String.format("error clear: %s", e.getMessage()));
System.out.printf("error clear: %s%n", e.getMessage());
}
}

Expand Down Expand Up @@ -172,16 +173,21 @@ void setColorToBarBuffer(short a, short c, BiColor color) {
}

private void initiate(int brightness) throws IOException {
i2CConfig.write((byte) (OSCILLATOR_TURN_ON)); // Turn on oscilator
i2CConfig.write(blinkRate(HT16K33_BLINK_OFF));
i2CConfig.write(setBrightness(brightness));
// i2CConfig.write((byte) (OSCILLATOR_TURN_ON)); // Turn on oscilator
// i2CConfig.write(blinkRate(HT16K33_BLINK_OFF));
// i2CConfig.write(setBrightness(brightness));
writeByte((byte) (OSCILLATOR_TURN_ON)); // Turn on oscilator
writeByte(blinkRate(HT16K33_BLINK_OFF));
writeByte(setBrightness(brightness));
}

private void writeDisplay() throws IOException {
int address = 0;
for (int i = 0; i < buffer.length; i++) {
i2CConfig.write(address++, (byte) (buffer[i] & 0xFF));
i2CConfig.write(address++, (byte) (buffer[i] >> 8));
// i2CConfig.write(address++, (byte) (buffer[i] & 0xFF));
// i2CConfig.write(address++, (byte) (buffer[i] >> 8));
writeByte(address++, (byte) (buffer[i] & 0xFF));
writeByte(address++, (byte) (buffer[i] >> 8));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package com.robo4j.hw.rpi.i2c.adafruitbackpack;

import com.pi4j.io.i2c.I2CBus;
import com.robo4j.hw.rpi.utils.I2cBus;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -146,10 +146,10 @@ public class AlphanumericDevice extends AbstractBackpack {
private final AtomicInteger position = new AtomicInteger(0);

public AlphanumericDevice() throws IOException {
this(I2CBus.BUS_1, 0x70, DEFAULT_BRIGHTNESS);
this(I2cBus.BUS_1, 0x70, DEFAULT_BRIGHTNESS);
}

public AlphanumericDevice(int bus, int address, int brightness) throws IOException {
public AlphanumericDevice(I2cBus bus, int address, int brightness) throws IOException {
super(bus, address, brightness);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package com.robo4j.hw.rpi.i2c.adafruitbackpack;

import com.robo4j.hw.rpi.utils.I2cBus;

import java.io.IOException;

/**
Expand All @@ -31,7 +33,7 @@ public class BiColor24BarDevice extends AbstractBackpack {

public static final int MAX_BARS = 24;

public BiColor24BarDevice(int bus, int address, int brightness) throws IOException {
public BiColor24BarDevice(I2cBus bus, int address, int brightness) throws IOException {
super(bus, address, brightness);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package com.robo4j.hw.rpi.i2c.adafruitbackpack;

import com.robo4j.hw.rpi.utils.I2cBus;

import java.io.IOException;

/**
Expand All @@ -31,13 +33,13 @@ public class BiColor8x8MatrixDevice extends AbstractBackpack implements MatrixLe
private static final short MATRIX_SIZE = 8;
private MatrixRotation rotation;

public BiColor8x8MatrixDevice(int bus, int address, int brightness, MatrixRotation rotation) throws IOException {
public BiColor8x8MatrixDevice(I2cBus bus, int address, int brightness, MatrixRotation rotation) throws IOException {
super(bus, address, brightness);
this.rotation = rotation;

}

public BiColor8x8MatrixDevice(int bus, int address, int brightness) throws IOException {
public BiColor8x8MatrixDevice(I2cBus bus, int address, int brightness) throws IOException {
super(bus, address, brightness);
this.rotation = MatrixRotation.DEFAULT_X_Y;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package com.robo4j.hw.rpi.i2c.adafruitbackpack;

import com.robo4j.hw.rpi.utils.I2cBus;

import java.io.IOException;

/**
Expand All @@ -27,7 +29,7 @@
*/
public final class LedBackpackFactory {

public static AbstractBackpack createDevice(int bus, int address, LedBackpackType type, int brightness)
public static AbstractBackpack createDevice(I2cBus bus, int address, LedBackpackType type, int brightness)
throws IOException {
switch (type) {
case BI_COLOR_BAR_24:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
*/

import com.robo4j.hw.rpi.i2c.adafruitlcd.impl.AdafruitLcdImpl.Direction;
import com.robo4j.hw.rpi.utils.I2cBus;

import java.io.IOException;

public interface AdafruitLcd {
// Not using the Pi4J bus specification here, since we may not be able to
// load the class (we can run with a mock up).
int DEFAULT_BUS = 1;
I2cBus DEFAULT_BUS = I2cBus.BUS_1;
int DEFAULT_ADDRESS = 0x20;

/**
Expand Down Expand Up @@ -231,4 +232,4 @@ public interface AdafruitLcd {
* exception
*/
void createChar(int location, byte[] pattern) throws IOException;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.pi4j.exception.InitializeException;
import com.robo4j.hw.rpi.i2c.adafruitlcd.impl.AdafruitLcdImpl;
import com.robo4j.hw.rpi.i2c.adafruitlcd.mockup.AdafruitLcdMockup;
import com.robo4j.hw.rpi.utils.I2cBus;

import java.io.IOException;

Expand All @@ -44,7 +45,7 @@ public static AdafruitLcd createLCD() throws IOException, InitializeException {
return new AdafruitLcdImpl();
}

public static AdafruitLcd createLCD(int bus, int address) throws IOException, InitializeException {
public static AdafruitLcd createLCD(I2cBus bus, int address) throws IOException, InitializeException {
if (Boolean.getBoolean(SYSTEM_PROPERTY_MOCK)) {
return new AdafruitLcdMockup(bus, address);
}
Expand Down
Loading

0 comments on commit 5ce6670

Please sign in to comment.