From cbe7ff02bc2d6ca5e1a6957deffa180c656df9ff Mon Sep 17 00:00:00 2001 From: hex45 Date: Tue, 24 Dec 2019 16:14:08 -0500 Subject: [PATCH] Add morse.cpp module Update Makefile for morse build --- src/Makefile | 5 +- src/morse/morse.cpp | 235 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 239 insertions(+), 1 deletion(-) create mode 100644 src/morse/morse.cpp diff --git a/src/Makefile b/src/Makefile index 0668a0c..5a12d79 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,4 +1,4 @@ -all: ../pisstv ../piopera ../pifsq ../pichirp ../sendiq ../tune ../freedv ../dvbrf ../pocsag ../spectrumpaint ../pifmrds ../rpitx ../corel8 ../pift8 +all: ../pisstv ../piopera ../pifsq ../pichirp ../morse ../sendiq ../tune ../freedv ../dvbrf ../pocsag ../spectrumpaint ../pifmrds ../rpitx ../corel8 ../pift8 CFLAGS = -Wall -g -O2 -Wno-unused-variable CXXFLAGS = -std=c++11 -Wall -g -O2 -Wno-unused-variable @@ -24,6 +24,9 @@ LDFLAGS_Pissb = librpitx/src/librpitx.a -lm -lrt -lpthread -lsndfile -lliquid ../pichirp : chirp/chirp.cpp librpitx/src/librpitx.a $(CCP) $(CXXFLAGS) -o ../pichirp chirp/chirp.cpp $(LDFLAGS) +../morse : morse/morse.cpp librpitx/src/librpitx.a + $(CCP) $(CXXFLAGS) -o ../morse morse/morse.cpp $(LDFLAGS) + ../sendiq : sendiq.cpp librpitx/src/librpitx.a $(CCP) $(CXXFLAGS) -o ../sendiq sendiq.cpp $(LDFLAGS) diff --git a/src/morse/morse.cpp b/src/morse/morse.cpp new file mode 100644 index 0000000..f5d9a3a --- /dev/null +++ b/src/morse/morse.cpp @@ -0,0 +1,235 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../librpitx/src/librpitx.h" + +#define MORSECODES 37 + +typedef struct morse_code +{ + uint8_t ch; + const char dits[8]; +} Morsecode; + +const Morsecode code_table[] = +{ + {' ', " "}, // space, 4 dits + {'0', "----- "}, + {'1', ".---- "}, + {'2', "..--- "}, + {'3', "...-- "}, + {'4', "....- "}, + {'5', "..... "}, + {'6', "-.... "}, + {'7', "--... "}, + {'8', "---.. "}, + {'9', "----. "}, + {'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', "--.. "} +}; + +/* +void SimpleTestOOK(uint64_t Freq) +{ + + int SR = 10; //10 HZ + int FifoSize = 21; //24 + ookburst ook(Freq, SR, 14, FifoSize,100); + + unsigned char TabSymbol[FifoSize] = {0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0}; + + ook.SetSymbols(TabSymbol, FifoSize); +} +*/ + +/** + Transmits CW OOK data at given rate and frequency. + */ +void Send_CW_OOK(const float freq, const float symbolrate, const char * cw) +{ + //SR = 4, upsample = 200 gives ~5 WPM with dit of 265ms + //float Freq = 433000000; + //int FifoSize = 21; //24 + //float symbolrate = 4; //10 HZ + float upsample = 125.0; + int FifoSize = strlen((char*)cw) - 1; + ookburst ook(freq, symbolrate, 14, FifoSize, upsample); + + unsigned char TabSymbol[FifoSize]; // = {0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0}; + for (int i = 0; i <= FifoSize; i++) + { + TabSymbol[i] = (cw[i] == '0') ? 0 : 1; + //printf("%d",TabSymbol[i]); + } + ook.SetSymbols(TabSymbol, FifoSize); +} + +/** + Converts morse character to CW OOK binary and sends to transmit. + */ +void morse_to_cw(const char * dits) +{ + char cw[23];// = "mmmmmmmmmmmmmmmmmmmmmmm"; + int dits_len = strlen(dits); + int a = 0; + for (int i = 0; i < dits_len; i++) + { + if (dits[i] == '.') + { + cw[a++] = '1'; + cw[a++] = '0'; + } + else if (dits[i] == '-') + { + cw[a++] = '1'; + cw[a++] = '1'; + cw[a++] = '1'; + cw[a++] = '0'; + } + else if (dits[i] == ' ') + { + cw[a++] = '0'; + } + } + cw[a] = '\0'; + + //printf("%s\n", cw); +// Send_CW_OOK(433000000, cw); +} + +void morse_to_cw(const char * dits, char * cw) +{ + //char cw[23];// = "mmmmmmmmmmmmmmmmmmmmmmm"; + int dits_len = strlen(dits); + int a = 0; + for (int i = 0; i < dits_len; i++) + { + if (dits[i] == '.') + { + cw[a++] = '1'; + cw[a++] = '0'; + } + else if (dits[i] == '-') + { + cw[a++] = '1'; + cw[a++] = '1'; + cw[a++] = '1'; + cw[a++] = '0'; + } + else if (dits[i] == ' ') + { + cw[a++] = '0'; + } + } + cw[a] = '\0'; + + //printf("%s\n", cw); + //Send_CW_OOK(cw); +} + +/** + Goes through message converting characters to morse and sends to + transmit one at a time. + */ +void text_to_morse(const char * txt) +{ + //const char *test_msg = "TEST MESSAGEZ1"; + int txt_len = strlen(txt); + for (int i = 0; i < txt_len; i++) + { + char tch = toupper(txt[i]); + for (int j = 0; j < MORSECODES; j++) + { + if (code_table[j].ch == tch) + { + char * dits = (char*)code_table[j].dits; + printf("%s", dits); + morse_to_cw(dits); + } + } + } +} + +char * text_to_morse(const char txt) +{ + char tch = toupper(txt); + for (int j = 0; j < MORSECODES; j++) + { + if (code_table[j].ch == tch) + { + char * dits = (char*)code_table[j].dits; + //printf("%s", dits); + //morse_to_cw(dits); + return dits; + } + } + return 0; +} + +int main(int argc, char * argv[]) +{ + const char * msg = "TEST MESSAGEZ1 MSG2 MSG3 MSG0 MSG9"; + float freq = 433000000; + float wpm = 5; + //KN4SWZ + //text_to_morse(test_msg); + //usage: ./morse freq(Hz) rate(Hz) msg + + if (argc < 4) + { + printf("usage: ./morse freq(Hz) rate(dits) MSG(\"quoted\")\n"); + exit(0); + } + + freq = atof(argv[1]); + wpm = atof(argv[2]); + msg = argv[3]; + printf("msg: %s\n", msg); + char * dits; + char cw[23]; + float symbol_rate = wpm/1.25; + + for (int i = 0; i < strlen(msg); i++) + { + dits = text_to_morse(msg[i]); + morse_to_cw(dits, cw); + printf("msg[%02d]: %c\tmorse[%s]\tcw[%s]\n", i, toupper(msg[i]), dits, cw); + Send_CW_OOK(freq, symbol_rate, cw); + } +}