-
Notifications
You must be signed in to change notification settings - Fork 0
/
morse.ino
728 lines (668 loc) · 18 KB
/
morse.ino
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
#include "driver/timer.h"
void IRAM_ATTR morse_on_timer();
class Morse_ {
public:
static constexpr int max_durations = 1000;
static constexpr int ms = 1000;
static constexpr int s = 1000000;
static constexpr int max_hist_len = 16;
bool buffer_ready;
bool buffer_processed;
int prev_input;
hw_timer_t* timer;
int64_t last_change_time;
int64_t start_time;
int64_t stop_time;
int silent_max;
int debounce_wait;
int64_t debounce_time;
int durations[max_durations];
int durations_len;
int shortest;
int longest;
int time_unit;
int input_type;
int input_pin;
int output_pin;
bool negate_input;
char tokens[max_durations + 1];
int token_count;
char result[max_durations];
int histogram[max_hist_len];
int bucket_boundaries[max_hist_len + 2]; // more precisely, top boundaries of histogram buckets. Starts implied.
int bins[3];
int bin_counts[3];
Morse_()
: buffer_ready(false),
buffer_processed(true),
prev_input(0),
timer(NULL),
last_change_time(0),
start_time(0),
stop_time(0),
silent_max(2 * s),
debounce_wait(10 * ms),
debounce_time(0),
durations_len(0),
shortest(0),
longest(0),
time_unit(250 * ms),
input_type(INPUT_PULLUP),
input_pin(0),
output_pin(-1),
negate_input(-1),
token_count(0){};
~Morse_() {
timerStop(timer);
timerDetachInterrupt(timer);
timerEnd(timer);
}
static constexpr char* morse_code[] = {
"", /* NUL */
"", /* SOH */
"", /* STX */
"", /* ETX */
"", /* EOT */
"", /* ENQ */
"", /* ACK */
"", /* BEL */
"......", /* BS */
"", /* HT */
".-.-", /* LF */
"", /* VT */
"", /* FF */
"", /* CR */
"", /* SO */
"", /* SI */
"", /* DLE */
"", /* DC1 */
"", /* DC2 */
"", /* DC3 */
"", /* DC4 */
"", /* NAK */
"", /* SYN */
"", /* ETB */
"", /* CAN */
"", /* EM */
"", /* SUB */
"", /* ESC */
"", /* FS */
"", /* GS */
"", /* RS */
"", /* US */
"/", /* */
"-.-.--", /* ! */
".-..-.", /* " */
"", /* # */
"...-..-", /* $ */
"", /* % */
".-...", /* & */
".----.", /* ' */
"-.--.", /* ( */
"-.--.-", /* ) */
"", /* * */
".-.-.", /* + */
"--..--", /* , */
"-....-", /* - */
".-.-.-", /* . */
"-..-.", /* / */
"-----", /* 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 */
"", /* [ */
"", /* \ */
"", /* ] */
"", /* ^ */
"..--.-", /* _ */
"", /* ` */
".-", /* 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 */
"", /* { */
"", /* | */
"", /* } */
"", /* ~ */
"", /* DEL */
};
void begin() {
timer = timerBegin(1000000);
timerAttachInterrupt(timer, &morse_on_timer);
timerAlarm(timer, 20 * ms, true, 0); // 50ms
if (output_pin >= 0) {
pinMode(input_pin, OUTPUT);
}
if (input_pin >= 0) {
pinMode(input_pin, input_type);
// With pullup, key pressed reads as 0, so negate the read
negate_input = (input_type == INPUT_PULLUP);
}
};
int64_t micros64() {
static uint32_t low32, high32;
int32_t new_low32 = micros();
if (new_low32 < low32) high32++;
low32 = new_low32;
return ((int64_t)high32 << 32) | low32;
};
void ReadMorse() {
// Buffer processing in progress, ignore inputs.
if (!buffer_processed) return;
if (durations_len >= max_durations) {
// Buffer full. Flush what you got.
buffer_ready = true;
buffer_processed = false;
return;
}
buffer_ready = false;
int change_time;
int inp = abs(digitalRead(input_pin) - negate_input);
if (inp != prev_input) {
// Input changed.
change_time = micros64();
prev_input = inp;
// debounce
if (change_time - debounce_time < debounce_wait) {
debounce_time = change_time;
return;
}
int delta = change_time - last_change_time;
if (inp) {
// keydown = register prior silence.
if (delta > silent_max) {
// ignore initial silence.
last_change_time = change_time;
// Serial.println("Ignore silence");
return;
}
// Serial.println("Keydown");
durations[durations_len++] = delta; // register silence duration
} else {
//keyup = signal completed.
//ignore tail end of reset longpress;
if (delta > silent_max && durations_len == 0) {
return;
}
// Serial.println("Keyup");
durations[durations_len++] = delta; // register signal duration
}
last_change_time = change_time;
} else {
// no button state change
if (inp) {
//longpress as first input makes no sense
if (durations_len == 0) return;
// long press = reset
if (micros64() > last_change_time + silent_max) {
reset();
// Serial.println("Reset");
}
} else {
// initial silence, no input = do nothing.
if (durations_len == 0) return;
// final silence for 2s = done, ready to parse.
if (micros64() > last_change_time + silent_max) {
// Serial.println("Ready");
buffer_ready = true;
buffer_processed = false;
}
}
}
};
void reset() {
durations_len = 0;
buffer_ready = false;
buffer_processed = true;
}
// Creates a 16-bucket histogram and 3-bucket binned averages.
void create_histogram_and_bins() {
// find the range of durations
int bins[3] = { 0 };
int bin_counts[3] = { 0 };
memset(histogram, 0, sizeof(histogram));
memset(bucket_boundaries, 0, sizeof(bucket_boundaries));
// find the bucket size
int hl = max_hist_len - 2; // we want to leave first and last bucket empty, to make median easier.
{
int range = longest - shortest; // Total range of the data
int bucket = range / (hl - 1); // Size of each bucket
bucket_boundaries[0] = longest - 1.5 * bucket; //start at half the bucket below lowest
for (int i = 1; i < max_hist_len + 2; ++i) {
// last boundary half the bucket below highest.
bucket_boundaries[i] = bucket_boundaries[i - 1] + bucket;
}
}
for (int i = 0; i < durations_len; ++i) {
int duration = durations[i];
for (int j = 0; j < max_hist_len; ++j) {
if (duration < bucket_boundaries[j]) {
++histogram[j - 1];
if (j < 6) {
bins[0] += duration;
++bin_counts[0];
} else if (j < 11) {
bins[1] += duration;
++bin_counts[1];
} else {
bins[2] += duration;
++bin_counts[2];
}
break;
}
}
}
if (bin_counts[1] == 0) {
}
for (int i = 0; i < 3; ++i) {
if (bin_counts[i] > 0) {
bins[i] /= bin_counts[i];
}
}
}
inline void swap(int* x, int* y) {
*x ^= *y;
*y ^= *x;
*x ^= *y;
}
inline int middle3(int a, int b, int c) {
return a > b ? (c > a ? a : (b > c ? b : c)) : (c > b ? b : (a > c ? a : c));
}
void median() {
int backup[max_hist_len];
memcpy(backup, histogram, sizeof(histogram));
for (int i = 1; i < max_hist_len - 1; ++i) {
histogram[i] = middle3(backup[i - 1], backup[i], backup[i + 1]);
}
}
inline int find_bucket(int v) {
if (v < bucket_boundaries[6]) return 0;
if (v < bucket_boundaries[11]) return 1;
return 2;
}
int find_smallest(int* arr, int len, bool zeroallowed = true) {
int smallest = 2147483647;
for (int i = 0; i < len; ++i) {
if (arr[i] < smallest && (zeroallowed || arr[i] > 0)) {
smallest = arr[i];
}
return smallest;
}
}
int find_biggest(int* arr, int len) {
int biggest = -2147483648;
for (int i = 0; i < len; ++i) {
if (arr[i] > biggest) {
biggest = arr[i];
}
return biggest;
}
}
void append_token(const char t) {
if (token_count < max_durations) {
tokens[token_count++] = t;
}
}
void append_null() {
tokens[token_count] = '\0';
}
void append_tokens(const char* t) {
int l = strlen(t);
if (token_count + l < token_count) {
strcpy((tokens + token_count), t);
token_count += l;
}
}
// The input is too short or too uniform to decide what it contains on its own merits;
// We're using whatever heuristics will help us determine what it is, primarily time_unit estimate.
void hinted_discriminator() {
token_count = 0;
// one character type only;
if (longest < 2 * shortest) {
if (longest + shortest / 2 < time_unit) // only dots/intra
{
for (int i = 0; i < durations_len; ++i) {
if (!(i & 1)) continue; // skip intra-character spaces
append_token('.');
}
} else // only dashes and spaces
{
for (int i = 0; i < durations_len; ++i) {
if (!(i & 1)) {
append_token(' ');
} else {
append_token('-');
}
}
}
append_null();
return;
}
// There are at least two types of entries;
if (bins[1] == 0) {
int avg = (bins[0] + bins[2]) / 2;
if (bins[2] < 5.5 * time_unit) { // Dots and dashes
for (int i = 0; i < durations_len; ++i) {
if (durations[i] < avg) {
if (!(i & 1)) continue;
append_token('.');
} else {
if (!(i & 1)) {
append_token(' ');
} else {
append_token('-');
}
}
}
} else if (bins[0] > 1.5 * time_unit) { // Dashes and spaces
for (int i = 0; i < durations_len; ++i) {
if (durations[i] < avg) {
if (!(i & 1)) {
append_token(' ');
} else {
append_token('-');
}
} else {
if (!(i & 1)) {
append_tokens(" / ");
} else {
append_token('-');
}
}
}
} else { // Dots and spaces
for (int i = 0; i < durations_len; ++i) {
if (durations[i] < avg) {
if (!(i & 1)) continue;
append_token('.');
} else {
if (!(i & 1)) {
append_tokens(" / ");
} else {
append_token('-');
}
}
}
}
append_null();
return;
}
// There are three types of entries
for (int i = 0; i < durations_len; ++i) {
int b = find_bucket(durations[i]);
switch (b) {
case 0:
if (!(i & 1)) break;
append_token('.');
break;
case 1:
if (!(i & 1)) {
append_token(' ');
} else {
append_token('-');
}
break;
case 2:
default:
if (!(i & 1)) {
append_tokens(" / ");
} else {
append_token('-');
}
break;
}
}
append_null();
return;
}
void sink() {
int smallest_land = find_smallest(histogram, max_hist_len, false);
for (int i = 0; i < max_hist_len; ++i) {
histogram[i] -= smallest_land;
}
}
// Perform analysis of input to determine time unit.
bool adjust_tu() {
int isles = 0;
int isle_starts[3];
int isle_ends[3];
median();
median();
median();
while (1) {
isles = 0;
bool was_on_isle = false; // on isle?
for (int i = 0; i < max_hist_len; ++i) {
bool am_on_isle = histogram[i] > 0;
if (was_on_isle != am_on_isle) {
if (am_on_isle) {
isle_starts[isles] = i;
isles++;
if (isles == 4) break;
} else {
isle_ends[isles] = i;
}
was_on_isle = am_on_isle;
}
}
// if we have one landmass, sink and hope to split it into 2-3.
// if we have many small islets, sink the smallest of them.
if (isles == 1 || isles > 3) {
sink();
} else {
break; // hopefully 2 or 3, but 0 can happen too.
}
}
if (isles == 0) // ouch, we've overdone sinking and drowned!
{
return false;
}
// Use the average binning buckets to gather only the records we are confident about
memset(bins, 0, sizeof(bins));
memset(bin_counts, 0, sizeof(bin_counts));
for (int i = 0; i < durations_len; ++i) {
int duration = durations[i];
for (int j = 0; j < isles; ++j) {
if (duration >= bucket_boundaries[isle_starts[j]] && duration <= bucket_boundaries[isle_ends[j]]) {
bins[j] += duration;
++bin_counts[j];
break;
}
}
}
for (int j = 0; j < isles; ++j) {
if (bin_counts[j] > 0) {
bins[j] /= bin_counts[j];
break;
}
}
// Estimate Time Unit from the bins -
// bin[0]'s avg is 1tu, bin[1]'s avg is 3tu, bin[2] is 7tu. Weight the average by count of records.
int found_tu = 0;
if (isles == 2) {
if (bins[0] > 1.5 * time_unit) // dashes and spaces
{
found_tu = ((bins[0] / 3) * bin_counts[0] + (bins[1] / 7) * bin_counts[1])
/ (bin_counts[0] + bin_counts[1]);
} else // dots and dashes
{
found_tu = (bins[0] * bin_counts[0] + (bins[1] / 3) * bin_counts[1])
/ (bin_counts[0] + bin_counts[1]);
}
} else // isles == 3
{
found_tu = (bins[0] * bin_counts[0] + (bins[1] / 3) * bin_counts[1] + (bins[2] / 7) * bin_counts[2])
/ (bin_counts[0] + bin_counts[1] + bin_counts[2]);
}
// adjust TU
time_unit = found_tu * 0.4 + time_unit * 0.6;
return true;
}
// Simple tokenizer based on TU
void tokenize() {
token_count = 0;
int thresh1 = time_unit * 1.5;
int thresh2 = time_unit * 5.5;
for (int i = 0; i < durations_len; ++i) {
int duration = durations[i];
if (duration < thresh1) {
if (!(i & 1)) continue;
append_token('.');
} else if (duration < thresh2) {
if (!(i & 1)) {
append_token(' ');
} else {
append_token('-');
}
} else {
if (!(i & 1)) {
append_tokens(" / ");
} else {
append_token('-');
}
}
}
append_null();
}
void visualize(int* data, int len) {
for (int i = 0; i < 10; i++) {
Serial.println("-10,10,0");
}
for (int i = 0; i < len; i++) {
int v = data[i] / 10000;
for (int j = 0; j < v; j++) {
Serial.print("-10,10,");
Serial.println((!(i & 1)));
}
}
for (int i = 0; i < 10; i++) {
Serial.println(0);
}
}
void parseMorse() {
char* res = result;
char* saveWord;
char* saveLetter;
char* word = strtok_r(tokens, "/", &saveWord);
while (word != NULL) {
char* letter = strtok_r(word, " ", &saveLetter);
while (letter != NULL) {
for (int i = 0; i < sizeof(morse_code)-1; ++i) {
if (strcmp(letter, morse_code[i]) == 0) {
*res = (char)i;
res++;
break;
}
}
letter = strtok_r(NULL, " ", &saveLetter);
}
*res = ' ';
res++;
word = strtok_r(NULL, "/", &saveWord);
}
*res = '\0';
}
bool ready() {
return buffer_ready;
}
void ack() {
reset();
Serial.println("Ack");
}
char* getParsed() {
if (!buffer_ready) return "";
// both approaches require these
//visualize(durations, durations_len);
shortest = find_smallest(durations, durations_len);
longest = find_biggest(durations, durations_len);
create_histogram_and_bins();
// If data is good enough to produce adjusted time unit, we can use a simple tokenizer.
if (durations_len > 6 && longest >= shortest * 2 && adjust_tu()) {
tokenize();
} else {
// Data not good enough. Fall back to heuristic approach
hinted_discriminator();
}
Serial.println(tokens);
parseMorse();
durations_len = 0;
return result;
}
};
Morse_ Morse;
void IRAM_ATTR morse_on_timer() {
Morse.ReadMorse();
};
void setup() {
Serial.begin(115200);
Morse.begin();
}
void loop() {
if (Morse.ready()) {
Serial.println(Morse.getParsed());
Morse.ack();
} else {
delay(100);
}
}