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
I am using RaspiPin.GPIO_25
since chose to wire my LED to physical pin 37, aka BCM GPIO 26
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(); } }
/** * This class tests LED by flashing some morse code. 2014 verion by * @author Ian Utting * @author Fabio Heday * * Chris Thiel added the finalize() method to aviod GpioPinExistsException * @version 10 Aug 18 */ public class LEDTester { public LED pin1; //unit of time, in milliseconds private final static int UNIT = 200; public static void main(String [] args) { LEDTester lt = new LEDTester(); String s = "SOS SOS SOS"; // if ( args.length > 0 && args[0] != null && !args[0].equals ("") ) s = args[0]; lt.flashMorse(s); lt.finalize(); } /* * create a new LED instance */ public LEDTester() { this(new LED()); } /* * Creates a LED instance based on a LED object */ public LEDTester(LED p) { pin1 = p; } /* * Flashes the LED 10 times */ public void flash10Times() { for(int i = 0; i < 10; i++) { pin1.flash(200); try { Thread.sleep (200); } catch (InterruptedException e) {} } } /* * Sends the morse code using the LED. */ public void flashMorse(String msg) { char [] morse = StringToMorse.translate(msg).toCharArray(); // . = 1, - = 3, intra-char = 1, inter-char = 3, space = 7.", for (char atom : morse) { if (atom == '.') { pin1.flash(UNIT); } else if (atom == '-') { pin1.flash(3*UNIT); } else if (atom == '/') { // inter-symbol gap sleep (1); // plus one leading and one trailing == 3 } else { // must be a space sleep (5); // plus one leading and one trailing == 7 } sleep (1); // common gap } } /* * Wait units times the defined unit of time */ private void sleep(int units) { try { Thread.sleep(units*UNIT); } catch (InterruptedException e) { } } protected void finalize(){ pin1.finalize(); } }
This is unchanged from the original
import java.util.*; import static java.lang.Character.*; /** * Translate a String into Morse code. * * Maps lower case letters to upper case. * Returned string contains the Morse equivalent of the parameter, * with codons sperated by slashes, and spaces represented by spaces.. * * @author Ian Utting * @version 1.0 */ public class StringToMorse { private static final Map<Character, String> code = new HashMap<>(); static { // Upper case letters /* A */ code.put('A', ".-"); /* B */ code.put('B', "-..."); /* C */ code.put('C', "-.-."); /* D */ code.put('D', "-.."); /* E */ code.put('E', "."); /* F */ code.put('F', "..-."); /* G */ code.put('G', "--."); /* H */ code.put('H', "...."); /* I */ code.put('I', ".."); /* J */ code.put('J', ".---"); /* K */ code.put('K', "-.-"); /* L */ code.put('L', ".-.."); /* M */ code.put('M', "--"); /* N */ code.put('N', "-."); /* O */ code.put('O', "---"); /* P */ code.put('P', ".--."); /* Q */ code.put('Q', "--.-"); /* R */ code.put('R', ".-."); /* S */ code.put('S', "..."); /* T */ code.put('T', "-"); /* U */ code.put('U', "..-"); /* V */ code.put('V', "...-"); /* W */ code.put('W', ".--"); /* X */ code.put('X', "-..-"); /* Y */ code.put('Y', "-.--"); /* Z */ code.put('Z', "--.."); // Lower case letters map to the same values as upper case /* A */ code.put('a', ".-"); /* B */ code.put('b', "-..."); /* C */ code.put('c', "-.-."); /* D */ code.put('d', "-.."); /* E */ code.put('e', "."); /* F */ code.put('f', "..-."); /* G */ code.put('g', "--."); /* H */ code.put('h', "...."); /* I */ code.put('i', ".."); /* J */ code.put('j', ".---"); /* K */ code.put('k', "-.-"); /* L */ code.put('l', ".-.."); /* M */ code.put('m', "--"); /* N */ code.put('n', "-."); /* O */ code.put('o', "---"); /* P */ code.put('p', ".--."); /* Q */ code.put('q', "--.-"); /* R */ code.put('r', ".-."); /* S */ code.put('s', "..."); /* T */ code.put('t', "-"); /* U */ code.put('u', "..-"); /* V */ code.put('v', "...-"); /* W */ code.put('w', ".--"); /* X */ code.put('x', "-..-"); /* Y */ code.put('y', "-.--"); /* Z */ code.put('z', "--.."); // Digits /* 0 */ code.put('0', "-----"); /* 1 */ code.put('1', ".----"); /* 2 */ code.put('2', "..---"); /* 3 */ code.put('3', "...--"); /* 4 */ code.put('4', "....-"); /* 5 */ code.put('5', "....."); /* 6 */ code.put('6', "-...."); /* 7 */ code.put('7', "--..."); /* 8 */ code.put('8', "---.."); /* 9 */ code.put('9', "----."); // punctuation /* . */ code.put('.', ".-.-.-"); /* , */ code.put(',', "--..--"); /* : */ code.put(':', "---..."); /* ? */ code.put('?', "..--.."); /* ' */ code.put('\'', ".----."); /* - */ code.put('-', "-....-"); /* / */ code.put('/', "-..-."); /* ( */ code.put('(', "-.--.-"); /* ) */ code.put(')', "-.--.-"); /* " */ code.put('"', ".-..-."); /* @ */ code.put('@', ".--.-."); /* = */ code.put('=', "-...-"); // Special code for a space character. code.put(' ', " "); } /** * Translate a string into its Morse code equivalent * * Spaces in message are encoded as spaces in the returned string. Codons are separated by slashes * * @param message The string to be translated * @return The encoded string. * @throws IllegalArgumentException if there is a character in message which is not codeable in Morse */ public static String translate(String message) { String result = new String(); for(char c : message.toCharArray()) { if (code.containsKey(c)) result = result + code.get(c) + "/"; else throw new IllegalArgumentException("Character " + c + "is not in Morse's code"); } // trim the trailing slash result = result.substring(0, result.length() - 1); return result; } }