📄 onewirecontainer20.java
字号:
// OneWireContainer20.java - OneWireContainer for family code 20 Devices.
//
// Copyright (C) 1999-2002 Smart Software Consulting
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// Smart Software Consulting
// 1688 Silverwood Court
// Danville, CA 94526-3079
// USA
//
// http://www.smartsc.com
//
package com.smartsc.onewire;
import com.dalsemi.onewire.OneWireException;
import com.dalsemi.onewire.adapter.DSPortAdapter;
import com.dalsemi.onewire.adapter.OneWireIOException;
import com.dalsemi.onewire.container.OneWireContainer;
import com.dalsemi.onewire.utils.CRC16;
public class OneWireContainer20
extends OneWireContainer
{
// Overriden methods for more informative results
public String getName()
{
return new String( "DS2450");
}
public String getAlternateNames()
{
return new String( "1-Wire Quad A/D Converter");
}
public String getDescription()
{
return new String( "1-Wire Quad A/D Converter to measure four " +
"high impedence inputs with a user selectable range of " +
"of 2.56V and 5.12V with a resolution of 1 to 16 bits.");
}
public void setupContainer( DSPortAdapter adapter, byte[] iBId)
{
super.setupContainer( adapter, iBId);
init();
}
protected void init()
{
// Init memory array to power-on values
for( int i = 0; i < 8; i += 2)
{
memory[0][i] = (byte)0x00;
memory[0][i+1] = (byte)0x00;
memory[1][i] = (byte)0x08;
memory[1][i+1] = (byte)0x8c;
memory[2][i] = (byte)0x00;
memory[2][i+1] = (byte)0xff;
memory[3][i] = (byte)0x00;
memory[3][i+1] = (byte)0x00;
}
// Init CRCs
memory[0][8] = (byte)0xdc;
memory[0][9] = (byte)0x25;
memory[1][8] = (byte)0x66;
memory[1][9] = (byte)0xe8;
memory[2][8] = (byte)0x94;
memory[2][9] = (byte)0x94;
memory[3][8] = (byte)0xff;
memory[3][9] = (byte)0xff;
}
public void readMemory()
throws OneWireException
{
byte[] cmd = { READ_MEMORY, 0, 0 };
byte[] cmd1 = { READ_MEMORY, 0, 0 };
int crc = CRC16.compute( cmd, 0, cmd.length, 0);
// Send MATCH ROM command to select this device. If device is no
// longer present, no exception will occur until the first CRC check.
adapter.select( getAddress());
adapter.dataBlock( cmd, 0, cmd.length);
for( int i = 0; i < memory.length; ++i)
{
memory[i] = adapter.getBlock( 8 + 2);
if( CRC16.compute( memory[i], 0, memory[i].length, crc) != 0xb001)
{
adapter.reset();
throw new OneWireIOException( "CRC error reading memory.");
}
crc = 0;
}
adapter.reset();
}
public void writeMemory()
throws OneWireException
{
// Send MATCH ROM command to select this device. If device is no
// longer present, no exception will occur until the first CRC check.
adapter.select( getAddress());
// Send Write Memory command
adapter.putByte( WRITE_MEMORY);
int crc = CRC16.compute( WRITE_MEMORY, 0);
// Send address
byte address = 8;
adapter.putByte( address);
crc = CRC16.compute( address, crc);
adapter.putByte( (byte)0);
crc = CRC16.compute( 0, crc);
for( int page = 1; page < 3; ++page)
{
for( int idx = 0; idx < 8; ++idx)
{
// Put memory byte and do CRC for it
adapter.putByte( memory[page][idx]);
crc = CRC16.compute( memory[page][idx], crc);
// Read CRC16 from device and do CRC for it
if( CRC16.compute( adapter.getBlock( 2), 0, 2, crc) != 0xb001)
{
adapter.reset();
throw new OneWireIOException( "CRC error writing memory.");
}
// Read back byte for confirmation
byte confirmByte = (byte)adapter.getByte();
// Validate confirmation
if( memory[page][idx] != confirmByte)
{
adapter.reset();
throw new OneWireIOException(
"Byte contents not confirmed writing memory.");
}
// Re-init CRC by
// "loading (not shifting) the new (incremented) address"
crc = ++address;
}
}
adapter.reset();
}
public void convert(
boolean convertA, boolean convertB,boolean convertC, boolean convertD)
throws OneWireException
{
// Build input select mask and calculate conversion time
char inputSelectMask = 0;
int uSecConversionTime = 160;
if( convertA)
{
inputSelectMask |= 1;
uSecConversionTime += 80 * getResolution( 0);
}
if( convertB)
{
inputSelectMask |= 2;
uSecConversionTime += 80 * getResolution( 1);
}
if( convertC)
{
inputSelectMask |= 4;
uSecConversionTime += 80 * getResolution( 2);
}
if( convertD)
{
inputSelectMask |= 8;
uSecConversionTime += 80 * getResolution( 3);
}
if( inputSelectMask == 0)
{
return;
}
// Create command block and CRC16
byte[] commandBlock = { (byte)CONVERT, (byte)inputSelectMask, 0 };
// Send MATCH ROM command to select this device. If device is no
// longer present, no exception will occur until the first CRC check.
adapter.select( getAddress());
// Send command block
adapter.dataBlock( commandBlock, 0, 3);
int crc = CRC16.compute( commandBlock, 0, commandBlock.length, 0);
// Read first byte of CRC from device
crc = CRC16.compute( adapter.getByte(), crc);
// Start strong pull-up (or at least try to).
// This must/should happen within 10 microseconds of reading
// the last byte of the CRC.
try
{
adapter.setPowerDuration( DSPortAdapter.DELIVERY_INFINITE);
adapter.startPowerDelivery( DSPortAdapter.CONDITION_AFTER_BYTE);
}
catch( OneWireException ibe)
{
System.out.println( "Unable to deliver power.");
}
// Read second (last) byte of CRC from device
crc = CRC16.compute( adapter.getByte(), crc);
// Validate CRC
if( crc != 0xb001)
{
// Disable strong pull-up
adapter.setPowerNormal();
adapter.reset();
throw new OneWireIOException(
"CRC error issuing CONVERT command.");
}
// Wait for conversion
try { Thread.sleep( (uSecConversionTime / 1000) + 1); }
catch( InterruptedException ie) {}
// Disable strong pull-up
adapter.setPowerNormal();
// Done
adapter.reset();
}
protected int scaleValue16( int channel, int value16)
{
int resolution = getResolution( channel);
int range = uVoltRanges[getInputRange(channel)];
int scaledValue = value16 >>> (16 - resolution);
scaledValue = (int)(((long)scaledValue * range) / (1 << resolution));
return scaledValue;
}
protected int unscaleValue16( int channel, int scaledValue)
{
int resolution = getResolution( channel);
int range = uVoltRanges[getInputRange(channel)];
int value16 = (int)(((long)scaledValue * (1 << resolution)) / range);
value16 <<= 16 - resolution;
return value16 & 0xffff;
}
public int getCurrentValue( int channel)
{
int value16 = ((memory[0][2*channel+1] & 0xff) << 8)
+ (memory[0][2*channel] & 0xff);
return scaleValue16( channel, value16);
}
public int getInputRange( int channel)
{
return memory[1][2*channel+1] & 1;
}
public void setInputRange( int channel, int inputRange)
{
memory[1][2*channel+1] = (byte)
((memory[1][2*channel+1] & INPUT_RANGE_MASK) | (inputRange & 1));
}
public int getOutputControl( int channel)
{
return (memory[1][2*channel] >>> 6) & 3;
}
public void setOutputControl( int channel, int outputControl)
{
memory[1][2*channel] = (byte)
((memory[1][2*channel] & OUTPUT_CONTROL_MASK)
| ((outputControl & 3) << 6) );
}
public int getResolution( int channel)
{
int resolution = memory[1][2*channel] & 0x0f;
if( resolution == 0)
{
resolution = 16;
}
return resolution;
}
public void setResolution( int channel, int resolution)
{
memory[1][2*channel] = (byte)
((memory[1][2*channel] & RESOLUTION_MASK)
| (resolution & 0x0f) );
}
public boolean getAlarmHigh( int channel)
{
return ((memory[1][2*channel+1] >>> 5) & 1) == 1;
}
public void setAlarmHigh( int channel, boolean enable)
{
memory[1][2*channel+1] &= ALARM_HIGH_MASK;
if( enable)
{
memory[1][2*channel+1] |= ~ALARM_HIGH_MASK;
}
}
public boolean getAlarmHighEnable( int channel)
{
return ((memory[1][2*channel+1] >>> 3) & 1) == 1;
}
public void setAlarmHighEnable( int channel, boolean enable)
{
memory[1][2*channel+1] &= ALARM_HIGH_ENABLE_MASK;
if( enable)
{
memory[1][2*channel+1] |= ~ALARM_HIGH_ENABLE_MASK;
}
}
public int getAlarmHighValue( int channel)
{
int value16 = (memory[2][2*channel+1] & 0xff) << 8;
return scaleValue16( channel, value16);
}
public void setAlarmHighValue( int channel, int uVolts)
{
if( uVolts < 0)
{
memory[2][2*channel+1] = 0;
}
else if( uVolts >= uVoltRanges[ getInputRange( channel)])
{
memory[2][2*channel+1] = (byte)0xff;
int resolution = getResolution( channel);
if( resolution < 8)
{
memory[2][2*channel+1] &= ~((1 << (8 - resolution)) - 1);
}
}
else
{
memory[2][2*channel+1] = (byte)
(unscaleValue16( channel, uVolts) >>> 8);
}
}
public boolean getAlarmLow( int channel)
{
return ((memory[1][2*channel+1] >>> 4) & 1) == 1;
}
public void setAlarmLow( int channel, boolean enable)
{
memory[1][2*channel+1] &= ALARM_LOW_MASK;
if( enable)
{
memory[1][2*channel+1] |= ~ALARM_LOW_MASK;
}
}
public boolean getAlarmLowEnable( int channel)
{
return ((memory[1][2*channel+1] >>> 2) & 1) == 1;
}
public void setAlarmLowEnable( int channel, boolean enable)
{
memory[1][2*channel+1] &= ALARM_LOW_ENABLE_MASK;
if( enable)
{
memory[1][2*channel+1] |= ~ALARM_LOW_ENABLE_MASK;
}
}
public int getAlarmLowValue( int channel)
{
int value16 = (memory[2][2*channel] & 0xff) << 8;
return scaleValue16( channel, value16);
}
public void setAlarmLowValue( int channel, int uVolts)
{
if( uVolts < 0)
{
memory[2][2*channel] = 0;
}
else if( uVolts >= uVoltRanges[ getInputRange( channel)])
{
memory[2][2*channel] = (byte)0xff;
int resolution = getResolution( channel);
if( resolution < 8)
{
memory[2][2*channel] &= ~((1 << (8 - resolution)) - 1);
}
}
else
{
memory[2][2*channel] = (byte)
(unscaleValue16( channel, uVolts) >>> 8);
}
}
public boolean getPORFlag()
{
return (memory[1][1] & (~POR_FLAG_MASK)) != 0;
}
public void setPORFlag( boolean porFlag)
{
memory[1][7] &= POR_FLAG_MASK;
if( porFlag)
{
memory[1][7] |= ~POR_FLAG_MASK;
}
}
public String getAdapterName()
{
return adapter.getAdapterName();
}
public String getPortName()
throws OneWireException
{
return adapter.getPortName();
}
private byte[][] memory = new byte[4][10];
public byte[][] getMemory()
{
// Clone it?
return memory;
}
public static final int[] uVoltRanges = {2560000, 5120000};
public static final byte INPUT_RANGE_MASK = (byte)0xfe;
public static final byte OUTPUT_CONTROL_MASK = (byte)0x3f;
public static final byte RESOLUTION_MASK = (byte)0xf0;
public static final byte ALARM_HIGH_MASK = (byte)0xdf;
public static final byte ALARM_HIGH_ENABLE_MASK = (byte)0xf7;
public static final byte ALARM_LOW_MASK = (byte)0xef;
public static final byte ALARM_LOW_ENABLE_MASK = (byte)0xfb;
public static final byte POR_FLAG_MASK = (byte)0x7f;
public static final byte READ_MEMORY = (byte)0xaa;
public static final byte WRITE_MEMORY = (byte)0x55;
public static final byte CONVERT = (byte)0x3c;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -