-
Notifications
You must be signed in to change notification settings - Fork 0
/
scouts_morse_code_alex.py
107 lines (89 loc) · 3.15 KB
/
scouts_morse_code_alex.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
from sense_hat import SenseHat
from time import sleep
from random import randint
sense = SenseHat()
import random
#LIGHT_COLOUR = (255, 0, 0)
#PIXEL_ARRAY = [LIGHT_COLOUR] * 64
r = random.randint(0,255)
g = random.randint(0,255)
b = random.randint(0,255)
global LIGHT_COLOUR
LIGHT_COLOUR = [r, g, b]
global RANDOM_COLOUR
RANDOM_COLOUR = [LIGHT_COLOUR] * 64
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':'--..',
'1':'.----', '2':'..---', '3':'...--',
'4':'....-', '5':'.....', '6':'-....',
'7':'--...', '8':'---..', '9':'----.',
'0':'-----', ', ':'--..--', '.':'.-.-.-',
'?':'..--..', '/':'-..-.', '-':'-....-',
'(':'-.--.', ')':'-.--.-', ' ':'*'}
TIMING_DICT = {'.': 0.2, '-': 1.0, ' ': 2.0, '*': 0.0}
# Function to encrypt the string
# according to the morse code chart
def encrypt(message):
message = message.upper()
cipher = ''
for letter in message:
if letter != ' ':
# Looks up the dictionary and adds the
# corresponding morse code
# along with a space to separate
# morse codes for different characters
cipher += MORSE_CODE_DICT[letter] + ' '
else:
# 1 space indicates different characters
# and 2 indicates different words
cipher += '*'
#print(cipher)
return cipher
def make_code_list(message):
#encrypt the message
codedmessage = encrypt(message)
# split message into letters
list = codedmessage.split(" ")
#return our list of letters
return list
# takes in list of encoded letters, outputs flashes of light
def morse_code(message):
import random
# get list
list = make_code_list(message)
# iterate through letter list
for letter in list:
# iterate through each beep
print("start of letter")
for beep in letter:
print (beep)
# display the lights
global LIGHT_COLOUR
sense.set_pixels([LIGHT_COLOUR] * 64)
# wait for time based on beep
sleep(TIMING_DICT[beep])
# turn off lights
sense.clear()
sleep(0.2)
#end of letter
if beep == '*':
r = random.randint(0,255)
g = random.randint(0,255)
b = random.randint(0,255)
LIGHT_COLOUR
LIGHT_COLOUR = [r, g, b]
global RANDOM_COLOUR
RANDOM_COLOUR = [LIGHT_COLOUR] * 64
sleep(3)
print("new word/colour change")
#sense.set_pixels(PIXEL_ARRAY)
#sleep(5)
#sense.clear()
morse_code("Alex is a creeper")