📄 onewirecontainer.java
字号:
} //-------- //-------- Device information methods //-------- /** * Retrieves the Dallas Semiconductor part number of the 1-Wire device * as a <code>String</code>. For example 'Crypto iButton' or 'DS1992'. * * @return 1-Wire device name */ public String getName () { synchronized (this) { return "Device type: " + (((address [0] & 0x0FF) < 16) ? ("0" + Integer.toHexString(address [0] & 0x0FF)) : Integer.toHexString(address [0] & 0x0FF)); } } /** * Retrieves the alternate Dallas Semiconductor part numbers or names. * A 'family' of 1-Wire Network devices may have more than one part number * depending on packaging. There can also be nicknames such as * 'Crypto iButton'. * * @return 1-Wire device alternate names */ public String getAlternateNames () { return ""; } /** * Retrieves a short description of the function of the 1-Wire device type. * * @return device functional description */ public String getDescription () { return "No description available."; } /** * Sets the maximum speed for this container. Note this may be slower then the * devices maximum speed. This method can be used by an application * to restrict the communication rate due 1-Wire line conditions. <p> * * @param newSpeed * <ul> * <li> 0 (SPEED_REGULAR) set to normal communciation speed * <li> 1 (SPEED_FLEX) set to flexible communciation speed used * for long lines * <li> 2 (SPEED_OVERDRIVE) set to normal communciation speed to * overdrive * <li> 3 (SPEED_HYPERDRIVE) set to normal communciation speed to * hyperdrive * <li> >3 future speeds * </ul> * * @param fallBack boolean indicating it is OK to fall back to a slower * speed if true * */ public void setSpeed (int newSpeed, boolean fallBack) { speed = newSpeed; speedFallBackOK = fallBack; } /** * Returns the maximum speed this iButton or 1-Wire device can * communicate at. * Override this method if derived iButton type can go faster then * SPEED_REGULAR(0). * * @return maximum speed * @see DSPortAdapter#setSpeed */ public int getMaxSpeed () { return adapter.SPEED_REGULAR; } /** * Gets the 1-Wire Network address of this device as an array of bytes. * * @return 1-Wire address * @see com.dalsemi.onewire.utils.Address */ public byte[] getAddress () { return address; } /** * Gets this device's 1-Wire Network address as a String. * * @return 1-Wire address * @see com.dalsemi.onewire.utils.Address */ public String getAddressAsString () { return Address.toString(address); } /** * Gets this device's 1-Wire Network address as a long. * * @return 1-Wire address * @see com.dalsemi.onewire.utils.Address */ public long getAddressAsLong () { return Address.toLong(address); } /** * Returns an <code>Enumeration</code> of <code>MemoryBank</code>. Default is no memory banks. * * @return enumeration of memory banks to read and write memory * on this iButton or 1-Wire device * @see MemoryBank */ public Enumeration getMemoryBanks () { return new Vector(0).elements(); } //-------- //-------- I/O Methods //-------- /** * Verifies that the iButton or 1-Wire device is present on * the 1-Wire Network. * * @return <code>true</code> if device present on the 1-Wire Network * * @throws OneWireIOException on a 1-Wire communication error such as * a read back verification fails. * @throws OneWireException if adapter is not open */ public boolean isPresent () throws OneWireIOException,OneWireException { synchronized (this) { return adapter.isPresent(address); } } /** * Verifies that the iButton or 1-Wire device is present * on the 1-Wire Network and in an alarm state. This does not * apply to all device types. * * @return <code>true</code> if device present and in alarm condition * * @throws OneWireIOException on a 1-Wire communication error such as * a read back verification fails. * @throws OneWireException if adapter is not open */ public boolean isAlarming () throws OneWireIOException,OneWireException { synchronized (this) { return adapter.isAlarming(address); } } /** * Go to the specified speed for this container. This method uses the * containers selected speed (method setSpeed(speed, fallback)) and * will optionally fall back to a slower speed if communciation failed. * Only call this method once to get the device into the desired speed * as long as the device is still responding. * * @throws OneWireIOException WHEN selected speed fails and fallback * is false * @throws OneWireException WHEN hypterdrive is selected speed * @see #setSpeed(int,boolean) */ public void doSpeed () throws OneWireIOException, OneWireException { boolean is_present = false; try { // check if already at speed and device present if ((speed == adapter.getSpeed()) && adapter.isPresent(address)) return; } catch (OneWireIOException e) { // VOID } // speed Overdrive if (speed == adapter.SPEED_OVERDRIVE) { try { // get this device and adapter to overdrive adapter.setSpeed(adapter.SPEED_REGULAR); adapter.reset(); adapter.putByte(( byte ) 0x69); adapter.setSpeed(adapter.SPEED_OVERDRIVE); } catch (OneWireIOException e) { // VOID } // get copy of address synchronized (this) { System.arraycopy(address, 0, addressCopy, 0, 8); adapter.dataBlock(addressCopy, 0, 8); } try { is_present = adapter.isPresent(address); } catch (OneWireIOException e) { // VOID } // check if new speed is OK if (!is_present) { // check if allow fallback if (speedFallBackOK) adapter.setSpeed(adapter.SPEED_REGULAR); else throw new OneWireIOException(address, "Failed to get device to selected speed (overdrive)"); } } // speed regular or flex else if ((speed == adapter.SPEED_REGULAR) || (speed == adapter.SPEED_FLEX)) adapter.setSpeed(speed); // speed hyperdrive, don't know how to do this else throw new OneWireException(address, "Speed selected (hyperdrive) is not supported by this method"); } //-------- //-------- Object Methods //-------- /** * Returns a hash code value for the object. This method is * supported for the benefit of hashtables such as those provided by * <code>java.util.Hashtable</code>. * * @return a hash code value for this object. * @see java.util.Hashtable */ public int hashCode() { if(this.address==null) return 0; else return (new Long(Address.toLong(this.address))).hashCode(); } /** * Indicates whether some other object is "equal to" this one. * @param obj the reference object with which to compare. * @return <code>true</code> if this object is the same as the obj * argument; <code>false</code> otherwise. */ public boolean equals(Object o) { if(o==this) return true; if(o instanceof OneWireContainer) { OneWireContainer owc = (OneWireContainer)o; // don't claim that all subclasses of a specific container are // equivalent to the parent container if(owc.getClass()==this.getClass()) return owc.getAddressAsLong()==this.getAddressAsLong(); } return false; } /** * Returns a string representation of the object. * * @return a string representation of the object. */ public String toString() { return Address.toString(this.address) + " " + this.getName(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -