-
Notifications
You must be signed in to change notification settings - Fork 1
/
morse.c
124 lines (105 loc) · 2.49 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
#include "morse.h"
#define MORSE_TIME_BOUNCE 1
static unsigned char dit_or_dah;
static unsigned char character_done;
static unsigned char printed_space;
static unsigned long int time_down;
static unsigned long int time_up;
static unsigned long int start_down_time;
static unsigned long int start_up_time;
static unsigned short int dit_time;
static unsigned short int avg_dah_time;
static unsigned char current_char;
void morse_init(void)
{
dit_or_dah = 1;
character_done = 1;
printed_space = 1;
time_down = 0;
time_up = 0;
start_down_time = 0;
start_up_time = 0;
dit_time = 10;
avg_dah_time = 240;
current_char = 0;
}
void morse_shift_bits(void)
{
if (time_down < dit_time / 3)
return;
current_char <<= 1;
dit_or_dah = 1;
if (time_down < dit_time) {
current_char++;
} else {
avg_dah_time = (time_down + avg_dah_time) / 2;
dit_time = (avg_dah_time / 3) * 2;
}
}
void morse_print_character(void)
{
printed_space = 0;
if (current_char > 63) {
switch (current_char) {
case 0x47: MORSE_PRINT(':'); break;
case 0x4c: MORSE_PRINT(','); break;
case 0x52: MORSE_PRINT(')'); break;
case 0x54: MORSE_PRINT('!'); break;
case 0x55: MORSE_PRINT(';'); break;
case 0x5e: MORSE_PRINT('-'); break;
case 0x61: MORSE_PRINT('\''); break;
case 0x65: MORSE_PRINT('@'); break;
case 0x6a: MORSE_PRINT('.'); break;
case 0x73: MORSE_PRINT('?'); break;
case 0x7a: MORSE_PRINT('s'); MORSE_PRINT('k'); break;
case 0xf6: MORSE_PRINT('$'); break;
default: MORSE_PRINT('#'); break;
}
} else if (current_char == 0x35) {
MORSE_PRINT('a'); MORSE_PRINT('r');
} else {
MORSE_PRINT(morse_code[current_char]);
}
}
void morse_print_space(void)
{
if (printed_space)
return;
printed_space = 1;
MORSE_PRINT(' ');
}
void morse_process(char audio_present)
{
if (!audio_present) {
start_up_time = 0;
if (start_down_time == 0)
start_down_time = MORSE_TIMER();
character_done = 0;
dit_or_dah = 0;
MORSE_DELAY(MORSE_TIME_BOUNCE);
if (current_char == 0)
current_char = 1;
} else {
if (start_up_time == 0)
start_up_time = MORSE_TIMER();
time_up = MORSE_TIMER() - start_up_time;
if (time_up < 10)
return;
if (time_up > avg_dah_time * 2)
morse_print_space();
if (start_down_time > 0) {
time_down = MORSE_TIMER() - start_down_time;
start_down_time = 0;
}
if (!dit_or_dah)
morse_shift_bits();
if (!character_done) {
if (time_up > dit_time) {
morse_print_character();
character_done = 1;
current_char = 0;
}
time_down = 0;
}
}
}