📄 onewirecontainer21.java
字号:
double lowTemp = getPhysicalRangeLowTemperature(); // low temp of thermochrons other than H or Z if (isDS1921HZ) lowTemp = lowTemp - (getTemperatureResolution() * 4); return lowTemp; } /** * This method returns the width of a histogram bin in degrees * Celsius. * * @return the width of a histogram bin for this thermochron. */ public double getHistogramBinWidth() { return (getTemperatureResolution() * 4); // 4 temperature readings per bin } /** * Converts a temperature from the DS1921 <code>byte</code> encoded * format to degrees Celsius. The raw temperature readings are unsigned * <code>byte</code> values, representing a 2.0 degree accuracy. * * @param tempByte raw DS1921 temperature reading * * @return temperature in degrees Celsius * * @see #encodeTemperature(double) */ public double decodeTemperature (byte tempByte) { // the formula for DS1921H/Z: // C = Tbyte * Tres + (Tlow - (4 * Tres)) // where C is decimal degrees Celsius. // and Tbyte is the byte to be decoded. // and Tlow is the low temperature of temperature range. // and Tres is the resolution of the DS1921. double decodedTemperature = 0.0; if (isDS1921HZ) { decodedTemperature = ((tempByte & 0x00ff) * temperatureResolution); decodedTemperature = decodedTemperature + (temperatureRangeLow - (4 * temperatureResolution)); } else { decodedTemperature = ((tempByte & 0x00ff) / 2.0) - 40.0; } return decodedTemperature; } /** * Converts a temperature in degrees Celsius to * a <code>byte</code> formatted for the DS1921. * * @param temperature the temperature (Celsius) to convert * * @return the temperature in raw DS1921 format * * @see #decodeTemperature(byte) */ public byte encodeTemperature (double temperature) { // the formula for DS1921H/Z: // Tbyte = ((C - Tlow) / Tres) + 4; // where Tbyte is the byte to be encoded. // and C is decimal degrees Celsius // and Tlow is the low temperature of temperature range // and Tres is the resolution of the DS1921 byte encodedTemperature = 0x00; if (isDS1921HZ) { double result = ((temperature - temperatureRangeLow) / temperatureResolution) + 4; encodedTemperature = (byte) ((int) result & 0x000000ff); } else { encodedTemperature = ( byte ) ((( int ) (2 * temperature) + 80) & 0x000000ff); } return encodedTemperature; } /** * Writes a byte of data into the DS1921's memory. Note that writing to * the register page while a mission is in progress ends that mission. * Also note that the preferred way to write a page is through the * <code>MemoryBank</code> objects returned from the <code>getMemoryBanks()</code> * method. * * @param memAddr the address for writing (in the range of 0x200-0x21F) * @param source the data <code>byte</code> to write * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from a 1-Wire device. 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 #readByte(int) * @see #getMemoryBanks() */ public void writeByte (int memAddr, byte source) throws OneWireIOException, OneWireException { // User should only need to write to the 32 byte register page byte[] buffer = new byte [5]; // break the address into its bytes byte msbAddress = ( byte ) ((memAddr >>> 8) & 0x0ff); byte lsbAddress = ( byte ) (memAddr & 0x0ff); /* check for valid parameters */ if ((msbAddress > 0x1F) || (msbAddress < 0)) throw new IllegalArgumentException( "OneWireContainer21-Address for write out of range."); /* perform the write and verification */ if (doSpeedEnable) doSpeed(); if (adapter.select(address)) { /* write to the scratchpad first */ buffer [0] = WRITE_SCRATCHPAD_COMMAND; buffer [1] = lsbAddress; buffer [2] = msbAddress; buffer [3] = source; adapter.dataBlock(buffer, 0, 4); /* read it back for the verification bytes required to copy it to mem */ adapter.select(address); buffer [0] = READ_SCRATCHPAD_COMMAND; for (int i = 1; i < 5; i++) buffer [i] = ( byte ) 0x0ff; adapter.dataBlock(buffer, 0, 5); // check to see if the data was written correctly if (buffer [4] != source) throw new OneWireIOException( "OneWireContainer21-Error writing data byte."); /* now perform the copy from the scratchpad to memory */ adapter.select(address); buffer [0] = COPY_SCRATCHPAD_COMMAND; // keep buffer[1]-buffer[3] because they contain the verification bytes buffer [4] = ( byte ) 0xff; adapter.dataBlock(buffer, 0, 5); /* now check to see that the part sent a 01010101 indicating a success */ if ((buffer [4] != ( byte ) 0xAA) && (buffer [4] != ( byte ) 0x55)) throw new OneWireIOException( "OneWireContainer21-Error writing data byte."); } else throw new OneWireException("OneWireContainer21-Device not present."); } /** * Reads a single byte from the DS1921. Note that the preferred manner * of reading from the DS1921 Thermocron is through the <code>readDevice()</code> * method or through the <code>MemoryBank</code> objects returned in the * <code>getMemoryBanks()</code> method. * * @param memAddr the address to read from (in the range of 0x200-0x21F) * * @return the data byte read * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from a 1-Wire device. 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 #writeByte(int,byte) * @see #readDevice() * @see #getMemoryBanks() */ public byte readByte (int memAddr) throws OneWireIOException, OneWireException { byte[] buffer = new byte [4]; // break the address up into bytes byte msbAddress = ( byte ) ((memAddr >> 8) & 0x000000ff); byte lsbAddress = ( byte ) (memAddr & 0x000000ff); /* check the validity of the address */ if ((msbAddress > 0x1F) || (msbAddress < 0)) throw new IllegalArgumentException( "OneWireContainer21-Address for read out of range."); /* read a user specified amount of memory and verify its validity */ if (doSpeedEnable) doSpeed(); if (adapter.select(address)) { buffer [0] = READ_MEMORY_CRC_COMMAND; buffer [1] = lsbAddress; buffer [2] = msbAddress; buffer [3] = ( byte ) 0x0ff; adapter.dataBlock(buffer, 0, 4); return buffer [3]; } else throw new OneWireException("OneWireContainer21-Device not present."); } /** * <p>Gets the status of the specified flag from the specified register. * This method actually communicates with the Thermocron. To improve * performance if you intend to make multiple calls to this method, * first call <code>readDevice()</code> and use the * <code>getFlag(int, byte, byte[])</code> method instead.</p> * * <p>The DS1921 Thermocron has two sets of flags. One set belongs * to the control register. When reading from the control register, * valid values for <code>bitMask</code> are:</p> * <ul> * <li><code> TIMER_ALARM_SEARCH_FLAG </code></li> * <li><code> TEMP_HIGH_SEARCH_FLAG </code></li> * <li><code> TEMP_LOW_SEARCH_FLAG </code></li> * <li><code> ROLLOVER_ENABLE_FLAG </code></li> * <li><code> MISSION_ENABLE_FLAG </code></li> * <li><code> MEMORY_CLEAR_ENABLE_FLAG </code></li> * <li><code> OSCILLATOR_ENABLE_FLAG </code></li> * </ul> * <p>When reading from the status register, valid values * for <code>bitMask</code> are:</p> * <ul> * <li><code> TIMER_ALARM_FLAG </code></li> * <li><code> TEMPERATURE_HIGH_FLAG </code></li> * <li><code> TEMPERATURE_LOW_FLAG </code></li> * <li><code> SAMPLE_IN_PROGRESS_FLAG </code></li> * <li><code> MISSION_IN_PROGRESS_FLAG </code></li> * <li><code> MEMORY_CLEARED_FLAG </code></li> * <li><code> TEMP_CORE_BUSY_FLAG </code></li> * </ul> * * @param register address of register containing the flag (valid values * are <code>CONTROL_REGISTER</code> and <code>STATUS_REGISTER</code>) * @param bitMask the flag to read (see above for available options) * * @return the status of the flag, where <code>true</code> * signifies a "1" and <code>false</code> signifies a "0" * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from a 1-Wire device. 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 #getFlag(int,byte,byte[]) * @see #readDevice() * @see #setFlag(int,byte,boolean) * @see #TIMER_ALARM_SEARCH_FLAG * @see #TEMP_HIGH_SEARCH_FLAG * @see #TEMP_LOW_SEARCH_FLAG * @see #ROLLOVER_ENABLE_FLAG * @see #MISSION_ENABLE_FLAG * @see #MEMORY_CLEAR_ENABLE_FLAG * @see #OSCILLATOR_ENABLE_FLAG * @see #TIMER_ALARM_FLAG * @see #TEMPERATURE_HIGH_FLAG * @see #TEMPERATURE_LOW_FLAG * @see #SAMPLE_IN_PROGRESS_FLAG * @see #MISSION_IN_PROGRESS_FLAG * @see #MEMORY_CLEARED_FLAG * @see #TEMP_CORE_BUSY_FLAG * * */ public boolean getFlag (int register, byte bitMask) throws OneWireIOException, OneWireException { return ((readByte(register) & bitMask) != 0); } /** * <p>Gets the status of the specified flag from the specified register. * This method is the preferred manner of reading the control and * status flags.</p> * * <p>For more information on valid values for the <code>bitMask</code> * parameter, see the {@link #getFlag(int,byte) getFlag(int,byte)} method.</p> * * @param register address of register containing the flag (valid values * are <code>CONTROL_REGISTER</code> and <code>STATUS_REGISTER</code>) * @param bitMask the flag to read (see {@link #getFlag(int,byte) getFlag(int,byte)} * for available options) * @param state current state of the device returned from <code>readDevice()</code> * * @return the status of the flag, where <code>true</code> * signifies a "1" and <code>false</code> signifies a "0" * * @see #getFlag(int,byte) * @see #readDevice() * @see #setFlag(int,byte,boolean,byte[]) */ public boolean getFlag (int register, byte bitMask, byte[] state) { return ((state [register & 31] & bitMask) != 0); } /** * <p>Sets the status of the specified flag in the specified register. * If a mission is in progress a <code>OneWireIOException</code> will be thrown * (one cannot write to the registers while a mission is commencing). This method * actually communicates with the DS1921 Thermocron. To improve * performance if you intend to make multiple calls to this method, * first call <code>readDevice()</code> and use the * <code>setFlag(int,byte,boolean,byte[])</code> method instead.</p> * * <p>For more information on valid values for the <code>bitMask</code> * parameter, see the {@link #getFlag(int,byte) getFlag(int,byte)} method.</p> * * @param register address of register containing the flag (valid values * are <code>CONTROL_REGISTER</code> and <code>STATUS_REGISTER</code>) * @param bitMask the flag to read (see {@link #getFlag(int,byte) getFlag(int,byte)} * for available options) * @param flagValue new value for the flag (<code>true</code> is logic "1") * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from a 1-Wire device. 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'. * In the case of the DS1921 Thermocron, this could also be due to a * currently running mission. * @throws OneWireException on a communication or setup error with the 1-Wire * adapter * * @see #getFlag(int,byte) * @see #getFlag(int,byte,byte[]) * @see #setFlag(int,byte,boolean,byte[]) * @see #readDevice() */ public void setFlag (int register,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -