1 /*
2 * $RCSfile: sample.cpp,v $
3 * $Revision: 1.13 $
4 * $Date: 2009/11/26 21:51:22 $
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 * or, to enable debug features
38 *
39 * g++ -ggdb sample.cpp -laiousbdbg -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-AO16-16A sample program version 1.13, 26 November 2009\n"
52 " AIOUSB library version %s, %s\n"
53 " This program demonstrates controlling a USB-AO16-16A 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_AO16_16A
87 && productID <= USB_AO12_4
88 ) {
89 // found a USB-AO16-16A 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 const int MAX_CHANNELS = 16;
115 DeviceProperties properties;
116 /*API*/ result = AIOUSB_GetDeviceProperties( deviceIndex, &properties );
117 if( result == AIOUSB_SUCCESS )
118 printf( "Device properties successfully retrieved\n" );
119 else {
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 setting output range
127 */
128 /*API*/ result = DACSetBoardRange( deviceIndex, DAC_RANGE_0_5V /* 0-5V */ );
129 if( result == AIOUSB_SUCCESS )
130 printf( "D/A output range successfully set\n" );
131 else
132 printf( "Error '%s' setting D/A ourput range\n"
133 /*API*/ , AIOUSB_GetResultCodeAsString( result ) );
134
135 /*
136 * demonstrate writing to one D/A channel
137 */
138 const int TEST_CHANNEL = 0; // channels are numbered 0 to MAX_CHANNELS - 1
139 const unsigned MAX_COUNTS = 0xffff; // the 16-bit and 12-bit devices both support 16-bit count values
140 const unsigned counts = MAX_COUNTS / 2; // half of full scale
141 /*API*/ result = DACDirect( deviceIndex, TEST_CHANNEL, counts );
142 if( result == AIOUSB_SUCCESS )
143 printf( "%u D/A counts successfully output to channel %d\n", counts, TEST_CHANNEL );
144 else
145 printf( "Error '%s' attempting to output %u D/A counts successfully to channel %d\n"
146 /*API*/ , AIOUSB_GetResultCodeAsString( result )
147 , counts, TEST_CHANNEL );
148
149 /*
150 * demonstrate writing to multiple D/A channels
151 */
152 unsigned short dacData[ MAX_CHANNELS * 2 ]; // channel/count pairs
153 for( int channel = 0; channel < ( int ) properties.DACChannels; channel++ ) {
154 dacData[ channel * 2 ] = channel;
155 dacData[ channel * 2 + 1 ] = ( unsigned short ) (
156 ( unsigned long ) ( channel + 1 )
157 * ( unsigned long ) MAX_COUNTS
158 / properties.DACChannels
159 );
160 } // for( int channel ...
161 /*API*/ result = DACMultiDirect( deviceIndex, dacData, properties.DACChannels );
162 if( result == AIOUSB_SUCCESS )
163 printf( "D/A counts successfully output to %u channels simultaneously\n", properties.DACChannels );
164 else
165 printf( "Error '%s' attempting to output D/A counts to %u channels simultaneously\n"
166 /*API*/ , AIOUSB_GetResultCodeAsString( result )
167 , properties.DACChannels );
168 } else
169 printf( "Failed to find USB-AO16-16A device\n" );
170 } else
171 printf( "No ACCES devices found on USB bus\n" );
172
173 /*
174 * MUST call AIOUSB_Exit() before program exits,
175 * but only if AIOUSB_Init() succeeded
176 */
177 /*API*/ AIOUSB_Exit();
178 } // if( result ...
179 return ( int ) result;
180 } // main()
181
182
183 /* end of file */