package com.knutejohnson.pi.jpigpio.examples;

import com.knutejohnson.pi.jpigpio.JPigpio.*;

/**
 * JPigpioExamples
 *
 * Some sample programs to demonstrate the use of JPigpio.  Example programs:
 * Clock, Read, Write, HC04, Servo, Tick and Versions.
 *
 * @version 2020-12-30
 * @author  Knute Johnson
 */
public class JPigpioExamples {
    /**
     * Program entry point.
     *
     * @param   args    command line arguments.  No arguments will print the
     *                  list of sample programs and their parameters.  To run
     *                  one of the samples enter the program name and required
     *                  parameters on the command line.
     */
    public static void main(String... args) throws Exception {
        System.out.print("JPigpioExamples  ");
        if (args.length == 0) {
            System.out.println(
             "\nUsage: java JPigpioExamples.jar [classname] [ parameters ]");
            System.out.println("    Clock gpio");
            System.out.println("    Read gpio");
            System.out.println("    Write gpio");
            System.out.println("    HC04 trig echo");
            System.out.println("    Servo gpio");
            System.out.println("    Tick");
            System.out.println("    Versions");
        } else {
            switch (args[0].toLowerCase()) {
                case "clock":
                 com.knutejohnson.pi.jpigpio.examples.Clock.main(args[1]);
                 break;
                case "read":
                 com.knutejohnson.pi.jpigpio.examples.Read.main(args[1]);
                 break;
                case "write":
                 com.knutejohnson.pi.jpigpio.examples.Write.main(args[1]);
                 break;
                case "hc04":
               com.knutejohnson.pi.jpigpio.examples.HC04.main(args[1],args[2]);
                 break;
                case "servo ":
                 com.knutejohnson.pi.jpigpio.examples.Servo.main(args[1]);
                 break;
                case "tick":
                 com.knutejohnson.pi.jpigpio.examples.Tick.main();
                 break;
                case "versions":
                 com.knutejohnson.pi.jpigpio.examples.Versions.main();
                 break;
                default:
                 System.out.println("unknown class");
                 break;
            }
        }
    }
}

package com.knutejohnson.pi.jpigpio.examples;

import java.io.*;

import com.knutejohnson.pi.jpigpio.*;

/**
 * Clock runs the hardware clock on the specified GPIO pin for 2 seconds.
 *
 * @version 2020-12-26
 * @author  Knute Johnson
 */
public class Clock {
    /**
     * Program entry point.
     *
     * @param   args    command line arguments, the first is the GPIO pin
     *                  number and the second is the clock frequency
     */
    public static void main(String... args) {
        System.out.println("Clock");

        if (args.length == 2) {
            int gpio = Integer.parseInt(args[0]);
            int clkfreq = Integer.parseInt(args[1]);

            try (JPigpio j = new JPigpio()) {
                int retcod = j.hardwareClock(gpio,clkfreq);
                if (retcod < 0)
                    System.out.println(JPigpio.errorToString(retcod));
                Thread.sleep(2000);
                retcod = j.hardwareClock(gpio,0);
                if (retcod < 0)
                    System.out.println(JPigpio.errorToString(retcod));
            } catch (IOException|InterruptedException ex) {
                ex.printStackTrace();
            }
        } else
            System.out.println("Usage: Clock gpio clkfreq");
    }
}
package com.knutejohnson.pi.jpigpio.examples;

import java.io.*;

import com.knutejohnson.pi.jpigpio.*;
import static com.knutejohnson.pi.jpigpio.JPigpioConstants.*;

/**
 * HC04 is a simple example using JPigpio to read data from an HC04 sonar
 * device.
 *
 * @version 2021-01-28
 * @author  Knute Johnson
 */
public class HC04 {
    /**
     * Program entry point.
     *
     * @param   args    command line arguments, first is trigger GPIO pin
     *                  number and the second is echo GPIO pin number.
     * @throws  IOException if an I/O error occurs
     */
    public static void main(String... args) throws Exception {
        System.out.println("HC04");

        if (args.length != 2) {
            System.out.println("Usage: HC04 trig echo");
            System.exit(-1);
        }

        int trig = Integer.parseInt(args[0]);
        int echo = Integer.parseInt(args[1]);

        JPigpio j = new JPigpio();
        j.modeSet(trig,PI_OUTPUT);    // set trigger to output
        j.modeSet(echo,PI_INPUT);     // set echo to input
        int h = j.notifyOpen();       // get notification handle
        if (h < 0) {
            System.out.println(JPigpio.errorToString(h));
            System.exit(-1);
        }
        int retcod = j.notifyBegin(h,1<<echo); // begin notifications on echo
        if (retcod < 0) {
            System.out.println(JPigpio.errorToString(retcod));
            System.exit(-1);
        }

        String devName = String.format("/dev/pigpio%d",h);
        System.out.println(devName);
        FileInputStream fis = new FileInputStream(devName);

        while (true) {
            j.gpioTrigger(trig,10,1);     // high for 10us

            long up = j.readReport(fis).tick;
            long dn = j.readReport(fis).tick;
            long us = j.tickDiff(up,dn);

            // ignore spurious results
            if (us < 100 || us >= 38000)
                continue;

            System.out.printf("%5dus - %3.0fcm\n",us,us / 58.0);

            Thread.sleep(200);
        }
    }
}

package com.knutejohnson.pi.jpigpio.examples;

import java.io.*;

import com.knutejohnson.pi.jpigpio.*;
import static com.knutejohnson.pi.jpigpio.JPigpioConstants.*;

/**
 * Read is a simple example to read the level of a GPIO.
 *
 * @version 2020-12-26
 * @author  Knute Johnson
 */
