forked from woodenphone/lego_dimensions_protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorse.py
115 lines (100 loc) · 2.46 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
#-------------------------------------------------------------------------------
# Name: morse
# Purpose: Demo of the gateway library
#
# Author: User
#
# Created: 21/11/2015
# Copyright: (c) User 2015
# Licence: <your licence>
#-------------------------------------------------------------------------------
import time
import lego_dimensions_gateway
import re
TIME_UNIT = 0.2# Base time unit in seconds
DASH = TIME_UNIT*3
DOT = TIME_UNIT*1
SPACE = TIME_UNIT*3
# codes transcribed from picture on http://thelivingpearl.com/2013/01/08/morse-code-and-dictionaries-in-python-with-sound/
MORSE_CODE_TABLE = {
# Letters
"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":"--..",
# Digits
"0":"-----",
"1":".----",
"2":"..---",
"3":"...--",
"4":"....-",
"5":".....",
"6":"-....",
"7":"--...",
"8":"---..",
"9":"----.",
}
def send_character(gateway, character):
character = character.lower()
print(character)
if character == " ":
time.sleep(SPACE)
else:
code = MORSE_CODE_TABLE[character]
for symbol in code:
# Flash for appropriate time
if symbol == ".":
gateway.switch_pad(
pad = 0,
colour = (255, 0, 0)# RGB
)
time.sleep(DOT)
elif symbol == "-":
gateway.switch_pad(
pad = 0,
colour = (0, 0, 255)# RGB
)
time.sleep(DASH)
# Turn pad back off
gateway.switch_pad(
pad = 0,
colour = (0, 0, 0)# RGB
)
time.sleep(TIME_UNIT)
return
def send_text(gateway, text):
clean_text = re.sub("[^a-z0-9 ]","", text.lower())
for character in clean_text:
send_character(gateway, character)
return
def demo():
gateway = lego_dimensions_gateway.Gateway(verbose=True)
while True:
text = "Lego Dimensions gateway morse code demonstration "
send_text(gateway, text)
def main():
demo()
if __name__ == '__main__':
main()