-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.py
More file actions
180 lines (157 loc) · 5.16 KB
/
function.py
File metadata and controls
180 lines (157 loc) · 5.16 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
import simpleaudio as sa # importing the audio playing library. Can be removed in vibrators
import os
import time
# Dictionary of alphanumeric and their respective morse code
code = {'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': '--..',
'1': '.----',
'2': '..---',
'3': '...--',
'4': '....-',
'5': '.....',
'6': '-....',
'7': '--...',
'8': '---..',
'9': '----.',
'0': '-----', }
# list of alphanumeric for check
alpha_list = ['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', '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', ' ']
def morse_to_alphanumeric(morse):
check = True
# check weather all character of the input is morse character ie. '.' and '-'
for i in morse:
if i != '.' and i != '-' and i != ' ':
check = False
if check: # if the check is passes then the conversion is executed
# this function return the respective character of the part of morse
def morse_to_char(morse):
global code
for i in code:
if code[i] == morse:
return i
k = 0
message = ''
# in this while loop the construction of the message takes place, converting each morse part to character
# and concatenate to the message string
while True:
space = False
code = ''
# in this for loop the morse parts are separated from the input string
for i in range(len(morse)):
if k > len(morse) - 1:
break
if morse[k] == ' ':
if morse[k + 1] == ' ' and k <= len(morse):
k += 1
space = True
k += 1
break
code += morse[k]
k += 1
# here the morse part is converted to character and added to the message string
message += morse_to_char(code)
if k >= len(morse) - 1:
break
if space:
message += ' '
return message
# return following statement if check fails
return 'Enter Valid Morse Code'
def alpha_to_morse(message):
check = True
# check weather all character of the input is alphanumeric
for i in message:
x = False
for j in alpha_list:
if i == j:
x = True
if not x:
check = False
break
if check:
morse = ''
index = 0
for i in message: # take each character of the message and do the conversion process
for j in code: # get the morse value of the character from the code dictionary
if i == j:
morse += code[j] # add the morse part to the morse string
break
morse += ' ' if index < len(message) - 1 else ''
index += 1
return morse
# return following statement if check fails
return 'Only Alphabets and Numbers are accepted'
# importing the sound files. Only for sound output
dat_sound = sa.WaveObject.from_wave_file(os.path.abspath("morse_code/dat.wav"))
dit_sound = sa.WaveObject.from_wave_file(os.path.abspath("morse_code/dit.wav"))
space_sound = sa.WaveObject.from_wave_file(os.path.abspath("morse_code/space.wav"))
# code needs to be tweeked to match vibrator
'''
DOT = 200000
DASH = 600000
GAP = 200000
SPACE = 1400000
'''
'''def morse_play(morse):
#T = 200000ms
for i in morse:
if i == '_':
t_dash = time.time_ns() + DASH
while time.time_ns() < t_dash:
return True
t_gap = time.time_ns() + GAP
while time.time_ns() < t_gap:
return False
if i == '.':
t_dot = time.time_ns() + DOT
while time.time_ns() < t_dot:
return True
t_gap = time.time_ns() + GAP
while time.time_ns() < t_gap:
return False
if i == ' ':
t_space = time.time_ns() + SPACE
while time.time_ns() < t_dot:
return True
t_gap = time.time_ns() + GAP
while time.time_ns() < t_gap:
return False
# play the sound respect to the morse
def morse_play(morse):
for i in morse:
if i == '-':
dat_play = dat_sound.play()
dat_play.wait_done()
if i == '.':
dit_play = dit_sound.play()
dit_play.wait_done()
if i == ' ':
space_play = space_sound.play()
space_play.wait_done()
'''