This is a simple class written to flash out morse code. It can be used to flash out data from the terminal or from a buffer

Committer:
mr63
Date:
Wed Sep 18 16:27:24 2013 +0000
Revision:
0:c3c4f0cd78cd
This is a simple Class that was written to "flash" out Morse Code.  It can be used to flash out data from the terminal, or from a buffer.  The main function gives examples of both.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mr63 0:c3c4f0cd78cd 1 #include "mbed.h"
mr63 0:c3c4f0cd78cd 2 #include "MorseCode.h"
mr63 0:c3c4f0cd78cd 3 float unit = .005;
mr63 0:c3c4f0cd78cd 4
mr63 0:c3c4f0cd78cd 5 const char Letters[37][7] = {
mr63 0:c3c4f0cd78cd 6 {2,1,0}, //a
mr63 0:c3c4f0cd78cd 7 {4,0,1,1,1},//b
mr63 0:c3c4f0cd78cd 8 {4,0,1,0,1},//c
mr63 0:c3c4f0cd78cd 9 {3,0,1,1},//d
mr63 0:c3c4f0cd78cd 10 {1,1},//e
mr63 0:c3c4f0cd78cd 11 {4,1,1,0,1},//f
mr63 0:c3c4f0cd78cd 12 {3,0,0,1},//g
mr63 0:c3c4f0cd78cd 13 {4,1,1,1,1},//h
mr63 0:c3c4f0cd78cd 14 {2,1,1},//i
mr63 0:c3c4f0cd78cd 15 {4,1,0,0,0},//j
mr63 0:c3c4f0cd78cd 16 {3,0,1,0},//k
mr63 0:c3c4f0cd78cd 17 {4,1,0,1,1},//l
mr63 0:c3c4f0cd78cd 18 {2,0,0},//m
mr63 0:c3c4f0cd78cd 19 {2,0,1},//n
mr63 0:c3c4f0cd78cd 20 {3,0,0,0},//o
mr63 0:c3c4f0cd78cd 21 {4,1,0,0,1},//p
mr63 0:c3c4f0cd78cd 22 {4,0,0,1,0},//q
mr63 0:c3c4f0cd78cd 23 {3,1,0,1},//r
mr63 0:c3c4f0cd78cd 24 {3,1,1,1},//s
mr63 0:c3c4f0cd78cd 25 {1,0},//t
mr63 0:c3c4f0cd78cd 26 {3,1,1,0},//u
mr63 0:c3c4f0cd78cd 27 {4,1,1,1,0},//v
mr63 0:c3c4f0cd78cd 28 {3,1,0,0},//w
mr63 0:c3c4f0cd78cd 29 {4,0,1,1,0},//x
mr63 0:c3c4f0cd78cd 30 {4,0,1,0,0},//y
mr63 0:c3c4f0cd78cd 31 {4,0,0,1,1},//z
mr63 0:c3c4f0cd78cd 32 {5,0,0,0,0,0},//0
mr63 0:c3c4f0cd78cd 33 {5,1,0,0,0,0},//1
mr63 0:c3c4f0cd78cd 34 {5,1,1,0,0,0},//2
mr63 0:c3c4f0cd78cd 35 {5,1,1,1,0,0},//3
mr63 0:c3c4f0cd78cd 36 {5,1,1,1,1,0},//4
mr63 0:c3c4f0cd78cd 37 {5,1,1,1,1,1},//5
mr63 0:c3c4f0cd78cd 38 {5,0,1,1,1,1},//6
mr63 0:c3c4f0cd78cd 39 {5,0,0,1,1,1},//7
mr63 0:c3c4f0cd78cd 40 {5,0,0,0,1,1},//8
mr63 0:c3c4f0cd78cd 41 {5,0,0,0,0,1},//9
mr63 0:c3c4f0cd78cd 42 {0}//space
mr63 0:c3c4f0cd78cd 43 };
mr63 0:c3c4f0cd78cd 44
mr63 0:c3c4f0cd78cd 45
mr63 0:c3c4f0cd78cd 46 MorseCode::MorseCode(PinName pin): _pin(pin) //Constructor
mr63 0:c3c4f0cd78cd 47 {
mr63 0:c3c4f0cd78cd 48
mr63 0:c3c4f0cd78cd 49 }
mr63 0:c3c4f0cd78cd 50
mr63 0:c3c4f0cd78cd 51 float MorseCode::get_unit()
mr63 0:c3c4f0cd78cd 52 {
mr63 0:c3c4f0cd78cd 53 return (unit);
mr63 0:c3c4f0cd78cd 54 }
mr63 0:c3c4f0cd78cd 55 void MorseCode::set_unit(float secs)
mr63 0:c3c4f0cd78cd 56 {
mr63 0:c3c4f0cd78cd 57 unit = secs;
mr63 0:c3c4f0cd78cd 58 }
mr63 0:c3c4f0cd78cd 59
mr63 0:c3c4f0cd78cd 60 void MorseCode::blink_buffer (unsigned char* Pbuffer) //Blink out String
mr63 0:c3c4f0cd78cd 61 {
mr63 0:c3c4f0cd78cd 62 unsigned char next_letter;
mr63 0:c3c4f0cd78cd 63 unsigned int i=0;
mr63 0:c3c4f0cd78cd 64 while (Pbuffer[i] != 0)
mr63 0:c3c4f0cd78cd 65 {
mr63 0:c3c4f0cd78cd 66 next_letter = Pbuffer[i];
mr63 0:c3c4f0cd78cd 67 Print_ASCII_Char(next_letter);
mr63 0:c3c4f0cd78cd 68 i++;
mr63 0:c3c4f0cd78cd 69 }
mr63 0:c3c4f0cd78cd 70 }
mr63 0:c3c4f0cd78cd 71
mr63 0:c3c4f0cd78cd 72
mr63 0:c3c4f0cd78cd 73 void MorseCode::blink_letter (unsigned int letter) //Blink out a single Ascii letter
mr63 0:c3c4f0cd78cd 74 {
mr63 0:c3c4f0cd78cd 75 if(letter == 36)
mr63 0:c3c4f0cd78cd 76 {
mr63 0:c3c4f0cd78cd 77 wait(unit*4); //space between words (3 + 4 = 7 units)
mr63 0:c3c4f0cd78cd 78 }
mr63 0:c3c4f0cd78cd 79 for(int i = 1; i<(int)((Letters[letter][0])+1);i++)
mr63 0:c3c4f0cd78cd 80 {
mr63 0:c3c4f0cd78cd 81 if(Letters[letter][i])
mr63 0:c3c4f0cd78cd 82 {
mr63 0:c3c4f0cd78cd 83 Dot();
mr63 0:c3c4f0cd78cd 84 }
mr63 0:c3c4f0cd78cd 85 else
mr63 0:c3c4f0cd78cd 86 {
mr63 0:c3c4f0cd78cd 87 Dash();
mr63 0:c3c4f0cd78cd 88 }
mr63 0:c3c4f0cd78cd 89 }
mr63 0:c3c4f0cd78cd 90 wait(unit*3); //space between letters
mr63 0:c3c4f0cd78cd 91 }
mr63 0:c3c4f0cd78cd 92
mr63 0:c3c4f0cd78cd 93 void MorseCode::Dot() //Sets Blink Duration for Dot
mr63 0:c3c4f0cd78cd 94 {
mr63 0:c3c4f0cd78cd 95 _pin=1;
mr63 0:c3c4f0cd78cd 96 wait (unit);
mr63 0:c3c4f0cd78cd 97 _pin = 0;
mr63 0:c3c4f0cd78cd 98 wait (unit);
mr63 0:c3c4f0cd78cd 99 }
mr63 0:c3c4f0cd78cd 100
mr63 0:c3c4f0cd78cd 101 void MorseCode::Dash() //Sets Blink Duration for Dash
mr63 0:c3c4f0cd78cd 102 {
mr63 0:c3c4f0cd78cd 103 _pin =1;
mr63 0:c3c4f0cd78cd 104 wait ((unit * 3.0));
mr63 0:c3c4f0cd78cd 105 _pin = 0;
mr63 0:c3c4f0cd78cd 106 wait (unit);
mr63 0:c3c4f0cd78cd 107 }
mr63 0:c3c4f0cd78cd 108
mr63 0:c3c4f0cd78cd 109 bool MorseCode::Print_ASCII_Char(unsigned char letter) //Determines if the ASCII value is valid and it calls blink_letter with the correct char value Upper/Lower/Numbers/Space
mr63 0:c3c4f0cd78cd 110 {
mr63 0:c3c4f0cd78cd 111 if(letter >= 0x61 && letter <= 0x7a)
mr63 0:c3c4f0cd78cd 112 {
mr63 0:c3c4f0cd78cd 113 blink_letter(letter-0x61); // it is lowercase
mr63 0:c3c4f0cd78cd 114 }
mr63 0:c3c4f0cd78cd 115 else if(letter >= 0x41 && letter <= 0x5a)
mr63 0:c3c4f0cd78cd 116 {
mr63 0:c3c4f0cd78cd 117 blink_letter(letter-0x41); // it is in caps
mr63 0:c3c4f0cd78cd 118 }
mr63 0:c3c4f0cd78cd 119 else if (letter >= 0x30 && letter <= 0x39)
mr63 0:c3c4f0cd78cd 120 {
mr63 0:c3c4f0cd78cd 121 blink_letter(letter-0x22 ); // it is a number
mr63 0:c3c4f0cd78cd 122 }
mr63 0:c3c4f0cd78cd 123 else if (letter == 0x20)
mr63 0:c3c4f0cd78cd 124 {
mr63 0:c3c4f0cd78cd 125 blink_letter(36); //it is a space
mr63 0:c3c4f0cd78cd 126 }
mr63 0:c3c4f0cd78cd 127 else
mr63 0:c3c4f0cd78cd 128 {
mr63 0:c3c4f0cd78cd 129 return(false);
mr63 0:c3c4f0cd78cd 130 }
mr63 0:c3c4f0cd78cd 131 return(true);
mr63 0:c3c4f0cd78cd 132 }