-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcw.c
1063 lines (990 loc) · 25.7 KB
/
cw.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
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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ex: set syntax=c tabstop=8 noexpandtab shiftwidth=8:
*
* cw-kbd is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* cw-kbd 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 Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with cw-kbd. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright © 2009-2010, Vernon Mauery (N7OH)
*/
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "cw-kbd.h"
#include "settings.h"
#include "util.h"
#include "cw.h"
#include "timer.h"
#include "tick.h"
#include "ringbuffer.h"
void didah_enqueue(didah_queue_t what);
uint8_t didah_dequeue(didah_queue_t *didah);
void didah_decode(didah_queue_t next);
static void cw_out_hyper_tick(void);
static cw_dq_cb_t cw_dq_cb;
static uint8_t dit_len;
static keying_mode_t keying_mode;
static bool word_space;
DECLARE_RINGBUFFER(cw_q, 128);
static void cw_nq(uint8_t c) {
ringbuffer_push(&cw_q, c);
if (!ms_tick_registered(TICK_CW_ADVANCE))
cw_out_hyper_tick();
}
static inline uint8_t cw_dq(void) {
return ringbuffer_pop(&cw_q);
}
/* encoding of the morse code is as follows:
* zeros at high order bits until start bit (one)
* following the start bit, a zero is a dit and
* a one is a dah. For example, 0x14 expanded
* into binary is 0b00010100, which is .-.. or L.
* ^ ^^
* leading zeros -| ||- start of dits/dahs
* |- start bit (one)
*
* Any uncoded ascii chars were left as zero.
*/
/* we may have to take some special care with prosigns since
* they really are just letters crammed together. When reading
* in the paddles, we have to make sure that there are no spaces
* between the prosign letters or they will come out as distinct
* letters.
*/
static const prog_uint8_t cw2ascii[] = {
/* 00000000 */ ' ',
/* 00000001 */ 0,
/* 00000010 */ 'e',
/* 00000011 */ 't',
/* 00000100 */ 'i',
/* 00000101 */ 'a',
/* 00000110 */ 'n',
/* 00000111 */ 'm',
/* 00001000 */ 's',
/* 00001001 */ 'u',
/* 00001010 */ 'r',
/* 00001011 */ 'w',
/* 00001100 */ 'd',
/* 00001101 */ 'k',
/* 00001110 */ 'g',
/* 00001111 */ 'o',
/* 00010000 */ 'h',
/* 00010001 */ 'v',
/* 00010010 */ 'f',
/* 00010011 */ 0,
/* 00010100 */ 'l',
/* 00010101 */ 0,
/* 00010110 */ 'p',
/* 00010111 */ 'j',
/* 00011000 */ 'b',
/* 00011001 */ 'x',
/* 00011010 */ 'c',
/* 00011011 */ 'y',
/* 00011100 */ 'z',
/* 00011101 */ 'q',
/* 00011110 */ 0,
/* 00011111 */ 0,
/* 00100000 */ '5',
/* 00100001 */ '4',
/* 00100010 */ 0,
/* 00100011 */ '3',
/* 00100100 */ 0,
/* 00100101 */ 0,
/* 00100110 */ 0,
/* 00100111 */ '2',
/* 00101000 */ 0,
/* 00101001 */ 0,
/* 00101010 */ '+',
/* 00101011 */ 0,
/* 00101100 */ 0,
/* 00101101 */ 0,
/* 00101110 */ 0,
/* 00101111 */ '1',
/* 00110000 */ '6',
/* 00110001 */ '=',
/* 00110010 */ '/',
/* 00110011 */ 0,
/* 00110100 */ 0,
/* 00110101 */ 0,
/* 00110110 */ '(',
/* 00110111 */ 0,
/* 00111000 */ '7',
/* 00111001 */ 0,
/* 00111010 */ 0,
/* 00111011 */ 0,
/* 00111100 */ '8',
/* 00111101 */ 0,
/* 00111110 */ '9',
/* 00111111 */ '0',
/* 01000000 */ 0,
/* 01000001 */ 0,
/* 01000010 */ 0,
/* 01000011 */ 0,
/* 01000100 */ 0,
/* 01000101 */ 0,
/* 01000110 */ 0,
/* 01000111 */ 0,
/* 01001000 */ 0,
/* 01001001 */ 0,
/* 01001010 */ 0,
/* 01001011 */ 0,
/* 01001100 */ '?',
/* 01001101 */ '_',
/* 01001110 */ 0,
/* 01001111 */ 0,
/* 01010000 */ 0,
/* 01010001 */ 0,
/* 01010010 */ '"',
/* 01010011 */ 0,
/* 01010100 */ 0,
/* 01010101 */ '.',
/* 01010110 */ 0,
/* 01010111 */ 0,
/* 01011000 */ 0,
/* 01011001 */ 0,
/* 01011010 */ 0,
/* 01011011 */ 0,
/* 01011100 */ 0,
/* 01011101 */ 0,
/* 01011110 */ '\'',
/* 01011111 */ 0,
/* 01100000 */ 0,
/* 01100001 */ 0,
/* 01100010 */ 0,
/* 01100011 */ 0,
/* 01100100 */ 0,
/* 01100101 */ 0,
/* 01100110 */ 0,
/* 01100111 */ 0,
/* 01101000 */ 0,
/* 01101001 */ 0,
/* 01101010 */ ';',
/* 01101011 */ '!',
/* 01101100 */ 0,
/* 01101101 */ ')',
/* 01101110 */ 0,
/* 01101111 */ 0,
/* 01110000 */ 0,
/* 01110001 */ 0,
/* 01110010 */ 0,
/* 01110011 */ ',',
/* 01110100 */ 0,
/* 01110101 */ 0,
/* 01110110 */ 0,
/* 01110111 */ 0,
/* 01111000 */ ':',
/* 01111001 */ 0,
/* 01111010 */ 0,
/* 01111011 */ 0,
/* 01111100 */ 0,
/* 01111101 */ 0,
/* 01111110 */ 0,
/* 01111111 */ 0,
};
struct prosign {
uint16_t bits;
char value[4];
};
static const PROGMEM struct prosign prosigns[] = {
{ 0b00010101, { 'A', 'A', 0 } }, /* End of line */
{ 0b00101000, { 'A', 'S', 0 } }, /* Stand by */
{ 0b00101010, { 'A', 'R', 0 } }, /* End of message */
{ 0b00110001, { 'B', 'T', 0 } }, /* break between address and text or text and signature */
{ 0b00110010, { 'N', 'R', 0 } }, /* NR, number */
{ 0b01000101, { 'S', 'K', 0 } }, /* out, clear, no more communications */
{ 0b01011010, { 'A', 'C', 0 } }, /* used for @ symbol */
{ 0b10000000, { '\b', 0 } }, /* this is the cheater version of error (backspace) */
{ 0b10001001, { '$', 0 } },
{ 0b100000000, { '\b', 0 } }, /* 8 dits == mistake */
{ 0b1000111000, { 'S', 'O', 'S', 0 } }, /* technically SOS is a prosign */
};
static const prog_uint8_t cw[] = {
/* 00 */ 0,
/* 01 */ 0,
/* 02 */ 0,
/* 03 */ 0,
/* 04 */ 0,
/* 05 */ 0,
/* 06 */ 0,
/* 07 */ 0,
/* 08 */ 0,
/* 09 */ 0,
/* 0a */ 0,
/* 0b */ 0,
/* 0c */ 0,
/* 0d */ 0,
/* 0e */ 0,
/* 0f */ 0,
/* 10 */ 0,
/* 11 */ 0,
/* 12 */ 0,
/* 13 */ 0,
/* 14 */ 0,
/* 15 */ 0,
/* 16 */ 0,
/* 17 */ 0,
/* 18 */ 0,
/* 19 */ 0,
/* 1a */ 0,
/* 1b */ 0,
/* 1c */ 0,
/* 1d */ 0,
/* 1e */ 0,
/* 1f */ 0,
/* 20 */ 0x01, /* ' ' */
/* 21 */ 0x6b, /* '!' */
/* 22 */ 0x52, /* '"' */
/* 23 */ 0, /* '#' */
/* 24 */ 0x89, /* '$' */
/* 25 */ 0, /* '%' */
/* 26 */ 0, /* '&' */
/* 27 */ 0, /* '´' */
/* 28 */ 0x36, /* '(' */
/* 29 */ 0x6d, /* ')' */
/* 2a */ 0, /* '*' */
/* 2b */ 0x2a, /* '+' */
/* 2c */ 0x73, /* ',' */
/* 2d */ 0x61, /* '-' */
/* 2e */ 0x55, /* '.' */
/* 2f */ 0x32, /* '/' */
/* 30 */ 0x3f, /* '0' */
/* 31 */ 0x2f, /* '1' */
/* 32 */ 0x27, /* '2' */
/* 33 */ 0x23, /* '3' */
/* 34 */ 0x21, /* '4' */
/* 35 */ 0x20, /* '5' */
/* 36 */ 0x30, /* '6' */
/* 37 */ 0x38, /* '7' */
/* 38 */ 0x3c, /* '8' */
/* 39 */ 0x3e, /* '9' */
/* 3a */ 0x78, /* ':' */
/* 3b */ 0x6a, /* ';' */
/* 3c */ 0, /* '<' */
/* 3d */ 0x31, /* '=' */
/* 3e */ 0, /* '>' */
/* 3f */ 0x4c, /* '?' */
/* 40 */ 0, /* '@' */
/* 41 */ 0x5, /* 'A' */
/* 42 */ 0x18, /* 'B' */
/* 43 */ 0x1a, /* 'C' */
/* 44 */ 0xc, /* 'D' */
/* 45 */ 0x2, /* 'E' */
/* 46 */ 0x12, /* 'F' */
/* 47 */ 0xe, /* 'G' */
/* 48 */ 0x10, /* 'H' */
/* 49 */ 0x4, /* 'I' */
/* 4a */ 0x17, /* 'J' */
/* 4b */ 0xd, /* 'K' */
/* 4c */ 0x14, /* 'L' */
/* 4d */ 0x7, /* 'M' */
/* 4e */ 0x6, /* 'N' */
/* 4f */ 0xf, /* 'O' */
/* 50 */ 0x16, /* 'P' */
/* 51 */ 0x1d, /* 'Q' */
/* 52 */ 0xa, /* 'R' */
/* 53 */ 0x8, /* 'S' */
/* 54 */ 0x3, /* 'T' */
/* 55 */ 0x9, /* 'U' */
/* 56 */ 0x11, /* 'V' */
/* 57 */ 0xb, /* 'W' */
/* 58 */ 0x19, /* 'X' */
/* 59 */ 0x1b, /* 'Y' */
/* 5a */ 0x1c, /* 'Z' */
/* 5b */ 0, /* '[' */
/* 5c */ 0, /* '\' */
/* 5d */ 0, /* ']' */
/* 5e */ 0, /* '^' */
/* 5f */ 0x4d, /* '_' */
/* 60 */ 0x5e, /* '`' */
/* 61 */ 0x5, /* 'a' */
/* 62 */ 0x18, /* 'b' */
/* 63 */ 0x1a, /* 'c' */
/* 64 */ 0xc, /* 'd' */
/* 65 */ 0x2, /* 'e' */
/* 66 */ 0x12, /* 'f' */
/* 67 */ 0xe, /* 'g' */
/* 68 */ 0x10, /* 'h' */
/* 69 */ 0x4, /* 'i' */
/* 6a */ 0x17, /* 'j' */
/* 6b */ 0xd, /* 'k' */
/* 6c */ 0x14, /* 'l' */
/* 6d */ 0x7, /* 'm' */
/* 6e */ 0x6, /* 'n' */
/* 6f */ 0xf, /* 'o' */
/* 70 */ 0x16, /* 'p' */
/* 71 */ 0x1d, /* 'q' */
/* 72 */ 0xa, /* 'r' */
/* 73 */ 0x8, /* 's' */
/* 74 */ 0x3, /* 't' */
/* 75 */ 0x9, /* 'u' */
/* 76 */ 0x11, /* 'v' */
/* 77 */ 0xb, /* 'w' */
/* 78 */ 0x19, /* 'x' */
/* 79 */ 0x1b, /* 'y' */
/* 7a */ 0x1c, /* 'z' */
/* 7b */ 0, /* '{' */
/* 7c */ 0, /* '|' */
/* 7d */ 0, /* '}' */
/* 7e */ 0, /* '~' */
/* 7f */ 0, /* DEL */
};
static void beeper_on(void);
static void beeper_off(void);
static void cw_led_on(void) {
// turn on led
CW_PORT |= CW_BIT;
// turn on beeper
beeper_on();
}
static void cw_led_off(void) {
// turn off led
CW_PORT &= ~CW_BIT;
// turn off beeper
beeper_off();
}
void cw_char(char c) {
if (c == '\'') {
c = '`';
}
cw_nq(c);
}
void cw_string(const char* str) {
while (str && *str) {
cw_char(*str);
str++;
}
}
enum cw_state {
cws_hyper = 0,
cws_idle,
cws_send_bit,
cws_dah,
cws_dah2,
cws_bit_sp,
cws_char_sp,
cws_word_sp,
cws_word_sp2,
cws_word_sp3,
} __attribute__((packed));
#ifdef DEBUG
prog_char cw_state_0[] PROGMEM = "cws_hyper";
prog_char cw_state_1[] PROGMEM = "cws_idle";
prog_char cw_state_2[] PROGMEM = "cws_send_bit";
prog_char cw_state_3[] PROGMEM = "cws_dah";
prog_char cw_state_4[] PROGMEM = "cws_dah2";
prog_char cw_state_5[] PROGMEM = "cws_bit_sp";
prog_char cw_state_6[] PROGMEM = "cws_char_sp";
prog_char cw_state_7[] PROGMEM = "cws_word_sp";
prog_char cw_state_8[] PROGMEM = "cws_word_sp2";
prog_char cw_state_9[] PROGMEM = "cws_word_sp3";
prog_char *cw_state_s[] PROGMEM = {
cw_state_0,
cw_state_1,
cw_state_2,
cw_state_3,
cw_state_4,
cw_state_5,
cw_state_6,
cw_state_7,
cw_state_8,
cw_state_9,
};
#endif /* DEBUG */
static void cw_out_advance_tick(void);
static void cw_out_normal_tick(void) {
/* return to normal speed */
ms_tick_register(cw_out_advance_tick, TICK_CW_ADVANCE, dit_len);
}
static void cw_out_hyper_tick(void) {
/* return to normal speed */
ms_tick_register(cw_out_advance_tick, TICK_CW_ADVANCE, 1);
}
static void cw_out_stop_tick(void) {
ms_tick_unregister(TICK_CW_ADVANCE);
}
static void cw_out_advance_tick(void) {
// state machine
static enum cw_state state = cws_idle;
static uint8_t byte, bit, orig_byte, idle_count;
didah_queue_t didah;
#ifdef DEBUG
static enum cw_state lstate = cws_send_bit;
if (lstate != state)
debug("cw_out: state %S\r\n", &cw_state_s[state]);
lstate = state;
#endif /* DEBUG */
if (state > cws_idle) {
idle_count = 0;
} else {
idle_count++;
if (idle_count == 0) {
cw_out_stop_tick();
return;
}
}
switch (state) {
case cws_hyper:
cw_out_hyper_tick();
state = cws_idle;
break;
case cws_idle:
if (didah_dequeue(NULL)) {
debug("have didahs to dequeue\r\n");
cw_out_normal_tick();
state = cws_send_bit;
return cw_out_advance_tick();
}
if ((byte = cw_dq())) {
debug("cw_dq byte %d\r\n", byte);
if (byte > 127) break;
orig_byte = byte;
if (byte == 32) {
didah_enqueue(SPACE);
state = cws_word_sp;
break;
}
byte = pgm_read_byte(&cw[byte]);
if (byte == 0) break;
bit = 0;
while (byte && !(byte&0x80)) {
byte <<= 1;
bit++;
}
byte <<= 1;
bit++;
while (bit < 8) {
didah_enqueue((byte & 0x80)?DAH:DIT);
byte <<= 1;
bit++;
}
didah_enqueue(SPACE);
state = cws_send_bit;
cw_out_normal_tick();
return cw_out_advance_tick();
}
break;
case cws_send_bit:
if (didah_dequeue(&didah)) {
switch (didah) {
case DIT:
state = cws_bit_sp;
cw_led_on();
break;
case DAH:
state = cws_dah;
cw_led_on();
break;
case SPACE:
state = cws_char_sp;
break;
}
} else {
state = cws_hyper;
return cw_out_advance_tick();
}
break;
case cws_dah: state = cws_dah2; break;
case cws_dah2: state = cws_bit_sp; break;
case cws_bit_sp:
// turn off the bit
cw_led_off();
state = cws_send_bit;
break;
case cws_char_sp: state = cws_hyper; break;
case cws_word_sp: state = cws_word_sp2; break;
case cws_word_sp2: state = cws_word_sp3; break;
case cws_word_sp3: state = cws_hyper; break;
}
}
enum keying_state {
keying_idle,
keying_left_press, /* enqueue left didahs per left didah cycle */
keying_right_press, /* enqueue right didahs per right didah cycle */
keying_both_press, /* enqueue didahs per last pressed didah cycle */
} __attribute__((packed));
enum keying_transition_events {
keying_x_tick = 1,
keying_x_left_key_press,
keying_x_left_key_release,
keying_x_right_key_press,
keying_x_right_key_release,
} __attribute__((packed));
#ifdef DEBUG
prog_char key_state_0[] PROGMEM = "keying_idle";
prog_char key_state_1[] PROGMEM = "keying_left_press";
prog_char key_state_2[] PROGMEM = "keying_right_press";
prog_char key_state_3[] PROGMEM = "keying_both_press";
prog_char *keying_state_s[] PROGMEM = {
key_state_0,
key_state_1,
key_state_2,
key_state_3,
};
prog_char key_evt_0[] PROGMEM = "keying_x_no_event";
prog_char key_evt_1[] PROGMEM = "keying_x_tick";
prog_char key_evt_2[] PROGMEM = "keying_x_left_key_press";
prog_char key_evt_3[] PROGMEM = "keying_x_left_key_release";
prog_char key_evt_4[] PROGMEM = "keying_x_right_key_press";
prog_char key_evt_5[] PROGMEM = "keying_x_right_key_release";
prog_char *keying_transition_events_s[] PROGMEM = {
key_evt_0,
key_evt_1,
key_evt_2,
key_evt_3,
key_evt_4,
key_evt_5,
};
#endif /* DEBUG */
#define DIDAH_Q_LEN 16
DECLARE_RINGBUFFER(cw_didah_q, DIDAH_Q_LEN);
void didah_enqueue(didah_queue_t what) {
if (!ms_tick_registered(TICK_CW_ADVANCE))
cw_out_hyper_tick();
debug("enqueue %d\r\n", what);
ringbuffer_push(&cw_didah_q, what);
}
uint8_t didah_dequeue(didah_queue_t *didah) {
uint8_t have_didahs;
have_didahs = !ringbuffer_empty(&cw_didah_q);
if (!didah) {
return have_didahs;
}
if (have_didahs) {
*didah = (didah_queue_t)ringbuffer_pop(&cw_didah_q);
debug("dequeue %d\r\n", *didah);
didah_decode(*didah);
return 1;
}
return 0;
}
void cw_clear_queues(void) {
ringbuffer_clear(&cw_q);
if (!ringbuffer_empty(&cw_didah_q)) {
ringbuffer_clear(&cw_didah_q);
didah_decode(SPACE);
}
}
void didah_decode(didah_queue_t next) {
static uint16_t bits = 0x01;
static uint8_t last_decoded;
uint8_t c;
switch (next) {
case DIT:
bits <<= 1;
debug("bits = %#x\r\n", bits);
break;
case DAH:
bits <<= 1;
bits |= 1;
debug("bits = %#x\r\n", bits);
break;
case SPACE:
debug("decoding %#x\r\n", bits);
if (bits == 0x01 && last_decoded > ' ') {
last_decoded = ' ';
if (cw_dq_cb)
cw_dq_cb(' ');
debug("decode: space\r\n");
/* find bits in a table */
} else if (bits < 127 && (c = pgm_read_byte(&cw2ascii[bits]))) {
debug("decode: %#x -> %d\r\n", bits&0xff, c);
if (cw_dq_cb)
cw_dq_cb(c);
last_decoded = c;
} else {
uint8_t i, j;
last_decoded = 0;
debug("prosign: bits = %#b\r\n", bits);
for (i=0; i<(sizeof(prosigns)/sizeof(struct prosign)); i++) {
debug("prosign_lookup: %#b\r\n",
pgm_read_word(&prosigns[i].bits));
if (bits == pgm_read_word(&prosigns[i].bits)) {
j = 0;
while ((c = pgm_read_byte(&prosigns[i].value[j]))) {
if (cw_dq_cb) {
if (j == 0 && c != '\b')
cw_dq_cb('/');
cw_dq_cb(c);
last_decoded = c;
}
j++;
}
break;
}
}
}
bits = 0x01;
break;
}
}
uint16_t didah_len[3];
#define other_didah(D) ((D+1)&0x01)
static didah_queue_t left_didah;
#define right_didah other_didah(left_didah)
void cw_set_left_key(didah_queue_t didah) {
ulog("setting left key: %#x\r", didah);
left_didah = other_didah(other_didah(didah));
settings_set_left_key(didah);
}
didah_queue_t cw_get_left_key(void) {
return left_didah;
}
static void cw_in_advance_tick(enum keying_transition_events event) {
static enum keying_state cstate = keying_idle;
static int16_t keyed_ticks[3];
static didah_queue_t last_keyed;
static uint8_t enqueued_spaces = 0;
enum keying_state nstate;
static uint16_t tick_events;
uint8_t iv = rcli();
nstate = cstate;
if (cstate == keying_idle && event == keying_x_tick) {
tick_events++;
if (tick_events == 1024) {
ulog("**** skipped 1024 idle tick events, going to sleep\r\n");
tick_events = 0;
ms_tick_unregister(TICK_CW_PARSE);
}
} else if (event != keying_x_tick) {
if (tick_events > 0)
debug("**** skipped %u tick events\r\n", tick_events);
tick_events = 0;
debug("cw_in: state %S (%S)\r\n", &keying_state_s[cstate], &keying_transition_events_s[event]);
}
/* figure out what happened to get us here */
switch (cstate) {
case keying_idle:
/* if we get a (left|right) keypress, enqueue and go to keying_\1_press */
switch (event) {
case keying_x_tick:
last_keyed = SPACE;
if (enqueued_spaces < 2) {
keyed_ticks[SPACE]++;
if (keyed_ticks[SPACE] == didah_len[SPACE]) {
if (word_space)
didah_enqueue(SPACE);
enqueued_spaces++;
keyed_ticks[SPACE] = 0;
} else if (keyed_ticks[SPACE] == didah_len[DIT]) {
didah_enqueue(SPACE);
enqueued_spaces++;
}
}
break;
case keying_x_left_key_press:
nstate = keying_left_press;
keyed_ticks[left_didah] = 0;
didah_enqueue(left_didah);
last_keyed = left_didah;
break;
case keying_x_left_key_release:
debug("BAD!!! keying_x_left_key_release in idle\r\n");
break;
case keying_x_right_key_press:
nstate = keying_right_press;
keyed_ticks[right_didah] = 0;
didah_enqueue(right_didah);
last_keyed = right_didah;
break;
case keying_x_right_key_release:
debug("BAD!!! keying_x_right_key_release in idle\r\n");
break;
}
break;
case keying_left_press:
/* if we have a right key press go to keying_both_press */
/* if we have a key release, go back to idle */
/* if we have a tick, stay and enqueue left didahs */
switch (event) {
case keying_x_tick:
if (keying_mode == keying_mode_paddle)
break;
if (keyed_ticks[left_didah]++ == didah_len[left_didah]) {
keyed_ticks[left_didah] = 0;
didah_enqueue(left_didah);
last_keyed = left_didah;
}
break;
case keying_x_left_key_press:
debug("BAD!!! keying_x_left_key_release in keying_left_press\r\n");
break;
case keying_x_left_key_release:
debug("keying_x_left_key_release: mode = %u, last_keyed = %d\r\n", keying_mode, last_keyed);
nstate = keying_idle;
enqueued_spaces = 0;
keyed_ticks[SPACE] = 0;
break;
case keying_x_right_key_press:
if (keying_mode & keying_mode_dumb) {
nstate = keying_right_press;
} else {
nstate = keying_both_press;
}
didah_enqueue(right_didah);
if (keying_mode & keying_mode_iambic) {
last_keyed = left_didah;
} else {
last_keyed = right_didah;
}
keyed_ticks[last_keyed] = 0;
break;
case keying_x_right_key_release:
if (keying_mode & keying_mode_dumb)
break;
debug("BAD!!! keying_x_right_key_release in keying_left_press\r\n");
break;
}
break;
case keying_right_press:
/* if we have a left key press, go to keying_both_press */
/* if we have a key release, go back to idle */
/* if we have a tick, stay and enqueue right didahs */
switch (event) {
case keying_x_tick:
if (keying_mode == keying_mode_paddle)
break;
if (keyed_ticks[right_didah]++ == didah_len[right_didah]) {
keyed_ticks[right_didah] = 0;
didah_enqueue(right_didah);
last_keyed = right_didah;
}
break;
case keying_x_left_key_press:
if (keying_mode & keying_mode_dumb) {
nstate = keying_left_press;
} else {
nstate = keying_both_press;
}
didah_enqueue(left_didah);
if (keying_mode & keying_mode_iambic) {
last_keyed = right_didah;
} else {
last_keyed = left_didah;
}
keyed_ticks[last_keyed] = 0;
break;
case keying_x_left_key_release:
if (keying_mode & keying_mode_dumb)
break;
debug("BAD!!! keying_x_left_key_release in keying_right_press\r\n");
break;
case keying_x_right_key_press:
debug("BAD!!! keying_x_right_key_release in keying_right_press\r\n");
break;
case keying_x_right_key_release:
debug("keying_x_right_key_release: mode = %u, last_keyed = %d\r\n", keying_mode, last_keyed);
nstate = keying_idle;
enqueued_spaces = 0;
keyed_ticks[SPACE] = 0;
break;
}
break;
case keying_both_press:
/* if we have N ticks, move on to enqueue and continue */
/* if we have a key release, go back to keying_*_press */
switch (event) {
case keying_x_tick:
if (keyed_ticks[last_keyed]++ == didah_len[last_keyed]) {
didah_enqueue(last_keyed);
if (keying_mode & keying_mode_iambic) {
last_keyed = other_didah(last_keyed);
}
keyed_ticks[last_keyed] = 0;
}
break;
case keying_x_left_key_press:
debug("BAD!!! keying_x_left_key_press in keying_both_ready\r\n");
break;
case keying_x_left_key_release:
keyed_ticks[right_didah] = didah_len[right_didah] - didah_len[DIT];
if (keying_mode & keying_mode_iambic_b) {
didah_enqueue(last_keyed);
}
nstate = keying_right_press;
break;
case keying_x_right_key_press:
debug("BAD!!! keying_x_right_key_press in keying_both_ready\r\n");
break;
case keying_x_right_key_release:
keyed_ticks[left_didah] = didah_len[left_didah] - didah_len[DIT];
if (keying_mode & keying_mode_iambic_b) {
didah_enqueue(last_keyed);
}
nstate = keying_left_press;
break;
}
break;
}
cstate = nstate;
sreg(iv);
}
static clock_select16_t beeper_clock;
static void beeper_on(void) {
timer3_set_scale(beeper_clock);
}
static void beeper_off(void) {
timer3_set_scale(t16_stopped);
BEEPER_PORT &= ~BEEPER_BIT;
}
void cw_set_word_space(bool spaces, bool save) {
if (save)
settings_set_autospace(spaces);
word_space = spaces;
}
void cw_set_frequency(uint8_t hz) {
uint16_t top;
uint32_t tmp;
debug("cw_set_frequency(%u)\r\n", hz);
if (hz < 8) {
debug("hz out of range\r\n");
hz = 22;
}
settings_set_frequency(hz);
tmp = (F_CPU/10) / hz / 2 - 1;
if (tmp <= 0xffff) {
top = (uint16_t)tmp;
beeper_clock = t16_no_prescaling;
} else {
top = (F_CPU/10) / hz / 2 / 64 - 1;
beeper_clock = t16_divide_by_64;
}
// we turn the beeper on independently with beeper_on
timer3_set_top(top);
}
void cw_set_speed(uint8_t wpm) {
debug("cw_set_speed(%u)\r\n", wpm);
if (wpm < 3 || wpm > 99) {
debug("wpm out of range\r\n");
wpm = 13;
}
settings_set_wpm(wpm);
dit_len = 1200 / wpm;
didah_len[DIT] = 2*(uint16_t)dit_len;
didah_len[DAH] = 4*(uint16_t)dit_len;
didah_len[SPACE] = 6*(uint16_t)dit_len;
if (ms_tick_registered(TICK_CW_ADVANCE))
cw_out_normal_tick();
}
void cw_set_keying_mode(keying_mode_t mode) {
debug("cw_set_keying_mode(%u)\r\n", mode);
keying_mode = mode;
settings_set_keying_mode(mode);
}
void toggle_bit(void) {
BEEPER_PORT ^= BEEPER_BIT;
}
void cw_tick(void) {
cw_in_advance_tick(keying_x_tick);
}
void cw_set_dq_callback(cw_dq_cb_t cb) {
cw_dq_cb = cb;
}
void cw_enable_outputs(uint8_t enable_what) {
if (enable_what & CW_ENABLE_BEEPER) {
debug("+++ enable beeper bit\r\n");
BEEPER_DDR |= BEEPER_BIT;
}
if (enable_what & CW_ENABLE_KEYER) {
debug("+++ enable keyer bit\r\n");
CW_DDR |= CW_BIT;
}
if (enable_what & CW_ENABLE_DIDAH) {
debug("+++ enable didah bits\r\n");
DIT_DDR |= DIT_BIT;
DAH_DDR |= DAH_BIT;
}
}
void cw_disable_outputs(uint8_t disable_what) {
if (disable_what & CW_ENABLE_BEEPER) {
debug("--- disable beeper bits\r\n");
BEEPER_DDR &= ~BEEPER_BIT;
}
if (disable_what & CW_ENABLE_KEYER) {
debug("--- disable keyer bits\r\n");
CW_DDR &= ~CW_BIT;
}
if (disable_what & CW_ENABLE_DIDAH) {
debug("--- disable didah bits\r\n");
DIT_DDR &= ~DIT_BIT;
DAH_DDR &= ~DAH_BIT;
}
}
static void output_didah(didah_queue_t didah, bool pressed) {
if (pressed) {
if (didah == DIT) {
DIT_PORT |= DIT_BIT;
} else {
DAH_PORT |= DAH_BIT;
}
} else {
if (didah == DIT) {
DIT_PORT &= ~DIT_BIT;
} else {
DAH_PORT &= ~DAH_BIT;
}
}
}