1 /*
   2  * $RCSfile: sample.cpp,v $
   3  * $Date: 2009/11/30 22:13:33 $
   4  * $Revision: 1.2 $
   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 /*API*/ USBDeviceManager deviceManager;
  31     try {
  32         const string VERSION = "1.2, 30 November 2009";
  33         /*
  34          * if you want to test writing to the EEPROM, set DEMO_EEPROM_WRITE to 'true'; after writing
  35          * to the EEPROM this sample program will attempt to restore the original EEPROM contents,
  36          * but you never know ...
  37          */
  38         const bool DEMO_EEPROM_WRITE = false;
  39 
  40         cout
  41             << "USB-IIRO-16 sample program version: " << VERSION << "\n"
  42 /*API*/     << "  AIOUSB C++ library version: " << deviceManager.VERSION_NUMBER << ", " << deviceManager.VERSION_DATE << "\n"
  43 /*API*/     << "  AIOUSB library version: " << deviceManager.getAIOUSBVersion() << ", " << deviceManager.getAIOUSBVersionDate() << "\n"
  44             << "  This program demonstrates controlling a USB-IIRO-16 device on\n"
  45             << "  the USB bus. For simplicity, it uses the first such device found\n"
  46             << "  on the bus.\n";
  47 /*API*/ deviceManager.open();
  48 /*API*/ DeviceList devices = deviceManager.getDeviceByProductID( USB_IIRO_16 );
  49         if( devices.size() > 0 ) {
  50 /*API*/     deviceManager.printDevices();
  51             USB_DIO_Family *const 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*/     BoolArray outputs( device->dio().getNumPorts() );
  98             outputs.assign( outputs.size(), true ); // all ports are outputs
  99 /*API*/     BoolArray values( device->dio().getNumChannels() );
 100             values.assign( values.size(), false );  // open all relays
 101 /*API*/     device->dio().configure( false, outputs, values );
 102             cout << "successful" << endl;
 103 
 104             /*
 105              * demonstrate writing outputs individually
 106              */
 107             cout
 108                 << "In the following demonstrations, you should hear the relays click for the first\n"
 109                 << "16 channels. The latter 16 channels are not connected to relays and will turn\n"
 110                 << "on and off silently. Also. the LED on the device should blink during these demos.\n";
 111             cout << "  CLosing relays:" << flush << dec;
 112 /*API*/     for( int channel = 0; channel < device->dio().getNumChannels(); channel++ ) {
 113                 cout << " " << channel << flush;
 114 /*API*/         device->dio().write( channel, true );   // close relay
 115                 usleep( 100000 );
 116             }   // for( int channel ...
 117             cout << endl << "  Opening relays:" << flush;
 118 /*API*/     for( int channel = 0; channel < device->dio().getNumChannels(); channel++ ) {
 119                 cout << " " << channel << flush;
 120 /*API*/         device->dio().write( channel, false );  // open relay
 121                 usleep( 100000 );
 122             }   // for( int channel ...
 123             cout << endl;
 124 
 125             /*
 126              * demonstrate writing outputs as a group
 127              */
 128             cout << "Closing all relays ... " << flush;
 129             values.assign( values.size(), true );   // close all relays
 130 /*API*/     device->dio().write( 0, values );
 131             cout << "successful" << endl;
 132             sleep( 2 );
 133             cout << "Opening all relays ... " << flush;
 134             values.assign( values.size(), false );  // open all relays
 135 /*API*/     device->dio().write( 0, values );
 136             cout << "successful" << endl;
 137         } else
 138             cout << "No USB-IIRO-16 devices found on USB bus" << endl;
 139 /*API*/ deviceManager.close();
 140     } catch( exception &ex ) {
 141         cerr << "Error \'" << ex.what() << "\' occurred while initializing device manager" << endl;
 142 /*API*/ if( deviceManager.isOpen() )
 143 /*API*/     deviceManager.close();
 144     }   // catch( ...
 145 }   // main()
 146 
 147 /* end of file */