Program for generating morse code via a callback.

Committer:
stevep
Date:
Mon Apr 30 21:05:30 2012 +0000
Revision:
0:44a2d3f18e9f
Child:
2:bdf2fa2879cf
publish morse generator

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stevep 0:44a2d3f18e9f 1 #include "morsegenerator.h"
stevep 0:44a2d3f18e9f 2
stevep 0:44a2d3f18e9f 3
stevep 0:44a2d3f18e9f 4 MorseGenerator::MorseGenerator(callback_type _callback) {
stevep 0:44a2d3f18e9f 5 generate_morse_map();
stevep 0:44a2d3f18e9f 6 callback = _callback;
stevep 0:44a2d3f18e9f 7 }
stevep 0:44a2d3f18e9f 8
stevep 0:44a2d3f18e9f 9 void MorseGenerator::generate_morse_map() {
stevep 0:44a2d3f18e9f 10 add_mappings("ABCDEFGHIJKLMNOPQRSTUVWXYZ", MORSE_LETTERS);
stevep 0:44a2d3f18e9f 11 add_mappings("1234567890", MORSE_NUMBERS);
stevep 0:44a2d3f18e9f 12 }
stevep 0:44a2d3f18e9f 13
stevep 0:44a2d3f18e9f 14 void MorseGenerator::add_mappings(std::string morse_chars, const std::string morse_codes[]) {
stevep 0:44a2d3f18e9f 15 for (int i=0; i < morse_chars.length(); i++) {
stevep 0:44a2d3f18e9f 16 morse_map.insert(MapType::value_type(morse_chars[i], morse_codes[i]));
stevep 0:44a2d3f18e9f 17 }
stevep 0:44a2d3f18e9f 18 }
stevep 0:44a2d3f18e9f 19
stevep 0:44a2d3f18e9f 20 void MorseGenerator::transmit(std::string message) {
stevep 0:44a2d3f18e9f 21 for (int i=0; i < message.length(); i++) {
stevep 0:44a2d3f18e9f 22 if (strncmp(&message[i], " ", 1) != 0) {
stevep 0:44a2d3f18e9f 23 transmit(message[i]);
stevep 0:44a2d3f18e9f 24 }
stevep 0:44a2d3f18e9f 25 wait_ms(DIT_MS * 7);
stevep 0:44a2d3f18e9f 26 }
stevep 0:44a2d3f18e9f 27 }
stevep 0:44a2d3f18e9f 28
stevep 0:44a2d3f18e9f 29 void MorseGenerator::transmit(char letter) {
stevep 0:44a2d3f18e9f 30 int d;
stevep 0:44a2d3f18e9f 31 std::string code = morse_map[letter];
stevep 0:44a2d3f18e9f 32
stevep 0:44a2d3f18e9f 33 for (int i=0; i < code.length(); i++) {
stevep 0:44a2d3f18e9f 34 if (strncmp(&code[i], ".", 1) == 0) {
stevep 0:44a2d3f18e9f 35 d = 1;
stevep 0:44a2d3f18e9f 36 } else {
stevep 0:44a2d3f18e9f 37 d = 3;
stevep 0:44a2d3f18e9f 38 }
stevep 0:44a2d3f18e9f 39 callback(1);
stevep 0:44a2d3f18e9f 40 wait_ms(DIT_MS * d);
stevep 0:44a2d3f18e9f 41 callback(0);
stevep 0:44a2d3f18e9f 42 wait_ms(DIT_MS);
stevep 0:44a2d3f18e9f 43 }
stevep 0:44a2d3f18e9f 44 }