morse_code_code
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
morse_code_code [2018/08/11 01:13] – [Links] frchris | morse_code_code [2018/08/11 02:14] – [LED class] frchris | ||
---|---|---|---|
Line 3: | Line 3: | ||
=====LED class===== | =====LED class===== | ||
+ | I am using '' | ||
+ | |||
<file java LED.java> | <file java LED.java> | ||
import com.pi4j.io.gpio.*; | import com.pi4j.io.gpio.*; | ||
Line 172: | Line 174: | ||
This is unchanged from the original | This is unchanged from the original | ||
<file java StringToMorse.java> | <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.txt · Last modified: 2018/08/11 02:16 by frchris