import java.awt.*; import java.awt.event.*; import xyzzy.hardware.*; class Thermo { static volatile boolean keepGoing = true; public static void main ( String[] argv ) { final Thread mainThread = Thread.currentThread(); final Frame fr = new Frame(); fr.addWindowListener( new WindowAdapter () { public void windowClosing ( final WindowEvent ev ) { ev.getWindow().dispose(); } public void windowClosed ( final WindowEvent ev ) { keepGoing = false; } }); final ThermometerGraph tg = new ThermometerGraph(); tg.setMaximum( 40 ); tg.setMinimum( 0 ); tg.setValue( 0 ); fr.add( tg ); fr.setBackground( SystemColor.control ); fr.pack(); fr.show(); try { final UsbIo usbio = UsbIo.getInstance(); try { final UsbIo.Device[] devices = usbio.findDevices(); /* At this moment, we can handle only one USBIO. */ if ( devices.length != 1 ) { if ( devices.length == 0 ) { System.err.println( "no USBIO is found" ); } else { System.err.println( "two or more USBIO are found" ); } return; } /* Configure the USBIO as an SPI controller: Port#1/bit3 is clock, Port#0/bit7 is data, Port#0/bit5 is select (active high). Other pins are assumed to be unconnected. */ final UsbSpiDevice spi = new UsbSpiDevice( devices[0], 3, 7, 0x20, 0x00, 0x20 ); final Ds1620 ds = new Ds1620( spi ); ds.open(); try { ds.setOneShot( true ); while ( keepGoing ) { tg.setValue( ds.getHiResTemperature() ); Thread.sleep( 5000 ); } } finally { ds.close(); } } finally { usbio.releaseDevices(); } } catch ( Throwable e ) { System.err.println( "Error/Exception: " + e.toString() ); e.printStackTrace(); } finally { System.exit( 0 ); } } }