📄 onewirecontainer.java
字号:
/*--------------------------------------------------------------------------- * Copyright (C) 1999,2000 Dallas Semiconductor Corporation, All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * Except as contained in this notice, the name of Dallas Semiconductor * shall not be used except as stated in the Dallas Semiconductor * Branding Policy. *--------------------------------------------------------------------------- */package com.dalsemi.onewire.container;// importsimport com.dalsemi.onewire.utils.Address;import java.util.Enumeration;import java.util.Vector;import com.dalsemi.onewire.adapter.*;import com.dalsemi.onewire.OneWireException;/** * A <code>OneWireContainer</code> encapsulates the <code>DSPortAdapter</code>, * the 1-Wire® network address, and methods to manipulate a specific 1-Wire device. A * 1-Wire device may be in the form of a stainless steel armored can, called an iButton®, * or in standard IC plastic packaging. * * <p>General 1-Wire device container class with basic communication functions. * This class should only be used if a device specific class is not available * or known. Most <code>OneWireContainer</code> classes will extend this basic class. * * <P> 1-Wire devices with memory can be accessed through the objects that * are returned from the {@link #getMemoryBanks() getMemoryBanks} method. See the * usage example below. </P> * * <H3> Usage </H3> * * <DL> * <DD> <H4> Example 1</H4> * Enumerate memory banks retrieved from the OneWireContainer * instance 'owd' and cast to the highest interface. See the * interface descriptions * {@link com.dalsemi.onewire.container.MemoryBank MemoryBank}, * {@link com.dalsemi.onewire.container.PagedMemoryBank PagedMemoryBank}, and * {@link com.dalsemi.onewire.container.PagedMemoryBank OTPMemoryBank} * for specific examples. * <PRE> <CODE> * MemoryBank mb; * PagedMemoryBank pg_mb; * OTPMemoryBank otp_mb; * * for(Enumeration bank_enum = owd.getMemoryBanks(); * bank_enum.hasMoreElements(); ) * { * // get the next memory bank, cast to MemoryBank * mb = (MemoryBank)bank_enum.nextElement(); * * // check if has paged services * if (mb instanceof PagedMemoryBank) * pg_mb = (PagedMemoryBank)mb; * * // check if has One-Time-Programable services * if (mb instanceof OTPMemoryBank) * otp_mb = (OTPMemoryBank)mb; * } * </CODE> </PRE> * </DL> * * @see com.dalsemi.onewire.container.MemoryBank * @see com.dalsemi.onewire.container.PagedMemoryBank * @see com.dalsemi.onewire.container.OTPMemoryBank * * @version 0.00, 28 Aug 2000 * @author DS */public class OneWireContainer{ //-------- //-------- Variables //-------- /** * Reference to the adapter that is needed to communicate with this * iButton or 1-Wire device. */ protected DSPortAdapter adapter; /** * 1-Wire Network Address of this iButton or 1-Wire * device. * Family code is byte at offset 0. * @see com.dalsemi.onewire.utils.Address */ protected byte[] address; /** * Temporary copy of 1-Wire Network Address of this * iButton or 1-Wire device. * @see com.dalsemi.onewire.utils.Address */ private byte[] addressCopy; /** * Communication speed requested. * <ul> * <li> 0 (SPEED_REGULAR) * <li> 1 (SPEED_FLEX) * <li> 2 (SPEED_OVERDRIVE) * <li> 3 (SPEED_HYPERDRIVE) * <li> >3 future speeds * </ul> * * @see DSPortAdapter#setSpeed */ protected int speed; /** * Flag to indicate that falling back to a slower speed then requested * is OK. */ protected boolean speedFallBackOK; //-------- //-------- Constructors //-------- /** * Create an empty container. Must call <code>setupContainer</code> before * using this new container.<p> * * This is one of the methods to construct a container. The others are * through creating a OneWireContainer with parameters. * * @see #OneWireContainer(DSPortAdapter,byte[]) * @see #OneWireContainer(DSPortAdapter,long) * @see #OneWireContainer(DSPortAdapter,String) * @see #setupContainer(DSPortAdapter,byte[]) * @see #setupContainer(DSPortAdapter,long) * @see #setupContainer(DSPortAdapter,String) */ public OneWireContainer () { } /** * Create a container with a provided adapter object * and the address of the iButton or 1-Wire device.<p> * * This is one of the methods to construct a container. The other is * through creating a OneWireContainer with NO parameters. * * @param sourceAdapter adapter object required to communicate with * this iButton. * @param newAddress address of this 1-Wire device * @see #OneWireContainer() * @see com.dalsemi.onewire.utils.Address */ public OneWireContainer (DSPortAdapter sourceAdapter, byte[] newAddress) { this.setupContainer(sourceAdapter, newAddress); } /** * Create a container with a provided adapter object * and the address of the iButton or 1-Wire device.<p> * * This is one of the methods to construct a container. The other is * through creating a OneWireContainer with NO parameters. * * @param sourceAdapter adapter object required to communicate with * this iButton. * @param newAddress address of this 1-Wire device * @see #OneWireContainer() * @see com.dalsemi.onewire.utils.Address */ public OneWireContainer (DSPortAdapter sourceAdapter, long newAddress) { this.setupContainer(sourceAdapter, newAddress); } /** * Create a container with a provided adapter object * and the address of the iButton or 1-Wire device.<p> * * This is one of the methods to construct a container. The other is * through creating a OneWireContainer with NO parameters. * * @param sourceAdapter adapter object required to communicate with * this iButton. * @param newAddress address of this 1-Wire device * @see #OneWireContainer() * @see com.dalsemi.onewire.utils.Address */ public OneWireContainer (DSPortAdapter sourceAdapter, String newAddress) { this.setupContainer(sourceAdapter, newAddress); } //-------- //-------- Setup and adapter methods //-------- /** * Provides this container with the adapter object used to access this device and * the address of the iButton or 1-Wire device. * * @param sourceAdapter adapter object required to communicate with * this iButton * @param newAddress address of this 1-Wire device * @see com.dalsemi.onewire.utils.Address */ public void setupContainer (DSPortAdapter sourceAdapter, byte[] newAddress) { // get a reference to the source adapter (will need this to communicate) adapter = sourceAdapter; // set the Address synchronized (this) { address = new byte [8]; addressCopy = new byte [8]; System.arraycopy(newAddress, 0, address, 0, 8); } // set desired speed to be SPEED_REGULAR by default with no fallback speed = adapter.SPEED_REGULAR; speedFallBackOK = false; } /** * Provides this container with the adapter object used to access this device and * the address of the iButton or 1-Wire device. * * @param sourceAdapter adapter object required to communicate with * this iButton * @param newAddress address of this 1-Wire device * @see com.dalsemi.onewire.utils.Address */ public void setupContainer (DSPortAdapter sourceAdapter, long newAddress) { // get a reference to the source adapter (will need this to communicate) adapter = sourceAdapter; // set the Address synchronized (this) { address = Address.toByteArray(newAddress); addressCopy = new byte [8]; } // set desired speed to be SPEED_REGULAR by default with no fallback speed = adapter.SPEED_REGULAR; speedFallBackOK = false; } /** * Provides this container with the adapter object used to access this device and * the address of the iButton or 1-Wire device. * * @param sourceAdapter adapter object required to communicate with * this iButton * @param newAddress address of this 1-Wire device * @see com.dalsemi.onewire.utils.Address */ public void setupContainer (DSPortAdapter sourceAdapter, String newAddress) { // get a reference to the source adapter (will need this to communicate) adapter = sourceAdapter; // set the Address synchronized (this) { address = Address.toByteArray(newAddress); addressCopy = new byte [8]; } // set desired speed to be SPEED_REGULAR by default with no fallback speed = adapter.SPEED_REGULAR; speedFallBackOK = false; } /** * Retrieves the port adapter object used to create this container. * * @return port adapter instance */ public DSPortAdapter getAdapter () { return adapter;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -