User Tools

Site Tools


morse_code_code

This is an old revision of the document!


The LED Tutorial is where I started, and I had to tweak the code so that I didn't get a “GpioPinExistsException.” I added a finalize() method

LED.java
import com.pi4j.io.gpio.*;
import com.pi4j.io.gpio.event.*;
 
/**
 * Write a description of class LED here.
 * @author Ian Utting @author Fabio Heday 
 * Chris Thiel added the finalize() method to aviod GpioPinExistsException
 * @version 10 Aug 18
 */
public class LED implements GpioPinListenerDigital
{
    /* The LED gpio*/
    private GpioPinDigitalOutput ledPin;
    private GpioController gpio;
    /**
     * Constructor for objects of class LED
     */
    public LED()
    {
        //GpioUtil.enableNonPrivilegedAccess();
 
        gpio = GpioFactory.getInstance();
 
        ledPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_25, "LED", PinState.LOW);
    }
 
    protected void finalize(){
        gpio.unprovisionPin(ledPin);
    }
 
    /**
     * Flash the LED for a given amount of time @param  ms   the time to flash the LED in milliseconds
     */
    public void flash(int ms)
    {
        ledPin.high();
        try {
            Thread.sleep(ms);
        }
        catch (InterruptedException e) {
        }
        ledPin.low();
    }
 
    /**
     * Turns on the LED
     */
    public void on()
    {
        ledPin.high();
    }
 
    /**
     * Turns off the LED
     */
    public void off()
    {
        ledPin.low();
    }
 
    /**
     * toggle LED state
     */
    public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event)
    {
        ledPin.toggle();
    }
}
morse_code_code.1533960413.txt.gz · Last modified: 2018/08/11 00:06 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki