MorseGenerator Library

Dependents:   MorseGenerator_Example

Files at this revision

API Documentation at this revision

Comitter:
screamer
Date:
Tue Sep 16 14:56:13 2014 +0000
Commit message:
initial revision of the MorseGenerator library

Changed in this revision

MorseGenerator.cpp Show annotated file Show diff for this revision Revisions of this file
MorseGenerator.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MorseGenerator.cpp	Tue Sep 16 14:56:13 2014 +0000
@@ -0,0 +1,44 @@
+#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);
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MorseGenerator.h	Tue Sep 16 14:56:13 2014 +0000
@@ -0,0 +1,63 @@
+#include <map>
+#include <string>
+#include "mbed.h"
+
+#define DIT_MS 100
+
+
+using namespace std;
+
+typedef map <char, string> MapType;
+typedef void (*callback_type)(int);
+
+const string MORSE_LETTERS[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
+const string MORSE_NUMBERS[] = {".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----"};
+
+/** MorseGenerator class.
+ *
+ * Example:
+ * @code
+ * #include "mbed.h"
+ * #include "main.h"
+ * #include "morsegenerator.h"
+ *
+ * DigitalOut myled(p18);
+ *
+ * void morse_callback(int val) {
+ *     myled = val;
+ * }
+ * 
+ * int main() {
+ *     MorseGenerator morse = MorseGenerator(morse_callback);
+ * 
+ *     while (1) {
+ *         morse.transmit("CQCQ DE M6SPX");
+ *     }
+ * }
+ * @endcode
+ */
+class MorseGenerator {
+    MapType morse_map;
+    callback_type callback;
+    
+    void generate_morse_map();
+    void add_mappings(string morse_chars, const string morse_codes[]);
+public:
+    /** Create a MorseGenerator that calls a specific callback
+     *
+     * @param callback called when turning on or off.
+     */
+    MorseGenerator(callback_type callback);
+    
+    /** transmit a string
+     *
+     * @param message to transmit.
+     */
+    void transmit(string message);
+    
+    /** transmit a char
+     *
+     * @param char to transmit.
+     */
+    void transmit(char letter);
+};
\ No newline at end of file