-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDipperV4.1CAT.py
1337 lines (1201 loc) · 72.4 KB
/
DipperV4.1CAT.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
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
import tkinter as tk
from tkinter import messagebox, ttk, simpledialog, Menu
import tkinter.font # Correct import for font handling
import pyaudio
import numpy as np
from scipy.fft import fft
from scipy.signal import butter, lfilter
import threading
import time
import os
import serial
from reedsolo import RSCodec, ReedSolomonError
import crcmod
import sys
import serial.tools.list_ports # For detecting COM ports
# Debug output at the very start
print("Script starting...")
# Debug output for imports
print("Imports completed")
# Check Python version
print(f"Python version: {sys.version}")
# Check current directory
print(f"Current directory: {os.getcwd()}")
# Verify file existence
script_path = os.path.abspath(__file__)
print(f"Script path: {script_path}")
try:
# Test dependency imports
print("Testing dependencies...")
p = pyaudio.PyAudio()
p.terminate()
np.array([1, 2, 3])
RSCodec(8)
crc16 = crcmod.mkCrcFun(0x11021, initCrc=0, xorOut=0xFFFF)
serial.tools.list_ports.comports()
print("Dependencies loaded successfully")
except Exception as e:
print(f"Dependency error: {e}")
sys.exit(1)
CHAR_SOUNDS = {
"A": [(2000, 2000, "tone")], "B": [(3000, 3000, "trill")], "C": [(4000, 2000, "slide")],
"D": [(1500, 1500, "tone")], "E": [(5000, 5000, "tone")], "F": [(2500, 3500, "slide")],
"G": [(1000, 1000, "trill")], "H": [(3500, 3500, "tone")], "I": [(4500, 4500, "tone")],
"J": [(2000, 4000, "slide")], "K": [(3000, 3000, "tone")], "L": [(4000, 4000, "trill")],
"M": [(1500, 2500, "slide")], "N": [(5000, 3000, "slide")], "O": [(1000, 1000, "tone")],
"P": [(3500, 3500, "trill")], "Q": [(2500, 2500, "tone")], "R": [(2000, 2000, "trill")],
"S": [(4500, 1500, "slide")], "T": [(3000, 5000, "slide")], "U": [(4000, 4000, "tone")],
"V": [(1500, 1500, "trill")], "W": [(1000, 2000, "slide")], "X": [(5000, 5000, "trill")],
"Y": [(3500, 2500, "slide")], "Z": [(2500, 2500, "trill")],
"0": [(1000, 1500, "slide")], "1": [(1500, 2000, "slide")], "2": [(2000, 2500, "slide")],
"3": [(2500, 3000, "slide")], "4": [(3000, 3500, "slide")], "5": [(3500, 4000, "slide")],
"6": [(4000, 4500, "slide")], "7": [(4500, 5000, "slide")], "8": [(5000, 4500, "slide")],
"9": [(2000, 1500, "slide")], "!": [(5000, 1000, "slide")], "/": [(3000, 4000, "slide")],
"-": [(2000, 2000, "trill")], ".": [(4500, 4500, "tone")], " ": [(1000, 1000, "tone")],
"@": [(6000, 7000, "slide")] # Added '@' with a unique sound pattern (high frequencies)
}
WORD_SOUNDS = {
"CQ": [(2000, 4000, "slide")], "DE": [(1500, 2500, "slide")],
}
SAMPLE_RATE = 44100
CHUNK = 1024
CALLSIGN_FILE = os.path.join(os.path.dirname(__file__), "mycallsign.txt")
SETTINGS_FILE = os.path.join(os.path.dirname(__file__), "radio_settings.txt")
SYMBOL_MAP = {**{char: i for i, char in enumerate(CHAR_SOUNDS.keys())},
**{word: i + len(CHAR_SOUNDS) for i, word in enumerate(WORD_SOUNDS.keys())}}
REVERSE_MAP = {i: char_or_word for char_or_word, i in SYMBOL_MAP.items()}
RS_CODEC = RSCodec(8)
V4_SYMBOLS = {
0: "A", 1: "B", 2: "C", 3: "D", 4: "E", 5: "F", 6: "G", 7: "H",
8: "I", 9: "K", 10: "L", 11: "M", 12: "N", 13: "O", 14: "P", 15: "Q"
}
V4_REVERSE_MAP = {v: k for k, v in V4_SYMBOLS.items()}
CONV_TABLE = {
(0, 0): [0, 0], (0, 1): [1, 1], (1, 0): [1, 0], (1, 1): [0, 1]
}
crc16 = crcmod.mkCrcFun(0x11021, initCrc=0, xorOut=0xFFFF)
class DipperModeApp:
def __init__(self, root):
print("Initializing DipperModeApp")
self.root = root
try:
self.root.title("DIPPER V4 by M0OLI")
self.root.geometry("1000x750") # Adjusted to a reasonable default width, user can resize
print("Root window created")
except Exception as e:
print(f"Root window error: {e}")
raise
# Initialize settings with no preset baud rate
self.settings = self.load_settings()
self.radio = tk.StringVar(value=self.settings.get("radio", "Icom IC-703"))
self.usb_address = tk.StringVar(value=self.settings.get("usb_address", ""))
self.mode_usb = tk.BooleanVar(value=self.settings.get("mode_usb", False))
self.mode_usb_digital = tk.BooleanVar(value=self.settings.get("mode_usb_digital", False))
self.mode_fm = tk.BooleanVar(value=self.settings.get("mode_fm", False))
self.serial_port = tk.StringVar(value=self.settings.get("serial_port", "NONE"))
self.baud_rate = tk.StringVar(value=self.settings.get("baud_rate", "")) # Empty by default, user-defined
self.rts = tk.BooleanVar(value=self.settings.get("rts", False))
self.dtr = tk.BooleanVar(value=self.settings.get("dtr", False))
self.ptt_port = tk.StringVar(value=self.settings.get("ptt_port", "NONE"))
self.ptt_rts = tk.BooleanVar(value=self.settings.get("ptt_rts", False))
self.ptt_dtr = tk.BooleanVar(value=self.settings.get("ptt_dtr", False))
# Audio settings
self.input_device_index = tk.IntVar(value=self.settings.get("input_device", -1))
self.output_device_index = tk.IntVar(value=self.settings.get("output_device", -1))
self.input_volume = tk.DoubleVar(value=self.settings.get("input_volume", 50.0)) # Default to 50.0
self.sensitivity = tk.DoubleVar(value=self.settings.get("sensitivity", 50.0)) # Default to 50.0 (0–100 scale)
self.input_devices, self.output_devices = self.get_audio_devices()
# Initialize speed, filter, and receive buffer variables
self.speed_var = tk.StringVar(value=self.settings.get("speed", "slow"))
self.filter_var = tk.StringVar(value=self.settings.get("filter", "none"))
self.receive_buffer = [] # Buffer for received characters before displaying
self.temp_receive_buffer = [] # Temporary buffer for accumulating symbols before display
self.light_colors = {"bg": "#FFFFFF", "fg": "#000000", "entry_bg": "#F0F0F0", "button_bg": "#D0D0D0"}
self.current_colors = self.light_colors
self.my_callsign_value = ""
try:
self.my_callsign_value = self.load_callsign()
if not self.my_callsign_value:
self.my_callsign_value = self.prompt_callsign()
self.save_callsign(self.my_callsign_value)
except Exception as e:
print(f"Callsign load error: {e}")
self.p = None
self.stream_out = None
self.stream_in = None
self.running = False
self.rx_thread = None
self.tx_thread = None
self.is_v4_mode = False
self.packet_buffer = {}
self.last_packet_id = -1
self.tx_queue = []
self.rx_ack = None
self.radio_serial = None # For CAT communication
# Initialize rx_output as None, to be set in GUI build
self.rx_output = None
print("Building GUI")
try:
self.main_frame = tk.Frame(root, bg=self.current_colors["bg"])
self.main_frame.pack(fill="both", expand=True, padx=20, pady=20)
# Menu Bar
menubar = Menu(self.root)
self.root.config(menu=menubar)
settings_menu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Settings", menu=settings_menu)
settings_menu.add_command(label="Configure Radio", command=self.show_radio_settings_window)
settings_menu.add_command(label="Audio Settings", command=self.show_audio_settings_window)
self.callsign_frame = tk.Frame(self.main_frame, bg=self.current_colors["bg"])
self.callsign_frame.pack(fill="x", pady=(0, 10))
tk.Label(self.callsign_frame, text=" My Callsign:", fg=self.current_colors["fg"],
bg=self.current_colors["bg"]).grid(row=0, column=0, padx=5, pady=5, sticky="e")
self.my_callsign = tk.Entry(self.callsign_frame, width=15, fg=self.current_colors["fg"],
bg=self.current_colors["entry_bg"], insertbackground=self.current_colors["fg"])
self.my_callsign.grid(row=0, column=1, padx=5, pady=5)
if self.my_callsign_value:
self.my_callsign.insert(0, self.my_callsign_value)
self.my_callsign.config(state="disabled")
tk.Label(self.callsign_frame, text="To Callsign:", fg=self.current_colors["fg"],
bg=self.current_colors["bg"]).grid(row=0, column=2, padx=5, pady=5, sticky="e")
self.to_callsign = tk.Entry(self.callsign_frame, width=15, fg=self.current_colors["fg"],
bg=self.current_colors["entry_bg"], insertbackground=self.current_colors["fg"])
self.to_callsign.grid(row=0, column=3, padx=5, pady=5)
self.dark_mode_var = tk.BooleanVar()
self.dark_mode_check = tk.Checkbutton(self.callsign_frame, text="Dark Mode",
variable=self.dark_mode_var, command=self.toggle_dark_mode,
fg=self.current_colors["fg"], bg=self.current_colors["bg"],
selectcolor=self.current_colors["entry_bg"])
self.dark_mode_check.grid(row=0, column=4, padx=20, pady=5)
self.v4_indicator_frame = tk.Frame(self.callsign_frame, bg=self.current_colors["bg"])
self.v4_indicator_frame.grid(row=0, column=5, padx=10, pady=5, sticky="e")
tk.Label(self.v4_indicator_frame, text="Robust RX/TX:", fg=self.current_colors["fg"],
bg=self.current_colors["bg"]).pack(side="left")
self.v4_indicator = tk.Canvas(self.v4_indicator_frame, width=20, height=20,
bg=self.current_colors["bg"], highlightthickness=0)
self.v4_indicator.create_rectangle(0, 0, 20, 20, fill="grey", tags="mode_light")
self.v4_indicator.pack(side="left")
self.tx_frame = tk.Frame(self.main_frame, bg=self.current_colors["bg"])
self.tx_frame.pack(fill="x", pady=(0, 10))
tk.Label(self.tx_frame, text="Transmit Message:", fg=self.current_colors["fg"],
bg=self.current_colors["bg"]).pack(anchor="w", pady=(0, 5))
self.tx_input = tk.Entry(self.tx_frame, width=60, fg=self.current_colors["fg"],
bg=self.current_colors["entry_bg"], insertbackground=self.current_colors["fg"])
self.tx_input.pack(fill="x", pady=(0, 5))
self.button_frame = tk.Frame(self.tx_frame, bg=self.current_colors["bg"])
self.button_frame.pack(anchor="w", pady=(0, 5))
self.tx_button = tk.Button(self.button_frame, text="Send", command=self.transmit,
fg=self.current_colors["fg"], bg=self.current_colors["button_bg"])
self.tx_button.pack(side="left", padx=5)
self.cq_button = tk.Button(self.button_frame, text="CQ", command=self.send_cq,
fg=self.current_colors["fg"], bg=self.current_colors["button_bg"])
self.cq_button.pack(side="left", padx=5)
self.speed_frame = tk.LabelFrame(self.tx_frame, text="Speed", fg=self.current_colors["fg"],
bg=self.current_colors["bg"])
self.speed_frame.pack(anchor="w", pady=5)
tk.Radiobutton(self.speed_frame, text="Slow (~6-13 WPM)", variable=self.speed_var, value="slow",
fg=self.current_colors["fg"], bg=self.current_colors["bg"],
selectcolor=self.current_colors["entry_bg"]).pack(side="left", padx=5)
tk.Radiobutton(self.speed_frame, text="Medium (~10-20 WPM)", variable=self.speed_var, value="medium",
fg=self.current_colors["fg"], bg=self.current_colors["bg"],
selectcolor=self.current_colors["entry_bg"]).pack(side="left", padx=5)
tk.Radiobutton(self.speed_frame, text="Fast (~15-30 WPM)", variable=self.speed_var, value="fast",
fg=self.current_colors["fg"], bg=self.current_colors["bg"],
selectcolor=self.current_colors["entry_bg"]).pack(side="left", padx=5)
tk.Radiobutton(self.speed_frame, text="Turbo (~40+ WPM)", variable=self.speed_var, value="turbo",
fg=self.current_colors["fg"], bg=self.current_colors["bg"],
selectcolor=self.current_colors["entry_bg"]).pack(side="left", padx=5)
tk.Radiobutton(self.speed_frame, text="Robust (~20-40 WPM)", variable=self.speed_var, value="robust",
fg=self.current_colors["fg"], bg=self.current_colors["bg"],
selectcolor=self.current_colors["entry_bg"]).pack(side="left", padx=5)
self.filter_frame = tk.LabelFrame(self.tx_frame, text="Frequency Filter", fg=self.current_colors["fg"],
bg=self.current_colors["bg"])
self.filter_frame.pack(anchor="w", pady=5)
tk.Radiobutton(self.filter_frame, text="No Filter", variable=self.filter_var, value="none",
fg=self.current_colors["fg"], bg=self.current_colors["bg"],
selectcolor=self.current_colors["entry_bg"]).pack(side="left", padx=5)
tk.Radiobutton(self.filter_frame, text="900-5100 Hz", variable=self.filter_var, value="900-5100",
fg=self.current_colors["fg"], bg=self.current_colors["bg"],
selectcolor=self.current_colors["entry_bg"]).pack(side="left", padx=5)
tk.Radiobutton(self.filter_frame, text="800-5200 Hz", variable=self.filter_var, value="800-5200",
fg=self.current_colors["fg"], bg=self.current_colors["bg"],
selectcolor=self.current_colors["entry_bg"]).pack(side="left", padx=5)
self.rx_frame = tk.Frame(self.main_frame, bg=self.current_colors["bg"])
self.rx_frame.pack(fill="both", expand=True, pady=(10, 0))
self.rx_header_frame = tk.Frame(self.rx_frame, bg=self.current_colors["bg"])
self.rx_header_frame.pack(fill="x")
tk.Label(self.rx_header_frame, text="Received Messages:", fg=self.current_colors["fg"],
bg=self.current_colors["bg"]).pack(side="left", pady=(0, 5))
self.clear_button = tk.Button(self.rx_header_frame, text="Clear", command=self.clear_receive,
fg=self.current_colors["fg"], bg=self.current_colors["button_bg"])
self.clear_button.pack(side="right", padx=5, pady=5)
# Redesign receive window using Text widget with scrollbars and monospaced font
self.rx_output_frame = tk.Frame(self.rx_frame, bg=self.current_colors["bg"])
self.rx_output_frame.pack(fill="both", expand=True, pady=(0, 10))
# Vertical scrollbar
v_scrollbar = tk.Scrollbar(self.rx_output_frame, orient="vertical")
v_scrollbar.pack(side="right", fill="y")
# Horizontal scrollbar
h_scrollbar = tk.Scrollbar(self.rx_output_frame, orient="horizontal")
h_scrollbar.pack(side="bottom", fill="x")
# Text widget for receive output with dynamic width, monospaced font, and no wrapping
self.rx_output = tk.Text(self.rx_output_frame,
font=tkinter.font.Font(family="Courier", size=10), # Use monospaced font
fg=self.current_colors["fg"], bg=self.current_colors["entry_bg"],
insertbackground=self.current_colors["fg"], wrap="none", # Disable default wrapping
yscrollcommand=v_scrollbar.set, xscrollcommand=h_scrollbar.set)
self.rx_output.pack(side="left", fill="both", expand=True)
# Configure scrollbars
v_scrollbar.config(command=self.rx_output.yview)
h_scrollbar.config(command=self.rx_output.xview)
self.rx_output.tag_config("sent", foreground="red")
self.rx_output.tag_config("underline", underline=True)
# Bind window resize to update dynamic width
self.root.bind("<Configure>", self.update_dynamic_width)
print("GUI built successfully")
except Exception as e:
print(f"GUI build error: {e}")
# Ensure rx_output exists even if GUI build fails for error logging
if not hasattr(self, 'rx_output'):
self.rx_output = tk.Text(self.root,
font=tkinter.font.Font(family="Courier", size=10), # Use monospaced font
fg=self.current_colors["fg"], bg=self.current_colors["entry_bg"],
insertbackground=self.current_colors["fg"])
self.rx_output.pack_forget() # Hidden but accessible for logging
if self.rx_output:
self.rx_output.insert(tk.END, f"GUI error: {e}\n")
print("Scheduling audio start")
self.root.after(100, self.start_audio)
def update_dynamic_width(self, event):
"""Update the dynamic width of the rx_output Text widget based on its current size."""
if hasattr(self, 'rx_output'):
# Get the current pixel width of the Text widget
pixel_width = self.rx_output.winfo_width()
if pixel_width > 0: # Ensure valid width
# Use font metrics to calculate characters per line (using Courier 10pt)
font = tkinter.font.Font(family="Courier", size=10)
char_width = font.measure("0") # Width of a single character (using '0' as a reference)
if char_width > 0:
chars_per_line = pixel_width // char_width
print(f"Dynamic width updated - Pixel width: {pixel_width}, Characters per line: {chars_per_line}")
self.dynamic_width = max(1, chars_per_line) # Ensure at least 1 character
else:
print("Warning: Invalid font width detected, using fallback width of 200 characters.")
self.dynamic_width = 200 # Increased fallback to ensure wider default
else:
self.dynamic_width = 200 # Fallback if width is invalid
def clear_receive(self):
"""Clears the received messages text box and resets the receive buffer."""
if self.rx_output:
self.rx_output.delete(1.0, tk.END)
self.receive_buffer = [] # Reset the receive buffer
self.temp_receive_buffer = [] # Reset the temporary buffer
def load_callsign(self):
if os.path.exists(CALLSIGN_FILE):
with open(CALLSIGN_FILE, "r") as f:
return f.read().strip()
return ""
def save_callsign(self, callsign):
with open(CALLSIGN_FILE, "w") as f:
f.write(callsign)
def prompt_callsign(self):
return simpledialog.askstring("Setup", "What is your callsign?", parent=self.root) or ""
def load_settings(self):
"""Load radio and audio settings from file, including sensitivity."""
settings = {}
if os.path.exists(SETTINGS_FILE):
try:
with open(SETTINGS_FILE, "r") as f:
for line in f:
if line.strip():
key, value = line.strip().split("=", 1)
if key == "baud_rate":
settings[key] = value # Store as string for user input
elif key == "input_volume":
settings[key] = float(value)
elif key == "sensitivity":
settings[key] = float(value) if value else 50.0 # Default to 50.0 if not set
elif key in ["rts", "dtr", "mode_usb", "mode_usb_digital", "mode_fm", "ptt_rts", "ptt_dtr"]:
settings[key] = value.lower() == "true"
elif key in ["input_device", "output_device"]:
settings[key] = int(value) if value != "-1" else -1
else:
settings[key] = value
except Exception as e:
print(f"Error loading settings: {e}")
return settings
def save_settings(self):
"""Save radio and audio settings to file, persisting until changed and saved again, including sensitivity."""
settings = {
"radio": self.radio.get(),
"usb_address": self.usb_address.get(),
"mode_usb": self.mode_usb.get(),
"mode_usb_digital": self.mode_usb_digital.get(),
"mode_fm": self.mode_fm.get(),
"serial_port": self.serial_port.get(),
"baud_rate": self.baud_rate.get(), # Store as user input string
"rts": self.rts.get(),
"dtr": self.dtr.get(),
"ptt_port": self.ptt_port.get(),
"ptt_rts": self.ptt_rts.get(),
"ptt_dtr": self.ptt_dtr.get(),
"input_device": self.input_device_index.get(),
"output_device": self.output_device_index.get(),
"input_volume": self.input_volume.get(),
"sensitivity": self.sensitivity.get(), # Save sensitivity (0.0–100.0)
"speed": self.speed_var.get(),
"filter": self.filter_var.get(),
}
try:
with open(SETTINGS_FILE, "w") as f:
for key, value in settings.items():
if key in ["rts", "dtr", "mode_usb", "mode_usb_digital", "mode_fm", "ptt_rts", "ptt_dtr"]:
f.write(f"{key}={value}\n")
else:
f.write(f"{key}={value}\n")
print("Radio settings saved successfully")
if self.rx_output:
self.rx_output.insert(tk.END, "Radio settings saved successfully\n")
except Exception as e:
if self.rx_output:
self.rx_output.insert(tk.END, f"Error saving settings: {str(e)}\n")
print(f"Error saving settings: {e}")
def get_audio_devices(self):
p = pyaudio.PyAudio()
input_devices = {}
output_devices = {}
for i in range(p.get_device_count()):
dev = p.get_device_info_by_index(i)
name = dev["name"]
index = dev["index"]
if dev["maxInputChannels"] > 0:
input_devices[index] = name
if dev["maxOutputChannels"] > 0:
output_devices[index] = name
p.terminate()
return input_devices, output_devices
def get_com_ports(self):
"""Get available COM ports for serial communication."""
return ["NONE"] + [port.device for port in serial.tools.list_ports.comports()]
def get_default_address(self, radio):
"""Return the default address based on the selected radio, empty for Generic."""
default_addresses = {
"Icom IC-703": "68H",
"Icom IC-705": "94H",
"Generic": "" # Empty for user customization
}
return default_addresses.get(radio, "")
def start_audio(self):
print("Starting audio")
try:
self.p = pyaudio.PyAudio()
self.input_devices, self.output_devices = self.get_audio_devices()
input_idx = self.input_device_index.get() if self.input_device_index.get() >= 0 else None
output_idx = self.output_device_index.get() if self.output_device_index.get() >= 0 else None
print(f"Attempting to open audio streams - Input device: {input_idx}, Output device: {output_idx}")
self.stream_out = self.p.open(format=pyaudio.paFloat32, channels=1, rate=SAMPLE_RATE, output=True,
output_device_index=output_idx)
self.stream_in = self.p.open(format=pyaudio.paFloat32, channels=1, rate=SAMPLE_RATE, input=True,
frames_per_buffer=CHUNK, input_device_index=input_idx)
self.running = True
self.rx_thread = threading.Thread(target=self.receive_loop, daemon=True)
self.rx_thread.start()
self.tx_thread = threading.Thread(target=self.transmit_loop, daemon=True)
self.tx_thread.start()
if self.rx_output:
self.rx_output.insert(tk.END, "Audio started successfully\n")
print("Audio threads started")
except pyaudio.PyAudioException as e:
if self.rx_output:
self.rx_output.insert(tk.END, f"Audio error: Another program may have control of the audio. Error: {str(e)}\n")
self.running = False
print(f"Audio startup error: {str(e)}. Please close other audio programs and try again.")
if self.root and self.rx_output:
self.root.after(0, lambda: messagebox.showwarning("Audio Error",
f"Audio setup failed: {str(e)}. Another program may have control of the audio. Close other audio programs and try again."))
if self.p:
self.p.terminate()
self.p = None
except Exception as e:
if self.rx_output:
self.rx_output.insert(tk.END, f"Audio failed to start: {str(e)}\n")
self.running = False
print(f"Audio startup error: {e}")
if self.root and self.rx_output:
self.root.after(0, lambda: messagebox.showwarning("Audio Warning",
f"Audio setup failed: {str(e)}. Continuing without audio."))
if self.p:
self.p.terminate()
self.p = None
def toggle_dark_mode(self):
if self.dark_mode_var.get():
self.current_colors = {"bg": "#000000", "fg": "#00FF00", "entry_bg": "#1A1A1A", "button_bg": "#333333"}
else:
self.current_colors = self.light_colors
self.root.configure(bg=self.current_colors["bg"])
self.main_frame.configure(bg=self.current_colors["bg"])
self.callsign_frame.configure(bg=self.current_colors["bg"])
self.tx_frame.configure(bg=self.current_colors["bg"])
self.rx_frame.configure(bg=self.current_colors["bg"])
self.rx_header_frame.configure(bg=self.current_colors["bg"])
self.speed_frame.configure(bg=self.current_colors["bg"])
self.filter_frame.configure(bg=self.current_colors["bg"])
self.v4_indicator_frame.configure(bg=self.current_colors["bg"])
self.rx_output_frame.configure(bg=self.current_colors["bg"])
for widget in (self.callsign_frame.winfo_children() +
self.tx_frame.winfo_children() +
self.rx_frame.winfo_children() +
self.rx_header_frame.winfo_children() +
self.speed_frame.winfo_children() +
self.filter_frame.winfo_children() +
self.rx_output_frame.winfo_children()):
if isinstance(widget, (tk.Label, tk.Checkbutton, tk.Radiobutton)):
widget.configure(fg=self.current_colors["fg"], bg=self.current_colors["bg"])
elif isinstance(widget, tk.Entry):
widget.configure(fg=self.current_colors["fg"], bg=self.current_colors["entry_bg"],
insertbackground=self.current_colors["fg"])
elif isinstance(widget, tk.Button):
widget.configure(fg=self.current_colors["fg"], bg=self.current_colors["button_bg"])
if self.rx_output:
self.rx_output.configure(fg=self.current_colors["fg"], bg=self.current_colors["entry_bg"],
insertbackground=self.current_colors["fg"])
self.clear_button.configure(fg=self.current_colors["fg"], bg=self.current_colors["button_bg"])
self.dark_mode_check.configure(selectcolor=self.current_colors["entry_bg"])
# Update button colors in dark mode
self.tx_button.config(bg=self.current_colors["button_bg"])
self.cq_button.config(bg=self.current_colors["button_bg"])
def generate_sound(self, pattern, duration=0.05):
audio = np.array([], dtype=np.float32)
if not pattern:
return np.zeros(int(SAMPLE_RATE * duration), dtype=np.float32)
for start_freq, end_freq, sound_type in pattern:
t = np.linspace(0, duration, int(SAMPLE_RATE * duration), False)
if sound_type == "tone":
signal = np.sin(2 * np.pi * start_freq * t)
elif sound_type == "slide":
freqs = np.linspace(start_freq, end_freq, len(t))
signal = np.sin(2 * np.pi * freqs * t)
elif sound_type == "trill":
signal = np.sin(2 * np.pi * start_freq * t) * np.sin(2 * np.pi * 20 * t)
audio = np.concatenate((audio, signal))
return audio / np.max(np.abs(audio))
def convolutional_encode(self, bits):
encoded = []
state = 0
for bit in bits:
encoded.extend(CONV_TABLE[(state, bit)])
state = bit
return encoded
def viterbi_decode(self, bits):
states = {0: (0, []), 1: (float('inf'), [])}
for i in range(0, len(bits), 2):
r1, r2 = bits[i], bits[i+1]
new_states = {}
for s in [0, 1]:
for b in [0, 1]:
next_s = b
o1, o2 = CONV_TABLE[(s, b)]
cost = states[s][0] + (r1 ^ o1) + (r2 ^ o2)
path = states[s][1] + [b]
if next_s not in new_states or cost < new_states[next_s][0]:
new_states[next_s] = (cost, path)
states = new_states
return min(states.values(), key=lambda x: x[0])[1]
def interleave(self, bits):
block_size = 16
padded = bits + [0] * (block_size - len(bits) % block_size) if len(bits) % block_size else bits
return [padded[i // 4 + (i % 4) * 4] for i in range(len(padded))]
def deinterleave(self, bits):
block_size = 16
deinterleaved = [0] * len(bits)
for i in range(len(bits)):
deinterleaved[i // 4 + (i % 4) * 4] = bits[i]
return deinterleaved
def detect_v4_preamble(self, data):
preamble_freqs = [(1500, 2000), (2500, 3000), (3500, 4000), (4500, 5000),
(2000, 1500), (2000, 2500), (3000, 3500)]
segment_size = int(SAMPLE_RATE * 0.05)
freq_axis = np.linspace(0, SAMPLE_RATE // 2, segment_size // 2)
detected = True
for i, (start_freq, end_freq) in enumerate(preamble_freqs):
chunk = data[i * segment_size:(i + 1) * segment_size]
if len(chunk) < segment_size:
return False
freqs = np.abs(fft(chunk)[:segment_size // 2])
peak_idx = np.argmax(freqs)
peak_freq = freq_axis[peak_idx]
if not (start_freq - 200 < peak_freq < end_freq + 200):
detected = False
break
return detected
def encode_v4_packet(self, text, packet_id):
bits = [int(b) for char in text for b in bin(ord(char))[2:].zfill(8)]
encoded_bits = self.convolutional_encode(bits)
interleaved_bits = self.interleave(encoded_bits)
symbols = []
for i in range(0, len(interleaved_bits), 4):
chunk = interleaved_bits[i:i+4]
value = sum(b << (3-j) for j, b in enumerate(chunk[:4]))
symbols.append(V4_SYMBOLS.get(value, " "))
header = [V4_SYMBOLS[packet_id % 16]]
crc = crc16(text.encode('utf-8'))
crc_symbols = [V4_SYMBOLS[(crc >> 12) & 0xF], V4_SYMBOLS[(crc >> 8) & 0xF],
V4_SYMBOLS[(crc >> 4) & 0xF], V4_SYMBOLS[crc & 0xF]]
return header + symbols + crc_symbols
def decode_v4_packet(self, symbols):
if len(symbols) < 5:
return None, None
packet_id = V4_REVERSE_MAP.get(symbols[0], 0)
payload_symbols = symbols[1:-4]
crc_symbols = symbols[-4:]
bits = [int(b) for sym in payload_symbols
for b in bin(V4_REVERSE_MAP.get(sym, 0))[2:].zfill(4)]
deinterleaved_bits = self.deinterleave(bits)
decoded_bits = self.viterbi_decode(deinterleaved_bits)
text = "".join(chr(sum(b << (7-j) for j, b in enumerate(decoded_bits[i:i+8])))
for i in range(0, len(decoded_bits), 8) if len(decoded_bits[i:i+8]) == 8)
crc = crc16(text.encode('utf-8'))
received_crc = sum(V4_REVERSE_MAP.get(s, 0) << (12 - 4*i) for i, s in enumerate(crc_symbols))
return packet_id, text if crc == received_crc else None
def send_ack_nack(self, packet_id, success):
if not self.stream_out:
return
self.set_v4_tx_indicator()
ack_sound = self.generate_sound(CHAR_SOUNDS["K" if success else "N"], 0.05)
audio_data = np.concatenate((ack_sound, np.zeros(int(SAMPLE_RATE * 0.025))))
self.stream_out.write(audio_data.astype(np.float32).tobytes())
self.reset_indicator()
def transmit(self):
if not self.stream_out:
messagebox.showwarning("Warning", "Audio not started!")
return
to_call = self.to_callsign.get().upper()
my_call = self.my_callsign.get().upper()
if not to_call or not my_call:
messagebox.showwarning("Warning", "Enter both callsigns!")
return
text = self.tx_input.get().strip().upper()
full_text = f"{to_call} DE {my_call} {text}" if text else f"{to_call} DE {my_call}"
speed = self.speed_var.get()
if speed == "robust":
packet_id = (self.last_packet_id + 1) % 16
self.last_packet_id = packet_id
self.tx_queue.append((full_text, packet_id))
else:
self.tx_queue.append((full_text, None))
self.tx_input.delete(0, tk.END)
self.start_radio_transmission() # Start TX for this message
# Change button color to red during TX
self.tx_button.config(bg="red")
def transmit_loop(self):
while self.running:
if self.tx_queue and self.stream_out:
# Change button color to red at the start of transmission
self.tx_button.config(bg="red")
self.cq_button.config(bg="red")
full_text, packet_id = self.tx_queue.pop(0)
speed = self.speed_var.get()
duration_map = {"slow": 0.2, "medium": 0.15, "fast": 0.1, "turbo": 0.03, "robust": 0.05}
duration = duration_map[speed]
gap_duration = duration / 2
self.start_radio_transmission() # Activate PTT at the start of transmission
audio_data = np.array([], dtype=np.float32)
if speed == "robust":
self.set_v4_tx_indicator()
preamble_audio = np.concatenate([self.generate_sound(CHAR_SOUNDS[num], 0.05)
for num in "1357924"])
symbols = self.encode_v4_packet(full_text, packet_id)
audio_data = preamble_audio
for sym in symbols:
pattern = CHAR_SOUNDS.get(sym, CHAR_SOUNDS[" "])
sound = self.generate_sound(pattern, 0.05)
audio_data = np.concatenate((audio_data, sound, np.zeros(int(SAMPLE_RATE * gap_duration))))
self.stream_out.write(audio_data.astype(np.float32).tobytes())
if self.rx_output:
self.rx_output.insert(tk.END, "Robust Sent: " + full_text + "\n", "sent")
self.reset_indicator()
self.stop_radio_transmission() # Deactivate PTT immediately after audio
time.sleep(0.2) # Wait for ACK/NACK
if self.rx_ack == "N": # Only re-queue on NACK
self.tx_queue.append((full_text, packet_id))
self.rx_ack = None
else:
encoded_symbols = self.encode_fec(full_text)
for symbol in encoded_symbols:
char = REVERSE_MAP.get(symbol, " ")
pattern = WORD_SOUNDS.get(char, CHAR_SOUNDS.get(char, []))
sound = self.generate_sound(pattern, duration)
audio_data = np.concatenate((audio_data, sound, np.zeros(int(SAMPLE_RATE * gap_duration))))
self.stream_out.write(audio_data.astype(np.float32).tobytes())
if self.rx_output:
self.rx_output.insert(tk.END, "Sent: " + full_text + "\n", "sent")
self.stop_radio_transmission() # Deactivate PTT immediately after audio
# Reset button colors to default after transmission
self.tx_button.config(bg=self.current_colors["button_bg"])
self.cq_button.config(bg=self.current_colors["button_bg"])
time.sleep(0.001) # Minimal sleep to prevent CPU overload, no additional delay
time.sleep(0.001) # Minimal sleep to prevent CPU overload
def send_cq(self):
if not self.stream_out:
messagebox.showwarning("Warning", "Audio not started!")
return
my_call = self.my_callsign.get().upper()
if not my_call:
messagebox.showwarning("Warning", "Enter your callsign!")
return
speed = self.speed_var.get()
single_cq = f"CQ CQ CQ DE {my_call}"
if speed == "robust":
packet_id = (self.last_packet_id + 1) % 16
self.last_packet_id = packet_id
self.tx_queue.append((single_cq, packet_id)) # Single CQ for Robust
else:
cq_text = f"{single_cq} {single_cq}" # Two repeats for old modes
self.tx_queue.append((cq_text, None))
self.start_radio_transmission() # Start TX for CQ
# Change button color to red during TX
self.cq_button.config(bg="red")
def test_ptt_connection(self):
"""Test the radio PTT by activating it for 1 second and then deactivating it immediately."""
if self.serial_port.get() != "NONE" and self.baud_rate.get():
self.start_radio_transmission() # Activate PTT for testing
# Schedule PTT release after exactly 1 second and reset button colors
self.root.after(1000, lambda: [self.stop_radio_transmission(), self.tx_button.config(bg=self.current_colors["button_bg"]), self.cq_button.config(bg=self.current_colors["button_bg"])])
else:
messagebox.showwarning("Radio Warning", "No serial port or baud rate selected for radio communication.")
def start_radio_transmission(self):
"""Activate PTT (TX) for the radio, dynamically adjusting for radio type."""
if self.serial_port.get() != "NONE" and self.baud_rate.get():
try:
radio = self.radio.get()
address = self.usb_address.get().replace('H', '') if self.usb_address.get() else ''
if not address:
raise ValueError("No address specified for radio communication.")
if not self.radio_serial or not self.radio_serial.is_open:
self.radio_serial = serial.Serial(
port=self.serial_port.get(),
baudrate=int(self.baud_rate.get()) if self.baud_rate.get().isdigit() else 9600, # Default to 9600 if invalid
timeout=0.1, # Reduced timeout for faster response
rtscts=self.rts.get(),
dsrdtr=self.dtr.get()
)
print(f"Radio serial connection established on {self.serial_port.get()} at {self.baud_rate.get()} baud")
if self.rx_output:
self.rx_output.insert(tk.END, f"Radio connection established on {self.serial_port.get()}\n")
# Generate CI-V command for PTT ON based on radio type
if radio in ["Icom IC-703", "Icom IC-705"]:
cmd = bytes.fromhex(f"FE FE {address} E0 1C 00 FD")
else: # Generic radio, use custom address for Icom-like CI-V
cmd = bytes.fromhex(f"FE FE {address} E0 1C 00 FD") # Default Icom CI-V format, user must ensure compatibility
self.radio_serial.write(cmd)
print(f"Sent PTT ON command: {cmd.hex()}")
if self.rx_output:
self.rx_output.insert(tk.END, f"Sent PTT ON command: {cmd.hex()}\n")
except (serial.SerialException, ValueError) as e:
print(f"Failed to start radio transmission: {str(e)}")
if self.rx_output:
self.rx_output.insert(tk.END, f"Failed to start radio transmission: {str(e)}\n")
if self.radio_serial:
self.radio_serial.close()
self.radio_serial = None
def stop_radio_transmission(self):
"""Deactivate PTT (TX) for the radio immediately after audio or test, dynamically adjusting for radio type."""
if self.radio_serial and self.radio_serial.is_open:
try:
radio = self.radio.get()
address = self.usb_address.get().replace('H', '') if self.usb_address.get() else ''
if not address:
raise ValueError("No address specified for radio communication.")
# Generate CI-V command for PTT OFF based on radio type
if radio in ["Icom IC-703", "Icom IC-705"]:
cmd = bytes.fromhex(f"FE FE {address} E0 1C 01 FD")
else: # Generic radio, use custom address for Icom-like CI-V
cmd = bytes.fromhex(f"FE FE {address} E0 1C 01 FD") # Default Icom CI-V format, user must ensure compatibility
self.radio_serial.write(cmd)
print(f"Sent PTT OFF command: {cmd.hex()}")
if self.rx_output:
self.rx_output.insert(tk.END, f"Sent PTT OFF command: {cmd.hex()}\n")
except serial.SerialException as e:
print(f"Failed to stop radio transmission: {str(e)}")
if self.rx_output:
self.rx_output.insert(tk.END, f"Failed to stop radio transmission: {str(e)}\n")
finally:
if self.radio_serial:
self.radio_serial.close()
self.radio_serial = None
def encode_fec(self, text):
symbol_list = []
words = text.split()
for word in words:
if word in WORD_SOUNDS:
symbol_list.append(SYMBOL_MAP[word])
else:
for char in word:
symbol_list.append(SYMBOL_MAP.get(char, SYMBOL_MAP[" "]))
speed = self.speed_var.get()
if speed in ["slow", "medium"]:
while len(symbol_list) % 23 != 0:
symbol_list.append(SYMBOL_MAP[" "])
blocks = [symbol_list[i:i+23] for i in range(0, len(symbol_list), 23)]
encoded_blocks = []
for block in blocks:
encoded = RSCodec.encode(block)
encoded_blocks.extend(encoded)
return encoded_blocks
return symbol_list
def decode_fec(self, symbols):
"""Decode FEC-encoded symbols for received data, ensuring full message accumulation."""
speed = self.speed_var.get()
if speed in ["slow", "medium"]:
blocks = [symbols[i:i+31] for i in range(0, len(symbols), 31)]
decoded_text = ""
for block in blocks:
if len(block) == 31:
try:
decoded = RSCodec.decode(block)[0] # Fixed: Corrected RSCodec.decode() call
decoded_text += "".join(REVERSE_MAP.get(sym, " ") for sym in decoded)
except ReedSolomonError:
decoded_text += "[ERROR] "
return decoded_text.strip()
return "".join(REVERSE_MAP.get(sym, " ") for sym in symbols).strip()
def decode_audio(self, data):
"""Decode audio data with adjustable sensitivity based on user settings (0–100 scale)."""
freqs = np.abs(fft(data)[:len(data)//2])
freq_axis = np.linspace(0, SAMPLE_RATE // 2, len(freqs))
peak_idx = np.argmax(freqs)
peak_freq = freq_axis[peak_idx]
# Convert sensitivity (0–100) to a threshold (0.0–1.0) for min_amplitude
sensitivity_value = self.sensitivity.get() / 100.0 # Scale from 0–100 to 0.0–1.0
min_amplitude = 0.01 + (sensitivity_value * 0.99) # Scale from 0.01 to 1.0 (higher sensitivity = lower threshold)
print(f"Decode audio - Sensitivity: {sensitivity_value}, Min Amplitude: {min_amplitude}, Peak Amplitude: {np.max(freqs)}") # Enhanced debug logging
if np.max(freqs) < min_amplitude * np.max(freqs): # Only process if signal exceeds threshold
return None
tolerance = 300 # Fixed tolerance for frequency matching
if self.is_v4_mode:
for sym, value in V4_REVERSE_MAP.items():
pattern = CHAR_SOUNDS[sym]
start_freq = pattern[0][0]
if abs(peak_freq - start_freq) < tolerance:
return sym
else:
for word, pattern in WORD_SOUNDS.items():
start_freq = pattern[0][0]
if abs(peak_freq - start_freq) < tolerance:
return SYMBOL_MAP[word]
for char, pattern in CHAR_SOUNDS.items():
start_freq = pattern[0][0]
if abs(peak_freq - start_freq) < tolerance:
return SYMBOL_MAP[char]
return None
def butter_bandpass(self, lowcut, highcut, fs, order=5):
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = butter(order, [low, high], btype='band')
return b, a
def apply_filter(self, data):
filter_type = self.filter_var.get()
if filter_type == "none":
return data
elif filter_type == "900-5100":
b, a = self.butter_bandpass(900, 5100, SAMPLE_RATE)
return lfilter(b, a, data)
elif filter_type == "800-5200":
b, a = self.butter_bandpass(800, 5200, SAMPLE_RATE)
return lfilter(b, a, data)
return data
def set_v4_rx_indicator(self):
self.v4_indicator.itemconfig("mode_light", fill="green")
if hasattr(self, "indicator_timeout"):
self.root.after_cancel(self.indicator_timeout)
self.indicator_timeout = self.root.after(3000, self.reset_indicator)
def set_v4_tx_indicator(self):
if hasattr(self, "indicator_timeout"):
self.root.after_cancel(self.indicator_timeout)
self.v4_indicator.itemconfig("mode_light", fill="red")
def reset_indicator(self):
self.v4_indicator.itemconfig("mode_light", fill="grey")
def receive_loop(self):
"""Receive loop with buffered text display and adjustable sensitivity, accumulating symbols to fill the dynamic width without space-based line breaks."""
buffer = []
gap_map = {"slow": 0.1, "medium": 0.075, "fast": 0.05, "turbo": 0.015, "robust": 0.025}
duration_map = {"slow": 0.2, "medium": 0.15, "fast": 0.1, "turbo": 0.03, "robust": 0.05}
preamble_detected = False
last_update_time = time.time()
update_interval = 2.0 # Increased to 2.0 seconds for better text accumulation
while self.running:
try:
speed = self.speed_var.get()
duration = duration_map[speed]
chunk_size = int(SAMPLE_RATE * (duration + gap_map[speed] * 2))
data = self.stream_in.read(chunk_size, exception_on_overflow=False)
data = np.frombuffer(data, dtype=np.float32)
gain = self.input_volume.get() / 100.0 # Use input_volume for gain
print(f"Receive loop - Input volume: {gain}, Peak amplitude: {np.max(np.abs(data))}") # Debug volume effect
data = data * gain
data = self.apply_filter(data)
if not preamble_detected and len(buffer) == 0:
if self.detect_v4_preamble(data[:int(SAMPLE_RATE * 0.4)]):
self.is_v4_mode = True
self.set_v4_rx_indicator()
preamble_detected = True
continue
symbol = self.decode_audio(data)
if symbol is not None:
buffer.append(symbol)
if self.is_v4_mode:
if len(buffer) >= 10:
packet_id, decoded_text = self.decode_v4_packet(buffer)
if decoded_text and packet_id not in self.packet_buffer:
self.packet_buffer[packet_id] = decoded_text
self.display_received_text("Robust: " + decoded_text + "\n\n") # Add extra newline for full line of space
self.send_ack_nack(packet_id, True)
self.rx_ack = "K"
elif not decoded_text:
self.send_ack_nack(packet_id, False)
self.rx_ack = "N"
buffer.clear()
preamble_detected = False
self.is_v4_mode = False
else:
block_size = 31 if speed in ["slow", "medium"] else 1
if len(buffer) >= block_size:
decoded_text = self.decode_fec(buffer[:block_size])
self.temp_receive_buffer.extend(decoded_text) # Accumulate in temporary buffer
print(f"Decoded text accumulated: {decoded_text}, Buffer length: {len(''.join(self.temp_receive_buffer))}") # Debug buffering
buffer = buffer[block_size:]
current_time = time.time()
if not hasattr(self, 'dynamic_width'):
self.update_dynamic_width(None) # Ensure dynamic_width is set
buffer_text = "".join(self.temp_receive_buffer)
# Flush only if buffer exceeds dynamic width or timeout occurs
if (len(buffer_text) >= self.dynamic_width or
current_time - last_update_time >= update_interval or not self.running):
if self.temp_receive_buffer:
self.display_received_text(buffer_text + "\n\n") # Add extra newline for full line of space
self.temp_receive_buffer = [] # Clear after displaying
last_update_time = current_time
time.sleep(0.01) # Slow down processing to reduce noise sensitivity
except Exception as e:
if self.running:
if self.rx_output:
self.rx_output.insert(tk.END, f"Receive error: {str(e)}\n")
print(f"Receive loop error: {e}")
break
def display_received_text(self, text):
"""Display received text in the receive window, buffering to fill the dynamic width without space-based line breaks."""
if not self.rx_output:
return
if not hasattr(self, 'dynamic_width'):
self.update_dynamic_width(None) # Initialize if not set
widget_width = self.dynamic_width # Dynamic width in characters
print(f"Display received text - widget_width: {widget_width}, text length: {len(text)}, text: {text[:50]}...") # Debug logging
# Split text into characters and buffer them
for char in text:
self.receive_buffer.append(char)
if len(self.receive_buffer) >= widget_width:
self.rx_output.insert(tk.END, ''.join(self.receive_buffer) + "\n\n") # Add extra newline for full line of space
self.receive_buffer = []
# Flush any remaining buffer at the end of a message, ensuring no partial lines
if self.receive_buffer:
remaining_text = ''.join(self.receive_buffer)
if remaining_text:
self.rx_output.insert(tk.END, remaining_text + "\n\n") # Add extra newline for full line of space