1 /*
   2  * $RCSfile: SampleClass.cpp,v $
   3  * $Date: 2010/01/30 00:00:31 $
   4  * $Revision: 1.1 $
   5  * jEdit:tabSize=4:indentSize=4:collapseFolds=1:
   6  */
   7 
   8 // {{{ build instructions
   9 /*
  10  * g++ SampleClass.cpp -lclassaiousb -laiousbcpp -lusb-1.0 -o SampleClass
  11  *   or
  12  * g++ -ggdb SampleClass.cpp -lclassaiousbdbg -laiousbcppdbg -lusb-1.0 -o SampleClass
  13  */
  14 // }}}
  15 
  16 // {{{ includes
  17 #include <iostream>
  18 #include <iterator>
  19 #include <iomanip>
  20 #include <stdio.h>
  21 #include <stdlib.h>
  22 #include <string.h>
  23 #include <USBDeviceManager.hpp>
  24 #include <USB_DA12_8A_Family.hpp>
  25 // }}}
  26 
  27 using namespace AIOUSB;
  28 using namespace std;
  29 
  30 int main( int argc, char *argv[] ) {
  31 /*API*/ USBDeviceManager deviceManager;
  32     try {
  33 /*API*/ deviceManager.open();
  34         cout <<
  35             "USB-DA12-8A sample program version 1.1, 29 January 2010\n"
  36 /*API*/     "  AIOUSB C++ class library version " << deviceManager.VERSION_NUMBER << ", " << deviceManager.VERSION_DATE << "\n"
  37 /*API*/     "  AIOUSB library version " << deviceManager.getAIOUSBVersion() << ", " << deviceManager.getAIOUSBVersionDate() << "\n"
  38             "\n"
  39             "  This program demonstrates controlling a USB-DA12-8A family device on\n"
  40             "  the USB bus. For simplicity, it uses the first such device found\n"
  41             "  on the bus and supports these product IDs: ";
  42 /*API*/ const StringArray supportedProductNames = USB_DA12_8A_Family::getSupportedProductNames();
  43         for( int index = 0; index < ( int ) supportedProductNames.size(); index++ ) {
  44             if( index > 0 )
  45                 cout << ", ";
  46             cout << supportedProductNames.at( index );
  47         }   // for( int index ...
  48         cout << endl;
  49 /*API*/ deviceManager.printDevices();
  50 /*API*/ USBDeviceArray devices = deviceManager.getDeviceByProductID( USB_DA12_8A_Family::getSupportedProductIDs() );
  51         if( devices.size() > 0 ) {
  52             USB_DA12_8A_Family &device = *( USB_DA12_8A_Family * ) devices.at( 0 ); // get first device found
  53 /*API*/     device.setCommTimeout( 1000 );
  54 
  55             /*
  56              * demonstrate accessing device properties
  57              */
  58 /*API*/     cout << "Found device \'" << device.getName() << "\' with serial number " << hex << device.getSerialNumber() << endl;
  59 
  60             /*
  61              * demonstrate writing D/A counts to one D/A channel
  62              */
  63 /*API*/     const int numChannels = device.dac().getNumChannels();
  64             const int TEST_CHANNEL = 0;         // channels are numbered 0 to numChannels - 1
  65             const unsigned short counts = DA12_AnalogOutputSubsystem::MAX_COUNTS / 2;   // half of full scale
  66 /*API*/     device.dac().writeCounts( TEST_CHANNEL, counts );
  67             cout << dec << counts << " D/A counts successfully output to channel " << TEST_CHANNEL << endl;
  68 
  69             /*
  70              * demonstrate writing D/A counts to multiple D/A channels
  71              */
  72             UShortArray countPoints( numChannels * 2 );     // channel/count pairs
  73             for( int channel = 0; channel < numChannels; channel++ ) {
  74                 countPoints.at( channel * 2 ) = channel;
  75                 countPoints.at( channel * 2 + 1 ) = ( unsigned short ) ( ( channel + 1 ) * DA12_AnalogOutputSubsystem::MAX_COUNTS / numChannels );
  76             }   // for( int channel ...
  77 /*API*/     device.dac().writeCounts( countPoints );
  78             cout << "Multiple D/A counts successfully output to " << numChannels << " channels" << endl;
  79 
  80             /*
  81              * demonstrate writing volts to one D/A channel; note that the hardware range is selected
  82              * by means of jumpers; this software setting is merely so that the correct D/A count values
  83              * are calculated for the current hardware range setting; this demonstration assumes the
  84              * hardware jumpers have been set to the +/-10V range
  85              */
  86             const int HARDWARE_RANGE = DA12_AnalogOutputSubsystem::RANGE_10V;
  87             const double volts = 5;
  88 /*API*/     device.dac().setRange( HARDWARE_RANGE );
  89 /*API*/     device.dac().writeVolts( TEST_CHANNEL, volts );
  90             cout << volts << " volts (" << device.dac().voltsToCounts( TEST_CHANNEL, volts ) << " D/A counts)"
  91                 << " successfully output to channel " << TEST_CHANNEL << endl;
  92 
  93             /*
  94              * demonstrate writing volts to multiple D/A channels
  95              */
  96 /*API*/     OutputVoltagePointArray voltPoints( numChannels );
  97             for( int channel = 0; channel < numChannels; channel++ ) {
  98 /*API*/         voltPoints.at( channel ).channel = channel;
  99                 voltPoints.at( channel ).volts = ( channel + 1 ) * 10.0 / numChannels;
 100             }   // for( int channel ...
 101 /*API*/     device.dac().writeVolts( voltPoints );
 102             cout << "Multiple volts successfully output to " << numChannels << " channels" << endl;
 103         } else
 104             cout << "No USB-DA12-8A devices found on USB bus" << endl;
 105 /*API*/ deviceManager.close();
 106     } catch( exception &ex ) {
 107         cerr << "Error \'" << ex.what() << "\' occurred while configuring device" << endl;
 108 /*API*/ if( deviceManager.isOpen() )
 109 /*API*/     deviceManager.close();
 110     }   // catch( ...
 111 }   // main()
 112 
 113 /* end of file */