public class Read {
    /**
     * Program entry point.
     *
     * @param   args    command line arguments, args[0] is the gpio number to
     *                  read
     */
    public static void main(String... args) {
        System.out.println("Read");

        if (args.length == 1) {
            int gpio = Integer.parseInt(args[0]);

            try (JPigpio j = new JPigpio()) {
                j.modeSet(gpio,PI_INPUT);
                int level = j.read(gpio);
                if (level >= 0)
                    System.out.printf("GPIO: %d - Level: %d\n",gpio,level);
                else
                    System.out.println(JPigpio.errorToString(level));
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        } else
            System.out.println("Usage: Read gpio");
    }
}

package com.knutejohnson.pi.jpigpio.examples;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

import com.knutejohnson.pi.jpigpio.*;
import static com.knutejohnson.pi.jpigpio.JPigpioConstants.*;

/**
 * Servo is a simple GUI test program to drive a small servo.
 *
 * @version 2020-12-26
 * @author  Knute Johnson
 */
public class Servo extends JFrame {
    /** Serial version UID */
    private static final long serialVersionUID = 0L;

    /** Reference to JPigpio object */
    private JPigpio j = null;

    /**
     * Create a new Servo object and center the servo.
     *
     * @param   gpio    GPIO pin for servo data
     */
    public Servo(int gpio) {
        super("JPigpio SERVO ");

        try {
            j = new JPigpio();
            j.setServoPulsewidth(gpio,1500);
        } catch (IOException ioe) {
            ioe.printStackTrace();
            System.exit(-1);

        }

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                try {
                    dispose();
                    j.setServoPulsewidth(gpio,1500);
                    Thread.sleep(500);
                    j.setServoPulsewidth(gpio,0);
                    j.disconnect();
                } catch (IOException|InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        });

        JSlider slider = new JSlider(500,2500,1500);
        slider.setLabelTable(slider.createStandardLabels(500));
        slider.setPaintLabels(true);
        slider.setMajorTickSpacing(100);
        slider.setPaintTicks(true);
        slider.addChangeListener(event -> {
            try {
                int retcod = j.setServoPulsewidth(gpio,slider.getValue());
                if (retcod < 0)
                    System.out.println(JPigpio.errorToString(retcod));
                retcod = j.getServoPulseWidth(gpio);
                if (retcod >= 0)
                    System.out.println(retcod);
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        });

        add(slider,BorderLayout.CENTER);

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        JMenu edit = menuBar.add(new JMenu("Edit"));
        JCheckBoxMenuItem invert = new JCheckBoxMenuItem("Invert");
        invert.addActionListener(event ->
         slider.setInverted(invert.getState()));
        edit.add(invert);

        setSize(400,100);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    /**
     * Program entry point.
     *
     * @param   args    command line arguments of which one is required with
     *                  the GPIO pin of the servo data wire.
     */
    public static void main(String... args) {
        if (args.length == 1)
            EventQueue.invokeLater(() -> {
                new Servo(Integer.parseInt(args[0]));
            });
        else
            System.out.println("Usage: Servo gpio");
    }
}

package com.knutejohnson.pi.jpigpio.examples;

import java.io.*;
import com.knutejohnson.pi.jpigpio.*;

/**
 * Tick
 *
 * Prints the number of pigpio ticks since boot.  From pigpio this is an
 * unsigned 32 bit number that rolls over after about 72 minutes.
 *
 * @version 2021-12-01
 * @author  Knute Johnson
 */
public class Tick {
    public static void main(String... args) {
        // create a new JPigpio object and connect it to the server on this
        // computer on the default port (8888)
        try (JPigpio j = new JPigpio()) {

            System.out.println("Tick");

            long ticks = j.tick();
            System.out.printf("    %10d - 0x%08X\n",ticks,ticks);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

package com.knutejohnson.pi.jpigpio.examples;

import java.io.*;

import com.knutejohnson.pi.jpigpio.*;
import static com.knutejohnson.pi.jpigpio.JPigpioConstants.*;

/**
 * Prints the pigpio version and hardware version of this Pi.
 *
 * @version 2020-12-26
 * @author  Knute Johnson
 */
public class Versions {
    /**
     * Program entry point.
     *
     * @param   args    command line arguments (not used)
     */
    public static void main(String... args) throws IOException {
        JPigpio j = new JPigpio();

        System.out.println("Versions");

        System.out.printf("    pigpio version: %d\n",j.pigpioVersion());

        System.out.printf("    JPigpio version: %s\n",JPigpio.getVersion());

        System.out.printf("    Hardware version: %X\n",j.hardwareVersion());

        j.disconnect();
    }
}

package com.knutejohnson.pi.jpigpio.examples;

import java.io.*;

import com.knutejohnson.pi.jpigpio.*;
import static com.knutejohnson.pi.jpigpio.JPigpioConstants.*;

/**
 * Write is a simple example to set a GPIO pin high for 1.5 seconds.
 *
 * @version 2020-12-30
 * @author  Knute Johnson
 */
public class Write {
    /**
     * Program entry point.
     *
     * @param   args    command line arguments, only one argument is used for
     *                  the GPIO pin number to turn on
     */
    public static void main(String... args) {
        System.out.println("Write");

        if (args.length == 1) {
            int gpio = Integer.parseInt(args[0]);

            try (JPigpio j = new JPigpio()) {
                j.setDebugMode(true);
                j.modeSet(gpio,PI_OUTPUT);
                int res = j.write(gpio,PI_ON);

                Thread.sleep(1500);
    
                j.write(gpio,PI_OFF);
            } catch (IOException|InterruptedException ex) {
                ex.printStackTrace();
            }
        } else
            System.out.println("Usage: Write gpio");
    }
}