-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorse_gen.py
executable file
·182 lines (157 loc) · 4.35 KB
/
morse_gen.py
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
#!/usr/bin/python3.6
from textwrap import wrap, indent, dedent
import math as m
from string import printable
morse = {
'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': '--..',
# numbers
'0': '-----',
'1': '.----',
'2': '..---',
'3': '...--',
'4': '....-',
'5': '.....',
'6': '-....',
'7': '--...',
'8': '---..',
'9': '----.',
# punctuation
'.': '.-.-.-',
',': '--..--',
'?': '..--..',
'!': '-.-.--',
'`': '.----.',
'/': '-..-.',
'&': '.-...',
':': '---...',
';': '-.-.-.',
'=': '-...-',
'+': '.-.-.',
'-': '-....-',
'(': '-.--.',
')': '-.--.-',
'_': '..--.-',
'"': '.-..-.',
'$': '...-..-',
'@': '.--.-.',
# 'error': '........'
}
for i,c in enumerate(range(ord('!'), ord('a'))):
print(chr(c) if chr(c) in morse else ' ', end='')
if (i % 16 == 15):
print()
else:
print(' ', end='')
def pack_code(rep):
"""
Encodes symbolic Morse repr as binary
>>> pack_code("--.---")
(0b111011, 6)
>>> pack_code("-.")
(0b10, 2)
"""
return (sum((1 if c == '-' else 0) << i for i, c in enumerate(rep)), len(rep))
def unpack_code(enc_code):
code, n = enc_code
return ''.join(('-' if code & (1 << (n-1-i)) else '.') for i in range(n))
for ch, coding in morse.items():
enc = pack_code(coding)
#print("%c: [%d] %s, %s" % (ch, enc[1], format(enc[0], "0"+str(enc[1])+"b"), unpack_code(enc)))
# print for C
itms = sorted([(ord(ch), pack_code(coding)) for ch, coding in morse.items()], key=lambda itm: itm[0])
cchi = 0
result = []
entry = None
for (chi, enc) in itms:
if (chi - cchi >= 1) or entry is None:
cchi = chi+1
entry = [chi, cchi, [enc]]
result.append(entry)
else:
cchi = chi+1
entry[1] = cchi
entry[2].append(enc)
branches = []
table_lengths = []
table_codes = []
var_name = 'c';
sym_offs_t = 'uint8_t'
consumerFunc = 'beepMorse'
offset = 0
for entry in result:
start, end, codes = entry
singular_condition = (start == end-1)
if chr(start) in printable:
start = "'%c'" % (start)
if chr(end) in printable:
end = "'%c'" % (end)
if singular_condition:
branches.append(dedent(f"""\
if ({var_name} == {start}) {{
sym_offs = {offset};
}} """))
else:
branches.append(dedent(f"""\
if ({var_name} >= {start} && {var_name} < {end}) {{
sym_offs = ({var_name} - {start}) + {offset};
}} """))
table_codes.extend(code for code, _ in codes)
table_lengths.extend(n for _, n in codes)
offset += len(codes)
branches.append("""{
return false;
}""")
def format_list_repr(xs, fmt_str, width=80, indent_sz=4):
n_digits = m.ceil(m.log10(max(xs)))
list_str = ', '.join(format(x, fmt_str[:-1] + str(n_digits) + fmt_str[-1]) for x in xs)
out = ''
for line in wrap(list_str, width):
out += '\n' + ' '*indent_sz + line
return out + '\n'
print(f"// Len(morse) = {len(morse)}")
print(f"static const uint8_t table_codes[{len(table_codes)}] PROGMEM = {{ "
f"{format_list_repr(table_codes, '#0b')} }};")
print(f"static const uint8_t table_lengths[{len(table_lengths)}] PROGMEM = {{ "
f"{format_list_repr(table_lengths, 'd')} }};")
print(f"""\
bool beepMorse(char c) {{
{sym_offs_t} sym_offs;
{indent(" else ".join(branches), ' ')}
uint8_t n_bits = table_lengths[sym_offs];
uint8_t word = table_codes[sym_offs];
uint16_t symTime = dotDuration;
while(n_bits-- > 0) {{
tone(PIN_SOUND_OUTPUT, TX_BEEP_FREQ);
delay((word & 1)? TX_MORSE_DASH_TIME : TX_MORSE_DOT_TIME);
noTone(PIN_SOUND_OUTPUT);
delay(TX_MORSE_DOT_TIME);
word >>= 1;
}}
return true;
}}
""")