-
Notifications
You must be signed in to change notification settings - Fork 28
/
MorseCodeCreator.py
140 lines (125 loc) · 3.57 KB
/
MorseCodeCreator.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
from machine import Pin, PWM
from time import sleep
# Create a dictionary of Morse Code. s is for Short (or dots), l is for Long (or dashes)
MorseCodes = {
' ': '',
'a': 'sl',
'b': 'lsss',
'c': 'lsls',
'd': 'lss',
'e': 's',
'f': 'ssls',
'g': 'lls',
'h': 'ssss',
'i': 'ss',
'j': 'slll',
'k': 'lsl',
'l': 'slss',
'm': 'll',
'n': 'ls',
'o': 'lll',
'p': 'slls',
'q': 'llsl',
'r': 'sls',
's': 'sss',
't': 'l',
'u': 'ssl',
'v': 'sssl',
'w': 'sll',
'x': 'lssl',
'y': 'lsll',
'z': 'llss',
'1': 'sllll',
'2': 'sslll',
'3': 'sssll',
'4': 'ssssl',
'5': 'sssss',
'6': 'lssss',
'7': 'llsss',
'8': 'lllss',
'9': 'lllls',
'0': 'lllll'}
button = Pin(10, Pin.IN, Pin.PULL_UP)
shortled = Pin(11, Pin.OUT)
longled = Pin(12, Pin.OUT)
speaker = PWM(Pin(13))
fast = 0.1
slow = 0.2
sound = True
light = True
pitch = 600
volume = 1500
speaker.freq(600) #pitch of sound. Higher number is higher pitch
shortled.low()
longled.low()
def letterlookup(stringvalue):
for k in MorseCodes:
if MorseCodes[k] == stringvalue:
return k
return " "
def blinkletter(letter):
if letter != "":
currentletter = MorseCodes[letter]
if letter == " ":
sleep(0.6)
return
print(letter + " : " + currentletter)
for c in currentletter:
if (c == 'l'):
blinkspeed = slow
if (c =='s'):
blinkspeed = fast
if light : shortled.high()
if sound :
speaker.freq(pitch)
speaker.duty_u16(volume)
sleep(blinkspeed)
if light : shortled.low()
if sound : speaker.duty_u16(0)
sleep(blinkspeed)
sleep(0.6)
def playmessage(message):
for c in message:
blinkletter(str.lower(c))
def recordmessage():
print("start typing your Morse Code message! Wait 5 seconds to exit")
TimeCount = 0
DelayCount = 0
CurrentLetter = ""
CurrentWord = ""
PreviousStatus = 1
while True:
if button.value() == 0: #Button being pressed
DelayCount = 0
TimeCount += 1
if TimeCount <= 15: #Its a short press!
shortled.high()
longled.low()
elif TimeCount > 15: #Its a Long Press!
shortled.low()
longled.high()
if PreviousStatus != button.value():
speaker.duty_u16(1500)
elif button.value() == 1: #Button not being pressed
speaker.duty_u16(0)
shortled.low()
longled.low()
if TimeCount > 0:
if TimeCount <= 15:
CurrentLetter = CurrentLetter + "s"
elif TimeCount > 15:
CurrentLetter = CurrentLetter + "l"
TimeCount = 0
DelayCount = DelayCount + 1
if DelayCount > 60:
if CurrentLetter != "":
CurrentWord = CurrentWord + letterlookup(CurrentLetter)
CurrentLetter = ""
print(CurrentWord)
if DelayCount == 300: #Add a space
CurrentWord = CurrentWord + " "
if DelayCount == 500:
print("You recorded " + CurrentWord)
print("Exiting recording mode")
return
sleep(0.01)