-
Notifications
You must be signed in to change notification settings - Fork 0
/
morse.py
101 lines (63 loc) · 2.02 KB
/
morse.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
# morse_code.py
# 2015-06-17
# Public Domain
# ./morse_code.py [ message [ micros [ gpio ] ] ]
import sys
import time
import pigpio
morse={
'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':'-----'}
NONE=0
DASH=3
DOT=1
GAP=1
LETTER_GAP=3-GAP
WORD_GAP=7-LETTER_GAP
micros = 100000
def transmit_string(pi, gpio, message):
print(message)
pi.wave_clear() # clear all waveforms
wf=[]
for C in message:
c=C.lower()
if c in morse:
k = morse[c]
for x in k:
if x == '.':
wf.append(pigpio.pulse(1<<gpio, NONE, DOT * micros))
else:
wf.append(pigpio.pulse(1<<gpio, NONE, DASH * micros))
wf.append(pigpio.pulse(NONE, 1<<gpio, GAP * micros))
wf.append(pigpio.pulse(NONE, 1<<gpio, LETTER_GAP * micros))
elif c == ' ':
wf.append(pigpio.pulse(NONE, 1<<gpio, WORD_GAP * micros))
pi.wave_add_generic(wf)
wid = pi.wave_create()
if wid >= 0:
pi.wave_send_once(wid)
if __name__ == '__main__':
text = """Now is the winter of our discontent
made glorious summer by this sun of York"""
if len(sys.argv) > 1:
text = sys.argv[1]
if len(sys.argv) > 2:
micros = int(sys.argv[2])
GPIO = 19
if len(sys.argv) > 3:
GPIO = int(sys.argv[3])
pi = pigpio.pi() # Connect to Pi.
if not pi.connected:
pi = pigpio.pi("192.168.3.74") # Connect to Pi.
pi.set_mode(GPIO, pigpio.OUTPUT)
transmit_string(pi, GPIO, text)
while pi.wave_tx_busy():
time.sleep(0.1)
print "Morse text transmitted"
pi.stop() # disconnect from Pi