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

Files at this revision

API Documentation at this revision

Comitter:
mr63
Date:
Wed Sep 18 16:27:24 2013 +0000
Commit message:
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.

Changed in this revision

MorseCode.cpp Show annotated file Show diff for this revision Revisions of this file
MorseCode.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MorseCode.cpp	Wed Sep 18 16:27:24 2013 +0000
@@ -0,0 +1,132 @@
+#include "mbed.h"
+#include "MorseCode.h"
+float unit = .005;
+
+const char Letters[37][7] = {
+										{2,1,0}, //a
+										{4,0,1,1,1},//b
+										{4,0,1,0,1},//c
+										{3,0,1,1},//d
+										{1,1},//e
+										{4,1,1,0,1},//f
+										{3,0,0,1},//g
+										{4,1,1,1,1},//h
+										{2,1,1},//i
+										{4,1,0,0,0},//j
+										{3,0,1,0},//k
+										{4,1,0,1,1},//l
+										{2,0,0},//m
+										{2,0,1},//n
+										{3,0,0,0},//o
+										{4,1,0,0,1},//p
+										{4,0,0,1,0},//q
+										{3,1,0,1},//r
+										{3,1,1,1},//s
+										{1,0},//t
+										{3,1,1,0},//u
+										{4,1,1,1,0},//v
+										{3,1,0,0},//w
+										{4,0,1,1,0},//x
+										{4,0,1,0,0},//y
+										{4,0,0,1,1},//z
+										{5,0,0,0,0,0},//0
+										{5,1,0,0,0,0},//1
+										{5,1,1,0,0,0},//2
+										{5,1,1,1,0,0},//3
+										{5,1,1,1,1,0},//4
+										{5,1,1,1,1,1},//5
+										{5,0,1,1,1,1},//6
+										{5,0,0,1,1,1},//7
+										{5,0,0,0,1,1},//8
+										{5,0,0,0,0,1},//9	
+										{0}//space
+};
+
+
+ MorseCode::MorseCode(PinName pin): _pin(pin) //Constructor
+ {    
+   
+ }
+ 
+float MorseCode::get_unit()
+{
+	return (unit);
+}
+void MorseCode::set_unit(float secs)
+{
+	unit = secs;
+}
+
+void MorseCode::blink_buffer (unsigned char* Pbuffer)  //Blink out String
+{
+	unsigned char next_letter;
+	unsigned int i=0;
+	while (Pbuffer[i] !=  0)
+	{
+				next_letter = Pbuffer[i];
+        Print_ASCII_Char(next_letter);	
+				i++;	
+	}
+}
+
+
+void MorseCode::blink_letter (unsigned int letter)  //Blink out a single Ascii letter
+{
+	if(letter == 36)
+	{
+		wait(unit*4);  //space between words (3 + 4 = 7 units)
+	}
+	for(int i = 1; i<(int)((Letters[letter][0])+1);i++)
+	{
+		if(Letters[letter][i])
+		{
+			Dot();
+		}
+		else
+		{
+			Dash();
+		}
+	}
+	wait(unit*3);  //space between letters
+}
+
+void MorseCode::Dot()  //Sets Blink Duration for Dot
+{
+	_pin=1;
+	wait (unit);
+	_pin = 0;
+	wait (unit);
+}
+
+void MorseCode::Dash()  //Sets Blink Duration for Dash
+{
+	_pin =1;
+	wait ((unit * 3.0));
+	_pin = 0;
+	wait (unit);
+}
+
+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
+{
+	if(letter >= 0x61  && letter <= 0x7a)
+	{
+		 blink_letter(letter-0x61);    // it is lowercase
+	}
+	else if(letter >= 0x41 && letter <= 0x5a)  
+	{
+		 blink_letter(letter-0x41);  // it is in caps
+	}
+	else if (letter >= 0x30 && letter <= 0x39)
+	{
+		 blink_letter(letter-0x22 );  // it is a number
+	}
+	else if (letter == 0x20)
+	{
+		 blink_letter(36); //it is a space
+	}
+	else
+	{
+		return(false);
+	}
+	return(true);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MorseCode.h	Wed Sep 18 16:27:24 2013 +0000
@@ -0,0 +1,20 @@
+#include "mbed.h"
+
+class MorseCode{
+	 public:      
+				MorseCode(PinName pin);
+
+				void blink_buffer (unsigned char * Pbuffer);
+				bool Print_ASCII_Char(unsigned char letter);
+				float get_unit();
+				void  set_unit(float);
+				
+	 private :    
+					// objects
+					DigitalOut _pin;
+					void blink_letter (unsigned int letter);
+					void Dash();
+					void Dot();	 
+ 
+};
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Sep 18 16:27:24 2013 +0000
@@ -0,0 +1,23 @@
+
+#include "MorseCode.h"
+
+Serial pc(USBTX,USBRX);
+MorseCode MC(LED1);
+unsigned char buffer[] = {"Nothing Like Morse Code"};
+
+int main() {
+    unsigned char pcinput = 0;
+	  bool Printable = false;
+	  MC.set_unit(.05);
+		MC.blink_buffer (buffer);
+    while(1) //Do what ever is between these brackets forever or until the micro is reset.
+			{
+
+				pcinput = pc.getc();
+				Printable=MC.Print_ASCII_Char(pcinput);
+				pc.putc(pcinput);
+				
+			}	
+}
+
+