forked from fablab-wue/piTelex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
txCode.py
331 lines (287 loc) · 10.1 KB
/
txCode.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
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
#!python3
"""
Telex Code Conversion
Baudot-Code = CCITT-1
Baudot-Murray-Code = CCITT-2
CCITT-2:
| 543.21 | 12.345 | A | 1 | PA| P1| US| Z1|
===========================================
| 000.00 | 00.000 | ▨ | ▨ | ⊝ | ⊝ | ▨ | #* |
| 000.01 | 10.000 | E | 3 | Е | 3 | 3 | 3 |
| 000.10 | 01.000 | ≡ |
| 000.11 | 11.000 | A | - | А | - | - | - |
| 001.00 | 00.100 | ⎵ |
| 001.01 | 10.100 | S | ' | С | ' | ⍾ | ' |
| 001.10 | 01.100 | I | 8 | И | 8 | 8 | 8 |
| 001.11 | 11.100 | U | 7 | У | 7 | 7 | 7 |
| 010.00 | 00.010 | ❮ |
| 010.01 | 10.010 | D | ✠ | Д | ✠ | $ | ✠ |
| 010.10 | 01.010 | R | 4 | Р | 4 | 4 | 4 |
| 010.11 | 11.010 | J | ⍾ | Й | Ю | ' | ; |
| 011.00 | 00.110 | N | , | Н | , | , | , |
| 011.01 | 10.110 | F | ▨ | Ф | Э | ! | [ |
| 011.10 | 01.110 | C | : | Ц | : | : | : |
| 011.11 | 11.110 | K | ( | К | ( | ( | ( |
| 100.00 | 00.001 | T | 5 | Т | 5 | 5 | 5 |
| 100.01 | 10.001 | Z | + | З | + | " | + |
| 100.10 | 01.001 | L | ) | Л | ) | ) | ) |
| 100.11 | 11.001 | W | 2 | В | 2 | 2 | 2 |
| 101.00 | 00.101 | H | ▨ | Х | Щ | ✠ | ⒑ |
| 101.01 | 10.101 | Y | 6 | Ы | 6 | 6 | 6 |
| 101.10 | 01.101 | P | 0 | П | 0 | 0 | 0 |
| 101.11 | 11.101 | Q | 1 | Я | 1 | 1 | 1 |
| 110.00 | 00.011 | O | 9 | О | 9 | 9 | 9 |
| 110.01 | 10.011 | B | ? | Б | ? | ? | ╳ |
| 110.10 | 01.011 | G | ▨ | Г | Ш | & | ] |
| 110.11 | 11.011 | 1... |
| 111.00 | 00.111 | M | . | М | . | . | . |
| 111.01 | 10.111 | X | / | Ь | / | / | / |
| 111.10 | 01.111 | V | = | Ж | = | ; | = |
| 111.11 | 11.111 | A... |
Note for Zuse: #* '#' in A... '*' in 1...
▨ not defined
❮ <CR> -> \ \r
≡ <LF> -> _ \n
⎵ <SPACE>
✠ WRU? -> @
⍾ BELL <BEL> -> %
1... FIGS -> >
A... LTRS -> <
⊝ <NUL> -> ~
⒑ Zuse ? -> §
╳ Zuse ? -> ^
Zuese Bu: #
Zuese Zi: * [ ] ╳ ⒑ ;
Free: ²³{}|^°§
ªº¹² ¹ᴬ
http://rabbit.eng.miami.edu/info/baudot.html <<< wrong figs order!
http://www.baudot.net/docs/smith--teletype-codes.pdf
"""
__author__ = "Jochen Krapf"
__email__ = "[email protected]"
__copyright__ = "Copyright 2020, JK"
__license__ = "GPL3"
__version__ = "0.1.0"
import time
import unicodedata
#from unidecode import unidecode
import logging
l = logging.getLogger("piTelex." + __name__)
#######
class BaudotMurrayCode:
# Baudot-Murray-Code to ASCII table
_LUT_BM2A_ITA2 = (
"~E\nA SIU\rDRJNFCKTZLWHYPQOBG>MXV<",
"~3\n- '87\r@4%,~:(5+)2~6019?~>./=<"
)
_LUT_BM2A_US = (
"~E\nA SIU\rDRJNFCKTZLWHYPQOBG>MXV<",
"~3\n- %87\r$4',!:(5\")2@6019?&>./;<"
)
_LUT_BM2A_MKT2 = (
"~E\nA SIU\rDRJNFCKTZLWHYPQOBG>MXV<",
"~3\n- '87\r@4Ю,Э:(5+)2Щ6019?Ш>./=<",
"~Е\nА СИУ\rДРЙНФЦКТЗЛВХЫПЯОБГ>МЬЖ<"
)
_LUT_BM2A_ZUSE = (
"#E\nA SIU\rDRJNFCKTZLWHYPQOBG>MXV<",
"*3\n- '87\r@4;,[:(5+)2^6019µ]>./=<"
)
# Baudot-Murray-Code mode switch codes
_LUT_BMsw_ITA2 = (0x1F, 0x1B)
_LUT_BMsw_US = (0x1F, 0x1B)
_LUT_BMsw_MKT2 = (0x1F, 0x1B, 0x00)
_LUT_BMsw_ZUSE = (0x1F, 0x1B)
# Baudot-Murray-Code valid ASCII table
#_valid_char = " ABCDEFGHIJKLMNOPQRSTUVWXYZ~3\n- '87\r@4%,~:(5+)2~6019?~]./=[#"
_valid_ASCII_convert_chars = " ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-+=:/()?.,'\n\r@~"
_LUT_convert_chars = {
'Ä': 'AE',
'Ö': 'OE',
'Ü': 'UE',
'ß': 'SS',
'\a': '%', # Bell
'\f': '(FF)', # Form Feed
'\x7f': '(DEL)', # Delete
'\t': '(TAB)', # Tab
'\v': '(VT)', # Vertical Tab
'\x1B': '(ESC)', # Escape
'\b': '(BS)', # Backspace
'\x08': '(BS)', # Backspace
'&': '(AND)',
'€': '(EUR)',
'$': '(USD)',
'<': '(LT)',
'>': '(GT)',
'|': '(PIPE)',
'*': '(STAR)',
'#': '(HASH)',
'@': '(AT)',
'"': "'",
';': ',.',
'!': '(./)',
'%': '(./.)',
'[': '(',
']': ')',
'{': '-(',
'}': ')-',
'\\': '/',
'_': '--',
}
CODING_ITA2 = 0
CODING_US = 1
CODING_MKT2 = 2
CODING_ZUSE = 3
# =====
@staticmethod
def translate(text:str) -> str:
return BaudotMurrayCode.ascii_to_tty_text(text)
# -----
@staticmethod
def ascii_to_tty_text(text:str) -> str:
ret = ''
text = text.upper()
for a in text:
try:
if a not in BaudotMurrayCode._valid_ASCII_convert_chars:
if a in BaudotMurrayCode._LUT_convert_chars:
a = BaudotMurrayCode._LUT_convert_chars.get(a, '?')
else:
nkfd_norm = unicodedata.normalize('NFKD', a)
a = u"".join([c for c in nkfd_norm if not unicodedata.combining(c)])
#a = unicodedata.normalize('NFD', a).encode('ascii', 'ignore')
#a = unidecode(a)
if a not in BaudotMurrayCode._valid_ASCII_convert_chars:
a = '?'
ret += a
except:
pass
return ret
# -----
@staticmethod
def do_flip_bits(code: bytes) -> bytes:
ret = bytearray()
for b in code:
rb = 0
if b & 1:
rb |= 16
if b & 2:
rb |= 8
if b & 4:
rb |= 4
if b & 8:
rb |= 2
if b & 16:
rb |= 1
ret.append(rb)
return ret
# =====
def __init__(self, loop_back:bool=False, coding:int=0, flip_bits=False, character_duration=0.15, show_BuZi:int=2):
self._mode = None # 0=LTRS 1=FIGS
self._flip_bits = flip_bits
self._loop_back = loop_back
self._show_BuZi = show_BuZi
self._loop_back_eat_bytes = 0
self._loop_back_expire_time = 0
self._character_duration = character_duration
if coding == self.CODING_US:
self._LUT_BM2A = self._LUT_BM2A_US
self._LUT_BMsw = self._LUT_BMsw_US
elif coding == self.CODING_MKT2:
self._LUT_BM2A = self._LUT_BM2A_MKT2
self._LUT_BMsw = self._LUT_BMsw_MKT2
elif coding == self.CODING_ZUSE:
self._LUT_BM2A = self._LUT_BM2A_ZUSE
self._LUT_BMsw = self._LUT_BMsw_ZUSE
else:
self._LUT_BM2A = self._LUT_BM2A_ITA2
self._LUT_BMsw = self._LUT_BMsw_ITA2
# -----
def reset(self):
self._ModeA2BM = None # 0=LTRS 1=FIGS
# -----
def encodeA2BM(self, ascii:str) -> bytes:
''' convert an ASCII string to a list of baudot-murray-coded bytes '''
ret = bytearray()
if not isinstance(ascii, str):
ascii = str(ascii)
ascii = ascii.upper()
if self._mode is None:
self._mode = 0 # letters
ret.append(self._LUT_BMsw[self._mode])
for a in ascii:
try: # symbol in current layer?
nm = self._mode
b = self._LUT_BM2A[nm].index(a)
ret.append(b)
if b in self._LUT_BMsw: # explicit Bu or Zi
self._mode = self._LUT_BMsw.index(b)
except ValueError:
try: # symbol in other layer?
nm += 1
if nm >= len(self._LUT_BM2A):
nm = 0
b = self._LUT_BM2A[nm].index(a)
ret.append(self._LUT_BMsw[nm])
ret.append(b)
self._mode = nm
except ValueError:
try: # symbol in other layer?
nm += 1
if nm >= len(self._LUT_BM2A):
nm = 0
b = self._LUT_BM2A[nm].index(a)
ret.append(self._LUT_BMsw[nm])
ret.append(b)
self._mode = nm
except: # symbol not found -> ignore
pass
except: # unknown -> ignore
pass
if ret and self._flip_bits:
ret = self.do_flip_bits(ret)
if self._loop_back:
length = len(ret)
self._loop_back_eat_bytes += length
time_act = time.time()
if self._loop_back_expire_time < time_act:
self._loop_back_expire_time = time_act
self._loop_back_expire_time += length * self._character_duration
return ret
# -----
def decodeBM2A(self, code:bytes) -> str:
''' convert a list/bytearray of baudot-murray-coded bytes to an ASCII string '''
ret = ''
if self._flip_bits:
code = self.do_flip_bits(code)
for b in code:
if self._loop_back and self._loop_back_eat_bytes:
if time.time()-self._loop_back_expire_time > 6: # about 40 characters
self._loop_back_eat_bytes = 0
else:
self._loop_back_eat_bytes -= 1
#if b == 2:
# print(self._loop_back_eat_bytes, time.time()-self._loop_back_expire_time) # debug
continue
try:
if b in self._LUT_BMsw:
mode = self._LUT_BMsw.index(b)
if self._mode != mode:
self._mode = mode
if self._show_BuZi == 0: # no BuZi
continue
if self._show_BuZi <= 1: # explicit BuZi
continue
if b >= 0x20:
ret += '{?#' + hex(b)[2:] + '}'
elif self._mode is None:
ret += '{?'
ret += self._LUT_BM2A[0][b]
ret += self._LUT_BM2A[1][b]
ret += '}'
else:
ret += self._LUT_BM2A[self._mode][b]
except:
ret += '{!}' # debug
return ret
#######