-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcw_keyer.py
More file actions
executable file
·711 lines (593 loc) · 23.5 KB
/
cw_keyer.py
File metadata and controls
executable file
·711 lines (593 loc) · 23.5 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
############################################################################################
#
# cw_keyer.py - Rev 1.0
# Copyright (C) 2021-5 by Joseph B. Attili, joe DOT aa2il AT gmail DOT com
#
# Routines for translating keyboard strokes into CW chars. The rig can be keyed
# via the DTR line or, more reliably, via a nanoIO ardunino-based interface.
#
############################################################################################
#
# 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.
#
############################################################################################
import time
import socket
import threading
import sys
import serial
############################################################################################
TEST_MODE = True
TEST_MODE = False
MIN_WPM=5
MAX_WPM=50
############################################################################################
# Table of Morse code elements
#morse=["" for i in range(128)]
morse=128*[""]
morse[32]=" ";
# Numbers
morse[48]="-----"; # 0
morse[49]=".----";
morse[50]="..---";
morse[51]="...--";
morse[52]="....-";
morse[53]=".....";
morse[54]="-....";
morse[55]="--...";
morse[56]="---..";
morse[57]="----."; # 9
# Punc
morse[33]="-.-.--"; # !
morse[34]=""; # " 0b01010010
morse[35]=""; # # 0b00000001
morse[36]="...-..-"; # $ 0b11001000
morse[39]=".----."; # ' 0b01011110
morse[40]="-.--."; # ( 0b00101101
morse[41]="-.--.-"; # ) 0b01101101
morse[44]="--..--"; # ,
morse[46]=".-.-.-"; # .
morse[47]="-..-."; # /
morse[58]="---..."; # :
morse[59]="-.-.-."; # :
morse[63]="..--.."; # ?
morse[64]=".--.-."; # @
morse[92]=".-..-."; # \
# Letters
morse[65]=".-"; # A
morse[66]="-...";
morse[67]="-.-.";
morse[68]="-..";
morse[69]=".";
morse[70]="..-."; # F
morse[71]="--.";
morse[72]="....";
morse[73]="..";
morse[74]=".---";
morse[75]="-.-"; # K
morse[76]=".-..";
morse[77]="--"; # M
morse[78]="-.";
morse[79]="---";
morse[80]=".--.";
morse[81]="--.-";
morse[82]=".-.";
morse[83]="...";
morse[84]="-";
morse[85]="..-";
morse[86]="...-";
morse[87]=".--";
morse[88]="-..-";
morse[89]="-.--"; # Y
morse[90]="--..";
# Prosigns - This matches what is the nano IO uses
morse[37]="...-.-"; # % = <SK> 0b01101000
morse[38]=".-..."; # & = <AS> 0b00011010
morse[42]=""; # * 0b00000001
morse[43]="-.--."; # + = <KN> 0b00101101
morse[45]="-....-"; # - = <BT> 0b00110001
morse[60]=".-..."; # < = <AS> 0b00100010
morse[61]="-...-"; # = = <BT> 0b00110001
morse[62]=".-.-."; # > = <AR> 0b00101010
############################################################################################
# The key object - this is where all the hard work is really done
class Keyer():
def __init__(self,P,DEFAULT_WPM=25,KEY_DOWN=False):
self.P = P
if DEFAULT_WPM<5:
self.DEFAULT_WPM=25
else:
self.DEFAULT_WPM=DEFAULT_WPM
self.KEY_DOWN = KEY_DOWN
# Set defaults
self.WPM = self.DEFAULT_WPM
self.dotlen = 1.2/self.WPM
self.time=time.time();
self.enable = True
self.stop = False
self.Udp=False
self.Cmd=[]
# We need locks to coordinate time-critical sections of the keying & practice threads
self.evt = threading.Event()
self.evt.clear()
self.evt2 = threading.Event()
self.evt2.clear()
# Compute weight (in dots) of each char
self.weight=128*[0]
for i in range(128):
for el in morse[i]:
if el in [' ','-']:
self.weight[i]+=3
else:
self.weight[i]+=1
self.weight[i]+=1
def busy(self):
status = self.P.keyer_device.get_status()
return (status[0] & 4) > 0
def abort(self):
P=self.P
print('CW_KEYER - ABORT! \t evt cleared')
self.evt.clear()
self.evt2.clear()
self.stop=True
if P.DIGI:
P.sock_xml.put_tx_buff( '',HALT=True )
time.sleep(.1)
P.sock_xml.ptt(0)
elif P.USE_KEYER:
#self.P.keyer_device.nano_write('\\')
P.keyer_device.abort()
return
# Routine to send a message in cw by toggling DTR line
# 1. The length of a dot is 1 time unit.
# 2. A dash is 3 time units.
# 3. The space between symbols (dots and dashes) of the same letter is 1 time unit.
# 4. The space between letters is 3 time units.
# 5. The space between words is 7 time units.
def send_cw(self,msg):
P = self.P
print('CW_KEYER->SEND CW: msg=',msg,
'\tUSE_KEYER=',P.USE_KEYER,
'\tVIA DTR=',P.SEND_VIA_DTR,
'VIA CMD=',P.SEND_VIA_CMD)
if P.DIGI:
return
# Init
if P.SEND_VIA_DTR:
ser = P.direct
else:
ser = P.ser
WPM = self.WPM
dotlen = self.dotlen
if P.SEND_VIA_CMD:
# When using hamlib command, send the char & let the rig do the rest
P.sock.send_morse(msg,VERBOSITY=1)
return
elif P.SEND_VIA_DTR:
pass
elif P.USE_KEYER:
# When using nano IO interface, send the char & let the nano do the rest
wght=0
for char in msg.upper():
wght+=self.weight[ord(char)]
dt=dotlen*wght
#print('\tsend_cw: msg=',msg,'\t@ wpm=',self.WPM,'\twght=',wght,'\tdt=',dt)
P.keyer_device.nano_write(msg)
#print('\t\tsent dt=',dt)
time.sleep(dt)
#print('\t\t\tdone dt=',dt,flush=True)
return
# If in practice mode, use pc audio instead
elif P.PRACTICE_MODE:
print('CW_KEYER->SEND_CW: msg=',msg,len(msg))
self.sidetone.send_cw(msg,self.WPM,0,True)
return
elif TEST_MODE:
# Disable for testing purposes
print("send_cw:",msg) # ,WPM,dotlen
return
# If we get here, we're toggling the DTR and have to do all the heavy lifiting.
# Loop over all chars in message to form symbols and timing
for char in msg.upper():
#print('\tchar=',char)
i=ord(char)
cw=morse[i]
dt=time.time() - self.time;
print('SEND_CW: sending ',char,'\tdotlen=',dotlen)
# Loop over all elements in this char
startup = 0.02 # Additional time needed to overcome OS limitations?
if i>=32: # or dt<3*dotlen ):
for el in cw:
if( el==' ' ):
# After each char, 3 short spaces have already been added (see code below).
# Hence, we only need 4 more short spaces to get letter spacing correct (7 short).
# This seems too long so we cheat and only 3 short spaces (6 short)
#print('Key Up')
ser.setDTR(False)
time.sleep(3*dotlen + startup)
elif( el=='.' ):
# Enable DTR for dot period
#print('Key Down')
ser.setDTR(self.enable)
time.sleep(dotlen + startup)
elif( el=='-' ):
# Enable DTR for 3 dot periods
#print('Key Down')
ser.setDTR(self.enable)
time.sleep(3*dotlen + startup)
# After each element, we insert a short space to effect element spacing
#print('Key Up')
ser.setDTR(False)
time.sleep(dotlen - startup)
# Effect spacing between letters - we've already added one short space
# so we only need 2 more to effect char spacing
ser.setDTR(False)
time.sleep(2*dotlen)
self.time=time.time();
# Set speed
def set_wpm(self,wpm,farnsworth=None,buffered=False):
P = self.P
ser = P.ser
if wpm>0:
print("CW_KEYER->SET_WPM: Setting speed to ",wpm)
self.WPM = wpm
self.dotlen=1.2/self.WPM
if P.USE_KEYER:
print("SET_WPM: Setting NANO speed to ",wpm)
if P.LOCK_SPEED:
P.keyer_device.set_wpm(wpm,idev=3,farnsworth=farnsworth,buffered=buffered)
if P.gui:
if hasattr(P.gui,'PaddlingWin'):
P.gui.PaddlingWin.WPM_TXT.set(str(wpm))
else:
P.PaddlingWin.WPM_TXT.set(str(wpm))
else:
P.keyer_device.set_wpm(wpm,farnsworth=farnsworth,buffered=buffered)
if P.gui:
# Can't fiddle with gui here since it would cause a memoery leak
# Tell watchdog to do it instead
P.WPM2=wpm
def txt2morse(self,msg):
print('\nTXT2MORSE: msg=',msg,len(msg))
tdown=0
times=[]
dotlen_ms = int(1000*self.dotlen)
for char in msg.upper():
i=ord(char)
if i>32 and i<128:
cw=morse[i]
for el in cw:
times.append(tdown)
if( el=='.' ):
tdown+=2*dotlen_ms # Element plus space between elements
elif( el=='-' ):
tdown+=4*dotlen_ms
tdown+=2*dotlen_ms # Space between letters = 3 dits
else:
tdown+=4*dotlen_ms # Space between letters = 7 dits
#print('IDEAL TIMING:',times,'\n')
# Get speed
def get_wpm(self):
return self.WPM
# Enable & disable TX keying
def enable(self):
self.enable = True
def disable(self):
self.enable = False
# This was the guts of cw_server - need to make more efficent but works for now
# Former UDP Commands for client/sever model:
# [RESET] Reset to defaults
# [EXIT] Exit server
# [WPM##] Set WPM to ##
# [+#] Increase WPM by #
# [-#] Decrease WPM by #
# [TUNE##] Key TX for ## sec.
# []
def send_msg(self,msg):
P = self.P
ser = P.ser
print('CW_KEYER->SEND_MSG: ',msg,' at ',self.WPM,' wpm - evt=',
self.evt.isSet(),'\tVIA DTR=',P.SEND_VIA_DTR,
'\tUSE KEYER=',P.USE_KEYER)
# Select manner we're going to key the rig
HAS_CMD = ('[' in msg) or ('[' in msg)
if not HAS_CMD:
# Send the entire message at once - there are no cmds buried in the msg
if P.SEND_VIA_CMD:
# Send via a hamlib command - most rigs dont support this!
print('\tSEND_MSG: Via hamlib command - msg=',msg,'\t@ wpm=',self.WPM)
P.sock.send_morse(msg,VERBOSITY=1)
return
elif P.SEND_VIA_DTR:
# Send by toggling DTR - need to do more work
pass
elif P.USE_KEYER:
# Send via external keyer & let it do all the hard work - most common pathway
print('\tSEND_MSG: Via keyer - msg=',msg,'\t@ wpm=',self.WPM,'\tevt set')
P.keyer_device.nano_write(msg)
self.evt.set() # Signal to other processes that we've sent the message
return
# If we get here, the mesage has a keyer command and/or we are keying the rig by toggling DTR
# Either way, we have to do the heavy lifting ...
self.stop = False
txt2=''
for ch in msg:
#print('\tch=',ch,flush=True)
self.stop = self.stop or P.Stopper.isSet()
if self.stop:
self.stop = False
print('SEND_MSG: Aborting msg ...')
break
# Check for any UDP commands - they are encapsolated by []
if ch=='[':
# Start of a Udp command
self.Udp=True
self.Cmd=[]
elif ch==']':
# End of a Udp command
self.Udp=False
cmd2=''.join(self.Cmd)
cmd2=cmd2.upper()
print("cmd2=",cmd2) # ,'\t',cmd2[:4])
# Execute the command
if cmd2[0]=='~':
# Direct command for the nano io
cmd2=''.join(self.Cmd)
print('Nano Cmd:',cmd2)
self.send_cw(''.join(self.Cmd))
# Allow the response to be echoed in process_chars
P.NANO_ECHO=True
elif cmd2=="RESET":
# Reset defaults & reopen comm port
print("Reseting WPM to ",self.DEFAULT_WPM," wpm ...")
self.set_wpm(self.DEFAULT_WPM)
print("Reseting serial port ...")
P.ser = serial.Serial(ser.PORT,ser.BAUD,timeout=0.1)
P.ser.setDTR(False)
#elif cmd2=="EXIT":
# Exit server
#print "Exiting server ..."
#sys.exit(0)
elif cmd2[:3]=="WPM":
# Set speed
self.set_wpm( int(cmd2[3:]) )
elif cmd2[:3]=="QSY":
# Change freq
df = float( cmd2[3:] )
frq = .001*P.sock.freq + df
print('SEND_MSG - QSY - DF=',df,P.sock.freq,frq)
P.sock.freq = P.sock.set_freq(frq)
elif cmd2[:3]=="LOG":
# log the qso
print('SEND_MSG: Logging - evt2 set ...',flush=True)
self.evt2.set()
P.gui.log_qso()
print('SEND_MSG: ... Done Logging',flush=True)
elif cmd2=="SERIAL":
# Substitute SERIAL NUMBER OUT
MY_CNTR=cut_numbers(P.MY_CNTR)
self.send_cw(MY_CNTR)
txt2+=MY_CNTR
elif cmd2=="MYNAME":
# Substitute my name
self.send_cw(MY_NAME)
txt2+=MY_NAME
elif cmd2=="MYSKCC":
# Substitute my skcc no.
self.send_cw(MY_SKCC)
txt2+=MY_SKCC
elif cmd2=="MYQTH":
# Substitute my name
self.send_cw(MY_QTH)
txt2+=MY_QTH
elif cmd2=="MYSEC":
# Substitute my name
self.send_cw(MY_SEC)
txt2+=MY_SEC
elif cmd2=="MYCALL":
# Substitute my call
self.send_cw(MY_CALL)
txt2+=MY_CALL
elif cmd2=="MYSTATE":
# Substitute my state
self.send_cw(MY_STATE)
txt2+=MY_STATE
elif cmd2=="MYCOUNTY":
# Substitute my county
self.send_cw(MY_COUNTY)
txt2+=MY_COUNTY
elif cmd2=="MYGRID":
# Substitute my state
self.send_cw(MY_GRID[0:4])
txt2+=MY_GRID[0:4]
elif cmd2=="MYPREC":
# Substitute my precicence
self.send_cw(MY_PREC)
txt2+=MY_PREC
elif cmd2=="MYCHECK":
# Substitute my check
self.send_cw(MY_CHECK)
txt2+=MY_CHECK
elif cmd2=="MYCQZ":
# Substitute my check
self.send_cw(MYCQZ)
txt2+=MYCQZ
elif cmd2=="CALL":
# Substitute his call
#print '@@@@@@@@@@@@@@@@@@@@@ CALL @@@@@@@@@@@@@@@@@@@@'
txt=P.gui.get_call()
#print txt
self.send_cw(txt)
txt2+=txt
elif cmd2=="NAME":
# Substitute his name
#print '@@@@@@@@@@@@@@@@@@@@@ NAME @@@@@@@@@@@@@@@@@@@@'
txt=P.gui.get_name()
#print txt
self.send_cw(txt)
txt2+=txt
elif cmd2=="RST":
# Substitute outgoing rst
#print '@@@@@@@@@@@@@@@@@@@@@ RST @@@@@@@@@@@@@@@@@@@@'
txt=P.gui.get_rst()
#print txt
self.send_cw(txt)
txt2+=txt
elif cmd2[:4]=="TUNE":
# Key TX
# print "Len=",len(cmd)
if len(cmd2)>4:
SEC=int(cmd2[4:])
print("Keying TX for ",SEC)
#pwr=read_tx_pwr(self)
P.sock.set_power(5)
if P.USE_KEYER:
P.keyer_device.tune(True)
time.sleep(SEC)
P.keyer_device.tune(False)
else:
ser.setDTR(True)
time.sleep(SEC)
ser.setDTR(False)
P.sock.set_power(99)
elif self.KEY_DOWN:
if P.USE_KEYER:
P.keyer_device.tune(False)
else:
ser.setDTR(False)
self.KEY_DOWN=False
print('TUNE - key up')
P.sock.set_power(99)
else:
#pwr=read_tx_pwr(self)
P.sock.set_power(5)
if P.USE_KEYER:
P.keyer_device.tune(True)
else:
ser.setDTR(True)
self.KEY_DOWN=True
print('TUNE - key down')
elif cmd2[0]=="+":
# Increase speed
dWPM = int(cmd2[1:])
self.set_wpm( self.WPM + dWPM ,buffered=True)
print("Increaing speed by",dWPM," to",self.WPM)
elif cmd2[0]=="-":
# Decrease speed
dWPM = int(cmd2[1:])
self.set_wpm( self.WPM - dWPM ,buffered=True)
print("Decreaing speed by",dWPM," to",self.WPM)
else:
print("\n*** SEND_MSG: WARNING - Unknown command -",cmd2,'\n')
#sys.exit(1)
else:
if self.Udp:
# Collecting a command
self.Cmd.append(ch)
else:
# Nothing special - key tranmitter
print("\tSending ",ch)
self.send_cw(""+ch)
txt2+=ch
# Signal to other processes that we've went the message
print('SEND_MSG: Setting evt...',flush=True)
self.evt.set()
if P.DIGI and len(txt2.strip())>0:
print('SEND_MSG: sending txt2=',txt2,'to fldigi ... len=',len(txt2.strip()))
P.sock_xml.put_tx_buff( chr(10)+txt2+chr(10)+"^r" ) # Pad with LineFeeds and go back into rx mode after we send it
P.sock_xml.ptt(1) # Start TX
return txt2
############################################################################################
# CW UDP server - runs in another thread
def cw_server(P,HOST='',PORT=7388):
print('Starting cw server - ',HOST,PORT,'\n')
keyer=P.keyer
# Open udp socket
try :
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(1) # Need a timeout so we can interrupt/terminate it
print('Socket created')
except socket.error as msg :
print('Failed to create socket. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
sys.exit(1)
# Bind socket to local host and port
try:
s.bind((HOST, PORT))
print('Socket bound',HOST,PORT)
except socket.error as msg:
print('CW_SERVER: Bind failed with Error Code ',msg[0],'-',msg[1])
print(HOST,PORT)
sys.exit(1)
# Keep talking with the client
#while True:
while not P.Stopper.isSet():
# Receive data from client (data, addr)
try:
#print 'CW_SERVER ...'
d = s.recvfrom(1024)
data = d[0]
addr = d[1]
if data:
print('Message[' + addr[0] + ':' + str(addr[1]) + '] - ' + data)
keyer.send_msg(data)
else:
print('CW_SERVER: blah - never should have got here!')
sys.exit(1)
except socket.error as msg:
# print "msg=",msg
pass
print('CW_SERVER Done.')
# CW UDP server - old, depricated
class CW_Server_OLD(threading.Thread):
def __init__(self,keyer,HOST='',PORT=7388):
self.keyer=keyer
print('Starting CW server - ',HOST,PORT)
# Open udp socket
try :
self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.settimeout(1) # Need a timeout so we can interrupt/terminate it
print('Socket created')
except socket.error as msg :
print('Failed to create socket. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
sys.exit(1)
# Bind socket to local host and port
try:
self.s.bind((HOST, PORT))
print('Socket bound')
except socket.error as msg:
print('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
sys.exit(1)
# Start the server
threading.Thread.__init__(self)
self.start() # This apparently calls self.run ?!
# The server listener runs in another thread
def run(self):
#print "Heydo!"
# Keep talking with the client
self.Done = False
while not(self.Done):
# Receive data from client (data, addr)
try:
d = self.s.recvfrom(1024)
data = d[0]
addr = d[1]
if data:
print('Message[' + addr[0] + ':' + str(addr[1]) + '] - ' + data)
self.keyer.send_msg(data)
else:
print('CW_Server: blah - never should have got here!')
sys.exit(1)
except socket.error as msg:
# print "msg=",msg
pass
# That's all folks - terminate
print("Server done.")
#sys.exit(1)