1 /*
   2  * $RCSfile: sample.cpp,v $
   3  * $Revision: 1.9 $
   4  * $Date: 2009/10/16 18:04:08 $
   5  * :tabSize=4:collapseFolds=1:
   6  *
   7  * AIOUSB library sample program
   8  */
   9 
  10 
  11 // {{{ notes and build instructions
  12 /*
  13  * This source code looks best with a tab width of 4.
  14  *
  15  * All the API functions that DO NOT begin "AIOUSB_" are standard API functions, largely
  16  * documented in http://accesio.com/MANUALS/USB%20Software%20Reference.pdf. The functions
  17  * that DO begin with "AIOUSB_" are "extended" API functions added to the Linux
  18  * implementation. Source code lines in this sample program that are prefixed with the
  19  * comment "/ * API * /" highlight calls to the AIOUSB API.
  20  *
  21  * LIBUSB (http://www.libusb.org/) must be installed on the Linux box (the AIOUSB code
  22  * was developed using libusb version 1.0.3). After installing libusb, it may also be
  23  * necessary to set an environment variable so that the libusb and aiousb header files can
  24  * be located:
  25  *
  26  *     export CPATH=/usr/local/include/libusb-1.0/:/usr/local/include/aiousb/
  27  *
  28  * Once libusb is installed properly, it should be possible to compile the sample program
  29  * using the simple command:
  30  *
  31  *     make
  32  *
  33  * Alternatively, one can "manually" compile the sample program using the command:
  34  *
  35  *     g++ sample.cpp -laiousb -lusb-1.0 -o sample
  36  */
  37 // }}}
  38 
  39 // {{{ includes
  40 #include <aiousb.h>
  41 #include <stdio.h>
  42 using namespace AIOUSB;
  43 // }}}
  44 
  45 int main( int argc, char **argv ) {
  46     printf(
  47         "USB-AO16-16A sample program $Revision: 1.9 $ $Date: 2009/10/16 18:04:08 $\n"
  48         "  (AIOUSB library %s)\n"
  49         "  This program demonstrates controlling a USB-AO16-16A device on\n"
  50         "  the USB bus. For simplicity, it uses the first such device found\n"
  51         "  on the bus.\n"
  52 /*API*/ , AIOUSB_GetVersion()
  53     );
  54 
  55     /*
  56      * MUST call AIOUSB_Init() before any meaningful AIOUSB functions;
  57      * AIOUSB_GetVersion() above is an exception
  58      */
  59 /*API*/ unsigned long result = AIOUSB_Init();
  60     if( result == AIOUSB_SUCCESS ) {
  61         /*
  62          * call GetDevices() to obtain "list" of devices found on the bus
  63          */
  64 /*API*/ unsigned long deviceMask = GetDevices();
  65         if( deviceMask != 0 ) {
  66             /*
  67              * at least one ACCES device detected, but we want one of a specific type
  68              */
  69 /*API*/     AIOUSB_ListDevices();               // print list of all devices found on the bus
  70             const int MAX_NAME_SIZE = 20;
  71             char name[ MAX_NAME_SIZE + 2 ];
  72             unsigned long productID, nameSize, numDIOBytes, numCounters;
  73             unsigned long deviceIndex = 0;
  74             bool deviceFound = false;
  75             while( deviceMask != 0 ) {
  76                 if( ( deviceMask & 1 ) != 0 ) {
  77                     // found a device, but is it the correct type?
  78                     nameSize = MAX_NAME_SIZE;
  79 /*API*/             result = QueryDeviceInfo( deviceIndex, &productID, &nameSize, name, &numDIOBytes, &numCounters );
  80                     if( result == AIOUSB_SUCCESS ) {
  81                         if(
  82                             productID >= USB_AO16_16A
  83                             && productID <= USB_AO12_4
  84                         ) {
  85                             // found a USB-AO16-16A family device
  86                             deviceFound = true;
  87                             break;              // from while()
  88                         }   // if( productID ...
  89                     } else
  90                         printf( "Error '%s' querying device at index %lu\n"
  91 /*API*/                     , AIOUSB_GetResultCodeAsString( result ), deviceIndex );
  92                 }   // if( ( deviceMask ...
  93                 deviceIndex++;
  94                 deviceMask >>= 1;
  95             }   // while( deviceMask ...
  96             if( deviceFound ) {
  97 /*API*/         AIOUSB_SetCommTimeout( deviceIndex, 500 );
  98 
  99                 __uint64_t serialNumber;
 100 /*API*/         result = GetDeviceSerialNumber( deviceIndex, &serialNumber );
 101                 if( result == AIOUSB_SUCCESS )
 102                     printf( "Serial number of device at index %lu: %llx\n", deviceIndex, serialNumber );
 103                 else
 104                     printf( "Error '%s' getting serial number of device at index %lu\n"
 105 /*API*/                 , AIOUSB_GetResultCodeAsString( result ), deviceIndex );
 106 
 107                 /*
 108                  * demonstrate getting enhanced device properties
 109                  */
 110                 const int MAX_CHANNELS = 16;
 111                 DeviceProperties properties;
 112 /*API*/         result = AIOUSB_GetDeviceProperties( deviceIndex, &properties );
 113                 if( result == AIOUSB_SUCCESS )
 114                     printf( "Device properties successfully retrieved\n" );
 115                 else {
 116                     properties.DACChannels = MAX_CHANNELS;
 117                     printf( "Error '%s' getting device properties\n"
 118 /*API*/                 , AIOUSB_GetResultCodeAsString( result ) );
 119                 }   // if( result ...
 120 
 121                 /*
 122                  * demonstrate setting output range
 123                  */
 124 /*API*/         result = DACSetBoardRange( deviceIndex, DAC_RANGE_0_5V /* 0-5V */ );
 125                 if( result == AIOUSB_SUCCESS )
 126                     printf( "D/A output range successfully set\n" );
 127                 else
 128                     printf( "Error '%s' setting D/A ourput range\n"
 129 /*API*/                 , AIOUSB_GetResultCodeAsString( result ) );
 130 
 131                 /*
 132                  * demonstrate writing to one D/A channel
 133                  */
 134                 const int TEST_CHANNEL = 0;         // channels are numbered 0 to MAX_CHANNELS - 1
 135                 const unsigned MAX_COUNTS = 0xffff; // the 16-bit and 12-bit devices both support 16-bit count values
 136                 const unsigned counts = MAX_COUNTS / 2; // half of full scale
 137 /*API*/         result = DACDirect( deviceIndex, TEST_CHANNEL, counts );
 138                 if( result == AIOUSB_SUCCESS )
 139                     printf( "%u D/A counts successfully output to channel %d\n", counts, TEST_CHANNEL );
 140                 else
 141                     printf( "Error '%s' attempting to output %u D/A counts successfully to channel %d\n"
 142 /*API*/                 , AIOUSB_GetResultCodeAsString( result )
 143                         , counts, TEST_CHANNEL );
 144 
 145                 /*
 146                  * demonstrate writing to multiple D/A channels
 147                  */
 148                 unsigned short dacData[ MAX_CHANNELS * 2 ];     // channel/count pairs
 149                 for( int channel = 0; channel < ( int ) properties.DACChannels; channel++ ) {
 150                     dacData[ channel * 2 ] = channel;
 151                     dacData[ channel * 2 + 1 ] = ( unsigned short ) (
 152                         ( unsigned long ) ( channel + 1 )
 153                         * ( unsigned long ) MAX_COUNTS
 154                         / properties.DACChannels
 155                     );
 156                 }   // for( int channel ...
 157 /*API*/         result = DACMultiDirect( deviceIndex, dacData, properties.DACChannels );
 158                 if( result == AIOUSB_SUCCESS )
 159                     printf( "D/A counts successfully output to %u channels simultaneously\n", properties.DACChannels );
 160                 else
 161                     printf( "Error '%s' attempting to output D/A counts to %u channels simultaneously\n"
 162 /*API*/                 , AIOUSB_GetResultCodeAsString( result )
 163                         , properties.DACChannels );
 164             } else
 165                 printf( "Failed to find USB-AO16-16A device\n" );
 166         } else
 167             printf( "No ACCES devices found on USB bus\n" );
 168 
 169         /*
 170          * MUST call AIOUSB_Exit() before program exits,
 171          * but only if AIOUSB_Init() succeeded
 172          */
 173 /*API*/ AIOUSB_Exit();
 174     }   // if( result ...
 175     return ( int ) result;
 176 }   // main()
 177 
 178 
 179 /* end of file */