-
Notifications
You must be signed in to change notification settings - Fork 0
/
morsetx.py
174 lines (148 loc) · 4.24 KB
/
morsetx.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
import time
import smbus2
import queue
from threading import Lock
from p20_defs import *
imc = {
'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': '-----',
'.': '.-.-.-',
',': '--..--',
'?': '..--..',
'\'': '.----.',
'!': '-.-.--',
'/': '-..-.',
'(': '-.--.',
')': '-.--.-',
'&': '.-...',
':': '---...',
';': '-.-.-.',
'=': '-...-',
'+': '.-.-.',
'-': '-....-',
'_': '..--.-',
'"': '.-..-.',
'$': '...-..-',
'@': '.--.-.',
'§': '_...-.-',
'#': '...-.-',
}
cw_thread = None
cw_thread_lock = Lock()
class Morser:
def __init__(self, logger, verbose=True, gpio_bus=1, speed=None, p20=None):
self.logger = logger
self.unit_time = None
self.set_speed(speed)
self.verbose = verbose
self.bus = smbus2.SMBus(gpio_bus)
self.p20 = p20
self.txq = queue.Queue()
def set_speed(self, speed):
if speed is None:
speed = 100
self.unit_time = 6 / speed
self.logger.info("CW speed set to %d", speed)
def transmit_sentence(self, sentence):
for (index, word) in enumerate(sentence.split()):
if index > 0:
self.wait_between_words()
self.transmit_word(word)
def transmit_word(self, word):
for (index, letter) in enumerate(word):
if index > 0:
self.wait_between_letters()
self.transmit_letter(letter)
def transmit_letter(self, letter):
code = imc.get(letter.upper(), '')
if code != '':
if self.verbose:
self.logger.debug('\nProcessing letter "{}" and code "{}"'.format(letter.upper(), code))
for (index, signal) in enumerate(code):
if index > 0:
self.wait_between_signals()
if signal == '.':
self.transmit_dot()
else:
self.transmit_dash()
else:
if self.verbose:
self.logger.error('\nInvalid input: {}'.format(letter))
def transmit_dot(self):
self.p20.bit_write(P20_CW_KEY, "LOW")
time.sleep(self.unit_time)
def transmit_dash(self):
self.p20.bit_write(P20_CW_KEY, "LOW")
time.sleep(self.unit_time * 3)
def wait_between_signals(self):
self.p20.bit_write(P20_CW_KEY, "HIGH")
time.sleep(self.unit_time)
def wait_between_letters(self):
self.p20.bit_write(P20_CW_KEY, "HIGH")
time.sleep(self.unit_time * 3)
def wait_between_words(self):
self.p20.bit_write(P20_CW_KEY, "HIGH")
time.sleep(self.unit_time * 7)
def send_message(self, message, repeat=1):
count = repeat
try:
while count:
if message != '':
if self.verbose:
self.logger.info('\nBegin Transmission')
self.transmit_sentence(message)
self.p20.bit_write(P20_CW_KEY, "HIGH")
count -= 1
if count == 0:
break
self.wait_between_words()
if self.verbose:
self.logger.info('\nEnd Transmission')
finally:
self.p20.bit_write(P20_CW_KEY, "HIGH")
def background_thread(self):
"""Example of how to send server generated CW."""
while True:
if not self.txq.empty():
item = self.txq.get_nowait()
self.logger.info("Transmitting CW: %s" % item)
self.send_message(item)
else:
time.sleep(3*self.unit_time)