📄 onewirecontainer28.java
字号:
/** * Gets the minimum temperature in Celsius. * * @return minimum temperature in Celsius for this * <code>OneWireContainer28</code> * * @see #getMaxTemperature */ public double getMinTemperature () { return -55.0; } //-------- //-------- Temperature I/O Methods //-------- /** * Performs a temperature conversion on <code>state</code> information. * * @param state byte array with device state information * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from this <code>OneWireContainer28</code>. * This could be caused by a physical interruption in the 1-Wire * Network due to shorts or a newly arriving 1-Wire device issuing a * 'presence pulse'. * @throws OneWireException on a communication or setup error with the 1-Wire * adapter * * @see #getTemperature */ public void doTemperatureConvert (byte[] state) throws OneWireIOException, OneWireException { int msDelay = 750; // in milliseconds // select the device if (adapter.select(address)) { // Setup Power Delivery adapter.setPowerDuration(adapter.DELIVERY_INFINITE); adapter.startPowerDelivery(adapter.CONDITION_AFTER_BYTE); // send the convert temperature command adapter.putByte(CONVERT_TEMPERATURE_COMMAND); // calculate duration of delay according to resolution desired switch (state [4]) { case RESOLUTION_9_BIT : msDelay = 94; break; case RESOLUTION_10_BIT : msDelay = 188; break; case RESOLUTION_11_BIT : msDelay = 375; break; case RESOLUTION_12_BIT : msDelay = 750; break; default : msDelay = 750; } // switch // delay for specified amount of time try { Thread.sleep(msDelay); } catch (InterruptedException e){} // Turn power back to normal. adapter.setPowerNormal(); // check to see if the temperature conversion is over if (adapter.getByte() != 0xFF) throw new OneWireIOException( "OneWireContainer28-temperature conversion not complete"); } else { // device must not have been present throw new OneWireIOException( "OneWireContainer28-device not present"); } } //-------- //-------- Temperature 'get' Methods //-------- /** * Gets the temperature value in Celsius from the <code>state</code> * data retrieved from the <code>readDevice()</code> method. * * @param state byte array with device state information for this * <code>OneWireContainer28</code> * * @return temperature in Celsius from the last * <code>doTemperatureConvert()</code> * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from this <code>OneWireContainer28</code>. * This could be caused by a physical interruption in the 1-Wire * Network due to shorts or a newly arriving 1-Wire device issuing a * 'presence pulse'. * * @see #doTemperatureConvert */ public double getTemperature (byte[] state) throws OneWireIOException { // Take these three steps: // 1) Make an 11-bit integer number out of MSB and LSB of the first 2 bytes from scratchpad // 2) Divide final number by 16 to retrieve the floating point number. // 3) Afterwards, test for the following temperatures: // 0x07D0 = 125.0C // 0x0550 = 85.0C // 0x0191 = 25.0625C // 0x00A2 = 10.125C // 0x0008 = 0.5C // 0x0000 = 0.0C // 0xFFF8 = -0.5C // 0xFF5E = -10.125C // 0xFE6F = -25.0625C // 0xFC90 = -55.0C double theTemperature = ( double ) 0.0; int inttemperature = state [1]; // inttemperature is automatically sign extended here. inttemperature = (inttemperature << 8) | (state [0] & 0xFF); // this converts 2 bytes into integer theTemperature = ( double ) (( double ) inttemperature / ( double ) 16); // converts integer to a double return (theTemperature); } /** * Gets the specified temperature alarm value in Celsius from the * <code>state</code> data retrieved from the <code>readDevice()</code> * method. * * @param alarmType valid value: <code>ALARM_HIGH</code> or * <code>ALARM_LOW</code> * @param state byte array with device state information * * @return temperature alarm trip values in Celsius for this * <code>OneWireContainer28</code> * * @see #hasTemperatureAlarms * @see #setTemperatureAlarm */ public double getTemperatureAlarm (int alarmType, byte[] state) { return ( double ) state [alarmType == ALARM_LOW ? 3 : 2]; } /** * Gets the current temperature resolution in Celsius from the * <code>state</code> data retrieved from the <code>readDevice()</code> * method. * * @param state byte array with device state information * * @return temperature resolution in Celsius for this * <code>OneWireContainer28</code> * * @see #RESOLUTION_9_BIT * @see #RESOLUTION_10_BIT * @see #RESOLUTION_11_BIT * @see #RESOLUTION_12_BIT * @see #hasSelectableTemperatureResolution * @see #getTemperatureResolutions * @see #setTemperatureResolution */ public double getTemperatureResolution (byte[] state) { double tempres = ( double ) 0.0; // calculate temperature resolution according to configuration byte switch (state [4]) { case RESOLUTION_9_BIT : tempres = ( double ) 0.5; break; case RESOLUTION_10_BIT : tempres = ( double ) 0.25; break; case RESOLUTION_11_BIT : tempres = ( double ) 0.125; break; case RESOLUTION_12_BIT : tempres = ( double ) 0.0625; break; default : tempres = ( double ) 0.0; } // switch return tempres; } //-------- //-------- Temperature 'set' Methods //-------- /** * Sets the temperature alarm value in Celsius in the provided * <code>state</code> data. * Use the method <code>writeDevice()</code> with * this data to finalize the change to the device. * * @param alarmType valid value: <code>ALARM_HIGH</code> or * <code>ALARM_LOW</code> * @param alarmValue alarm trip value in Celsius * @param state byte array with device state information * * @see #hasTemperatureAlarms * @see #getTemperatureAlarm */ public void setTemperatureAlarm (int alarmType, double alarmValue, byte[] state) throws OneWireException, OneWireIOException { if ((alarmType != ALARM_LOW) && (alarmType != ALARM_HIGH)) throw new IllegalArgumentException("Invalid alarm type."); if (alarmValue > 125.0 || alarmValue < -55.0) throw new IllegalArgumentException( "Value for alarm not in accepted range. Must be -55 C <-> +125 C."); state [(alarmType == ALARM_LOW) ? 3 : 2] = ( byte ) alarmValue; } /** * Sets the current temperature resolution in Celsius in the provided * <code>state</code> data. Use the method <code>writeDevice()</code> * with this data to finalize the change to the device. * * @param resolution temperature resolution in Celsius. Valid values are * <code>RESOLUTION_9_BIT</code>, * <code>RESOLUTION_10_BIT</code>, * <code>RESOLUTION_11_BIT</code> and * <code>RESOLUTION_12_BIT</code>. * @param state byte array with device state information * * @see #RESOLUTION_9_BIT * @see #RESOLUTION_10_BIT * @see #RESOLUTION_11_BIT * @see #RESOLUTION_12_BIT * @see #hasSelectableTemperatureResolution * @see #getTemperatureResolution * @see #getTemperatureResolutions */ public void setTemperatureResolution (double resolution, byte[] state) throws OneWireException { byte configbyte = RESOLUTION_12_BIT; synchronized (this) { // calculate configbyte from given resolution if (resolution == 0.5) configbyte = RESOLUTION_9_BIT; if (resolution == 0.25) configbyte = RESOLUTION_10_BIT; if (resolution == 0.125) configbyte = RESOLUTION_11_BIT; if (resolution == 0.0625) configbyte = RESOLUTION_12_BIT; state [4] = configbyte; } } /** * Retrieves this <code>OneWireContainer28</code> state information. * The state information is returned as a byte array. Pass this byte * array to the '<code>get</code>' and '<code>set</code>' methods. * If the device state needs to be changed, then call the * <code>writeDevice()</code> to finalize the changes. * * @return <code>OneWireContainer28</code> state information. * Device state looks like this: * <pre> * 0 : temperature LSB * 1 : temperature MSB * 2 : trip high * 3 : trip low * 4 : configuration register (for resolution) * 5 : reserved * 6 : reserved * 7 : reserved * 8 : an 8 bit CRC of the previous 8 bytes * </pre> * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from this <code>OneWireContainer28</code>. * This could be caused by a physical interruption in the 1-Wire * Network due to shorts or a newly arriving 1-Wire device issuing a * 'presence pulse'. * @throws OneWireException on a communication or setup error with the 1-Wire * adapter * * @see #writeDevice */ public byte[] readDevice () throws OneWireIOException, OneWireException { byte[] data; data = recallE2(); return data; } /** * Writes to this <code>OneWireContainer28</code> <code>state</code>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -