-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
460 lines (426 loc) · 16.9 KB
/
main.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
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
import serial
import time
import sys
import glob
import os
import flet as ft
import jaconv
import pyaudio
import numpy as np
from pykakasi import kakasi
kakasi = kakasi()
global receive_break
receive_break = False
def get_serial_port():
if sys.platform == "linux" or sys.platform == "linux2":
ports = glob.glob('/dev/tty.usbserial*')
elif sys.platform == "darwin":
ports = glob.glob('/dev/tty.usbserial*')
elif sys.platform == "win32":
ports = ['COM%s' % (i + 1) for i in range(256)]
else:
raise Exception("Unsupported platform")
result = []
for port in ports:
try:
s = serial.Serial(port)
s.close()
result.append(port)
except (OSError, serial.SerialException):
pass
return result
def convert_to_morse_code(text: str, lang="en"):
if lang == "ja":
morse_code = {
'あ': '--.--', 'い': '.-', 'う': '..-', 'え': '-.---', 'お': '.-...', 'か': '.-..', 'き': '-.-..', 'く': '...-', 'け': '-.--', 'こ': '----',
'さ': '-.-.-', 'し': '--.-.', 'す': '---.-', 'せ': '.---.', 'そ': '---.', 'た': '-.', 'ち': '..-.', 'つ': '.--.', 'て': '.-.--', 'と': '..-..',
'な': '.-.', 'に': '-.-.', 'ぬ': '....', 'ね': '--.-', 'の': '..--', 'は': '-...', 'ひ': '--..-', 'ふ': '--..', 'へ': '.', 'ほ': '-..',
'ま': '-..-', 'み': '..-.-', 'む': '-', 'め': '-...-', 'も': '-..-.', 'や': '.--', 'ゆ': '-..--', 'よ': '--',
'ら': '...', 'り': '--.', 'る': '-.--.', 'れ': '---', 'ろ': '.-.-', 'わ': '-.-', 'を': '.---', 'ん': '.-.-.',
'゛': '..', '゜': '..--.', 'ー': '.--.-', '、': '.-.-.-', '(': '-.--.-', ')': '.-..-.', ' ': " "
}
morse_code_text = ""
for char in text:
if char in morse_code:
morse_code_text += morse_code[jaconv.kata2hira(char)] + " "
return morse_code_text
else:
morse_code = {
'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': '--..',
'1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.', '0': '-----',
',': '--..--', '.': '.-.-.-', '?': '..--..', '/': '-..-.', '-': '-....-', '(': '-.--.', ')': '-.--.-', " ": " "
}
morse_code_text = ""
for char in text:
if char.upper() in morse_code:
morse_code_text += morse_code[char.upper()] + " "
return morse_code_text
def convert_hex_to_morse_code(hex: str):
morse_code = {
'0': '-', '1': '.', "2": '.-', '3': '..', '4': '.--', '5': '.-.', '6': '..-', '7': '...',
'8': '.---', '9': '.--.', 'A': '.-.-', 'B': '.-..', 'C': '..--', 'D': '..-.', 'E': '...-', 'F': '....',
}
morse_code_text = ""
for char in hex:
if char.upper() in morse_code:
morse_code_text += morse_code[char.upper()] + " "
else:
morse_code_text += " "
return morse_code_text
def convert_morse_to_text(morse: str, lang="en"):
if lang == "ja":
morse_code = {
'--.--': 'あ', '.-': 'い', '..-': 'う', '-.---': 'え', '.-...': 'お', '.-..': 'か', '-.-..': 'き', '...-': 'く', '-.--': 'け', '----': 'こ',
'-.-.-': 'さ', '--.-.': 'し', '---.-': 'す', '.---.': 'せ', '---.': 'そ', '-.': 'た', '..-.': 'ち', '.--.': 'つ', '.-.--': 'て', '..-..': 'と',
'.-.': 'な', '-.-.': 'に', '....': 'ぬ', '--.-': 'ね', '..--': 'の', '-...': 'は', '--..-': 'ひ', '--..': 'ふ', '.': 'へ', '-..': 'ほ',
'-..-': 'ま', '..-.-': 'み', '-': 'む', '-...-': 'め', '-..-.': 'も', '.--': 'や', '-..--': 'ゆ', '--': 'よ',
'...': 'ら', '--.': 'り', '-.--.': 'る', '---': 'れ', '.-.-': 'ろ', '-.-': 'わ', '.---': 'を', '.-.-.': 'ん',
'..': '゛', '..--.': '゜', '.--.-': 'ー', '.-.-.-': '、', '-.--.-': '(', '.-..-.': ')', "": " "
}
morse_text = ""
for char in morse.split(" "):
if char in morse_code:
morse_text += morse_code[char]
return morse_text
elif lang == "en":
morse_code = {
'.-': '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',
'.----': '1', '..---': '2', '...--': '3', '....-': '4', '.....': '5', '-....': '6', '--...': '7', '---..': '8', '----.': '9', '-----': '0',
'--..--': ',', '.-.-.-': '.', '..--..': '?', '-..-.': '/', '-....-': '-', '-.--.': '(', '-.--.-': ')', "": " "
}
morse_text = ""
for char in morse.split(" "):
if char in morse_code:
morse_text += morse_code[char]
return morse_text
else:
morse_code = {
'-': '0', '.': '1', '.-': '2', '..': '3', '.--': '4', '.-.': '5', '..-': '6', '...': '7',
'.---': '8', '.--.': '9', '.-.-': 'A', '.-..': 'B', '..--': 'C', '..-.': 'D', '...-': 'E', '....': 'F',
}
morse_text = ""
for char in morse.split(" "):
if char in morse_code:
morse_text += morse_code[char]
return morse_text
def main(page: ft.Page):
page.title = "FTR-102 通信"
page.vertical_alignment = ft.MainAxisAlignment.CENTER
# ----- モールス -----
t = ft.Text()
sendText = ft.TextField(
label="メッセージ",
multiline=True,
min_lines=1,
)
sendText.border_color = ft.colors.WHITE
speed = ft.Slider(
label="{value}%",
min=0,
max=100,
divisions=10,
value=50,
)
lang = ft.RadioGroup(content=ft.Column([
ft.Radio(value="ja", label="和文"),
ft.Radio(value="en", label="欧文"),
]), value="en")
langReceive = ft.RadioGroup(content=ft.Column([
ft.Radio(value="ja", label="和文"),
ft.Radio(value="en", label="欧文"),
ft.Radio(value="bin", label="バイナリ"),
]), value="en")
receiveMorse = ft.TextField(
label="受信信号",
multiline=True,
min_lines=1,
read_only=True,
)
receiveText = ft.TextField(
label="受信メッセージ",
multiline=True,
min_lines=1,
read_only=True,
)
def receive_stop(e):
global receive_break
receive_break = True
receiveStop.disabled = True
page.update()
receiveStop = ft.ElevatedButton(
text="停止", on_click=receive_stop, disabled=True)
def start_receive(e):
global receive_break
global stop
stop = False
receiveStop.disabled = False
page.update()
print("Receiving message...")
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1,
rate=44100, input=True, frames_per_buffer=1024)
send_sec = 0
space_sec = 0
while True:
if receive_break:
print("Receiving stopped")
break
data = stream.read(1024)
x = np.frombuffer(data, dtype="int16") / 32768.0
if x.max() <= 0.03 and send_sec == 0 and space_sec > 1.25:
break
if x.max() > 0.03:
send_sec += 10 / 441
elif send_sec > 0:
if send_sec >= 0.3 * (1 / (speed.value / 50)):
receiveMorse.value += "-"
page.update()
elif send_sec >= 0.1 * (1 / (speed.value / 50)):
receiveMorse.value += "."
page.update()
send_sec = 0
space_sec = 0
else:
space_sec += 10 / 441
if space_sec > 0.15 * (1 / (speed.value / 50)) and len(receiveMorse.value) != 0:
if len(receiveMorse.value) > 2:
if receiveMorse.value[-2] == " ":
continue
receiveMorse.value += " "
page.update()
space_sec = 0
stream.stop_stream()
stream.close()
p.terminate()
receiveText.value = convert_morse_to_text(
receiveMorse.value, langReceive.value)
receiveStop.disabled = True
print("Received message")
page.update()
receive_break = False
def close_page(e):
global receive_break
receive_break = True
page.update()
def clear_receive(e):
receiveText.value = ""
receiveMorse.value = ""
page.update()
receiveText.border_color = ft.colors.WHITE
receiveMorse.border_color = ft.colors.WHITE
receiveClear = ft.ElevatedButton(text="クリア", on_click=clear_receive)
receiveStart = ft.ElevatedButton(
text="開始", on_click=start_receive)
global stop
stop = False
def send(e):
print("Sending message")
global stop
stop = False
stop_button.disabled = False
page.update()
converted = kakasi.convert(sendText.value)
message = list()
for char in converted:
message += list(char['hira'])
port = get_serial_port()
if len(port) == 0:
print("No serial port found")
return
read = serial.Serial(port[0], 9600, dsrdtr=False)
read.dtr = False
time.sleep(1)
for char in message:
if stop:
stop = False
break
encoded = convert_to_morse_code(char, lang.value)
print(f"Sending {char} as {encoded}")
for signal in encoded:
if stop:
break
if signal == ".":
read.dtr = True
time.sleep(0.1 * (1 / (speed.value / 50)))
read.dtr = False
time.sleep(0.05 * (1 / (speed.value / 50)))
elif signal == "-":
read.dtr = True
time.sleep(0.3 * (1 / (speed.value / 50)))
read.dtr = False
time.sleep(0.05 * (1 / (speed.value / 50)))
else:
time.sleep(0.15 * (1 / (speed.value / 50)))
sendText.value = ""
read.close()
print("Message sent")
page.update()
sendButton = ft.ElevatedButton(text="送信", on_click=send)
# ----- バイナリ -----
global file_path
file_path = ""
def send_binary(e):
global file_path
global stop
stop = False
stop_button.disabled = False
page.update()
if (file_path == ""):
print("No file selected")
return
print("Sending binary")
port = get_serial_port()
if len(port) == 0:
print("No serial port found")
return
read = serial.Serial(port[0], 9600, dsrdtr=False)
read.dtr = False
time.sleep(1)
file_size = os.path.getsize(file_path)
with open(file_path, "rb") as f:
byte = f.read(file_size)
hex_data = byte.hex()
read_bytes = 0
for i in range(0, len(hex_data), 2):
if stop:
stop = False
break
progress.value = read_bytes / len(hex_data)
page.update()
encoded = convert_hex_to_morse_code(hex_data[i:i+2])
print(f"Sending {hex_data[i:i+2]} as {encoded}")
for signal in encoded:
if signal == ".":
read.dtr = True
time.sleep(0.1 * (1 / (speed.value / 50)))
read.dtr = False
time.sleep(0.05 * (1 / (speed.value / 50)))
elif signal == "-":
read.dtr = True
time.sleep(0.3 * (1 / (speed.value / 50)))
read.dtr = False
time.sleep(0.05 * (1 / (speed.value / 50)))
else:
time.sleep(0.15 * (1 / (speed.value / 50)))
read_bytes += 1
read.close()
print("Binary sent")
page.update()
def set_file_name(e):
file_name.value = e.files[0].name
global file_path
file_path = e.files[0].path
page.update()
def stop_send(e):
global stop
stop = True
stop_button.disabled = True
file_picker = ft.FilePicker(
on_result=set_file_name)
page.overlay.append(file_picker)
file_name = ft.Text()
pick_button = ft.ElevatedButton(
text="ファイルを選択", on_click=lambda e: file_picker.pick_files(allow_multiple=False))
progress = ft.ProgressBar()
send_button = ft.ElevatedButton(text="送信", on_click=send_binary)
stop_button = ft.ElevatedButton(
text="停止", on_click=stop_send, disabled=True)
info = """#### **FTR-102 通信プログラム**
このプログラムは、FTR-102を使用してモールス信号やバイナリデータを送受信するためのプログラムです。
バージョン: 1.0
開発者: [wamo](https://github.com/opera7133)
"""
pages = [
[ft.Text("速度", size=20),
speed, ft.Text("言語", size=20), lang, ft.Text("メッセージ", size=20), sendText, sendButton, stop_button, t],
[ft.Text("速度", size=20),
speed, ft.Text("バイナリ", size=20),
ft.Row([pick_button, file_name], spacing=5), progress, send_button, stop_button],
[ft.Text("受信", size=20), speed, ft.Text("言語", size=20), langReceive, receiveMorse, receiveText, ft.Row(
[receiveStart, receiveStop, receiveClear], spacing=5)],
[ft.Text("情報", size=20), ft.Markdown(
info, on_tap_link=lambda e: page.launch_url(e.data),)]
]
current_page = pages[0]
def route_change(route):
page.views.clear()
page.views.append(
ft.View(
"/",
[
ft.Row([rail, ft.VerticalDivider(width=1), ft.Column(
current_page, expand=True)], expand=True)
]
)
)
if page.route == "/binary":
page.views[0] = ft.View(
"/binary",
[
ft.Row([rail, ft.VerticalDivider(width=1), ft.Column(
pages[1], expand=True)], expand=True)
]
)
if page.route == "/receive":
page.views[0] = ft.View(
"/receive",
[
ft.Row([rail, ft.VerticalDivider(width=1), ft.Column(
pages[2], expand=True)], expand=True)
]
)
if page.route == "/info":
page.views[0] = ft.View(
"/info",
[
ft.Row([rail, ft.VerticalDivider(width=1), ft.Column(
pages[3], expand=True)], expand=True)
]
)
page.update()
def view_pop(view):
page.views.pop()
top_view = page.views[-1]
page.go(top_view.route)
def change_page(e: ft.ControlEvent):
page.go(f"/{['', 'binary', 'receive', 'info'][int(e.data)]}")
rail = ft.NavigationRail(
destinations=[
ft.NavigationRailDestination(
icon=ft.icons.MORE_HORIZ_OUTLINED,
selected_icon=ft.icons.MORE_HORIZ,
label="モールス",
),
ft.NavigationRailDestination(
icon=ft.icons.CODE_OUTLINED,
selected_icon=ft.icons.CODE,
label="バイナリ",
),
ft.NavigationRailDestination(
icon=ft.icons.CLOUD_DOWNLOAD_OUTLINED,
selected_icon=ft.icons.CLOUD_DOWNLOAD,
label="受信",
),
ft.NavigationRailDestination(
icon=ft.icons.INFO_OUTLINED,
selected_icon=ft.icons.INFO,
label="情報",
),
],
label_type=ft.NavigationRailLabelType.ALL,
selected_index=0,
on_change=change_page,
)
theme = ft.Theme()
theme.page_transitions.macos = ft.PageTransitionTheme.NONE
theme.page_transitions.windows = ft.PageTransitionTheme.NONE
page.theme = theme
page.on_route_change = route_change
page.on_view_pop = view_pop
page.go(page.route)
page.on_close = close_page
ft.app(target=main)