onewirecontainer05.java
来自「这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统」· Java 代码 · 共 543 行 · 第 1/2 页
JAVA
543 行
*/ public boolean hasActivitySensing () { return false; } /** * Checks 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 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 false; } /** * Checks 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 //-------- /** * 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' * * @see com.dalsemi.onewire.container.OneWireSensor#readDevice() * @see #hasLevelSensing() */ public boolean getLevel (int channel, byte[] state) { return ((state [0] & 0x02) == 0x02); } /** * Checks the latch state of the indicated channel. * * @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 channel latch is 'on' * or conducting and <code>false</code> if channel latch is 'off' and not * conducting. Note that the actual output when the latch is 'on' * is returned from the <code>isHighSideSwitch()</code> method. * * @see com.dalsemi.onewire.container.OneWireSensor#readDevice() * @see #isHighSideSwitch() * @see #setLatchState(int,boolean,boolean,byte[]) */ public boolean getLatchState (int channel, byte[] state) { return ((state [0] & 0x01) == 0x01); } /** * Checks if the indicated channel has experienced activity. * This occurs when the level on the PIO pins changes. To clear * the activity that is reported, call <code>clearActivity()</code>. * To avoid an exception, verify that this device supports activity * sensing by calling the method <code>hasActivitySensing()</code>. * * @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 activity was detected and <code>false</code> if no activity was detected * * @throws OneWireException if this device does not have activity sensing * * @see #hasActivitySensing() * @see #clearActivity() */ public boolean getSensedActivity (int channel, byte[] state) throws OneWireException { //i don't do this throw new OneWireException("Sense Activity not supported"); } /** * Clears the activity latches the next time possible. For * example, on a DS2406/07, this happens the next time the * status is read with <code>readDevice()</code>. * * @throws OneWireException if this device does not support activity sensing * * @see com.dalsemi.onewire.container.OneWireSensor#readDevice() * @see #getSensedActivity(int,byte[]) */ public void clearActivity () throws OneWireException { //i don't do this throw new OneWireException("Sense Activity not supported"); } //-------- //-------- Switch 'set' Methods //-------- /** * Sets the latch state of the indicated channel. * The method <code>writeDevice()</code> must be called to finalize * changes to the device. Note that multiple 'set' methods can * be called before one call to <code>writeDevice()</code>. * * @param channel channel to execute this operation, in the range [0 to (<code>getNumberChannels(byte[])</code> - 1)] * @param latchState <code>true</code> to set the channel latch 'on' * (conducting) and <code>false</code> to set the channel latch 'off' (not * conducting). Note that the actual output when the latch is 'on' * is returned from the <code>isHighSideSwitch()</code> method. * @param doSmart If latchState is 'on'/<code>true</code> then doSmart indicates * if a 'smart on' is to be done. To avoid an exception * check the capabilities of this device using the * <code>hasSmartOn()</code> method. * @param state current state of the device returned from <code>readDevice()</code> * * @see #hasSmartOn() * @see #getLatchState(int,byte[]) * @see com.dalsemi.onewire.container.OneWireSensor#writeDevice(byte[]) */ public void setLatchState (int channel, boolean latchState, boolean doSmart, byte[] state) { if (latchState) state [0] = ( byte ) (state [0] | 0x01); else state [0] = ( byte ) (state [0] & 0xfe); } /** * Retrieves the 1-Wire device sensor state. This state is * returned as a byte array. Pass this byte array to the 'get' * and 'set' methods. If the device state needs to be changed then call * the 'writeDevice' to finalize the changes. * * @return 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 byte[] readDevice () throws OneWireIOException, OneWireException { //first let's make sure we can talk to the part //speed is not critical with the 2405 so i'll just call doSpeed() doSpeed(); //this ain't a hard part--it's only gonna take 1 byte byte[] state = new byte [1]; //here's the 'bitmap' //bit 0 : switch state (0 for conducting, 1 for non-conducting) //bit 1 : sensed level (0 for low, 1 for high) state [0] = ( byte ) 0; if (isPresent()) { if (isAlarming()) state [0] = 1; } else throw new OneWireIOException("Device not present"); if (isPresent()) { // Byte after 'search' indicates level if (adapter.getByte() != 0) state [0] = ( byte ) (state [0] | 0x02); } else throw new OneWireIOException("Device not present"); return state; } /** * 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 { doSpeed(); boolean value = ((state [0] & 0x01) == 0x01); boolean compare = isAlarming(); // check to see if already in the correct state if (compare == value) return; // incorrect state so toggle else if (adapter.select(address)) { // verify compare = isAlarming(); if (compare == value) return; } throw new OneWireIOException("Failure to change DS2405 latch state"); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?