Thorkild Pedersen / Mbed 2 deprecated morsemsg

Dependencies:   mbed

Committer:
tfind
Date:
Tue Oct 26 23:13:58 2010 +0000
Revision:
0:023075f6fafd

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tfind 0:023075f6fafd 1 #include "mbed.h"
tfind 0:023075f6fafd 2
tfind 0:023075f6fafd 3 DigitalOut myled(LED1);
tfind 0:023075f6fafd 4
tfind 0:023075f6fafd 5 /// Send the string message in morse code on 'myled
tfind 0:023075f6fafd 6 void morse_string(char *msg);
tfind 0:023075f6fafd 7
tfind 0:023075f6fafd 8 // Timing constants for morse code generation
tfind 0:023075f6fafd 9 #define TDOT 0.2
tfind 0:023075f6fafd 10 #define TDASH (3*TDOT)
tfind 0:023075f6fafd 11 #define TSPLIT (1*TDOT)
tfind 0:023075f6fafd 12 #define TSPACE (3*TDASH)
tfind 0:023075f6fafd 13
tfind 0:023075f6fafd 14 int main() {
tfind 0:023075f6fafd 15 while (1) {
tfind 0:023075f6fafd 16 morse_string("hello world");
tfind 0:023075f6fafd 17 wait(2*TSPACE);
tfind 0:023075f6fafd 18 }
tfind 0:023075f6fafd 19 }
tfind 0:023075f6fafd 20
tfind 0:023075f6fafd 21 /// Table with morse code bit-patterns.
tfind 0:023075f6fafd 22 /// Each symbols dot/dash sequence is stored starting at bit 0 where
tfind 0:023075f6fafd 23 /// a set bit encodes a dash and cleared bit a dot. The sequence is terminated by a set trailing bit-
tfind 0:023075f6fafd 24 unsigned char alphabet[] = {
tfind 0:023075f6fafd 25 0x06, // .- a
tfind 0:023075f6fafd 26 0x11, // -... b
tfind 0:023075f6fafd 27 0x15, // -.-. c
tfind 0:023075f6fafd 28 0x09, // -.. d
tfind 0:023075f6fafd 29 0x02, // . e
tfind 0:023075f6fafd 30 0x14, // ..-. f
tfind 0:023075f6fafd 31 0x0b, // --. g
tfind 0:023075f6fafd 32 0x10, // .... h
tfind 0:023075f6fafd 33 0x08, // .. i
tfind 0:023075f6fafd 34 0x1e, // .--- j
tfind 0:023075f6fafd 35 0x0d, // -.- k
tfind 0:023075f6fafd 36 0x12, // .-.. l
tfind 0:023075f6fafd 37 0x07, // -- m
tfind 0:023075f6fafd 38 0x05, // -. n
tfind 0:023075f6fafd 39 0x0F, // --- o
tfind 0:023075f6fafd 40 0x16, // .--. p
tfind 0:023075f6fafd 41 0x1b, // --.- q
tfind 0:023075f6fafd 42 0x0a, // .-. r
tfind 0:023075f6fafd 43 0x08, // ... s
tfind 0:023075f6fafd 44 0x03, // - t
tfind 0:023075f6fafd 45 0x0c, // ..- u
tfind 0:023075f6fafd 46 0x18, // ...- v
tfind 0:023075f6fafd 47 0x0e, // .-- w
tfind 0:023075f6fafd 48 0x19, // -..- x
tfind 0:023075f6fafd 49 0x1d, // -.-- y
tfind 0:023075f6fafd 50 0x13, // --.. z
tfind 0:023075f6fafd 51 0x3f, // ----- 0
tfind 0:023075f6fafd 52 0x3e, // .---- 1
tfind 0:023075f6fafd 53 0x3c, // ..--- 2
tfind 0:023075f6fafd 54 0x38, // ...-- 3
tfind 0:023075f6fafd 55 0x30, // ....- 4
tfind 0:023075f6fafd 56 0x20, // ..... 5
tfind 0:023075f6fafd 57 0x21, // -.... 6
tfind 0:023075f6fafd 58 0x23, // --... 7
tfind 0:023075f6fafd 59 0x27, // ---.. 8
tfind 0:023075f6fafd 60 0x2f, // ----. 9
tfind 0:023075f6fafd 61 0x73, // --..-- ,
tfind 0:023075f6fafd 62 0x6a, // .-.-.- .
tfind 0:023075f6fafd 63 0x00
tfind 0:023075f6fafd 64 };
tfind 0:023075f6fafd 65
tfind 0:023075f6fafd 66 /// Convert a character to a morse-code bit pattern.
tfind 0:023075f6fafd 67 unsigned char getCode(char symbol) {
tfind 0:023075f6fafd 68 if (symbol >= 'a' && symbol <= 'z') return alphabet[symbol-'a'];
tfind 0:023075f6fafd 69 else if (symbol >= '0' && symbol <= '9') return alphabet[symbol-'0'+26];
tfind 0:023075f6fafd 70 else if (symbol == '.') return alphabet[37];
tfind 0:023075f6fafd 71 else if (symbol == ',') return alphabet[36];
tfind 0:023075f6fafd 72 else return 0;
tfind 0:023075f6fafd 73 }
tfind 0:023075f6fafd 74
tfind 0:023075f6fafd 75 /// Pattern of current symbol being send.
tfind 0:023075f6fafd 76 unsigned char morse_pattern = 0;
tfind 0:023075f6fafd 77
tfind 0:023075f6fafd 78 /// Set the symbol to be morsed.
tfind 0:023075f6fafd 79 /// Returns bit pattern of morse code or zero if symbol not found (e.g. for a space)
tfind 0:023075f6fafd 80 int morse_setsymbol(char symbol) {
tfind 0:023075f6fafd 81 return morse_pattern = getCode(symbol);
tfind 0:023075f6fafd 82 }
tfind 0:023075f6fafd 83
tfind 0:023075f6fafd 84 /// Generate next dot/dash to transmit
tfind 0:023075f6fafd 85 /// Returns 0 for dot, 1 for dash and -1 for finished
tfind 0:023075f6fafd 86 int morse_next() {
tfind 0:023075f6fafd 87 int retval = (morse_pattern & 1) ? 1 : 0;
tfind 0:023075f6fafd 88 morse_pattern >>= 1;
tfind 0:023075f6fafd 89 if (! morse_pattern) retval = -1;
tfind 0:023075f6fafd 90 return retval;
tfind 0:023075f6fafd 91 }
tfind 0:023075f6fafd 92
tfind 0:023075f6fafd 93 void morse_string(char *msg) {
tfind 0:023075f6fafd 94 int x;
tfind 0:023075f6fafd 95 for (; *msg; msg++) {
tfind 0:023075f6fafd 96 if (!morse_setsymbol(*msg)) {
tfind 0:023075f6fafd 97 wait(TSPACE);
tfind 0:023075f6fafd 98 continue;
tfind 0:023075f6fafd 99 }
tfind 0:023075f6fafd 100 for (x=morse_next(); x != -1; x=morse_next()) {
tfind 0:023075f6fafd 101 myled = 1;
tfind 0:023075f6fafd 102 wait(x ? TDASH : TDOT);
tfind 0:023075f6fafd 103 myled = 0;
tfind 0:023075f6fafd 104 wait(TSPLIT);
tfind 0:023075f6fafd 105 }
tfind 0:023075f6fafd 106 wait(TDASH);
tfind 0:023075f6fafd 107 }
tfind 0:023075f6fafd 108 }