Update simulator.py
Browse files- simulator.py +102 -2
simulator.py
CHANGED
@@ -1,4 +1,101 @@
|
|
|
|
|
|
|
|
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import tkinter as tk
|
3 |
from tkinter import ttk
|
4 |
import time
|
@@ -140,7 +237,8 @@ class MorseDecoderApp:
|
|
140 |
|
141 |
|
142 |
def display_led(self, morse_code):
|
143 |
-
|
|
|
144 |
self.decode_button.config(state=tk.DISABLED) # Disable button during processing
|
145 |
self.morse_input.config(state=tk.DISABLED) # Disable input field during processing
|
146 |
|
@@ -198,4 +296,6 @@ class MorseDecoderApp:
|
|
198 |
if __name__ == "__main__":
|
199 |
root = tk.Tk()
|
200 |
app = MorseDecoderApp(root)
|
201 |
-
root.mainloop()
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import time
|
3 |
+
import threading
|
4 |
|
5 |
+
# Morse code dictionary
|
6 |
+
MORSE_CODE_DICT = {
|
7 |
+
'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.',
|
8 |
+
'G': '--.', 'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..',
|
9 |
+
'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.',
|
10 |
+
'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-',
|
11 |
+
'Y': '-.--', 'Z': '--..', '1': '.----', '2': '..---', '3': '...--',
|
12 |
+
'4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..',
|
13 |
+
'9': '----.', '0': '-----', ', ': '--..--', '.': '.-.-.-', '?': '..--..',
|
14 |
+
'/': '-..-.', '-': '-....-', '(': '-.--.', ')': '-.--.-'
|
15 |
+
}
|
16 |
+
|
17 |
+
dot_duration = 0.2 # seconds
|
18 |
+
dash_duration = dot_duration * 3
|
19 |
+
space_duration = dot_duration * 7
|
20 |
+
letter_space_duration = dot_duration * 3
|
21 |
+
|
22 |
+
led_html = """
|
23 |
+
<div style="width: 50px; height: 50px; border-radius: 50%; background-color: {color};"></div>
|
24 |
+
"""
|
25 |
+
|
26 |
+
def morse_to_text(morse_code):
|
27 |
+
morse_code = morse_code.replace("/", " / ") # Add space for word separator
|
28 |
+
words = morse_code.split(" / ")
|
29 |
+
text = ""
|
30 |
+
for word in words:
|
31 |
+
letters = word.split() #Splits into individual Morse codes
|
32 |
+
for letter in letters:
|
33 |
+
try:
|
34 |
+
found = False
|
35 |
+
for key, value in MORSE_CODE_DICT.items():
|
36 |
+
if value == letter:
|
37 |
+
text += key
|
38 |
+
found = True
|
39 |
+
break
|
40 |
+
if not found:
|
41 |
+
raise ValueError(f"Invalid Morse code: {letter}")
|
42 |
+
except ValueError as e:
|
43 |
+
raise e
|
44 |
+
text += " "
|
45 |
+
return text.strip()
|
46 |
+
|
47 |
+
def display_led(morse_code, callback):
|
48 |
+
"""Simulates the LED display and updates the Gradio LED component."""
|
49 |
+
try:
|
50 |
+
for symbol in morse_code:
|
51 |
+
if symbol == '.':
|
52 |
+
callback(led_html.format(color="yellow"))
|
53 |
+
time.sleep(dot_duration)
|
54 |
+
callback(led_html.format(color="gray"))
|
55 |
+
time.sleep(dot_duration)
|
56 |
+
elif symbol == '-':
|
57 |
+
callback(led_html.format(color="yellow"))
|
58 |
+
time.sleep(dash_duration)
|
59 |
+
callback(led_html.format(color="gray"))
|
60 |
+
time.sleep(dot_duration)
|
61 |
+
elif symbol == ' ':
|
62 |
+
time.sleep(letter_space_duration)
|
63 |
+
elif symbol == '/':
|
64 |
+
time.sleep(space_duration)
|
65 |
+
except Exception as e:
|
66 |
+
return f"Error during LED display: {e}" # Return error message
|
67 |
+
return led_html.format(color="gray") # Ensure led is turned off after the process
|
68 |
+
|
69 |
+
def decode_morse(morse_code):
|
70 |
+
try:
|
71 |
+
decoded_text = morse_to_text(morse_code.upper())
|
72 |
+
except ValueError as e:
|
73 |
+
return str(e), led_html.format(color="gray") # Return error message
|
74 |
+
|
75 |
+
# Use a GradioState to keep track of the LED display
|
76 |
+
def update_led(led_status):
|
77 |
+
return led_status
|
78 |
+
|
79 |
+
# Call the display_led function in a separate thread to keep the UI responsive
|
80 |
+
threading.Thread(target=display_led, args=(morse_code, update_led)).start()
|
81 |
+
|
82 |
+
return decoded_text, led_html.format(color="gray")
|
83 |
+
|
84 |
+
# Gradio Interface
|
85 |
+
with gr.Blocks() as iface:
|
86 |
+
morse_input = gr.Textbox(label="Morse Code Input", placeholder="Enter Morse Code Here")
|
87 |
+
decoded_output = gr.Textbox(label="Decoded Text")
|
88 |
+
led_display = gr.HTML(led_html.format(color="gray"), label="LED Output") # Initialize with gray
|
89 |
+
|
90 |
+
morse_input.change(fn=decode_morse,
|
91 |
+
inputs=morse_input,
|
92 |
+
outputs=[decoded_output, led_display])
|
93 |
+
# To run it locally
|
94 |
+
iface.launch()
|
95 |
+
|
96 |
+
# To run it on Hugging Face Spaces
|
97 |
+
|
98 |
+
"""
|
99 |
import tkinter as tk
|
100 |
from tkinter import ttk
|
101 |
import time
|
|
|
237 |
|
238 |
|
239 |
def display_led(self, morse_code):
|
240 |
+
#Displays the Morse code using the LED simulation. Runs in a separate thread to avoid freezing the GUI.
|
241 |
+
|
242 |
self.decode_button.config(state=tk.DISABLED) # Disable button during processing
|
243 |
self.morse_input.config(state=tk.DISABLED) # Disable input field during processing
|
244 |
|
|
|
296 |
if __name__ == "__main__":
|
297 |
root = tk.Tk()
|
298 |
app = MorseDecoderApp(root)
|
299 |
+
root.mainloop()
|
300 |
+
|
301 |
+
"""
|