1 /*
   2  * $RCSfile: Sample.java,v $
   3  * $Date: 2010/01/29 23:00:04 $
   4  * $Revision: 1.1 $
   5  * jEdit:tabSize=4:indentSize=4:collapseFolds=1:
   6  */
   7 
   8 // {{{ build instructions
   9 /*
  10  * to compile:
  11  *   javac -cp ../../java/aiousb.jar Sample.java
  12  * to run:
  13  *   java -cp ../../java/aiousb.jar:. Sample
  14  */
  15 // }}}
  16 
  17 // {{{ imports
  18 import com.acces.aiousb.*;
  19 import java.io.*;
  20 import java.util.*;
  21 // }}}
  22 
  23 class Sample {
  24     public static final String VERSION = "1.1, 29 January 2010";
  25 
  26     public static void main( String args[] ) {
  27         USBDeviceManager deviceManager = null;
  28         try {
  29 /*API*/     deviceManager = new USBDeviceManager();
  30 /*API*/     deviceManager.open();
  31             System.out.println(
  32                   "USB-DA12-8A sample program version: " + VERSION + "\n"
  33 /*API*/         + "  AIOUSB Java library version: " + deviceManager.VERSION_NUMBER + ", " + deviceManager.VERSION_DATE + "\n"
  34 /*API*/         + "  AIOUSB library version: " + deviceManager.getAIOUSBVersion() + ", " + deviceManager.getAIOUSBVersionDate() + "\n"
  35                 + "  JRE version: " + System.getProperty( "java.version" ) + "\n"
  36                 + "  OS version: " + System.getProperty( "os.name" )
  37                     + " " + System.getProperty( "os.arch" )
  38                     + " " + System.getProperty( "os.version" ) + "\n"
  39                 + "  This program demonstrates controlling a USB-DA12-8A device on\n"
  40                 + "  the USB bus. For simplicity, it uses the first such device found\n"
  41                 + "  on the bus."
  42             );
  43 /*API*/     deviceManager.printDevices();
  44 /*API*/     USBDevice[] devices = deviceManager.getDeviceByProductID( USBDeviceManager.USB_DA12_8A_REV_A, USBDeviceManager.USB_DA12_8A );
  45             if( devices.length > 0 ) {
  46                 USB_DA12_8A_Family device = ( USB_DA12_8A_Family ) devices[ 0 ];    // get first device found
  47 /*API*/         device.setCommTimeout( 1000 );
  48 
  49                 /*
  50                  * demonstrate accessing device properties
  51                  */
  52 /*API*/         System.out.println( "Found device \'" + device.getName() + "\' with serial number " + Long.toHexString( device.getSerialNumber() ) );
  53 
  54                 /*
  55                  * demonstrate writing D/A counts to one D/A channel
  56                  */
  57 /*API*/         final int numChannels = device.dac().getNumChannels();
  58                 final int TEST_CHANNEL = 0;     // channels are numbered 0 to numChannels - 1
  59                 final char counts = DA12_AnalogOutputSubsystem.MAX_COUNTS / 2;  // half of full scale
  60 /*API*/         device.dac().writeCounts( TEST_CHANNEL, counts );
  61                 System.out.println( ( int ) counts + " D/A counts successfully output to channel " + TEST_CHANNEL );
  62 
  63                 /*
  64                  * demonstrate writing D/A counts to multiple D/A channels
  65                  */
  66                 char[] countPoints = new char[ numChannels * 2 ];   // channel/count pairs
  67                 for( int channel = 0; channel < numChannels; channel++ ) {
  68                     countPoints[ channel * 2 ] = ( char ) channel;
  69                     countPoints[ channel * 2 + 1 ] = ( char ) ( ( channel + 1 ) * DA12_AnalogOutputSubsystem.MAX_COUNTS / numChannels );
  70                 }   // for( int channel ...
  71 /*API*/         device.dac().writeCounts( countPoints );
  72                 System.out.println( "Multiple D/A counts successfully output to " + numChannels + " channels" );
  73 
  74                 /*
  75                  * demonstrate writing volts to one D/A channel; note that the hardware range is selected
  76                  * by means of jumpers; this software setting is merely so that the correct D/A count values
  77                  * are calculated for the current hardware range setting; this demonstration assumes the
  78                  * hardware jumpers have been set to the +/-10V range
  79                  */
  80                 final int HARDWARE_RANGE = DA12_AnalogOutputSubsystem.RANGE_10V;
  81                 final double volts = 5;
  82 /*API*/         device.dac().setRange( HARDWARE_RANGE );
  83 /*API*/         device.dac().writeVolts( TEST_CHANNEL, volts );
  84                 System.out.println( volts + " volts (" + ( int ) device.dac().voltsToCounts( TEST_CHANNEL, volts ) + " D/A counts)"
  85                     + " successfully output to channel " + TEST_CHANNEL );
  86 
  87                 /*
  88                  * demonstrate writing volts to multiple D/A channels
  89                  */
  90 /*API*/         OutputVoltagePoint[] voltPoints = new OutputVoltagePoint[ numChannels ];
  91                 for( int channel = 0; channel < numChannels; channel++ )
  92 /*API*/             voltPoints[ channel ] = new OutputVoltagePoint( channel, ( channel + 1 ) * 10.0 / numChannels );
  93 /*API*/         device.dac().writeVolts( voltPoints );
  94                 System.out.println( "Multiple volts successfully output to " + numChannels + " channels" );
  95             } else
  96                 System.out.println( "No USB-DA12-8A devices found on USB bus" );
  97 /*API*/     deviceManager.close();
  98         } catch( Exception ex ) {
  99             System.err.println( "Error \'" + ex.toString() + "\' occurred while manipulating device" );
 100             if(
 101                 deviceManager != null
 102 /*API*/         && deviceManager.isOpen()
 103             )
 104 /*API*/         deviceManager.close();
 105         }   // catch( ...
 106     }   // main()
 107 }   // class Sample
 108 
 109 /* end of file */