diff --git a/de.ibapl.onewire4j/src/main/java/de/ibapl/onewire4j/container/OneWireDevice26.java b/de.ibapl.onewire4j/src/main/java/de/ibapl/onewire4j/container/OneWireDevice26.java index b14925a..45aeec6 100644 --- a/de.ibapl.onewire4j/src/main/java/de/ibapl/onewire4j/container/OneWireDevice26.java +++ b/de.ibapl.onewire4j/src/main/java/de/ibapl/onewire4j/container/OneWireDevice26.java @@ -85,15 +85,26 @@ public byte getStatusConfiguration() { return data[0]; } + final public boolean isStatusConfiguration(final byte bitMask) { + return (data[0] & bitMask) == bitMask; + } + public double getTemperature() { - return (data[2] | (data[1] & 0xFF)) / 256.0; // converts integer to a double data[2] carries the sign + if (isStatusConfiguration(StatusConfiguration.TB)) { + throw new RuntimeException("Temperature conversion not completed yet!"); + } + return data[2] + (data[1] & 0xFF) / 256.0; // converts integer to a double data[2] carries the sign data[1] stores the fraction. } public double getVoltage() { - return ((((data[4] & 0xFF) << 8) | (data[3] & 0xFF))) / 100.0; // unsigned + if (isStatusConfiguration(StatusConfiguration.ADB)) { + throw new RuntimeException("Voltage conversion not completed yet!"); + } + return ((((data[4] & 0xFF) << 8) | (data[3] & 0xFF))) / 100.0; // unsigned 1 bit = 10mV } public double getCurrent(double valueSensResistor) { + // 1 bit = 0.2441mV return (((data[6] << 8) | (data[5] & 0xFF))) / (4096 * valueSensResistor); // data[6] carries the sign } @@ -106,7 +117,7 @@ public void setStatusConfiguration(byte value) { } public boolean isVDD() { - return (data[0] & OneWireDevice26.StatusConfiguration.AD) == OneWireDevice26.StatusConfiguration.AD; + return isStatusConfiguration(OneWireDevice26.StatusConfiguration.AD); } public void setVDD(boolean value) { @@ -116,6 +127,19 @@ public void setVDD(boolean value) { data[0] &= ~OneWireDevice26.StatusConfiguration.AD; } } + + /** + * Clean the read only data + */ + public void clearRO() { + data[0] &= 0x0F; + data[1] = 0; + data[2] = 0; + data[3] = 0; + data[4] = 0; + data[5] = 0; + data[6] = 0; + } } public static class ScratchpadPage1Data extends ScratchpadPageXData { diff --git a/de.ibapl.onewire4j/src/main/java/de/ibapl/onewire4j/devices/DS2438WithHIH4031.java b/de.ibapl.onewire4j/src/main/java/de/ibapl/onewire4j/devices/DS2438WithHIH4031.java index 75f1ceb..8a298af 100644 --- a/de.ibapl.onewire4j/src/main/java/de/ibapl/onewire4j/devices/DS2438WithHIH4031.java +++ b/de.ibapl.onewire4j/src/main/java/de/ibapl/onewire4j/devices/DS2438WithHIH4031.java @@ -31,6 +31,12 @@ */ public class DS2438WithHIH4031 { + @FunctionalInterface + public interface HumidityTemperatureSink { + + void data(final double humidity, final double temp); + } + final OneWireDevice26 ds2438; public DS2438WithHIH4031(OneWireDevice26 ds2438) { @@ -47,29 +53,64 @@ public double getHIH4031Humidity(OneWireAdapter adapter) throws IOException { if (data.isVDD()) { VDD = data.getVoltage(); data.setVDD(false); + data.clearRO(); ds2438.setScratchpadPage0(adapter, data); data = ds2438.getScratchpadPage0(adapter, false, true); VAD = data.getVoltage(); } else { VAD = data.getVoltage(); data.setVDD(true); + data.clearRO(); ds2438.setScratchpadPage0(adapter, data); data = ds2438.getScratchpadPage0(adapter, false, true); VDD = data.getVoltage(); - } + final double result = (((VAD / VDD) - 0.16) / 0.0062) / (1.0546 - 0.00216 * T); if (result > 100.0) { - throw new RuntimeException("humidity can't be > 100.0% : " + result + "(VAD = " + VAD + " VDD = " + VDD + ")"); + throw new RuntimeException("@" + ds2438.getAddressAsString() + ": humidity can't be > 100.0% : " + result + "(VAD = " + VAD + " VDD = " + VDD + " T = " + T + " )"); } else if (result < 0.0) { - throw new RuntimeException("humidity can't be < 0.0% : " + result + "(VAD = " + VAD + " VDD = " + VDD + ")"); + throw new RuntimeException("@" + ds2438.getAddressAsString() + ": humidity can't be < 0.0% : " + result + "(VAD = " + VAD + " VDD = " + VDD + " T = " + T + ")"); } else { return result; } } + public void getHIH4031HumidityAndTemperature(OneWireAdapter adapter, HumidityTemperatureSink sink) throws IOException { + final double VDD; + final double VAD; + final double temp; + + OneWireDevice26.ScratchpadPage0Data data = ds2438.getScratchpadPage0(adapter, true, true); + temp = data.getTemperature(); + if (data.isVDD()) { + VDD = data.getVoltage(); + data.setVDD(false); + data.clearRO(); + ds2438.setScratchpadPage0(adapter, data); + data = ds2438.getScratchpadPage0(adapter, false, true); + VAD = data.getVoltage(); + } else { + VAD = data.getVoltage(); + data.setVDD(true); + data.clearRO(); + ds2438.setScratchpadPage0(adapter, data); + data = ds2438.getScratchpadPage0(adapter, false, true); + VDD = data.getVoltage(); + } + + final double humidity = (((VAD / VDD) - 0.16) / 0.0062) / (1.0546 - 0.00216 * temp); + if (humidity > 100.0) { + throw new RuntimeException("@" + ds2438.getAddressAsString() + ": humidity can't be > 100.0% : " + humidity + "(VAD = " + VAD + " VDD = " + VDD + " T = " + temp + " )"); + } else if (humidity < 0.0) { + throw new RuntimeException("@" + ds2438.getAddressAsString() + ": humidity can't be < 0.0% : " + humidity + "(VAD = " + VAD + " VDD = " + VDD + " T = " + temp + ")"); + } else { + sink.data(humidity, temp); + } + } + public double getTemperature(OneWireAdapter adapter) throws IOException { - OneWireDevice26.ScratchpadPage0Data page0 = ds2438.getScratchpadPage0(adapter, true, true); + OneWireDevice26.ScratchpadPage0Data page0 = ds2438.getScratchpadPage0(adapter, true, false); return page0.getTemperature(); }