1 /*
   2  * $RCSfile: Sample.java,v $
   3  * $Date: 2009/11/30 21:32:09 $
   4  * $Revision: 1.2 $
   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.2, 30 November 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 /*API*/ USBDeviceManager deviceManager = new USBDeviceManager();
  34         System.out.println(
  35             "USB-IIRO-16 sample program version: " + VERSION + "\n"
  36 /*API*/     + "  AIOUSB Java library version: " + deviceManager.VERSION_NUMBER + ", " + deviceManager.VERSION_DATE + "\n"
  37 /*API*/     + "  AIOUSB library version: " + deviceManager.getAIOUSBVersion() + ", " + deviceManager.getAIOUSBVersionDate() + "\n"
  38             + "  JRE version: " + System.getProperty( "java.version" ) + "\n"
  39             + "  OS version: " + System.getProperty( "os.name" )
  40                     + " " + System.getProperty( "os.arch" )
  41                     + " " + System.getProperty( "os.version" ) + "\n"
  42             + "  This program demonstrates controlling a USB-IIRO-16 device on\n"
  43             + "  the USB bus. For simplicity, it uses the first such device found\n"
  44             + "  on the bus."
  45         );
  46         try {
  47 /*API*/     deviceManager.open().printDevices();
  48 /*API*/     Vector<USBDevice> devices = deviceManager.getDeviceByProductID( deviceManager.USB_IIRO_16 );
  49             if( devices.size() > 0 ) {
  50                 USB_DIO_Family device = ( USB_DIO_Family ) devices.get( 0 );
  51 /*API*/         device.reset()
  52 /*API*/             .setCommTimeout( 1000 );
  53 
  54                 /*
  55                  * demonstrate writing EEPROM
  56                  */
  57                 if( DEMO_EEPROM_WRITE ) {
  58                     System.out.print( "Writing to EEPROM ... " );
  59                     final int offset = 0x100;
  60                     String message = "USB-IIRO-16 sample program";
  61 /*API*/             byte[] prevData = device.customEEPROMRead( offset, message.length() );  // preserve current EEPROM contents
  62 /*API*/             device.setCommTimeout( 10000 )  // writing the entire EEPROM (which we're not doing here) can take a long time
  63 /*API*/                 .customEEPROMWrite( offset, message.getBytes() );
  64 /*API*/             String readMessage = new String( device.customEEPROMRead( offset, message.length() ) );
  65 /*API*/             device.customEEPROMWrite( offset, prevData )
  66 /*API*/                 .setCommTimeout( 1000 );
  67                     System.out.println(
  68                         readMessage.equals( message )
  69                             ? "successful"
  70                             : "failed: data read back did not compare" );
  71                 }   // if( DEMO_EEPROM_WRITE )
  72 
  73                 /*
  74                  * demonstrate reading EEPROM
  75                  */
  76                 System.out.println( "EEPROM contents:\n"
  77 /*API*/             + Arrays.toString( device.customEEPROMRead( 0, device.CUSTOM_EEPROM_SIZE ) ) );
  78 
  79                 /*
  80                  * demonstrate DIO configuration
  81                  */
  82                 System.out.print( "Configuring digital I/O ... " );
  83 /*API*/         boolean[] outputs = new boolean[ device.dio().getNumPorts() ];
  84                 Arrays.fill( outputs, true );   // all ports are outputs
  85 /*API*/         boolean[] values = new boolean[ device.dio().getNumChannels() ];
  86                 Arrays.fill( values, false );   // open all relays
  87 /*API*/         device.dio().configure( false, outputs, values );
  88                 System.out.println( "successful" );
  89 
  90                 /*
  91                  * demonstrate writing outputs individually
  92                  */
  93                 System.out.println(
  94                       "In the following demonstrations, you should hear the relays click for the first\n"
  95                     + "16 channels. The latter 16 channels are not connected to relays and will turn\n"
  96                     + "on and off silently. Also. the LED on the device should blink during these demos."
  97                 );
  98                 System.out.print( "  CLosing relays:" );
  99 /*API*/         for( int channel = 0; channel < device.dio().getNumChannels(); channel++ ) {
 100                     System.out.print( " " + channel );
 101 /*API*/             device.dio().write( channel, true );    // close relay
 102                     Thread.sleep( 100 );
 103                 }   // for( int channel ...
 104                 System.out.print( "\n  Opening relays:" );
 105 /*API*/         for( int channel = 0; channel < device.dio().getNumChannels(); channel++ ) {
 106                     System.out.print( " " + channel );
 107 /*API*/             device.dio().write( channel, false );   // open relay
 108                     Thread.sleep( 100 );
 109                 }   // for( int channel ...
 110                 System.out.println();
 111 
 112                 /*
 113                  * demonstrate writing outputs as a group
 114                  */
 115                 System.out.print( "Closing all relays ... " );
 116                 Arrays.fill( values, true );    // close all relays
 117 /*API*/         device.dio().write( 0, values );
 118                 System.out.println( "successful" );
 119                 Thread.sleep( 2000 );
 120                 System.out.print( "Opening all relays ... " );
 121                 Arrays.fill( values, false );   // open all relays
 122 /*API*/         device.dio().write( 0, values );
 123                 System.out.println( "successful" );
 124             }   // if( devices.size() ...
 125 /*API*/     deviceManager.close();
 126         } catch( Exception ex ) {
 127             System.err.println( "Error \'" + ex.toString() + "\' occurred while manipulating device" );
 128 /*API*/     if( deviceManager.isOpen() )
 129 /*API*/         deviceManager.close();
 130         }   // catch( ...
 131     }   // main()
 132 
 133 }   // class Sample
 134 
 135 /* end of file */