-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtty.c
277 lines (238 loc) · 6.09 KB
/
rtty.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "rtty.h"
#include "baudtimer.h"
#include "HD44780.h"
#include "math.h"
//RTTY notes:
//RTTYrite has a " in stead of a +, we use that.
//Also, it sends BEL in place of '.
const unsigned int RTTY_LETTERS_TO_HANTRONIX[32] = {
RTTY_NULL,//0x00
HD_cap_E,//0x01
RTTY_LF,//0x02
HD_cap_A,//0x03
RTTY_SP,//0x04
HD_cap_S,//0x05
HD_cap_I,//0x06
HD_cap_U,//0x07
RTTY_CR,//0x08
HD_cap_D,//0x09
HD_cap_R,//0x0A
HD_cap_J,//0x0B
HD_cap_N,//0x0C
HD_cap_F,//0x0D
HD_cap_C,//0x0E
HD_cap_K,//0x0F
HD_cap_T,//0x10
HD_cap_Z,//0x11
HD_cap_L,//0x12
HD_cap_W,//0x13
HD_cap_H,//0x14
HD_cap_Y,//0x15
HD_cap_P,//0x16
HD_cap_Q,//0x17
HD_cap_O,//0x18
HD_cap_B,//0x19
HD_cap_G,//0x1A
RTTY_FIGS,//0x1B
HD_cap_M,//0x1C
HD_cap_X,//0x1D
HD_cap_V,//0x1E
RTTY_LTRS//0x1F
};
const unsigned int RTTY_FIGURES_TO_HANTRONIX[32] = {
RTTY_NULL,//0x00
HD_three,//0x01
RTTY_LF,//0x02
HD_subtract,//0x03
RTTY_SP,//0x04
HD_apostrophe,//0x05
HD_eight,//0x06
HD_seven,//0x07
RTTY_CR,//0x08
RTTY_ENQ,//0x09
HD_four,//0x0A
RTTY_BEL,//0x0B
HD_comma,//0x0C
HD_exclamation,//0x0D
HD_colon,//0x0E
HD_leftparenthesis,//0x0F
HD_five,//0x10
HD_doublequote,//0x11 //Should be HD_add, but RTTYrite is non-standard
HD_rightparenthesis,//0x12
HD_two,//0x13
HD_pound,//0x14
HD_six,//0x15
HD_zero,//0x16
HD_one,//0x17
HD_nine,//0x18
HD_question,//0x19
HD_and,//0x1A
RTTY_FIGS,//0x1B
HD_period,//0x1C
HD_divide,//0x1D
HD_semicolon,//0x1E
RTTY_LTRS//0x1F
};
uint16 validateRTTY(uint16 character);
void printRTTY(uint16 character);
//RTTY global communicator variables
#define rttyNone 2
#define rttyMark 1
#define rttySpace 0
unsigned int rtty_mode = RTTY_LTRS;
//Useful RTTY Constants
int16 rttySymbolTime;
int16 rttySwitchTime;
//RTTY Decode state variables
int32 rttyMarkTime, rttySpaceTime,
rttyDecodeSymbol, rttyDam;
uint16 rttyCurrentSymbol, rttyProcessMark, rttyProcessSpace,
rttyCharacter, rttySymbolCount;
F16 lp_alpha, lp_beta;
F16 e_lp;
void rtty_init() {
rttySymbolTime = (uint16)((((float)Fosc / 2.0) / 8.0) / 45.45); // The timer is driven by Fosc / 2 through a 256 prescaler and we are looking for 45.45 baud symbols
rttySwitchTime = rttySymbolTime / 4;
rttyMarkTime = 0;
rttySpaceTime = 0;
rttyDecodeSymbol = rttyMark;
rttyDam = 0;
rttyCurrentSymbol = rttyNone;
rttyProcessMark = 0;
rttyProcessSpace = 0;
rttyCharacter = 0;
rttySymbolCount = 0;
//Initialize phase detector DSP
//Low pass filter for RTTY
float alpha_float = 1500.0f;
float beta_float = expf(-1.0f * alpha_float * 1.0f / ((float)SAMPLE_RATE));
lp_alpha = floatToF16(alpha_float * 1.0f / ((float)SAMPLE_RATE));
lp_beta = floatToF16(beta_float);
}
/*F16 tmp[1024];
uint16 idxtmp = 0;*/
void rtty_process(F16 e) {
uint16 Telaps = baud_time_get();
/*tmp[idxtmp] = e;
if(idxtmp > 1023)
idxtmp = 0;
else
idxtmp++;*/
e_lp = F16add(F16unsafeMul(4, lp_beta, e_lp), F16unsafeMul(4, lp_alpha, e));
if(e_lp <= 0)
rttyCurrentSymbol = rttyMark;
else if(e_lp > 0)
rttyCurrentSymbol = rttySpace;
//Watch for a symbol switch
// If an unexpected signal detection change occurs, track it to see if real
if(rttyCurrentSymbol == rttyDecodeSymbol) {
//If it turns out to be fake, do nothing (because time added to right bin anyway)
rttyDam = 0;
} else {
//If it turns out to be real, evaluate other timer and switch decode symbol
rttyDam += Telaps;
if(rttyDam > rttySwitchTime) {
//If need to switch symbols, evaluate the old decodeSymbol before updating to new
switch(rttyDecodeSymbol) {
case rttyMark:
if(rttyCurrentSymbol == rttySpace) rttySpaceTime = rttyDam - Telaps; //The '- Telaps' term avoids double adding
rttyMarkTime -= rttyDam - Telaps;
rttyProcessMark = 1;
break;
case rttySpace:
if(rttyCurrentSymbol == rttyMark) rttyMarkTime = rttyDam - Telaps;
rttySpaceTime -= rttyDam - Telaps;
rttyProcessSpace = 1;
break;
}
//Update to new symbol
rttyDecodeSymbol = rttyCurrentSymbol;
//Reset the dam
rttyDam = 0;
}
}
switch(rttyDecodeSymbol) {
case rttyMark:
rttyMarkTime += Telaps;
break;
case rttySpace:
rttySpaceTime += Telaps;
break;
}
if(rttyProcessMark) {
while(rttyMarkTime > (rttySymbolTime >> 1)) {
rttyCharacter = (rttyCharacter << 1) | 0x0001;
rttyMarkTime -= rttySymbolTime;
rttySymbolCount++;
}
rttyMarkTime = 0;
rttyProcessMark = 0;
}
if(rttyProcessSpace) {
while(rttySpaceTime > (rttySymbolTime >> 1)) {
rttyCharacter = (rttyCharacter << 1) & 0xFFFE;
rttySpaceTime -= rttySymbolTime;
rttySymbolCount++;
}
rttySpaceTime = 0;
rttyProcessSpace = 0;
}
while(rttySymbolCount >= 8) {
uint16 overshoot = rttySymbolCount - 8;
uint16 adjustedCharacter = rttyCharacter >> overshoot;
if(validateRTTY(adjustedCharacter)) {
adjustedCharacter = bitrev((adjustedCharacter >> 2) & 0x1F, 5);
rttySymbolCount -= 8;
//printRTTY(adjustedCharacter);
} else
rttySymbolCount--;
}
}
uint16 validateRTTY(uint16 character) {
uint16 stopBits = character & 0x0003, startBit = (character & 0x0080) ^ 0x0080;
if((startBit | stopBits) == 0x0083)
return 1;
else
return 0;
}
void printRTTY(uint16 character) {
switch(rtty_mode) {
case RTTY_LTRS:
character = RTTY_LETTERS_TO_HANTRONIX[character];
break;
case RTTY_FIGS:
character = RTTY_FIGURES_TO_HANTRONIX[character];
break;
}
switch(character) {
case (RTTY_NULL):
printDisplay(HD_space);
break;
case (RTTY_LF):
break;
case (RTTY_SP):
printDisplay(HD_space);
break;
case (RTTY_CR):
break;
case (RTTY_FIGS):
rtty_mode = RTTY_FIGS;
break;
case (RTTY_LTRS):
rtty_mode = RTTY_LTRS;
break;
case (RTTY_ENQ):
printDisplay(HD_space);
printDisplay(HD_low_e);
printDisplay(HD_space);
break;
case (RTTY_BEL):
printDisplay(HD_space);
printDisplay(HD_low_b);
printDisplay(HD_space);
break;
default:
printDisplay(character);
break;
}
}