-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlaintext_boradcast_morse_code.py
363 lines (313 loc) · 10.7 KB
/
Plaintext_boradcast_morse_code.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
from microbit import *
import radio
import random
# Constants
CONFIRM_MESSAGE = "confirm"
PAIR_DELIMITER = ","
# Global variables
pairing_started = False
paired = False
confirm_wait = False
confirmed = False
new_group = 0
# Radio configuration
radio.on()
# Pairing class
class PairingClass:
def __init__(self):
pass # Constructor
def random_int(self, _min, _max, sys_time):
random.seed(sys_time)
return random.randint(_min, _max)
def data_string(self, new_group, delimiter):
return "%s%s" % (new_group, delimiter)
# Convert the char array into a string
def convertCharToString(str):
# initialization of string to ""
ini = ""
# Add each char into the string
for ch in str:
ini += ch
# return the converted string
return ini
def text_to_morse(text):
# Define a dictionary mapping characters to Morse code
morse_code_dict = {
"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": "--..",
"0": "-----",
"1": ".----",
"2": "..---",
"3": "...--",
"4": "....-",
"5": ".....",
"6": "-....",
"7": "--...",
"8": "---..",
"9": "----.",
" ": " ", # Space
",": "--..--", # Mapping for comma
".": ".-.-.-", # Mapping for period
"?": "..--..", # Mapping for question mark
"/": "-..-.", # Mapping for slash
"(": "-.--.", # Mapping for opening parenthesis
")": "-.--.-", # Mapping for closing parenthesis
"!": "-.-.--",
}
# Convert text to uppercase to ensure all characters are handled consistently
text = text.upper()
# Convert each character in the text to Morse code and join them together
morse_code = " ".join([morse_code_dict.get(char, "") for char in text])
return morse_code
def morse_to_text(morse_code):
# Define a dictionary mapping Morse code to characters
morse_code_dict = {
".-": "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",
"-----": "0",
".----": "1",
"..---": "2",
"...--": "3",
"....-": "4",
".....": "5",
"-....": "6",
"--...": "7",
"---..": "8",
"----.": "9",
"": " ", # Space (added to handle delimiter)
"--..--": ",",
".-.-.-": ".",
"..--..": "?",
"-..-.": "/",
"-.--.": "(",
"-.--.-": ")",
"-.-.--": "!",
}
# Split Morse code into individual characters (letters) based on spaces
morse_letters = morse_code.split(" ")
# Convert each Morse code letter to its corresponding English character
english_text = "".join(
[morse_code_dict.get(letter, "") for letter in morse_letters]
)
return english_text
# Pairing instance
pairing = PairingClass()
# Handle all potential data incoming from the radio
def on_data_received():
global pairing_started, paired, confirm_wait, confirmed, new_group
# Now the micro-bit receives data of a pairing request
received_data = radio.receive()
# If data is not empty
if received_data is not None:
# If the receiver micro-bit has already paired with The sender:
# Then the receiver micro-bit will scroll the received data on the screen
if paired:
display.scroll(received_data)
# HANDLE EMPTY DATA
if received_data == "":
pass # Do nothing
# HANDLE CONFIRMATION MESSAGE
# (Explanation)
elif confirm_wait and not paired:
if received_data == CONFIRM_MESSAGE:
paired = True
confirm_wait = False
# HANDLE POTENTIAL PAIRING DATA
# (Explanation)
elif not confirm_wait and not paired:
# Get the running time of the board.
started_waiting = running_time()
while True:
# Now the micro-bit is PAIRED! ➕
display.show("+")
# Wait for button input
# Press Button A + Button B together to confirm the pairing request
if button_a.is_pressed() and button_b.is_pressed():
display.clear()
data_list = received_data.split(PAIR_DELIMITER)
new_group = int(data_list[0])
confirmed = True
pairing_started = False
display.scroll(int(data_list[0]))
break
# Don't pair if the user doesn't confirm within 10 seconds
elif running_time() - started_waiting > 10000:
display.show("X")
sleep(700)
display.clear()
pairing_started = False
break
# Main function
def main():
global pairing_started, paired, confirm_wait, confirmed, new_group
# Set flags
paired = False
pairing_started = False
confirm_wait = False
confirmed = False
# Handle pairing procedure
while True:
on_data_received()
# Wait for button input to initiate pairing
if button_b.is_pressed():
pairing_started = True
print("B pressed")
# Handle user confirm
on_data_received()
sleep(100)
if confirmed:
radio.send(CONFIRM_MESSAGE)
paired = True
pairing_started = False
# Handle start pairing
elif pairing_started:
display.scroll("PAIRING")
new_group = pairing.random_int(0, 255, running_time())
sleep(100)
random_nums = pairing.data_string(new_group, PAIR_DELIMITER)
radio.send(random_nums)
sleep(100)
started_waiting = running_time()
confirm_wait = True
while True:
on_data_received()
sleep(50)
if paired:
display.scroll(new_group)
confirm_wait = False
pairing_started = False
break
elif running_time() - started_waiting > 10000:
display.scroll("X")
pairing_started = False
confirm_wait = False
break
# Handle complete pairing
if paired:
radio.config(group=new_group)
paired = True
pairing_started = False
confirm_wait = False
confirmed = False
break
# Clears buffer
radio.send("")
# Create an empty array for storing characters of the message
message = []
# Main loop
while True:
on_data_received()
message_sent = False
if button_a.is_pressed() and paired:
# display.scroll(messages[current_message_index], wait=False)
display.scroll("START..")
sleep(500) # Adjust as needed
while not message_sent:
# Start Typing Msaage In Morse Code
# Press button A is '.'
if button_a.is_pressed():
display.scroll("A")
message.append(".")
print(message)
sleep(500) # Adjust as needed
# Press button B to add '-'
if button_b.is_pressed():
display.scroll("B")
message.append("-")
print(message)
sleep(500) # Adjust as needed
# Touch the logo to add the space
if pin_logo.is_touched():
display.scroll("L")
message.append(" ")
print(message)
sleep(500) # Adjust as needed
# There are two ways to see the message students type
# 1. Press button A & B at the same time
# 2. Touch pin 0
# Then the micro:bit will show the message
if (button_a.is_pressed() and button_b.is_pressed()) or accelerometer.was_gesture("shake"):
display.scroll("ALL")
display.show(Image.HEART)
print("Show the message")
display.show(message)
print(convertCharToString(message))
sleep(500) # Adjust as needed
# Move the micro:bit towards the gound slowly send the English messages
if accelerometer.was_gesture('face down'):
selected_message = convertCharToString(message)
decrypted_message = morse_to_text(selected_message)
# Print the message
print("Morse Code: ", selected_message)
print("Decrypted Text: ", decrypted_message)
print("Sending English Message....")
# Send the meesgae
radio.send(decrypted_message)
message_sent = True
display.clear()
sleep(500) # Adjust as needed
# Move the micro:bit to the right slowly send the English messages
if accelerometer.was_gesture('right'):
selected_message = convertCharToString(message)
decrypted_message = morse_to_text(selected_message)
# Print the message
print("Morse Code: ", selected_message)
print("Decrypted Text: ", decrypted_message)
print("Sending Encrypted Message....")
# Send the meesgae
radio.send(selected_message)
message_sent = True
display.clear()
sleep(500) # Adjust as needed
sleep(100)
# Call the main function if the script is running directly
if __name__ == "__main__":
main()