-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMorse.py
166 lines (151 loc) · 4.05 KB
/
Morse.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
from sense_hat import SenseHat
import os
import time
import pygame
from pygame.locals import *
#one unit
pygame.init()
pygame.display.set_mode((640, 480))
sh = SenseHat()
sh.clear() # Blank the LED matrix
W = [255, 255, 255] # White
B = [0, 0, 0] #Black
R = [255, 0 ,0] #Red
G = [0, 255, 0] #Green
letter = ""
#matrix
dot = [
B, B, B, B, B, B, B, B,
B, B, B, B, B, B, B, B,
B, B, B, B, B, B, B, B,
B, B, B, W, W, B, B, B,
B, B, B, W, W, B, B, B,
B, B, B, B, B, B, B, B,
B, B, B, B, B, B, B, B,
B, B, B, B, B, B, B, B
]
line = [
B, B, B, B, B, B, B, B,
B, B, B, B, B, B, B, B,
B, B, B, B, B, B, B, B,
B, B, W, W, W, W, B, B,
B, B, W, W, W, W, B, B,
B, B, B, B, B, B, B, B,
B, B, B, B, B, B, B, B,
B, B, B, B, B, B, B, B
]
def computeLetter(result):
if result == ".-":
sh.show_letter("A", W, B)
elif result == "-...":
sh.show_letter("B", W, B)
elif result == "-.-.":
sh.show_letter("C", W, B)
elif result == "-..":
sh.show_letter("D", W, B)
elif result == ".":
sh.show_letter("E", W, B)
elif result == "..-.":
sh.show_letter("F", W, B)
elif result == "--.":
sh.show_letter("G", W, B)
elif result == "....":
sh.show_letter("H", W, B)
elif result == "..":
sh.show_letter("I", W, B)
elif result == ".---":
sh.show_letter("J", W, B)
elif result == "-.-":
sh.show_letter("K", W, B)
elif result == ".-..":
sh.show_letter("L", W, B)
elif result == "--":
sh.show_letter("M", W, B)
elif result == "-.":
sh.show_letter("N", W, B)
elif result == "---":
sh.show_letter("O", W, B)
elif result == ".--.":
sh.show_letter("P", W, B)
elif result == "--.-":
sh.show_letter("Q", W, B)
elif result == ".-.":
sh.show_letter("R", W, B)
elif result == "...":
sh.show_letter("S", W, B)
elif result == "-":
sh.show_letter("T", W, B)
elif result == "..-":
sh.show_letter("U", W, B)
elif result == "...-":
sh.show_letter("V", W, B)
elif result == ".--":
sh.show_letter("W", W, B)
elif result == "-..-":
sh.show_letter("X", W, B)
elif result == "-.--":
sh.show_letter("Y", W, B)
elif result == "--..":
sh.show_letter("Z", W, B)
elif result == ".----":
sh.show_letter("1", W, B)
elif result == "..---":
sh.show_letter("2", W, B)
elif result == "...--":
sh.show_letter("3", W, B)
elif result == "....-":
sh.show_letter("4", W, B)
elif result == ".....":
sh.show_letter("5", W, B)
elif result == "-....":
sh.show_letter("6", W, B)
elif result == "--...":
sh.show_letter("7", W, B)
elif result == "---..":
sh.show_letter("8", W, B)
elif result == "----.":
sh.show_letter("9", W, B)
elif result == "-----":
sh.show_letter("0", W, B)
else:
sh.show_letter("", R, R)
time.sleep(0.5)
sh.clear()
def released(event, time):
if time < 0.15:
sh.set_pixels(dot)
return "."
else:
sh.set_pixels(line)
return "-"
def eventHandler(event):
if event.type == KEYDOWN:
if event.key == pygame.K_RETURN:
return "pressed"
elif event.type == KEYUP:
if event.key == pygame.K_RETURN:
return "released"
last = -1;
afterLetter = -1
pressed = False
while True:
if last != -1:
if time.time() - last > 0.6 and pressed == False:
computeLetter(letter)
letter = ""
last = -1
afterLetter = time.time()
if afterLetter != -1:
if time.time() - afterLetter > 1:
sh.clear()
afterLetter = -1
for event in pygame.event.get():
inp = eventHandler(event)
if inp == "pressed":
last = time.time()
afterLetter = -1
pressed = True
if inp == "released":
letter = letter + released(event, time.time() - last)
pressed = False
last = time.time()