diff options
-rw-r--r-- | Morse/Morse.cpp | 161 | ||||
-rw-r--r-- | Morse/Morse.h | 61 |
2 files changed, 222 insertions, 0 deletions
diff --git a/Morse/Morse.cpp b/Morse/Morse.cpp new file mode 100644 index 0000000..fa0f5ad --- /dev/null +++ b/Morse/Morse.cpp @@ -0,0 +1,161 @@ +/* + Morse code transmitter, version 0.1 + Copyright (C) 2010 Mike Beattie + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ +#include "Morse.h" + +#define DEBUG(x) x +//#define DEBUG(x) + +Morse::Morse( int wpm, function signalOn, function signalOff ) { + _dit = 1200 / wpm; + _dah = _dit * 3; + + _userSignalOn = signalOn; + _userSignalOff = signalOff; + + // LETTERS 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 + letters[0].code = B01000000; letters[0].length = 2; + letters[1].code = B10000000; letters[1].length = 4; + letters[2].code = B10100000; letters[2].length = 4; + letters[3].code = B10000000; letters[3].length = 3; + letters[4].code = B00000000; letters[4].length = 1; + letters[5].code = B00100000; letters[5].length = 4; + letters[6].code = B11000000; letters[6].length = 3; + letters[7].code = B00000000; letters[7].length = 4; + letters[8].code = B00000000; letters[8].length = 2; + letters[9].code = B01110000; letters[9].length = 4; + letters[10].code = B10100000; letters[10].length = 3; + letters[11].code = B01000000; letters[11].length = 4; + letters[12].code = B11000000; letters[12].length = 2; + letters[13].code = B10000000; letters[13].length = 2; + letters[14].code = B11100000; letters[14].length = 3; + letters[15].code = B01100000; letters[15].length = 4; + letters[16].code = B11010000; letters[16].length = 4; + letters[17].code = B01000000; letters[17].length = 3; + letters[18].code = B00000000; letters[18].length = 3; + letters[19].code = B10000000; letters[19].length = 1; + letters[20].code = B00100000; letters[20].length = 3; + letters[21].code = B00010000; letters[21].length = 4; + letters[22].code = B01100000; letters[22].length = 3; + letters[23].code = B10010000; letters[23].length = 4; + letters[24].code = B10110000; letters[24].length = 4; + letters[25].code = B11000000; letters[25].length = 4; + + // FIGURES ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 0 : + figures[0].code = B01111000; figures[0].length = 6; + figures[1].code = B10110000; figures[1].length = 5; + figures[2].code = B10110100; figures[2].length = 6; + figures[3].code = B10010000; figures[3].length = 4; + figures[4].code = B01010000; figures[4].length = 5; + figures[5].code = B11001100; figures[5].length = 6; + figures[6].code = B10000100; figures[6].length = 6; + figures[7].code = B01010100; figures[7].length = 6; + figures[8].code = B10010000; figures[8].length = 5; + figures[9].code = B11111000; figures[9].length = 5; + figures[10].code = B01111000; figures[10].length = 5; + figures[11].code = B00111000; figures[11].length = 5; + figures[12].code = B00011000; figures[12].length = 5; + figures[13].code = B00001000; figures[13].length = 5; + figures[14].code = B00000000; figures[14].length = 5; + figures[15].code = B10000000; figures[15].length = 5; + figures[16].code = B11000000; figures[16].length = 5; + figures[17].code = B11100000; figures[17].length = 5; + figures[18].code = B11110000; figures[18].length = 5; + figures[19].code = B11100000; figures[19].length = 6; + + // MISCELLANEOUS SIGNS + understood.code = B00010000; understood.length = 5; + error.code = B00000000; error.length = 8; + invitationToTransmit.code = B10100000; invitationToTransmit.length = 3; + wait.code = B01000000; wait.length = 5; + endOfWork.code = B00010100; endOfWork.length = 6; + startingSignal.code = B10101000; startingSignal.length = 5; + commercialAt.code = B00000000; commercialAt.length = 6; + questionMark.code = B00110000; questionMark.length = 6; + quotationmarks.code = B01001000; quotationmarks.length = 6; + doubleHyphen.code = B10001000; doubleHyphen.length = 5; +} + +void Morse::setwpm( int wpm ) { + _dit = 1200 / wpm; + _dah = _dit * 3; +} + +void Morse::transmitSign( signal s ) { + byte mask = B10000000; + do { + _userSignalOn(); + + if(s.code & mask) { + DEBUG(Serial.print("-")); + delay(_dah); + } else { + DEBUG(Serial.print(".")); + delay(_dit); + } + + _userSignalOff(); + // delay the standard single element for intra-character gaps. + delay(_dit); + mask >>= 1; + } while(--s.length > 0); +} + +void Morse::transmitString( char str[] ) { + DEBUG(Serial.print(" * Sending morse: ")); + DEBUG(Serial.println(str)); + DEBUG(Serial.print(" ")); + int i = 0; + while (str[i]) { + if( str[i] >= 97 && str[i] <= 122 ) + str[i] -= 32; + if( str[i] >= 65 && str[i] <= 90 ) { + transmitSign( letters[str[i] - 65] ); + } else if ( str[i] >= 39 && str[i] <= 58 ) { + transmitSign( figures[ str[i] - 39 ] ); + } else { + switch( str[i] ) { + case '?' : + transmitSign( questionMark ); + break; + case '"' : + transmitSign( quotationmarks ); + break; + case '@' : + transmitSign( commercialAt ); + break; + case '=' : + transmitSign( doubleHyphen ); + break; + case ' ' : + // We delay the 'standard' 7 elements for an inter-word + // gap, but as we have the inter-character delay below + // we only need 4 elements here. + DEBUG(Serial.println()); + DEBUG(Serial.print(" ")); + delay(4 * _dit); + break; + } + } + // delay the 'standard' 3 elements for inter-character gaps. + DEBUG(Serial.print(" ")); + delay(3 * _dit); + i++; + } + DEBUG(Serial.println()); +} + diff --git a/Morse/Morse.h b/Morse/Morse.h new file mode 100644 index 0000000..19682dd --- /dev/null +++ b/Morse/Morse.h @@ -0,0 +1,61 @@ +/* + Morse code transmitter, version 0.1 + Copyright (C) 2010 Mike Beattie + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + + +#ifndef Morse_h +#define Morse_h + +#include "WProgram.h" + +typedef void (*function)(void); + +typedef struct { + byte code; + byte length; + } signal; + + +class Morse { + public: + Morse( int wpm, function signalOn, function signalOff ); + void setwpm( int wpm ); + void transmitSign( signal sig ); + void transmitString( char str[] ); + + signal letters[26], + figures[20], + doubleHyphen, + quotationmarks, + questionMark, + commercialAt, + startingSignal, + endOfWork, + wait, + invitationToTransmit, + error, + understood; + + private: + int _dah; + int _dit; + function _userSignalOn; + function _userSignalOff; +}; + +#endif + |