morse_code_code
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| morse_code_code [2018/08/11 00:14] – frchris | morse_code_code [2018/08/11 02:16] (current) – frchris | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| - | + | =====LED class===== | |
| + | I am using '' | ||
| <file java LED.java> | <file java LED.java> | ||
| import com.pi4j.io.gpio.*; | import com.pi4j.io.gpio.*; | ||
| Line 75: | Line 77: | ||
| </ | </ | ||
| - | -[[The | + | =====LEDTester Class===== |
| - | | + | <file java LEDTester.java> |
| - | | + | /** |
| + | * 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 | ||
| + | { | ||
| + | | ||
| + | |||
| + | //unit of time, in milliseconds | ||
| + | private final static int UNIT = 200; | ||
| + | |||
| + | public static void main(String | ||
| + | | ||
| + | |||
| + | 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 == '/' | ||
| + | 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(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | =====String to Morse class===== | ||
| + | This is unchanged from the original | ||
| + | <file java StringToMorse.java> | ||
| + | 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< | ||
| + | static { | ||
| + | // Upper case letters | ||
| + | /* A */ code.put(' | ||
| + | /* B */ code.put(' | ||
| + | /* C */ code.put(' | ||
| + | /* D */ code.put(' | ||
| + | /* E */ code.put(' | ||
| + | /* F */ code.put(' | ||
| + | /* G */ code.put(' | ||
| + | /* H */ code.put(' | ||
| + | /* I */ code.put(' | ||
| + | /* J */ code.put(' | ||
| + | /* K */ code.put(' | ||
| + | /* L */ code.put(' | ||
| + | /* M */ code.put(' | ||
| + | /* N */ code.put(' | ||
| + | /* O */ code.put(' | ||
| + | /* P */ code.put(' | ||
| + | /* Q */ code.put(' | ||
| + | /* R */ code.put(' | ||
| + | /* S */ code.put(' | ||
| + | /* T */ code.put(' | ||
| + | /* U */ code.put(' | ||
| + | /* V */ code.put(' | ||
| + | /* W */ code.put(' | ||
| + | /* X */ code.put(' | ||
| + | /* Y */ code.put(' | ||
| + | /* Z */ code.put(' | ||
| + | |||
| + | // Lower case letters map to the same values as upper case | ||
| + | /* A */ code.put(' | ||
| + | /* B */ code.put(' | ||
| + | /* C */ code.put(' | ||
| + | /* D */ code.put(' | ||
| + | /* E */ code.put(' | ||
| + | /* F */ code.put(' | ||
| + | /* G */ code.put(' | ||
| + | /* H */ code.put(' | ||
| + | /* I */ code.put(' | ||
| + | /* J */ code.put(' | ||
| + | /* K */ code.put(' | ||
| + | /* L */ code.put(' | ||
| + | /* M */ code.put(' | ||
| + | /* N */ code.put(' | ||
| + | /* O */ code.put(' | ||
| + | /* P */ code.put(' | ||
| + | /* Q */ code.put(' | ||
| + | /* R */ code.put(' | ||
| + | /* S */ code.put(' | ||
| + | /* T */ code.put(' | ||
| + | /* U */ code.put(' | ||
| + | /* V */ code.put(' | ||
| + | /* W */ code.put(' | ||
| + | /* X */ code.put(' | ||
| + | /* Y */ code.put(' | ||
| + | /* Z */ code.put(' | ||
| + | |||
| + | // Digits | ||
| + | /* 0 */ code.put(' | ||
| + | /* 1 */ code.put(' | ||
| + | /* 2 */ code.put(' | ||
| + | /* 3 */ code.put(' | ||
| + | /* 4 */ code.put(' | ||
| + | /* 5 */ code.put(' | ||
| + | /* 6 */ code.put(' | ||
| + | /* 7 */ code.put(' | ||
| + | /* 8 */ code.put(' | ||
| + | /* 9 */ code.put(' | ||
| + | |||
| + | // 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 | ||
| + | * @return The encoded string. | ||
| + | * @throws IllegalArgumentException | ||
| + | | ||
| + | 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(" | ||
| + | } | ||
| + | // trim the trailing slash | ||
| + | result = result.substring(0, | ||
| + | return result; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
morse_code_code.1533960878.txt.gz · Last modified: 2018/08/11 00:14 by frchris