1 /*
   2  * $RCSfile: Sample.java,v $
   3  * $Date: 2009/12/25 18:30:47 $
   4  * $Revision: 1.3 $
   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.3, 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-DIO-32 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-DIO-32 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_DIO_32 );
  51             if( devices.length > 0 ) {
  52                 USB_DIO_32_Family device = ( USB_DIO_32_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-DIO-32 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*/         boolean[] outputs = new boolean[ device.dio().getNumPorts() ];
  86                 Arrays.fill( outputs, true );   // all ports are outputs
  87 /*API*/         boolean[] values = new boolean[ device.dio().getNumChannels() ];
  88                 Arrays.fill( values, false );   // turn all outputs off
  89 /*API*/         device.dio().configure( false, outputs, values );
  90                 System.out.println( "successful" );
  91 
  92                 /*
  93                  * demonstrate writing outputs individually
  94                  */
  95                 System.out.print( "Turning all outputs on:" );
  96 /*API*/         for( int channel = 0; channel < device.dio().getNumChannels(); channel++ ) {
  97                     System.out.print( " " + channel );
  98 /*API*/             device.dio().write( channel, true );
  99                     Thread.sleep( 100 );
 100                 }   // for( int channel ...
 101                 System.out.print( "\nTurning all outputs off:" );
 102 /*API*/         for( int channel = 0; channel < device.dio().getNumChannels(); channel++ ) {
 103                     System.out.print( " " + channel );
 104 /*API*/             device.dio().write( channel, false );
 105                     Thread.sleep( 100 );
 106                 }   // for( int channel ...
 107                 System.out.println();
 108 
 109                 /*
 110                  * demonstrate writing outputs as a group
 111                  */
 112                 System.out.print( "Turning all outputs on ... " );
 113                 Arrays.fill( values, true );
 114 /*API*/         device.dio().write( 0, values );
 115                 System.out.println( "successful" );
 116                 Thread.sleep( 2000 );
 117                 System.out.print( "Turning all outputs off ... " );
 118                 Arrays.fill( values, false );
 119 /*API*/         device.dio().write( 0, values );
 120                 System.out.println( "successful" );
 121             } else
 122                 System.out.println( "No USB-DIO-32 devices found on USB bus" );
 123 /*API*/     deviceManager.close();
 124         } catch( Exception ex ) {
 125             System.err.println( "Error \'" + ex.toString() + "\' occurred while manipulating device" );
 126             if(
 127                 deviceManager != null
 128 /*API*/         && deviceManager.isOpen()
 129             )
 130 /*API*/         deviceManager.close();
 131         }   // catch( ...
 132     }   // main()
 133 
 134 }   // class Sample
 135 
 136 /* end of file */