1 /*
   2  * $RCSfile: Sample.java,v $
   3  * $Date: 2009/12/25 19:11:55 $
   4  * $Revision: 1.4 $
   5  * jEdit:tabSize=4:indentSize=4:collapseFolds=1:
   6  */
   7 
   8 // {{{ build instructions
   9 /*
  10  * to compile:
  11  *   javac -cp ../../java/aiousb.jar Sample.java
  12  * to run:
  13  *   java -cp ../../java/aiousb.jar:. Sample
  14  */
  15 // }}}
  16 
  17 // {{{ imports
  18 import com.acces.aiousb.*;
  19 import java.io.*;
  20 import java.util.*;
  21 // }}}
  22 
  23 class Sample {
  24     public static final String VERSION = "1.4, 25 December 2009";
  25     /*
  26      * if you want to test writing to the EEPROM, set DEMO_EEPROM_WRITE to 'true'; after writing
  27      * to the EEPROM this sample program will attempt to restore the original EEPROM contents,
  28      * but you never know ...
  29      */
  30     protected static final boolean DEMO_EEPROM_WRITE = false;
  31 
  32     public static void main( String args[] ) {
  33         USBDeviceManager deviceManager = null;
  34         try {
  35 /*API*/     deviceManager = new USBDeviceManager();
  36 /*API*/     deviceManager.open();
  37             System.out.println(
  38                   "USB-IIRO-16 sample program version: " + VERSION + "\n"
  39 /*API*/         + "  AIOUSB Java library version: " + deviceManager.VERSION_NUMBER + ", " + deviceManager.VERSION_DATE + "\n"
  40 /*API*/         + "  AIOUSB library version: " + deviceManager.getAIOUSBVersion() + ", " + deviceManager.getAIOUSBVersionDate() + "\n"
  41                 + "  JRE version: " + System.getProperty( "java.version" ) + "\n"
  42                 + "  OS version: " + System.getProperty( "os.name" )
  43                     + " " + System.getProperty( "os.arch" )
  44                     + " " + System.getProperty( "os.version" ) + "\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."
  48             );
  49 /*API*/     deviceManager.printDevices();
  50 /*API*/     USBDevice[] devices = deviceManager.getDeviceByProductID( USBDeviceManager.USB_IIRO_16 );
  51             if( devices.length > 0 ) {
  52                 USB_DIO_Family device = ( USB_DIO_Family ) devices[ 0 ];    // get first device found
  53 /*API*/         device.reset()
  54 /*API*/             .setCommTimeout( 1000 );
  55 
  56                 /*
  57                  * demonstrate writing EEPROM
  58                  */
  59                 if( DEMO_EEPROM_WRITE ) {
  60                     System.out.print( "Writing to EEPROM ... " );
  61                     final int offset = 0x100;
  62                     String message = "USB-IIRO-16 sample program";
  63 /*API*/             byte[] prevData = device.customEEPROMRead( offset, message.length() );  // preserve current EEPROM contents
  64 /*API*/             device.setCommTimeout( 10000 )  // writing the entire EEPROM (which we're not doing here) can take a long time
  65 /*API*/                 .customEEPROMWrite( offset, message.getBytes() );
  66 /*API*/             String readMessage = new String( device.customEEPROMRead( offset, message.length() ) );
  67 /*API*/             device.customEEPROMWrite( offset, prevData )
  68 /*API*/                 .setCommTimeout( 1000 );
  69                     System.out.println(
  70                         readMessage.equals( message )
  71                             ? "successful"
  72                             : "failed: data read back did not compare" );
  73                 }   // if( DEMO_EEPROM_WRITE )
  74 
  75                 /*
  76                  * demonstrate reading EEPROM
  77                  */
  78                 System.out.println( "EEPROM contents:\n"
  79 /*API*/             + Arrays.toString( device.customEEPROMRead( 0, device.CUSTOM_EEPROM_SIZE ) ) );
  80 
  81                 /*
  82                  * demonstrate DIO configuration
  83                  */
  84                 System.out.print( "Configuring digital I/O ... " );
  85 /*API*/         final int numPorts = device.dio().getNumPorts();
  86                 final int numOutputPorts = numPorts / 2;
  87 /*API*/         final int numChannels = device.dio().getNumChannels();
  88                 final int numOutputChannels = numChannels / 2;
  89                 final int numInputChannels = numChannels - numOutputChannels;
  90                 boolean[] outputs = new boolean[ numPorts ];
  91                 Arrays.fill( outputs, 0, numOutputPorts, true );            // relays are outputs
  92                 Arrays.fill( outputs, numOutputPorts, numPorts, false );    // other ports are inputs
  93                 boolean[] values = new boolean[ numOutputChannels ];
  94                 Arrays.fill( values, true );                                // open all relays
  95 /*API*/         device.dio().configure( false, outputs, values );
  96                 System.out.println( "successful" );
  97 
  98                 /*
  99                  * demonstrate writing outputs individually
 100                  */
 101                 System.out.println(
 102                       "In the following demonstrations, you should hear the relays click\n"
 103                     + "and see the LED on the device blink."
 104                 );
 105                 System.out.print( "  CLosing relays:" );
 106                 for( int channel = 0; channel < numOutputChannels; channel++ ) {
 107                     System.out.print( " " + channel );
 108 /*API*/             device.dio().write( channel, false );   // close relay
 109                     Thread.sleep( 100 );
 110                 }   // for( int channel ...
 111                 System.out.print( "\n  Opening relays:" );
 112                 for( int channel = 0; channel < numOutputChannels; channel++ ) {
 113                     System.out.print( " " + channel );
 114 /*API*/             device.dio().write( channel, true );    // open relay
 115                     Thread.sleep( 100 );
 116                 }   // for( int channel ...
 117                 System.out.println();
 118 
 119                 /*
 120                  * demonstrate writing outputs as a group
 121                  */
 122                 System.out.print( "Closing all relays ... " );
 123                 Arrays.fill( values, false );   // close all relays
 124 /*API*/         device.dio().write( 0, values );
 125                 System.out.println( "successful" );
 126                 Thread.sleep( 2000 );
 127                 System.out.print( "Opening all relays ... " );
 128                 Arrays.fill( values, true );    // open all relays
 129 /*API*/         device.dio().write( 0, values );
 130                 System.out.println( "successful" );
 131 
 132                 /*
 133                  * demonstrate reading inputs individually
 134                  */
 135                 Thread.sleep( 1000 );
 136                 System.out.print( "CLosing every other relay:" );
 137                 for( int channel = 0; channel < numOutputChannels; channel += 2 ) {
 138                     System.out.print( " " + channel );
 139 /*API*/             device.dio().write( channel, false );   // close relay
 140                     Thread.sleep( 100 );
 141                 }   // for( int channel ...
 142                 System.out.println( "\nReading back relays:" );
 143                 for( int channel = 0; channel < numOutputChannels; channel++ ) {
 144 /*API*/             System.out.println( "  Channel " + channel + " = " + device.dio().read( channel ) );
 145                 }   // for( int channel ...
 146                 System.out.println( "Reading isolated inputs:" );
 147                 for( int channel = 0; channel < numInputChannels; channel++ ) {
 148 /*API*/             System.out.println( "  Channel " + ( numOutputChannels + channel ) + " = " + device.dio().read( numOutputChannels + channel ) );
 149                 }   // for( int channel ...
 150 
 151                 /*
 152                  * open all relays
 153                  */
 154                 Arrays.fill( values, true );
 155 /*API*/         device.dio().write( 0, values );
 156             } else
 157                 System.out.println( "No USB-IIRO-16 devices found on USB bus" );
 158 /*API*/     deviceManager.close();
 159         } catch( Exception ex ) {
 160             System.err.println( "Error \'" + ex.toString() + "\' occurred while manipulating device" );
 161             if(
 162                 deviceManager != null
 163 /*API*/         && deviceManager.isOpen()
 164             )
 165 /*API*/         deviceManager.close();
 166         }   // catch( ...
 167     }   // main()
 168 }   // class Sample
 169 
 170 /* end of file */