1 /*
   2  * $RCSfile: sample.cpp,v $
   3  * $Revision: 1.1 $
   4  * $Date: 2010/01/29 23:00:04 $
   5  * jEdit:tabSize=4:indentSize=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/AIOUSB_API_Reference.html. 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 -laiousbcpp -lusb-1.0 -o sample
  36  *
  37  * or, to enable debug features
  38  *
  39  *     g++ -ggdb sample.cpp -laiousbcppdbg -lusb-1.0 -o sample
  40  */
  41 // }}}
  42 
  43 // {{{ includes
  44 #include <aiousb.h>
  45 #include <stdio.h>
  46 using namespace AIOUSB;
  47 // }}}
  48 
  49 int main( int argc, char **argv ) {
  50     printf(
  51         "USB-DA12-8A sample program version 1.1, 29 January 2010\n"
  52         "  AIOUSB library version %s, %s\n"
  53         "  This program demonstrates controlling a USB-DA12-8A device on\n"
  54         "  the USB bus. For simplicity, it uses the first such device found\n"
  55         "  on the bus.\n"
  56 /*API*/ , AIOUSB_GetVersion(), AIOUSB_GetVersionDate()
  57     );
  58 
  59     /*
  60      * MUST call AIOUSB_Init() before any meaningful AIOUSB functions;
  61      * AIOUSB_GetVersion() above is an exception
  62      */
  63 /*API*/ unsigned long result = AIOUSB_Init();
  64     if( result == AIOUSB_SUCCESS ) {
  65         /*
  66          * call GetDevices() to obtain "list" of devices found on the bus
  67          */
  68 /*API*/ unsigned long deviceMask = GetDevices();
  69         if( deviceMask != 0 ) {
  70             /*
  71              * at least one ACCES device detected, but we want one of a specific type
  72              */
  73 /*API*/     AIOUSB_ListDevices();               // print list of all devices found on the bus
  74             const int MAX_NAME_SIZE = 20;
  75             char name[ MAX_NAME_SIZE + 2 ];
  76             unsigned long productID, nameSize, numDIOBytes, numCounters;
  77             unsigned long deviceIndex = 0;
  78             bool deviceFound = false;
  79             while( deviceMask != 0 ) {
  80                 if( ( deviceMask & 1 ) != 0 ) {
  81                     // found a device, but is it the correct type?
  82                     nameSize = MAX_NAME_SIZE;
  83 /*API*/             result = QueryDeviceInfo( deviceIndex, &productID, &nameSize, name, &numDIOBytes, &numCounters );
  84                     if( result == AIOUSB_SUCCESS ) {
  85                         if(
  86                             productID >= USB_DA12_8A_REV_A
  87                             && productID <= USB_DA12_8E
  88                         ) {
  89                             // found a USB-DA12-8A/E family device
  90                             deviceFound = true;
  91                             break;              // from while()
  92                         }   // if( productID ...
  93                     } else
  94                         printf( "Error '%s' querying device at index %lu\n"
  95 /*API*/                     , AIOUSB_GetResultCodeAsString( result ), deviceIndex );
  96                 }   // if( ( deviceMask ...
  97                 deviceIndex++;
  98                 deviceMask >>= 1;
  99             }   // while( deviceMask ...
 100             if( deviceFound ) {
 101 /*API*/         AIOUSB_SetCommTimeout( deviceIndex, 500 );
 102 
 103                 __uint64_t serialNumber;
 104 /*API*/         result = GetDeviceSerialNumber( deviceIndex, &serialNumber );
 105                 if( result == AIOUSB_SUCCESS )
 106                     printf( "Serial number of device at index %lu: %llx\n", deviceIndex, ( long long ) serialNumber );
 107                 else
 108                     printf( "Error '%s' getting serial number of device at index %lu\n"
 109 /*API*/                 , AIOUSB_GetResultCodeAsString( result ), deviceIndex );
 110 
 111                 /*
 112                  * demonstrate getting enhanced device properties
 113                  */
 114                 DeviceProperties properties;
 115 /*API*/         result = AIOUSB_GetDeviceProperties( deviceIndex, &properties );
 116                 if( result == AIOUSB_SUCCESS )
 117                     printf( "Device properties successfully retrieved\n" );
 118                 else {
 119                     const int MAX_CHANNELS = 8;
 120                     properties.DACChannels = MAX_CHANNELS;
 121                     printf( "Error '%s' getting device properties\n"
 122 /*API*/                 , AIOUSB_GetResultCodeAsString( result ) );
 123                 }   // if( result ...
 124 
 125                 /*
 126                  * demonstrate writing to one D/A channel
 127                  */
 128                 const int TEST_CHANNEL = 0;         // channels are numbered 0 to properties.DACChannels - 1
 129                 const unsigned MAX_COUNTS = 0xfff;
 130                 const unsigned counts = MAX_COUNTS / 2; // half of full scale
 131 /*API*/         result = DACDirect( deviceIndex, TEST_CHANNEL, counts );
 132                 if( result == AIOUSB_SUCCESS )
 133                     printf( "%u D/A counts successfully output to channel %d\n", counts, TEST_CHANNEL );
 134                 else
 135                     printf( "Error '%s' attempting to output %u D/A counts successfully to channel %d\n"
 136 /*API*/                 , AIOUSB_GetResultCodeAsString( result )
 137                         , counts, TEST_CHANNEL );
 138 
 139                 /*
 140                  * demonstrate writing to multiple D/A channels
 141                  */
 142                 unsigned short dacData[ properties.DACChannels * 2 ];       // channel/count pairs
 143                 for( int channel = 0; channel < ( int ) properties.DACChannels; channel++ ) {
 144                     dacData[ channel * 2 ] = channel;
 145                     dacData[ channel * 2 + 1 ] = ( unsigned short ) (
 146                         ( unsigned long ) ( channel + 1 )
 147                         * ( unsigned long ) MAX_COUNTS
 148                         / properties.DACChannels
 149                     );
 150                 }   // for( int channel ...
 151 /*API*/         result = DACMultiDirect( deviceIndex, dacData, properties.DACChannels );
 152                 if( result == AIOUSB_SUCCESS )
 153                     printf( "D/A counts successfully output to %u channels simultaneously\n", properties.DACChannels );
 154                 else
 155                     printf( "Error '%s' attempting to output D/A counts to %u channels simultaneously\n"
 156 /*API*/                 , AIOUSB_GetResultCodeAsString( result )
 157                         , properties.DACChannels );
 158             } else
 159                 printf( "Failed to find USB-DA12-8A device\n" );
 160         } else
 161             printf( "No ACCES devices found on USB bus\n" );
 162 
 163         /*
 164          * MUST call AIOUSB_Exit() before program exits,
 165          * but only if AIOUSB_Init() succeeded
 166          */
 167 /*API*/ AIOUSB_Exit();
 168     }   // if( result ...
 169     return ( int ) result;
 170 }   // main()
 171 
 172 
 173 /* end of file */