1 /*
   2  * $RCSfile: sample.cpp,v $
   3  * $Date: 2009/12/25 19:11:55 $
   4  * $Revision: 1.6 $
   5  * jEdit:tabSize=4:indentSize=4:collapseFolds=1:
   6  */
   7 
   8 // {{{ build instructions
   9 /*
  10  * g++ sample.cpp -lclassaiousb -laiousbcpp -lusb-1.0 -o sample
  11  *   or
  12  * g++ -ggdb sample.cpp -lclassaiousbdbg -laiousbcppdbg -lusb-1.0 -o sample
  13  */
  14 // }}}
  15 
  16 // {{{ includes
  17 #include <iostream>
  18 #include <iomanip>
  19 #include <stdio.h>
  20 #include <stdlib.h>
  21 #include <string.h>
  22 #include <USBDeviceManager.hpp>
  23 #include <USB_DIO_Family.hpp>
  24 // }}}
  25 
  26 using namespace AIOUSB;
  27 using namespace std;
  28 
  29 int main( int argc, char *argv[] ) {
  30     const string VERSION = "1.6, 25 December 2009";
  31     /*
  32      * if you want to test writing to the EEPROM, set DEMO_EEPROM_WRITE to 'true'; after writing
  33      * to the EEPROM this sample program will attempt to restore the original EEPROM contents,
  34      * but you never know ...
  35      */
  36     const bool DEMO_EEPROM_WRITE = false;
  37 
  38 /*API*/ USBDeviceManager deviceManager;
  39     try {
  40 /*API*/ deviceManager.open();
  41         cout
  42             << "USB-IIRO-16 sample program version: " << VERSION << "\n"
  43 /*API*/     << "  AIOUSB C++ library version: " << deviceManager.VERSION_NUMBER << ", " << deviceManager.VERSION_DATE << "\n"
  44 /*API*/     << "  AIOUSB library version: " << deviceManager.getAIOUSBVersion() << ", " << deviceManager.getAIOUSBVersionDate() << "\n"
  45             << "  This program demonstrates controlling a USB-IIRO-16 device on\n"
  46             << "  the USB bus. For simplicity, it uses the first such device found\n"
  47             << "  on the bus.\n";
  48 /*API*/ deviceManager.printDevices();
  49 /*API*/ USBDeviceArray devices = deviceManager.getDeviceByProductID( USB_IIRO_16 );
  50         if( devices.size() > 0 ) {
  51             USB_DIO_Family &device = *( USB_DIO_Family * ) devices.at( 0 ); // get first device found
  52 /*API*/     device.reset()
  53 /*API*/         .setCommTimeout( 1000 );
  54 
  55             /*
  56              * demonstrate writing EEPROM
  57              */
  58             if( DEMO_EEPROM_WRITE ) {
  59                 cout << "Writing to EEPROM ... " << flush;
  60                 const int offset = 0x100;
  61                 string message = "USB-IIRO-16 sample program";
  62 /*API*/         UCharArray prevData = device.customEEPROMRead( offset, message.length() );  // preserve current EEPROM contents
  63                 UCharArray data( message.length() );
  64                 for( int index = 0; index < ( int ) message.length(); index++ )
  65                     data.at( index ) = message.at( index );
  66 /*API*/         device.setCommTimeout( 10000 )  // writing the entire EEPROM (which we're not doing here) can take a long time
  67 /*API*/             .customEEPROMWrite( offset, data );
  68 /*API*/         UCharArray readData = device.customEEPROMRead( offset, message.length() );
  69                 string readMessage( readData.size(), ' ' );
  70                 for( int index = 0; index < ( int ) readData.size(); index++ )
  71                     readMessage.at( index ) = readData.at( index );
  72 /*API*/         device.customEEPROMWrite( offset, prevData )
  73 /*API*/             .setCommTimeout( 1000 );
  74                 cout
  75                     << ( ( readMessage.compare( message ) == 0 )
  76                         ? "successful"
  77                         : "failed: data read back did not compare" )
  78                     << endl;
  79             }   // if( DEMO_EEPROM_WRITE )
  80 
  81             /*
  82              * demonstrate reading EEPROM
  83              */
  84             cout
  85                 << "EEPROM contents:\n"
  86                 << '['
  87                 << flush << hex << setw( 2 ) << setfill( '0' );
  88 /*API*/     UCharArray data = device.customEEPROMRead( 0, device.CUSTOM_EEPROM_SIZE );
  89             for( int index = 0; index < ( int ) data.size() - 1; index++ )
  90                 cout << ( unsigned ) data.at( index ) << ", ";
  91             cout << ( unsigned ) data.at( data.size() - 1 ) << "]\n";
  92 
  93             /*
  94              * demonstrate DIO configuration
  95              */
  96             cout << "Configuring digital I/O ... " << flush;
  97 /*API*/     const int numPorts = device.dio().getNumPorts();
  98             const int numOutputPorts = numPorts / 2;
  99 /*API*/     const int numChannels = device.dio().getNumChannels();
 100             const int numOutputChannels = numChannels / 2;
 101             const int numInputChannels = numChannels - numOutputChannels;
 102             BoolArray outputs( numPorts );
 103             for( int port = 0; port < numOutputPorts; port++ )
 104                 outputs.at( port ) = true;          // relays are outputs
 105             for( int port = numOutputPorts; port < numPorts; port++ )
 106                 outputs.at( port ) = false;         // other ports are inputs
 107             BoolArray values( numOutputChannels );
 108             values.assign( values.size(), true );   // open all relays
 109 /*API*/     device.dio().configure( false, outputs, values );
 110             cout << "successful" << endl;
 111 
 112             /*
 113              * demonstrate writing outputs individually
 114              */
 115             cout
 116                 << "In the following demonstrations, you should hear the relays click\n"
 117                 << "and see the LED on the device blink.\n";
 118             cout << "  CLosing relays:" << flush << dec;
 119             for( int channel = 0; channel < numOutputChannels; channel++ ) {
 120                 cout << " " << channel << flush;
 121 /*API*/         device.dio().write( channel, false );   // close relay
 122                 usleep( 100000 );
 123             }   // for( int channel ...
 124             cout << endl << "  Opening relays:" << flush;
 125             for( int channel = 0; channel < numOutputChannels; channel++ ) {
 126                 cout << " " << channel << flush;
 127 /*API*/         device.dio().write( channel, true );    // open relay
 128                 usleep( 100000 );
 129             }   // for( int channel ...
 130             cout << endl;
 131 
 132             /*
 133              * demonstrate writing outputs as a group
 134              */
 135             cout << "Closing all relays ... " << flush;
 136             values.assign( values.size(), false );  // close all relays
 137 /*API*/     device.dio().write( 0, values );
 138             cout << "successful" << endl;
 139             sleep( 2 );
 140             cout << "Opening all relays ... " << flush;
 141             values.assign( values.size(), true );   // open all relays
 142 /*API*/     device.dio().write( 0, values );
 143             cout << "successful" << endl;
 144 
 145             /*
 146              * demonstrate reading inputs individually
 147              */
 148             sleep( 1 );
 149             cout << "CLosing every other relay:";
 150             for( int channel = 0; channel < numOutputChannels; channel += 2 ) {
 151                 cout << " " << channel << flush;
 152 /*API*/         device.dio().write( channel, false );   // close relay
 153                 usleep( 100000 );
 154             }   // for( int channel ...
 155             cout << endl << "Reading back relays:" << endl;
 156             for( int channel = 0; channel < numOutputChannels; channel++ ) {
 157 /*API*/         cout << "  Channel " << channel << " = " << device.dio().read( channel ) << endl;
 158             }   // for( int channel ...
 159             cout << "Reading isolated inputs:" << endl;
 160             for( int channel = 0; channel < numInputChannels; channel++ ) {
 161 /*API*/         cout << "  Channel " <<  ( numOutputChannels + channel ) << " = " << device.dio().read( numOutputChannels + channel ) << endl;
 162             }   // for( int channel ...
 163 
 164             /*
 165              * open all relays
 166              */
 167             values.assign( values.size(), true );
 168 /*API*/     device.dio().write( 0, values );
 169         } else
 170             cout << "No USB-IIRO-16 devices found on USB bus" << endl;
 171 /*API*/ deviceManager.close();
 172     } catch( exception &ex ) {
 173         cerr << "Error \'" << ex.what() << "\' occurred while manipulating device" << endl;
 174 /*API*/ if( deviceManager.isOpen() )
 175 /*API*/     deviceManager.close();
 176     }   // catch( ...
 177 }   // main()
 178 
 179 /* end of file */