Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
morsegenerator.cpp
- Committer:
- stevep
- Date:
- 2015-02-03
- Revision:
- 5:c37175de8627
- Parent:
- 2:bdf2fa2879cf
File content as of revision 5:c37175de8627:
#include "morsegenerator.h"
MorseGenerator::MorseGenerator(callback_type _callback) {
generate_morse_map();
callback = _callback;
}
void MorseGenerator::generate_morse_map() {
add_mappings("ABCDEFGHIJKLMNOPQRSTUVWXYZ", MORSE_LETTERS);
add_mappings("1234567890", MORSE_NUMBERS);
}
void MorseGenerator::add_mappings(string morse_chars, const string morse_codes[]) {
for (int i=0; i < morse_chars.length(); i++) {
morse_map.insert(MapType::value_type(morse_chars[i], morse_codes[i]));
}
}
void MorseGenerator::transmit(string message) {
for (int i=0; i < message.length(); i++) {
if (strncmp(&message[i], " ", 1) != 0) {
transmit(message[i]);
}
wait_ms(DIT_MS * 7);
}
}
void MorseGenerator::transmit(char letter) {
int d;
string code = morse_map[letter];
for (int i=0; i < code.length(); i++) {
if (strncmp(&code[i], ".", 1) == 0) {
d = 1;
} else {
d = 3;
}
callback(1);
wait_ms(DIT_MS * d);
callback(0);
wait_ms(DIT_MS);
}
}
