📄 onewirecontainer1f.java
字号:
* @throws OneWireException on a communication or setup error with the 1-Wire * adapter */ public byte[] readDevice () throws OneWireIOException, OneWireException { byte[] ret_buf = new byte [4]; if (doSpeedEnable) doSpeed(); // read the status byte byte[] tmp_buf = deviceOperation(READ_WRITE_STATUS_COMMAND, ( byte ) 0x00FF, 2); // extract the status byte ret_buf [0] = tmp_buf [2]; return ret_buf; } /** * Writes the 1-Wire device sensor state that * have been changed by 'set' methods. Only the state registers that * changed are updated. This is done by referencing a field information * appended to the state data. * * @param state 1-Wire device sensor state * * @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 */ public void writeDevice (byte[] state) throws OneWireIOException, OneWireException { int extra = 0; byte command, first_byte; byte[] tmp_buf = null; if (doSpeedEnable) doSpeed(); // check for both switches set to on if ((Bit.arrayReadBit(MAIN_OFFSET, BITMAP_OFFSET, state) == 1) && (Bit.arrayReadBit(AUX_OFFSET, BITMAP_OFFSET, state) == 1)) { if ((state [MAIN_OFFSET] != SWITCH_OFF) && (state [AUX_OFFSET] != SWITCH_OFF)) throw new OneWireException(address, "Attempting to set both channels on, only single channel on at a time"); } // check if need to set control if (Bit.arrayReadBit(STATUS_OFFSET, BITMAP_OFFSET, state) == 1) { // create a command based on bit 6/7 of status first_byte = 0; // mode bit if (Bit.arrayReadBit(7, STATUS_OFFSET, state) == 1) first_byte |= ( byte ) 0x20; // Control output if (Bit.arrayReadBit(6, STATUS_OFFSET, state) == 1) first_byte |= ( byte ) 0xC0; tmp_buf = deviceOperation(READ_WRITE_STATUS_COMMAND, first_byte, 2); state [0] = ( byte ) tmp_buf [2]; } // check for AUX state change command = 0; if (Bit.arrayReadBit(AUX_OFFSET, BITMAP_OFFSET, state) == 1) { if ((state [AUX_OFFSET] == SWITCH_ON) || (state [AUX_OFFSET] == SWITCH_SMART)) { command = SMART_ON_AUX_COMMAND; extra = 2; } else { command = ALL_LINES_OFF_COMMAND; extra = 0; } } // check for MAIN state change if (Bit.arrayReadBit(MAIN_OFFSET, BITMAP_OFFSET, state) == 1) { if (state [MAIN_OFFSET] == SWITCH_ON) { command = DIRECT_ON_MAIN_COMMAND; extra = 0; } else if (state [MAIN_OFFSET] == SWITCH_SMART) { command = SMART_ON_MAIN_COMMAND; extra = 2; } else { command = ALL_LINES_OFF_COMMAND; extra = 0; } } // check if there are events to clear and not about to do clear anyway if ((clearActivityOnWrite) && (command != ALL_LINES_OFF_COMMAND)) { if ((Bit.arrayReadBit(4, STATUS_OFFSET, state) == 1) || (Bit.arrayReadBit(5, STATUS_OFFSET, state) == 1)) { // clear the events deviceOperation(ALL_LINES_OFF_COMMAND, ( byte ) 0xFF, 0); // set the channels back to the correct state if (command == 0) { if (Bit.arrayReadBit(0, STATUS_OFFSET, state) == 0) command = SMART_ON_MAIN_COMMAND; else if (Bit.arrayReadBit(2, STATUS_OFFSET, state) == 0) command = SMART_ON_AUX_COMMAND; extra = 2; } } } // check if there is a command to send if (command != 0) tmp_buf = deviceOperation(command, ( byte ) 0xFF, extra); // if doing a SMART_ON, then look at result data for presence if ((command == SMART_ON_MAIN_COMMAND) || (command == SMART_ON_AUX_COMMAND)) { // devices on branch indicated if 3rd byte is 0 devicesOnBranch = (tmp_buf[2] == 0); } else devicesOnBranch = false; // clear clear activity on write clearActivityOnWrite = false; // clear the bitmap state [BITMAP_OFFSET] = 0; } /** * <P>Force a power-on reset for parasitically powered 1-Wire * devices connected to the main or auziliary output of the DS2409. </P> * * <P>IMPORTANT: the duration of the discharge time should be 100ms minimum.</P> <BR> * * @param time number of milliseconds the lines are * to be discharged for (minimum 100) * * @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 */ public void dischargeLines (int time) throws OneWireIOException, OneWireException { // Error checking if (time < 100) time = 100; if (doSpeedEnable) doSpeed(); // discharge the lines deviceOperation(DISCHARGE_COMMAND, ( byte ) 0xFF, 0); // wait for desired time and return. try { Thread.sleep(time); } catch (InterruptedException e) { // DRAIN } // clear the discharge deviceOperation(READ_WRITE_STATUS_COMMAND, ( byte ) 0x00FF, 2); } //-------- //-------- Switch Feature methods //-------- /** * Checks to see if the channels of this switch are 'high side' * switches. This indicates that when 'on' or <code>true</code>, the switch output is * connect to the 1-Wire data. If this method returns <code>false</code> * then when the switch is 'on' or <code>true</code>, the switch is connected * to ground. * * @return <code>true</code> if the switch is a 'high side' switch, * <code>false</code> if the switch is a 'low side' switch * * @see #getLatchState(int,byte[]) */ public boolean isHighSideSwitch () { return true; } /** * Checks to see if the channels of this switch support * activity sensing. If this method returns <code>true</code> then the * method <code>getSensedActivity(int,byte[])</code> can be used. * * @return <code>true</code> if channels support activity sensing * * @see #getSensedActivity(int,byte[]) * @see #clearActivity() */ public boolean hasActivitySensing () { return true; } /** * Checks to see if the channels of this switch support * level sensing. If this method returns <code>true</code> then the * method <code>getLevel(int,byte[])</code> can be used. * * @return <code>true</code> if channels support level sensing * * @see #getLevel(int,byte[]) */ public boolean hasLevelSensing () { return true; } /** * Checks to see if the channels of this switch support * 'smart on'. Smart on is the ability to turn on a channel * such that only 1-Wire device on this channel are awake * and ready to do an operation. This greatly reduces * the time to discover the device down a branch. * If this method returns <code>true</code> then the * method <code>setLatchState(int,boolean,boolean,byte[])</code> * can be used with the <code>doSmart</code> parameter <code>true</code>. * * @return <code>true</code> if channels support 'smart on' * * @see #setLatchState(int,boolean,boolean,byte[]) */ public boolean hasSmartOn () { return true; } /** * Checks to see if the channels of this switch require that only one * channel is on at any one time. If this method returns <code>true</code> then the * method <code>setLatchState(int,boolean,boolean,byte[])</code> * will not only affect the state of the given * channel but may affect the state of the other channels as well * to insure that only one channel is on at a time. * * @return <code>true</code> if only one channel can be on at a time. * * @see #setLatchState(int,boolean,boolean,byte[]) */ public boolean onlySingleChannelOn () { return true; } //-------- //-------- Switch 'get' Methods //-------- /** * Query to get the number of channels supported by this switch. * Channel specific methods will use a channel number specified * by an integer from [0 to (<code>getNumberChannels(byte[])</code> - 1)]. Note that * all devices of the same family will not necessarily have the * same number of channels. The DS2406 comes in two packages--one that * has a single channel, and one that has two channels. * * @param state current state of the device returned from <code>readDevice()</code> * * @return the number of channels for this device */ public int getNumberChannels (byte[] state) { return 2; } /** * Checks the sensed level on the indicated channel. * To avoid an exception, verify that this switch * has level sensing with the <code>hasLevelSensing()</code>. * Level sensing means that the device can sense the logic * level on its PIO pin. * * @param channel channel to execute this operation, in the range [0 to (<code>getNumberChannels(byte[])</code> - 1)] * @param state current state of the device returned from <code>readDevice()</code> * * @return <code>true</code> if level sensed is 'high' and <code>false</code> if level sensed is 'low'
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -