forked from Qyon/STM32_RTTY
-
Notifications
You must be signed in to change notification settings - Fork 15
/
morse.c
151 lines (132 loc) · 3.3 KB
/
morse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Morse Code Playback Functions
// Mark Jessop 2018-04
// OK1TE 2018-11
//
// Based on code from https://github.com/Paradoxis/Arduino-morse-code-translator/blob/master/main.ino
//
#include "morse.h"
#include "config.h"
#include "radio.h"
#include "delay.h"
// All morse delays
#define MORSE_DELAY (1200 / MORSE_WPM)
#define MORSE_DELAY_DOT (MORSE_DELAY * 1)
#define MORSE_DELAY_DASH (MORSE_DELAY * 3)
#define MORSE_DELAY_SPACE (MORSE_DELAY * 7)
// All morse characters
const char MORSE_DOT = '.';
const char MORSE_DASH = '-';
// Letters
const char* const MORSE_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
};
// Numerals.
const char* const MORSE_NUMBERS[] = {
"-----", // 0
".----", // 1
"..---", // 2
"...--", // 3
"....-", // 4
".....", // 5
"-....", // 6
"--...", // 7
"---..", // 8
"----." // 9
};
// Symbols
const char Morse_Slash[] = "-..-.";
const char Morse_Equal[] = "-...-";
const char Morse_FullStop[] = ".-.-.-";
const char Morse_Comma[] = "--..--";
const char Morse_QuestionMark[] = "..--..";
const char Morse_Plus[] = ".-.-.";
const char Morse_AtSign[] = ".--.-.";
// Send a single character
void sendDotOrDash (char unit) {
radio_enable_tx();
// Unit is a dot (500 ms)
if (unit == MORSE_DOT) {
_delay_ms(MORSE_DELAY_DOT);
}
// Unit is a dash (1500 ms)
else if (unit == MORSE_DASH) {
_delay_ms(MORSE_DELAY_DASH);
}
// Inter-element gap
radio_inhibit_tx();
_delay_ms(MORSE_DELAY);
}
void sendMorseSequence (const char* sequence) {
// Counter
int i = 0;
// Loop through every character until an 'end-of-line' (null) character is found
while (sequence[i] != 0) {
sendDotOrDash(sequence[i]);
i++;
}
// Delay between every letter
_delay_ms(MORSE_DELAY * 3);
}
void sendMorse(const char* message){
int i = 0;
while (message[i] != 0){
const char current = message[i];
// Lower case letters
if (current >= 'a' && current <= 'z') {
sendMorseSequence(MORSE_LETTERS[current - 'a']);
}
// Capital case letters
else if (current >= 'A' && current <= 'Z') {
sendMorseSequence(MORSE_LETTERS[current - 'A']);
}
// Numbers
else if (current >= '0' && current <= '9') {
sendMorseSequence(MORSE_NUMBERS[current - '0']);
}
else switch (current) {
case '/': sendMorseSequence(Morse_Slash);
break;
case '=': sendMorseSequence(Morse_Equal);
break;
case '.': sendMorseSequence(Morse_FullStop);
break;
case ',': sendMorseSequence(Morse_Comma);
break;
case '?': sendMorseSequence(Morse_QuestionMark);
break;
case '+': sendMorseSequence(Morse_Plus);
break;
case '@': sendMorseSequence(Morse_AtSign);
break;
default: // Treat all other characters as a space.
_delay_ms(MORSE_DELAY_SPACE);
}
i++;
}
radio_disable_tx();
}