package com.teravation.labjack.demo;

/**
 * This is a demonstration of the LabJackJava API, printing the status of the D and IO ports on the LabJack at
 * the specified interval.
 *
 * <p />Copyright &copy; 2003 <a href="http://www.teravation.com">Teravation</a>. All rights reserved.
 *
 * @created (2/8/2003 1:40:51 AM)
 * @version 2.0
 * @author Chris Reigrut
 */
import com.teravation.labjack.*;
public class LabJackDIODemo {
    private int delayMs = 500;
    
    /**
     * LabJackDIODemo constructor (empty)
     */
    private LabJackDIODemo() {
        super();
    }
    
    /**
     * LabjackDIODemo constructor (specifies delay period)
     *
     * @created (2/8/2003 1:42:46 AM)
     * @param delayMs the number of milliseconds to delay during each loop
     */
    public LabJackDIODemo(int delayMs) {
        super();
        this.setDelayMs(delayMs);
    }
    
    /**
     * Returns the current loop delay value.
     *
     * @created (2/8/2003 1:41:49 AM)
     * @return the current loop delay value.
     */
    private int getDelayMs() {
        return delayMs;
    }
    
    /**
     * Insert the method's description here.
     * @created (2/8/2003 1:41:05 AM)
     * @param args java.lang.String[]
     */
    public static void main(String[] args) {
        int delay = 500;
        if (args.length > 0) {
            try {
                delay = Integer.parseInt(args[0]);
            } catch (NumberFormatException nfe) {
                System.out.println("Invalid delay...using delay of " + delay + "ms");
            }
            if (delay < 0) {
                System.out.println("Invalid delay...using delay of " + delay + "ms");
            } else {
                System.out.println("Delay = " + delay + "ms");
            }
        }
        LabJackDIODemo demo = new LabJackDIODemo(delay);
        demo.run();
    }
    
    /**
     * The main demo application--it uses the first labjack found and
     * prints the status of the D and IO channels (X = set, 0 = not set)
     * every <code>delayMs</code> milliseconds.
     *
     * @created (2/8/2003 1:41:22 AM)
     */
    public void run() {
        LabJack[] labjacks = null;
        try {
            // Get the list of all available LabJacks
            labjacks = new LabJackFactory().getLabJacks();

            if (labjacks == null || labjacks.length == 0) {
                // If there are no LabJacks found, print a message and exit
                System.out.println("No LabJacks found!");
                System.exit(0);
            }
            // Use the first labjack found
            LabJack lj = labjacks[0];
            System.out.println("Using " + lj);

            while (true) {
                // Update the LabJack object with all of the D & IO values from the device
                lj.updateAllAOsAndDigitals();

                // Print a row such as XOOX OOXOOOXOOOXOOXOX
                //                     (IO)        (D)
                // where channel 0 is on the left.

                // Print an X if the IO channel is set, O if not
                for (int io = 0; io < LabJackConstants.IO_CHANNELS; io++) {
                    System.out.print(lj.getIO(io) ? "X" : "O");
                }
                System.out.print(" ");
                // Print an X if the D channel is set, O if not
                for (int d = 0; d < LabJackConstants.D_CHANNELS; d++) {
                    System.out.print(lj.getD(d) ? "X" : "O");
                }
                System.out.println();
                try {
                    // Sleep for the specified period of time
                    Thread.sleep(this.getDelayMs());
                } catch (InterruptedException ie) {
                }
            }
        } catch (LabJackException lje) {
            // If an exception is found, print the stack trace and exit
            lje.printStackTrace(System.err);
        }
    }
    
    /**
     * Sets the number of milliseconds to delay during each loop.
     * 
     * @created (2/8/2003 1:41:49 AM)
     * @param newDelayMs the number of milliseconds to delay during each loop.
     */
    private void setDelayMs(int newDelayMs) {
        delayMs = newDelayMs;
    }
}