📄 onewirecontainer41.java
字号:
/*--------------------------------------------------------------------------- * Copyright (C) 2002-2003 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 java.util.*;import java.lang.reflect.*;import com.dalsemi.onewire.utils.*;import com.dalsemi.onewire.*;import com.dalsemi.onewire.adapter.*;import com.dalsemi.onewire.debug.*;/** * <P> 1-Wire® container for a Temperature and Humidity/A-D Logging iButton, DS1922. * This container encapsulates the functionality of the 1-Wire family type <B>22</B> (hex). * </P> * * <H3> Features </H3> * <UL> * <LI> Logs up to 8192 consecutive temperature/humidity/A-D measurements in * nonvolatile, read-only memory * <LI> Real-Time clock * <LI> Programmable high and low temperature alarms * <LI> Programmable high and low humidity/A-D alarms * <LI> Automatically 'wakes up' and logs temperature at user-programmable intervals * <LI> 4096 bits of general-purpose read/write nonvolatile memory * <LI> 256-bit scratchpad ensures integrity of data transfer * <LI> On-chip 16-bit CRC generator to verify read operations * </UL> * * <H3> Memory </H3> * * <P> The memory can be accessed through the objects that are returned * from the {@link #getMemoryBanks() getMemoryBanks} method. </P> * * The following is a list of the MemoryBank instances that are returned: * * <UL> * <LI> <B> Scratchpad with CRC and Password support </B> * <UL> * <LI> <I> Implements </I> {@link com.dalsemi.onewire.container.MemoryBank MemoryBank}, * {@link com.dalsemi.onewire.container.PagedMemoryBank PagedMemoryBank} * <LI> <I> Size </I> 32 starting at physical address 0 * <LI> <I> Features</I> Read/Write not-general-purpose volatile * <LI> <I> Pages</I> 1 page of length 32 bytes * <LI> <I> Page Features </I> page-device-CRC * <li> <i> Extra information for each page</i> Target address, offset, length 3 * <LI> <i> Supports Copy Scratchpad With Password command </I> * </UL> * <LI> <B> Main Memory </B> * <UL> * <LI> <I> Implements </I> {@link com.dalsemi.onewire.container.MemoryBank MemoryBank}, * {@link com.dalsemi.onewire.container.PagedMemoryBank PagedMemoryBank} * <LI> <I> Size </I> 512 starting at physical address 0 * <LI> <I> Features</I> Read/Write general-purpose non-volatile * <LI> <I> Pages</I> 16 pages of length 32 bytes giving 29 bytes Packet data payload * <LI> <I> Page Features </I> page-device-CRC * <LI> <I> Read-Only and Read/Write password </I> if enabled, passwords are required for * reading from and writing to the device. * </UL> * <LI> <B> Register control </B> * <UL> * <LI> <I> Implements </I> {@link com.dalsemi.onewire.container.MemoryBank MemoryBank}, * {@link com.dalsemi.onewire.container.PagedMemoryBank PagedMemoryBank} * <LI> <I> Size </I> 64 starting at physical address 512 * <LI> <I> Features</I> Read/Write not-general-purpose non-volatile * <LI> <I> Pages</I> 2 pages of length 32 bytes * <LI> <I> Page Features </I> page-device-CRC * <LI> <I> Read-Only and Read/Write password </I> if enabled, passwords are required for * reading from and writing to the device. * </UL> * <LI> <B> Temperature/Humidity/A-D log </B> * <UL> * <LI> <I> Implements </I> {@link com.dalsemi.onewire.container.MemoryBank MemoryBank}, * {@link com.dalsemi.onewire.container.PagedMemoryBank PagedMemoryBank} * <LI> <I> Size </I> 8192 starting at physical address 4096 * <LI> <I> Features</I> Read-only not-general-purpose non-volatile * <LI> <I> Pages</I> 256 pages of length 32 bytes * <LI> <I> Page Features </I> page-device-CRC * <LI> <I> Read-Only and Read/Write password </I> if enabled, passwords are required for * reading from and writing to the device. * </UL> * </UL> * * <H3> Usage </H3> * * <p>The code below starts a mission with the following characteristics: * <ul> * <li>Rollover flag enabled.</li> * <li>Sets both channels (temperature and humidity) to low resolution</li> * <li>High temperature alarm of 28.0° and a low temperature alarm of 23.0° C.</li> * <li>High humidity alarm of 70%RH and a low temperature alarm of 20%RH.</li> * <li>Sets the Real-Time Clock to the host system's clock.</li> * <li>The mission will start in 2 minutes.</li> * <li>A sample rate of 1.5 minutes.</li> * </ul></p> * <pre><code> * // "ID" is a byte array of size 8 with an address of a part we * // have already found with family code 22 hex * // "access" is a DSPortAdapter * OneWireContainer41 ds1922 = (OneWireContainer41)access.getDeviceContainer(ID); * ds1922.setupContainer(access,ID); * // stop the currently running mission, if there is one * ds1922.stopMission(); * // clear the previous mission results * ds1922.clearMemory(); * // set the high temperature alarm to 28 C * ds1922.setMissionAlarm(ds1922.TEMPERATURE_CHANNEL, ds1922.ALARM_HIGH, 28); * ds1922.setMissionAlarmEnable(ds1922.TEMPERATURE_CHANNEL, * ds1922.ALARM_HIGH, true); * // set the low temperature alarm to 23 C * ds1922.setMissionAlarm(ds1922.TEMPERATURE_CHANNEL, ds1922.ALARM_LOW, 23); * ds1922.setMissionAlarmEnable(ds1922.TEMPERATURE_CHANNEL, * ds1922.ALARM_LOW, true); * // set the high humidity alarm to 70%RH * ds1922.setMissionAlarm(ds1922.DATA_CHANNEL, ds1922.ALARM_HIGH, 70); * ds1922.setMissionAlarmEnable(ds1922.DATA_CHANNEL, * ds1922.ALARM_HIGH, true); * // set the low humidity alarm to 20%RH * ds1922.setMissionAlarm(ds1922.DATA_CHANNEL, ds1922.ALARM_LOW, 20); * ds1922.setMissionAlarmEnable(ds1922.DATA_CHANNEL, * ds1922.ALARM_LOW, true); * // set both channels to low resolution. * ds1922.setMissionResolution(ds1922.TEMPERATURE_CHANNEL, * ds1922.getMissionResolutions()[0]); * ds1922.setMissionResolution(ds1922.DATA_CHANNEL, * ds1922.getMissionResolutions()[0]); * // enable both channels * boolean[] enableChannel = new boolean[ds1922.getNumberMissionChannels()]; * enableChannel[ds1922.TEMPERATURE_CHANNEL] = true; * enableChannel[ds1922.DATA_CHANNEL] = true; * // now start the mission with a sample rate of 1 minute * ds1922.startMission(90, 2, true, true, enableChannel); * </code></pre> * <p>The following code processes the mission log:</p> * <code><pre> * System.out.println("Temperature Readings"); * if(ds1922.getMissionChannelEnable(owc.TEMPERATURE_CHANNEL)) * { * int dataCount = * ds1922.getMissionSampleCount(ds1922.TEMPERATURE_CHANNEL); * System.out.println("SampleCount = " + dataCount); * for(int i=0; i<dataCount; i++) * { * System.out.println( * ds1922.getMissionSample(ds1922.TEMPERATURE_CHANNEL, i)); * } * } * System.out.println("Humidity Readings"); * if(ds1922.getMissionChannelEnable(owc.DATA_CHANNEL)) * { * int dataCount = * ds1922.getMissionSampleCount(ds1922.DATA_CHANNEL); * System.out.println("SampleCount = " + dataCount); * for(int i=0; i<dataCount; i++) * { * System.out.println( * ds1922.getMissionSample(ds1922.DATA_CHANNEL, i)); * } * } * </pre></code> * * <p>Also see the usage examples in the {@link com.dalsemi.onewire.container.TemperatureContainer TemperatureContainer} * and {@link com.dalsemi.onewire.container.ClockContainer ClockContainer} * and {@link com.dalsemi.onewire.container.ADContainer ADContainer} * interfaces.</p> * * For examples regarding memory operations, * <uL> * <li> See the usage example in * {@link com.dalsemi.onewire.container.OneWireContainer OneWireContainer} * to enumerate the MemoryBanks. * <li> See the usage examples in * {@link com.dalsemi.onewire.container.MemoryBank MemoryBank} and * {@link com.dalsemi.onewire.container.PagedMemoryBank PagedMemoryBank} * for bank specific operations. * </uL> * * <H3> DataSheet </H3> * <P>DataSheet link is unavailable at time of publication. Please visit the website * and search for DS1922 or DS2422 to find the current datasheet. * <DL> * <DD><A HREF="http://www.maxim-ic.com/">Maxim Website</A> * </DL> * * @see com.dalsemi.onewire.container.OneWireSensor * @see com.dalsemi.onewire.container.SwitchContainer * @see com.dalsemi.onewire.container.TemperatureContainer * @see com.dalsemi.onewire.container.ADContainer * @see com.dalsemi.onewire.container.MissionContainer * @see com.dalsemi.onewire.container.PasswordContainer * * @version 1.00, 1 June 2003 * @author shughes * */public class OneWireContainer41 extends OneWireContainer implements PasswordContainer, MissionContainer, ClockContainer, TemperatureContainer, ADContainer, HumidityContainer{ // enables/disables debugging private static final boolean DEBUG = false; // when reading a page, the memory bank may throw a crc exception if the device // is sampling or starts sampling during the read. This value sets how many // times the device retries before passing the exception on to the application. private static final int MAX_READ_RETRY_CNT = 15; // the length of the Read-Only and Read/Write password registers private static final int PASSWORD_LENGTH = 8; // indicates whether or not the device configuration has been read // and all the ranges for the part have been set. private boolean isContainerVariablesSet = false; // memory bank for scratchpad private MemoryBankScratchCRCPW scratch = null; // memory bank for general-purpose user data private MemoryBankNVCRCPW userDataMemory = null; // memory bank for control register private MemoryBankNVCRCPW register = null; // memory bank for mission log private MemoryBankNVCRCPW log = null; // Maxim/Dallas Semiconductor Part number private String partNumber = null; // Temperature range low temperaturein degrees Celsius private double temperatureRangeLow = -40.0; // Temperature range width in degrees Celsius private double temperatureRangeWidth = 125.0; // Temperature resolution in degrees Celsius private double temperatureResolution = 0.5; // A-D Reference voltage private double adReferenceVoltage = 5.02d; // Number of valid bits in A-D Result private int adDeviceBits = 10; // Force mission results to return value as A-D, not humidity private boolean adForceResults = false; // should we update the Real time clock? private boolean updatertc = false; // should we check the speed private boolean doSpeedEnable = true; /** The current password for readingfrom this device.*/ private final byte[] readPassword = new byte[8]; private boolean readPasswordSet = false; private boolean readOnlyPasswordEnabled = false; /** The current password for reading/writing from/to this device. */ private final byte[] readWritePassword = new byte[8]; private boolean readWritePasswordSet = false; private boolean readWritePasswordEnabled = false; /** indicates whether or not the results of a mission are successfully loaded */ private boolean isMissionLoaded = false; /** holds the missionRegister, which details the status of the current mission */ private byte[] missionRegister = null; /** The mission logs */ private byte[] dataLog = null, temperatureLog = null; /** Number of bytes used to store temperature values (0, 1, or 2) */ private int temperatureBytes = 0; /** Number of bytes used to stroe data valuas (0, 1, or 2) */ private int dataBytes = 0; /** indicates whether or not the log has rolled over */ private boolean rolledOver = false; /** start time offset for the first sample, if rollover occurred */ private int timeOffset = -1; /** the time (unix time) when mission started */ private long missionTimeStamp = -1; /** The rate at which samples are taken, and the number of samples */ private int sampleRate = -1, sampleCount = -1; /** total number of samples, including rollover */ private int sampleCountTotal; // indicates whether or not to use calibration for the humidity values private boolean useHumdCalibrationRegisters = false; // reference humidities that the calibration was calculated over private double Href1 = 20, Href2 = 60, Href3 = 90; // the average value for each reference point private double Hread1 = 0, Hread2 = 0, Hread3 = 0; // the average error for each reference point private double Herror1 = 0, Herror2 = 0, Herror3 = 0; // the coefficients for calibration private double humdCoeffA, humdCoeffB, humdCoeffC; // indicates whether or not to use calibration for the temperature values private boolean useTempCalibrationRegisters = false; // reference temperatures that the calibration was calculated over private double Tref1 = 0, Tref2 = 0, Tref3 = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -