Created
January 26, 2011 17:53
-
-
Save ashton/797106 to your computer and use it in GitHub Desktop.
Morse Lib for morse code translation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Morse.cpp - Library for morse code translating. | |
Created by Matheus Ashton Silva, January 24th, 2011. | |
Release under CC license http://creativecommons.org/ | |
**/ | |
#include "WProgram.h" | |
#include "Morse.h" | |
Morse::Morse(int ledPin, int buzzPin, int buttonPin, const LiquidCrystal& lcd) : _lcd( lcd ) { | |
pinMode(ledPin, OUTPUT); | |
pinMode(buzzPin, OUTPUT); | |
pinMode(buttonPin,INPUT); | |
digitalWrite(buttonPin,LOW); | |
_ledPin = ledPin; | |
_buzzPin = buzzPin; | |
_buttonPin = buttonPin; | |
/* Tempo de duracao do dot e do dash em ms */ | |
dotTime = 250; | |
dashTime = dotTime * 3; | |
/* Frequencia do buzzer */ | |
buzzFrequency = 440; | |
/* Array de estruturas para traducao */ | |
MorseDict dict[] = { | |
{ 'A', ".-" }, | |
{ 'B', "-..." }, | |
{ 'C', "-.-." }, | |
{ 'D', "-.." }, | |
{ 'E', "." }, | |
{ 'F', "..-." }, | |
{ 'G', "--." }, | |
{ 'H', "...." }, | |
{ 'I', ".." }, | |
{ 'J', ".---" }, | |
{ 'K', "-.-" }, | |
{ 'L', ".-.." }, | |
{ 'M', "--" }, | |
{ 'N', "-." }, | |
{ 'O', "---" }, | |
{ 'P', ".--." }, | |
{ 'Q', "--.-" }, | |
{ 'R', ".-." }, | |
{ 'S', "..." }, | |
{ 'T', "-" }, | |
{ 'U', "..-" }, | |
{ 'V', "...-" }, | |
{ 'W', ".--" }, | |
{ 'X', "-..-" }, | |
{ 'Y', "-.--" }, | |
{ 'Z', "--.." }, | |
{ '1', ".----" }, | |
{ '2', "..---" }, | |
{ '3', "...--" }, | |
{ '4', "....-" }, | |
{ '5', "....." }, | |
{ '6', "-...." }, | |
{ '7', "--..." }, | |
{ '8', "---.." }, | |
{ '9', "----." }, | |
{ '0', "-----" }, | |
{ '##', "-...-" }, | |
}; | |
/* inicializando o array interno, gambiarra pq o C++ nao aceita inicializar nada que nao seja tipo primitivo no cabecalho */ | |
for(int i = 0; i < 36; i++) { | |
_dict[i] = dict[i]; | |
} | |
/* Ativando o LCD */ | |
_lcd.begin(16,2); | |
_lcd.setCursor(0,0); | |
_lcd.blink(); | |
} | |
/** | |
* Funcao que faz acender o led e apitar o buzzer como dot(.) | |
*/ | |
void Morse::dot() { | |
digitalWrite(_ledPin, HIGH); | |
tone(_buzzPin, buzzFrequency); | |
delay(dotTime); | |
digitalWrite(_ledPin, LOW); | |
noTone(_buzzPin); | |
delay(dotTime); | |
} | |
/** | |
* Funcao que faz acender o led e apitar o buzzer como dash(-) | |
*/ | |
void Morse::dash() { | |
digitalWrite(_ledPin, HIGH); | |
tone(_buzzPin, buzzFrequency); | |
delay(dashTime); | |
digitalWrite(_ledPin, LOW); | |
noTone(_buzzPin); | |
delay(dotTime); | |
} | |
/** | |
* Funcao que le a string da comunicacao serial para a traducao para morse | |
*/ | |
void Morse::readFromSerial(char text[]) { | |
/* limpando o array */ | |
for(int i = 0;i<100;i++) { | |
text[i] = ' '; | |
} | |
int idx = 0; | |
if(Serial.available()) { | |
while(Serial.available()) { | |
int sb = Serial.read(); | |
text[idx] = sb; | |
idx++; | |
delay(50); | |
} | |
Serial.flush(); | |
} | |
} | |
/** | |
* Funcao que ouve a comunicacao comunicacao serial | |
* pega o texto quando ele estiver disponivel e | |
* percorre o mesmo passando os caracteres para a | |
* funcao que traduz os caracteres para morse | |
*/ | |
void Morse::translate2Morse() { | |
if(Serial.available()) { | |
char* text = (char*) calloc(100,sizeof(char)); | |
Morse::readFromSerial(text); | |
int size = strlen(text); | |
for(int i = 0; i < size; i++) { | |
Morse::morseByChar(toupper(text[i])); | |
} | |
free(text); | |
} | |
} | |
/** | |
* Funcao que pega um caracter e acende e apita | |
* o codigo morse referente aquele caracter | |
*/ | |
void Morse::morseByChar(char ch) { | |
int dictSize = sizeof(_dict) / sizeof(MorseDict); | |
for(int i = 0; i < dictSize; i++) { | |
if(_dict[i].letter == ch) { | |
Morse::doMorse(_dict[i].code); | |
delay(dashTime); | |
} | |
} | |
} | |
/** | |
* Funcao que pega o codigo morse e parseia | |
* apitando e acendendo dot ou dash | |
*/ | |
void Morse::doMorse(String code) { | |
for(int i = 0; i < code.length(); i++) { | |
if(code.charAt(i) == '.') | |
Morse::dot(); | |
else | |
Morse::dash(); | |
delay(dotTime); | |
} | |
} | |
/** | |
* Funcao que pega o sinal do botao e interpreta o codigo morse | |
* traduzindo para caracteres, imprimindo-os no LCD | |
*/ | |
void Morse::translate2Text() { | |
/* Se o botao estiver pressionado .. */ | |
if(digitalRead(_buttonPin) == HIGH) { | |
digitalWrite(_ledPin,HIGH); | |
tone(_buzzPin, buzzFrequency); | |
if(onTime <= 0) { | |
onTime = millis(); | |
} | |
/* Se ele acabou de ser solto */ | |
} else if(onTime > 0) { | |
if(digitalRead(_ledPin) == HIGH) { | |
digitalWrite(_ledPin, LOW); | |
noTone(_buzzPin); | |
} | |
offTime = millis(); | |
int diff = offTime - onTime; | |
onTime = 0; | |
/* eliminando ruido */ | |
if(diff > 50) { | |
/* dotTime com threshold */ | |
if(diff > 50 && diff <= 500) { | |
toTranslate += "."; | |
} else if(diff > 500 && diff <= 1000) { | |
toTranslate += "-"; | |
} | |
spaceTimeOn = 0; | |
} | |
} else { | |
if(digitalRead(_ledPin) == HIGH) { | |
digitalWrite(_ledPin, LOW); | |
noTone(_buzzPin); | |
} | |
/* Se ele nao esta pressionado, verificar o tempo que ficou sem ser pressionado | |
* se foi o tempo de um dash, (que eh o intervalo entre as letras), interpreta o | |
* codigo morse ate agora processado e escreve o caracter correspondente no LCD | |
* se nao, (eh pq ainda nao se formou uma letra completa), aguarda o botao ser | |
* pressionado novamente | |
*/ | |
if(spaceTimeOn <= 0) | |
spaceTimeOn = millis(); | |
int diffSpaceTime = 0; | |
if(spaceTimeOn > 0) { | |
spaceTimeOff = millis(); | |
diffSpaceTime = spaceTimeOff - spaceTimeOn; | |
} | |
if(diffSpaceTime > 749 && diffSpaceTime <= 1000) { | |
spaceTimeOn = 0; | |
if(toTranslate == "-...-") { | |
_lcd.print(" "); | |
} else { | |
int dictSize = sizeof(_dict) / sizeof(MorseDict); | |
for(int i = 0; i < dictSize; i++) { | |
if(_dict[i].code == toTranslate) | |
_lcd.print(_dict[i].letter); | |
} | |
} | |
toTranslate = ""; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Morse.h - Library for morse code translating. | |
Created by Matheus Ashton Silva, January 24th, 2011. | |
Release under CC license http://creativecommons.org/ | |
*/ | |
#ifndef Morse_h | |
#define Morse_h | |
#include "WProgram.h" | |
#include "LiquidCrystal.h" | |
struct MorseDict { | |
char letter; | |
String code; | |
}; | |
class Morse { | |
public: | |
Morse(int ledPin, int buzzPin, int buttonPin, const LiquidCrystal& lcd); | |
void translate2Morse(); | |
void translate2Text(); | |
private: | |
int _ledPin; | |
int _buzzPin; | |
int _buttonPin; | |
LiquidCrystal _lcd; | |
int dotTime; | |
int dashTime; | |
int pauseTime; | |
int buzzFrequency; | |
unsigned long onTime; | |
unsigned long offTime; | |
unsigned long spaceTimeOn; | |
unsigned long spaceTimeOff; | |
String toTranslate; | |
MorseDict _dict[36]; | |
void dot(); | |
void dash(); | |
void pause(); | |
void morseByChar(char ch); | |
void doMorse(String code); | |
void readFromSerial(char text[]); | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment