| 1 |
2 |
ZTEX |
/*!
|
| 2 |
|
|
Java Driver API for the ZTEX Firmware Kit
|
| 3 |
|
|
Copyright (C) 2008-2009 ZTEX e.K.
|
| 4 |
|
|
http://www.ztex.de
|
| 5 |
|
|
|
| 6 |
|
|
This program is free software; you can redistribute it and/or modify
|
| 7 |
|
|
it under the terms of the GNU General Public License version 3 as
|
| 8 |
|
|
published by the Free Software Foundation.
|
| 9 |
|
|
|
| 10 |
|
|
This program is distributed in the hope that it will be useful, but
|
| 11 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 13 |
|
|
General Public License for more details.
|
| 14 |
|
|
|
| 15 |
|
|
You should have received a copy of the GNU General Public License
|
| 16 |
|
|
along with this program; if not, see http://www.gnu.org/licenses/.
|
| 17 |
|
|
!*/
|
| 18 |
|
|
|
| 19 |
|
|
/*
|
| 20 |
|
|
Scan bus for devices with ZTEX descriptor 1 and/or Cypress EZ-USB FX2 devices
|
| 21 |
|
|
*/
|
| 22 |
|
|
package ztex;
|
| 23 |
|
|
|
| 24 |
|
|
import java.io.*;
|
| 25 |
|
|
import java.util.*;
|
| 26 |
|
|
|
| 27 |
|
|
import ch.ntb.usb.*;
|
| 28 |
|
|
|
| 29 |
|
|
public class ZtexScanBus1 {
|
| 30 |
|
|
private Vector<ZtexDevice1> devices = new Vector<ZtexDevice1>();
|
| 31 |
|
|
|
| 32 |
|
|
// ******* ZtexScanBus1 ********************************************************
|
| 33 |
|
|
public ZtexScanBus1 (int usbVendorId, int usbProductId, boolean scanCypress, boolean quiet, int interfaceVersion, int productId0, int productId1, int productId2, int productId3 ) {
|
| 34 |
|
|
LibusbJava.usb_find_busses();
|
| 35 |
|
|
LibusbJava.usb_find_devices();
|
| 36 |
|
|
|
| 37 |
|
|
Usb_Bus bus = LibusbJava.usb_get_busses();
|
| 38 |
|
|
|
| 39 |
|
|
while ( bus != null ) {
|
| 40 |
|
|
Usb_Device dev = bus.getDevices();
|
| 41 |
|
|
while ( dev != null ) {
|
| 42 |
|
|
try {
|
| 43 |
|
|
try {
|
| 44 |
|
|
ZtexDevice1 zdev = new ZtexDevice1( dev, usbVendorId, usbProductId );
|
| 45 |
|
|
if ( ( scanCypress && zdev.isCypress() ) ||
|
| 46 |
|
|
( zdev.valid() && (interfaceVersion<0 || zdev.interfaceVersion()==interfaceVersion) && zdev.compatible(productId0, productId1, productId2, productId3) ) ) {
|
| 47 |
|
|
devices.add( zdev );
|
| 48 |
|
|
}
|
| 49 |
|
|
}
|
| 50 |
|
|
catch ( ZtexDescriptorException e ) {
|
| 51 |
|
|
if ( scanCypress && usbVendorId == ZtexDevice1.cypressVendorId && usbProductId == ZtexDevice1.cypressProductId ) {
|
| 52 |
|
|
try {
|
| 53 |
|
|
ZtexDevice1 zdev = new ZtexDevice1( dev, -1, -1 );
|
| 54 |
|
|
if ( zdev.isCypress() ) devices.add( zdev );
|
| 55 |
|
|
}
|
| 56 |
|
|
catch ( ZtexDescriptorException e2 ) {
|
| 57 |
|
|
if ( ! quiet )
|
| 58 |
|
|
System.err.println( e2.getLocalizedMessage() ); // should never occur
|
| 59 |
|
|
}
|
| 60 |
|
|
}
|
| 61 |
|
|
else {
|
| 62 |
|
|
if ( ! quiet )
|
| 63 |
|
|
System.err.println( e.getLocalizedMessage() );
|
| 64 |
|
|
}
|
| 65 |
|
|
}
|
| 66 |
|
|
}
|
| 67 |
|
|
catch ( UsbException e ) {
|
| 68 |
|
|
if ( ! quiet )
|
| 69 |
|
|
System.err.println( e.getLocalizedMessage() );
|
| 70 |
|
|
}
|
| 71 |
|
|
dev = dev.getNext();
|
| 72 |
|
|
}
|
| 73 |
|
|
bus = bus.getNext();
|
| 74 |
|
|
}
|
| 75 |
|
|
}
|
| 76 |
|
|
|
| 77 |
|
|
public ZtexScanBus1 (int usbVendorId, int usbProductId, boolean scanCypress, boolean quiet, int interfaceVersion ) {
|
| 78 |
|
|
this(usbVendorId, usbProductId, scanCypress, quiet, interfaceVersion, -1,-1,-1,-1 );
|
| 79 |
|
|
}
|
| 80 |
|
|
|
| 81 |
|
|
public ZtexScanBus1 (int usbVendorId, int usbProductId, boolean scanCypress, boolean quiet ) {
|
| 82 |
|
|
this(usbVendorId, usbProductId, scanCypress, quiet, -1, -1,-1,-1,-1 );
|
| 83 |
|
|
}
|
| 84 |
|
|
|
| 85 |
|
|
// ******* printBus ************************************************************
|
| 86 |
|
|
public void printBus( PrintStream out ) {
|
| 87 |
|
|
for (int i=0; i<devices.size(); i++ ) {
|
| 88 |
|
|
out.println( i + ": " + devices.elementAt(i).toString() );
|
| 89 |
|
|
}
|
| 90 |
|
|
}
|
| 91 |
|
|
|
| 92 |
|
|
// ******* numberOfDevices *****************************************************
|
| 93 |
|
|
public final int numberOfDevices () {
|
| 94 |
|
|
return devices.size();
|
| 95 |
|
|
}
|
| 96 |
|
|
|
| 97 |
|
|
// ******* device **************************************************************
|
| 98 |
|
|
public final ZtexDevice1 device (int i) throws IndexOutOfBoundsException {
|
| 99 |
|
|
if ( i<0 || i>=devices.size() )
|
| 100 |
|
|
throw new IndexOutOfBoundsException( "Device number out of range. Valid numbers are 0.." + (devices.size()-1) );
|
| 101 |
|
|
return devices.elementAt(i);
|
| 102 |
|
|
}
|
| 103 |
|
|
}
|
| 104 |
|
|
|