import com.LabJack.GenericSPI; /** * SPI class for the Linear Technology LTC2413 ADC. * *
This class extends Generic by interpreting the raw conversion results from * a Linear Technology LTC2413 ADC.
*/ public class LTC2413 extends GenericSPI { public static class LTC2413Exception extends Exception { public final static int CONVERSION_INCOMPLETE = 0; public final static int OVERRANGE = 1; public final static int UNDERRANGE = 2; public LTC2413Exception( int cause ) { super( getMessage( cause ) ); } static String getMessage( int cause ) { switch( cause ) { case CONVERSION_INCOMPLETE: return "Conversion incomplete"; case OVERRANGE: return "Measurement over-range"; case UNDERRANGE: return "Measurement under-range"; default: return "Unknown cause"; } } } float reference; /** * Constructor. * * @param reference Voltage reference value used for scaling LTC2413 conversion results. */ public LTC2413( float reference ) { super( 32 ); this.reference = reference; } /** * Decodes and returns voltage reading. * * @return Voltage reading. * * @throws LTC2413Exception */ public float getVoltage() throws LTC2413Exception { int bits = getBits(); if( ( bits & 0x80000000 ) != 0 ) { throw new LTC2413Exception( LTC2413Exception.CONVERSION_INCOMPLETE ); } boolean sign = ( bits & 0x20000000 ) != 0 ? true : false; // true = positive int conversion = bits & 0x1fffffff; if( sign && ( bits & 0x10000000 ) != 0 ) { throw new LTC2413Exception( LTC2413Exception.OVERRANGE ); } else if( !sign && ( ( bits & 0x10000000 ) == 0 ) ) { throw new LTC2413Exception( LTC2413Exception.UNDERRANGE ); } float result = reference / 2f * (float)conversion / 0x0fffffff; if( !sign ) { result = -result; } return result; } }