-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmorseCode.py
More file actions
124 lines (112 loc) · 2.44 KB
/
morseCode.py
File metadata and controls
124 lines (112 loc) · 2.44 KB
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
import RPi.GPIO as GPIO
import time
import os
import sys
from morseDict import *
GPIO.setmode(GPIO.BCM)
GPIO.setup(25,GPIO.OUT)
iSpeed = .05
GPIO.setup(18,GPIO.OUT)
GPIO.output(18,GPIO.OUT)
def sendMessage(message):
messageKey()
for c in message:
if c==' ':
time.sleep(iSpeed*3)
else:
morseMessage=morsetab[c]
for m in morseMessage:
if m=='.':
lightOn(iSpeed)
else:
lightOn(iSpeed*3)
time.sleep(iSpeed)
time.sleep(iSpeed*2)
endMessage()
def messageKey():
time.sleep(iSpeed)
lightOn(iSpeed)
time.sleep(iSpeed)
lightOn(iSpeed)
def endMessage():
lightOn(iSpeed*6)
def lightReading(totalTime):
key1 = False
key2 = False
key3 = False
startRead = False
currentMark = 0
currentGap = 0
elasped = 0
message=[]
word=''
for i in range(0,int(totalTime/iSpeed)):
now=time.time()
lightValue = readLight()
if (key1 == True and key2 == True and key3 == True):
startRead = True
elif (lightValue < 50 and key1 == False):
key1 = True
elif (key1 == True and lightValue > 50 and key2 == False):
key2 = True
elif (key2 == True and lightValue < 50 and key3 == False):
key3 = True
else:
key1 = False
key2 = False
key3 = False
if startRead == True:
if lightValue < 50:
currentMark = currentMark + 1
currentGap = 0
else:
if currentMark >= 2:
word+='-'
elif currentMark == 1:
word+='.'
currentGap = currentGap + 1
currentMark = 0
if currentGap > 1 and len(word)>0:
message.append(word)
word=''
if currentGap > 5:
message.append(' ')
if currentMark > 4:
key1 = False
key2 = False
key3 = False
startRead = False
print morseProcess(message)
currentMark = 0
currentGap = 0
word=''
message=[]
elasped=time.time()-now
time.sleep(iSpeed-elasped)
def morseProcess(morseList):
totalMessage = ''
for morse in morseList:
if morse == ' ':
totalMessage += ' '
else:
letter = [ key for key,val in morsetab.items() if val==morse ]
totalMessage += letter[0]
return totalMessage
def lightOff(timeInterval=-1):
GPIO.output(25,True)
if (timeInterval!=-1):
time.sleep(timeInterval)
GPIO.output(25,False)
def lightOn(timeInterval=-1):
GPIO.output(25,False)
if (timeInterval!=-1):
time.sleep(timeInterval)
GPIO.output(25,True)
def readLight():
reading = 0
GPIO.setup(18,GPIO.IN)
while (GPIO.input(18) == GPIO.LOW):
reading += 1
GPIO.setup(18,GPIO.OUT)
GPIO.output(18,GPIO.LOW)
return reading