-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpymorse.py
More file actions
206 lines (171 loc) · 5.18 KB
/
pymorse.py
File metadata and controls
206 lines (171 loc) · 5.18 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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
What I need :
A function to get the user text and remove all what's not needed
* Consider spaces
* Consider é à è etc...
* Consider punctuation
* Consider Maj
1 for -
0 for .
A function to translate the text to morse code
A function to translate morse code to text
A function to output the morse code to GPIO
A function to capture morse code from GPIO
"""
#import RPi.GPIO as GPIO
import re, time, os
# This file contains the morse dictionnairy. Please feel free to add more translations to it ! :-)
from morse_dict import *
# The FM lib
import PiFm
def choose_mode(mode="1"):
user_text = raw_input("Please choose the mode you want to use.\n1 - Broadcast\n2- Receive. >> ")
if mode == 1:
get_user_text()
elif mode == 2:
pass
def get_user_text():
user_text = raw_input("Please enter the message you would like to broadcast. >> ")
user_text = user_text.lower()
word_list = list(user_text)
return word_list
def long_pulse(pin):
PiFm.play_sound("audio/long.wav")
#GPIO.output(pin,GPIO.HIGH)
print("-")
time.sleep(1)
#GPIO.output(pin,GPIO.LOW)
def short_pulse(pin):
PiFm.play_sound("audio/short.wav")
#GPIO.output(pin,GPIO.HIGH)
print(".")
time.sleep(0.5)
#GPIO.output(pin,GPIO.LOW)
def short_gap():
print("Short gap")
time.sleep(1.5)
def long_gap():
print("Long gap")
time.sleep(3.5)
def text_to_morse_code(alpha_text):
morse_code = []
for letter in alpha_text:
# For each letter, translate it to a sequence, then add a short gap
if letter == " ":
morse_code.append(morse_dict[letter])
else:
morse_code.append(morse_dict[letter])
morse_code.append("2")
morse_code = ''.join(map(str, morse_code))
morse_code = list(morse_code)
return morse_code
def broadcast_code(code_to_broadcast, pin):
# Set the board as BOARD
#GPIO.setmode(GPIO.BOARD)
print("Set the board to BOARD")
# Setup the n th pin to OUTPUT
#GPIO.setup(pin, GPIO.OUT)
print("Set the "+str(pin)+"th to OUTPUT")
# Starting the broadcast
print("\n===================\nStarting Broadcast\n===================\n")
start_broadcast = [0,1,0,1]
for x in start_broadcast:
if x == 1:
long_pulse(pin)
if x == 0:
short_pulse(pin)
print("\n===================\nBroadcasting\n===================\n")
for number in code_to_broadcast:
if number == '1':
long_pulse(pin)
elif number == '0':
short_pulse(pin)
# Between letters
elif number == '2':
short_gap()
# Between words
elif number == '3':
long_gap()
#Boardcast end of transmission.
print("\n===================\nEnding Broadcast\n===================\n")
end_broadcast = [0,0,0,1,0,1]
for y in end_broadcast:
if y == 1:
long_pulse(pin)
if y == 0:
short_pulse(pin)
#GPIO.cleanup()
print("Cleaned up the board.")
def get_code_broadcast():
#
#GPIO.output(pin,True)
print("Hello")
if __name__ == '__main__':
code = get_user_text()
code = text_to_morse_code(code)
broadcast_code(code,7)
"""
# Todo
def morse_code_to_text(morse_code):
morse_code = []
for letter in morse_code:
if letter == "01":
morse_code.append("a")
if letter == "1000":
morse_code.append("b")
if letter == "1010":
morse_code.append("c")
if letter == "100":
morse_code.append("d")
if letter == "e" or letter == "è" or letter == "é" or letter == "ê":
morse_code.append("0")
if letter == "f":
morse_code.append("0010")
if letter == "g":
morse_code.append("110")
if letter == "h":
morse_code.append("0000")
if letter == "i" or letter == "î" or letter == "ï":
morse_code.append("00")
if letter == "j":
morse_code.append("0111")
if letter == "k":
morse_code.append("101")
if letter == "l":
morse_code.append("0100")
if letter == "m":
morse_code.append("11")
if letter == "n":
morse_code.append("10")
if letter == "o":
morse_code.append("111")
if letter == "p":
morse_code.append("0110")
if letter == "q":
morse_code.append("1101")
if letter == "r":
morse_code.append("010")
if letter == "s":
morse_code.append("111")
if letter == "t":
morse_code.append("0")
if letter == "u":
morse_code.append("001")
if letter == "v":
morse_code.append("0001")
if letter == "w":
morse_code.append("011")
if letter == "x":
morse_code.append("1001")
if letter == "y":
morse_code.append("1011")
if letter == "z":
morse_code.append("1100")
if letter == ".":
morse_code.append("010101")
else:
pass
print(morse_code)
print("Hello")"""