A MorseGenerator library that uses LED to blink morse code
Dependents: MorseGenerator_Example2 9v1
MorseGenerator.cpp@1:bad1b4ca49df, 2014-09-17 (annotated)
- Committer:
- screamer
- Date:
- Wed Sep 17 17:09:04 2014 +0300
- Revision:
- 1:bad1b4ca49df
- Parent:
- 0:0ad54d10593c
* fixed - main.h usage
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wm | 0:0ad54d10593c | 1 | #include "MorseGenerator.h" |
wm | 0:0ad54d10593c | 2 | |
wm | 0:0ad54d10593c | 3 | |
wm | 0:0ad54d10593c | 4 | MorseGenerator::MorseGenerator(callback_type _callback) { |
wm | 0:0ad54d10593c | 5 | generate_morse_map(); |
wm | 0:0ad54d10593c | 6 | callback = _callback; |
wm | 0:0ad54d10593c | 7 | } |
wm | 0:0ad54d10593c | 8 | |
wm | 0:0ad54d10593c | 9 | void MorseGenerator::generate_morse_map() { |
wm | 0:0ad54d10593c | 10 | add_mappings("ABCDEFGHIJKLMNOPQRSTUVWXYZ", MORSE_LETTERS); |
wm | 0:0ad54d10593c | 11 | add_mappings("1234567890", MORSE_NUMBERS); |
wm | 0:0ad54d10593c | 12 | } |
wm | 0:0ad54d10593c | 13 | |
wm | 0:0ad54d10593c | 14 | void MorseGenerator::add_mappings(string morse_chars, const string morse_codes[]) { |
wm | 0:0ad54d10593c | 15 | for (int i=0; i < morse_chars.length(); i++) { |
wm | 0:0ad54d10593c | 16 | morse_map.insert(MapType::value_type(morse_chars[i], morse_codes[i])); |
wm | 0:0ad54d10593c | 17 | } |
wm | 0:0ad54d10593c | 18 | } |
wm | 0:0ad54d10593c | 19 | |
wm | 0:0ad54d10593c | 20 | void MorseGenerator::transmit(string message) { |
wm | 0:0ad54d10593c | 21 | for (int i=0; i < message.length(); i++) { |
wm | 0:0ad54d10593c | 22 | if (strncmp(&message[i], " ", 1) != 0) { |
wm | 0:0ad54d10593c | 23 | transmit(message[i]); |
wm | 0:0ad54d10593c | 24 | } |
wm | 0:0ad54d10593c | 25 | wait_ms(DIT_MS * 7); |
wm | 0:0ad54d10593c | 26 | } |
wm | 0:0ad54d10593c | 27 | } |
wm | 0:0ad54d10593c | 28 | |
wm | 0:0ad54d10593c | 29 | void MorseGenerator::transmit(char letter) { |
wm | 0:0ad54d10593c | 30 | int d; |
wm | 0:0ad54d10593c | 31 | string code = morse_map[letter]; |
wm | 0:0ad54d10593c | 32 | |
wm | 0:0ad54d10593c | 33 | for (int i=0; i < code.length(); i++) { |
wm | 0:0ad54d10593c | 34 | if (strncmp(&code[i], ".", 1) == 0) { |
wm | 0:0ad54d10593c | 35 | d = 1; |
wm | 0:0ad54d10593c | 36 | } else { |
wm | 0:0ad54d10593c | 37 | d = 3; |
wm | 0:0ad54d10593c | 38 | } |
wm | 0:0ad54d10593c | 39 | callback(1); |
wm | 0:0ad54d10593c | 40 | wait_ms(DIT_MS * d); |
wm | 0:0ad54d10593c | 41 | callback(0); |
wm | 0:0ad54d10593c | 42 | wait_ms(DIT_MS); |
wm | 0:0ad54d10593c | 43 | } |
wm | 0:0ad54d10593c | 44 | } |