-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOLDrouter.py
226 lines (198 loc) · 5.72 KB
/
OLDrouter.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
import RPi.GPIO as GPIO, time, os
import time
import sys
#Variables used by photoresistor for receiving Morse Code
DEBUG = 1
GPIO.setmode(GPIO.BCM)
value = 0
oncounter = 0
offcounter = 0
currentLetter = []
phrase = ""
message = []
begin = 0
#Variables used by LED for flashing Morse Code
#Set Morse Code's time unit
timeunit = 1;
#Dictionary of Morse Code
MorseCodeLetters = {' ': 'X',
'=': '...-.-',
'0': '-----',
'1': '.----',
'2': '..---',
'3': '...--',
'4': '....-',
'5': '.....',
'6': '-....',
'7': '--...',
'8': '---..',
'9': '----.',
'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': '--..'}
#Set up the Raspberry Pi's connection to the board
LED_PIN = 25
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)
def RCtime(RCpin):
reading = 0
GPIO.setup(RCpin, GPIO.OUT)
GPIO.output(RCpin, GPIO.LOW)
time.sleep(0.1)
GPIO.setup(RCpin, GPIO.IN)
while(GPIO.input(RCpin) == GPIO.LOW):
reading +=1
return reading
#Function that flashes the LED in convention with Morse Code's dot
def dot():
print("dot");
GPIO.output(LED_PIN, True)
time.sleep(1*timeunit)
GPIO.output(LED_PIN, False)
time.sleep(1*timeunit)
#Function that flashes the LED in convention with Morse Code's dash
def dash():
print("dash");
GPIO.output(LED_PIN, True)
time.sleep(3*timeunit)
GPIO.output(LED_PIN, False)
time.sleep(1*timeunit)
def repeat():
input = phrase
print(phrase);
#iterate through ever letter of the input
for letter in input:
print(letter)
#look up each letter in the MorseCode dictionary
for symbol in MorseCodeLetters[letter]:
if symbol == '-':
dash()
elif symbol == '.':
dot()
#if the symbol is an X (which corresponds to a space between words), wait for 7 units
elif symbol == 'X':
#sleep for 4 addtional time units (since it will also sleep 3 at the end of the letter)
#Total sleep (7 units) for new word
time.sleep(4*timeunit)
else:
print("Error with MorseCode dictionary!")
#sleep at the end of a letter
time.sleep(3*timeunit)
return 0
#Dictionary of Morse Code
MorseCode = {'-----' : '0',
'.----' : '1',
'..---' : '2',
'...--' : '3',
'....-' : '4',
'.....' : '5',
'-....' : '6',
'--...' : '7',
'---..': '8',
'----.' : '9',
'.-' : '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',
'...-.-': 'SK'}
while True:
value = RCtime(18)
#print(value)
#If value returned from RCtime is greater than 200, LED is OFF
if value > 200:
#If oncounter is greater than zero, the LED was JUST turned off
if oncounter > 0:
if 7 <= oncounter <= 11:
print("Dot");
currentLetter.append('.')
if 21 <= oncounter <= 33:
print("Dash");
currentLetter.append('-')
oncounter = 0
offcounter = offcounter + 1
#If there is a long pause, then the message has ended
if offcounter > 100 and begin != 0:
key = ''.join(currentLetter)
if MorseCode[key] == 'SK' :
if phrase[0] == '1':
phrase = phrase + "SK"
print("message received!")
print(phrase)
exit()
else:
phrase = phrase + "="
print("repeating the message...")
repeat()
exit()
#If value returned from RCtime is less than 200, LED is ON
else:
#Begin keeps track of how many times this has been called
begin = begin + 1
#If oncounter is greater than zero, the LED was JUST turned ON
if offcounter > 0:
print(offcounter);
if 1 <= offcounter <= 10:
#The space between parts of the same letter (1 unit)
pass
if 11 <= offcounter <= 19 and begin != 1:
#The space between letter (3 units)
key = ''.join(currentLetter)
#Print the letter that corresponds to the Morse Code signal
print(MorseCode[key])
phrase = phrase + MorseCode[key]
currentLetter[:] = []
if 20 <= offcounter <= 55 and begin != 1:
#The space between words (7 units)
key = ''.join(currentLetter)
print(MorseCode[key])
phrase = phrase + MorseCode[key]
print(" ")
phrase = phrase + " "
currentLetter[:] = []
offcounter = 0
oncounter = oncounter + 1