Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 34 additions & 26 deletions src/main/java/ev3dev/sensors/Battery.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import ev3dev.hardware.EV3DevDevice;
import ev3dev.hardware.EV3DevFileSystem;
import ev3dev.hardware.EV3DevPlatform;
import ev3dev.utils.Sysfs;
import ev3dev.utils.DataChannelRereader;
import lejos.hardware.Power;
import lombok.extern.slf4j.Slf4j;

import java.io.Closeable;
import java.io.IOException;

/**
* The class Battery interacts with EV3Dev to get information about battery used.
*
Expand All @@ -15,19 +18,10 @@
* @see <a href="https://github.com/ev3dev/ev3dev-lang/blob/develop/wrapper-specification.md#direct-attribute-mappings-5">https://github.com/ev3dev/ev3dev-lang/blob/develop/wrapper-specification.md#direct-attribute-mappings-5</a>
*/
@Slf4j
public class Battery extends EV3DevDevice implements Power {

private final String BATTERY;
private final String BATTERY_EV3;
private final String BATTERY_PISTORMS;
private final String BATTERY_BRICKPI;
private final String BATTERY_BRICKPI3;
public class Battery extends EV3DevDevice implements Power, Closeable {

private String BATTERY_PATH;
private final String VOLTAGE = "voltage_now";
private final String CURRENT = "current_now";

private String BATTERY_PATH_LOCAL = "";
private final DataChannelRereader voltageRereader;
private final DataChannelRereader currentRereader;

private static Battery instance;

Expand All @@ -49,28 +43,37 @@ private Battery() {

LOGGER.debug("Init sensor");

BATTERY = ev3DevProperties.getProperty("battery");
BATTERY_EV3 = ev3DevProperties.getProperty("ev3.battery");
BATTERY_PISTORMS = ev3DevProperties.getProperty("pistorms.battery");
BATTERY_BRICKPI = ev3DevProperties.getProperty("brickpi.battery");
BATTERY_BRICKPI3 = ev3DevProperties.getProperty("brickpi3.battery");
String battery = ev3DevProperties.getProperty("battery");
String batteryEv3 = ev3DevProperties.getProperty("ev3.battery");
String batteryPistorms = ev3DevProperties.getProperty("pistorms.battery");
String batteryBrickpi = ev3DevProperties.getProperty("brickpi.battery");
String batteryBrickpi3 = ev3DevProperties.getProperty("brickpi3.battery");

//TODO Create separator variable for the whole project
BATTERY_PATH = EV3DevFileSystem.getRootPath() + "/" + BATTERY;
String batteryPath = EV3DevFileSystem.getRootPath() + "/" + battery;
String batteryPathLocal = "";
if (CURRENT_PLATFORM.equals(EV3DevPlatform.EV3BRICK)) {
BATTERY_PATH_LOCAL += BATTERY_PATH + "/" + BATTERY_EV3;
batteryPathLocal += batteryPath + "/" + batteryEv3;
} else if (CURRENT_PLATFORM.equals(EV3DevPlatform.PISTORMS)) {
BATTERY_PATH_LOCAL += BATTERY_PATH + "/" + BATTERY_PISTORMS;
batteryPathLocal += batteryPath + "/" + batteryPistorms;
} else if (CURRENT_PLATFORM.equals(EV3DevPlatform.BRICKPI)) {
BATTERY_PATH_LOCAL += BATTERY_PATH + "/" + BATTERY_BRICKPI;
batteryPathLocal += batteryPath + "/" + batteryBrickpi;
} else if (CURRENT_PLATFORM.equals(EV3DevPlatform.BRICKPI3)) {
BATTERY_PATH_LOCAL += BATTERY_PATH + "/" + BATTERY_BRICKPI3;
batteryPathLocal += batteryPath + "/" + batteryBrickpi3;
}
String voltage = "voltage_now";
voltageRereader = new DataChannelRereader(batteryPathLocal + "/" + voltage);
String current = "current_now";
currentRereader = new DataChannelRereader(batteryPath + "/" + batteryEv3 + "/" + current);
}

public int getVoltageMicroVolts() {
return Integer.parseInt(voltageRereader.readString());
}

@Override
public int getVoltageMilliVolt() {
return (int) Sysfs.readFloat(BATTERY_PATH_LOCAL + "/" + VOLTAGE) / 1000;
return getVoltageMicroVolts() / 1000;
}

/**
Expand All @@ -79,7 +82,7 @@ public int getVoltageMilliVolt() {
* @return voltage
*/
public float getVoltage() {
return Sysfs.readFloat(BATTERY_PATH_LOCAL + "/" + VOLTAGE) / 1000000;
return getVoltageMicroVolts() / 1000000f;
}

//TODO Review output
Expand All @@ -92,7 +95,7 @@ public float getVoltage() {
*/
public float getBatteryCurrent() {
if (CURRENT_PLATFORM.equals(EV3DevPlatform.EV3BRICK)) {
return Sysfs.readFloat(BATTERY_PATH + "/" + BATTERY_EV3 + "/" + CURRENT);
return Float.parseFloat(currentRereader.readString());
} else {
LOGGER.warn("This method is not available for {} & {}", EV3DevPlatform.PISTORMS, EV3DevPlatform.BRICKPI);
return -1f;
Expand All @@ -105,4 +108,9 @@ public float getMotorCurrent() {
throw new UnsupportedOperationException("This feature is not implemented");
}

@Override
public void close() throws IOException {
voltageRereader.close();
currentRereader.close();
}
}
12 changes: 7 additions & 5 deletions src/main/java/ev3dev/utils/DataChannelRereader.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,23 @@ public class DataChannelRereader implements Closeable {
*
* @param path path to the file to reread
* @param bufferLength length of the buffer to hold the structure
* @throws IOException when things go wrong
*/
public DataChannelRereader(Path path, int bufferLength) throws IOException {
public DataChannelRereader(Path path, int bufferLength) {
this.path = path;
this.byteBuffer = ByteBuffer.allocate(bufferLength);
this.channel = FileChannel.open(path);
try {
this.channel = FileChannel.open(path);
} catch (IOException e) {
throw new RuntimeException("Problem opening path: " + path, e);
}
}

/**
* Create a DataChannelRereader for pathString with the default 32-byte buffer.
*
* @param pathString Path to the file to reread
* @throws IOException when things go wrong
*/
public DataChannelRereader(String pathString) throws IOException {
public DataChannelRereader(String pathString) {
this(Paths.get(pathString),32);
}

Expand Down