forked from jg00dman/m0rvj_microbit_cw_transceiver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
m0rvj_microbit_cw_transceiver.py
203 lines (192 loc) · 5.96 KB
/
m0rvj_microbit_cw_transceiver.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
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
##########################
# v 1.0 Initial Public Release
# This is a working CW Radio. 'B' is key. 'A' shows message received in text.
# Connect Speaker or Headphones to Pin 0 and Ground
# Or connect Piezo Buzzer or Haptic Motor for Silent operation to pin1 and ground
# pin2 is capacitive touch keying.
# Copyright (C) 2017 Revd. John Goodman M0RVJ,
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>L
#
# This uses some public domain code by Giles Booth.
# See here http://www.suppertime.co.uk/blogmywiki/2016/05/microbit-morse-code-transmitter/
###########################
from microbit import *
import radio
import music
radio.on()
radio.reset()
#Customise these settings for tx, pwr, channel and speed.
radio.config(power=7)
radio.config(channel=98)
WPM = 15
txenabled = True #in case you want to practice without sending
sidetone = 550
dotlength = int( 60000 / ( WPM * 50 ) )
dashlength = dotlength * 3
interelement = dotlength
interletter = dotlength * 2
interword = dotlength * 7
DOT_THRESHOLD = dotlength * 2
DASH_THRESHOLD = dotlength * 5
WORD_THRESHOLD = dotlength * 7
DOT = Image("00000:00000:00900:00000:00000")
DASH = Image("00000:00000:09990:00000:00000")
ANT = Image("90909:09990:00900:00900:00900")
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":"--..",
"0":"-----",
"1":".----",
"2":"..---",
"3":"...--",
"4":"....-",
"5":".....",
"6":"-....",
"7":"--...",
"8":"---..",
"9":"----.",
".":".-.-.",
",":"--..--",
"/":"-..-.",
"?":"..--.."
}
# reverse dict.
decodemorse = {v: k for k, v in morse.items()}
# convert string to cw
def EncodeMorse(message):
m = message.upper()
enc = ""
for c in m:
enc = enc + morse.get(c," ")
if morse.get(c," ") != " ":
enc = enc + " "
return enc
# function to flash and play cw
def FlashMorse(pattern):
for c in pattern:
if c == ".":
display.show(DOT)
pin1.write_digital(1)
music.pitch(sidetone, dotlength)
display.clear()
pin1.write_digital(0)
sleep(interelement)
elif c == "-":
display.show(DASH)
pin1.write_digital(1)
music.pitch(sidetone, dashlength)
display.clear()
pin1.write_digital(0)
sleep(interelement)
elif c == " ":
sleep(interletter)
return
def ReceiveCW():
display.show(ANT)
message = ''
started = running_time()
while True:
waiting = running_time() - started
received = radio.receive()
if received:
FlashMorse(EncodeMorse(received))
message += received
if button_b.is_pressed():
return # breakin to immediate keying
if pin2.is_touched():
return # breakin to immediate keying
if button_a.was_pressed():
display.scroll(message) # nb blocks receive while showing...
message = ''
if waiting > WORD_THRESHOLD * 2:
display.show(ANT)
if len(message) > 15: #keep message buffer short...
message = message[1:16]
if accelerometer.was_gesture("shake"):
display.scroll("CW TRX by M0RVJ")
def Keyer():
buffer = ''
message = ''
started = running_time()
while True:
waited = running_time() - started
key_down_time = None
while button_b.is_pressed(): ## button B keying
if not key_down_time:
key_down_time = running_time()
music.pitch(sidetone, -1, pin0, False)
pin1.write_digital(1)
while True:
if not button_b.is_pressed():
music.stop()
pin1.write_digital(0)
break
while pin2.is_touched(): ## touch keying
if not key_down_time:
key_down_time = running_time()
music.pitch(sidetone, -1, pin0, False)
pin1.write_digital(1)
while True:
if not pin2.is_touched():
music.stop()
pin1.write_digital(0)
break
key_up_time = running_time()
if key_down_time:
duration = key_up_time - key_down_time
if duration < DOT_THRESHOLD:
buffer += '.'
display.show(DOT)
elif duration:
buffer += '-'
display.show(DASH)
started = running_time()
elif len(buffer) > 0 and waited > DASH_THRESHOLD:
display.clear()
character = decodemorse.get(buffer, '?')
buffer = ''
display.show(character)
if txenabled:
radio.send(character)
message += character
if waited > WORD_THRESHOLD * 2:
return
while True:
Keyer()
ReceiveCW()