-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpiokeyer.py
executable file
·561 lines (472 loc) · 16.7 KB
/
gpiokeyer.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
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
#!/usr/bin/python3
#
# Author: Tom N4LSJ -- EXPERIMENTAL CODE ONLY FOR TINKERING
# -- USE AT YOUR OWN RISK ONLY
# -- AUTHOR ASSUMES NO LIABILITY
#
# Keyer for Raspberry Pi
#
# You can also use the Pi's GPIO pins, which is what I switched to later on.
# They appear to safely drive the Yaesu, despite their supposedly being
# only 3.3v
#
# For keying, you can use an NPN transistor to key your radio. I tied
# a GPIO pin (see the keyer= var below) to the base using a 2.2K resistor.
# Emitter grounded, collector goes to PTT on your radio, also with a
# resistor in series. You might also need to use capacitors to keep RFI
# from keying the transistor # That's up to you, you're a ham operator!
# (I know this because I had to do it.) I put mine between base and ground.
#
# You can also use a 5V reed relay with a cap across the contacts.
#
import datetime
DEBUGTS=datetime.datetime.utcnow()
import time
import serial
import RPi.GPIO as GPIO
from tkinter import *
from tkinter.simpledialog import askstring
from tkinter.simpledialog import askinteger
from tkinter.simpledialog import askfloat
from tkinter.simpledialog import messagebox
from os.path import expanduser
from os import path
from time import sleep
global ee
global sending
global keyer
global ser
global mycall
global geom
global othercall
global configfn
global wpm
global cw
global tuning
global tuning_idt
global clock_id
global hotsend
hotsend=1
sending = 0
ee = 0
othercall=''
spinning=0
spinny=['|','/','-','\\']
curspinny=0
tuning=0
tuning_idt=''
################################# YOU SET THESE
mycall='CHANGEME' # EMPTY ON PURPOSE
wpm="13" # WPM TO USE IF NO CONFIG FILE (FIX THIS)
keyer = 11 # was 17 # GPIO PIN FOR KEYING TO TRANSISTOR
################################# END OF YOU SET THESE
GPIO.setmode(GPIO.BOARD)
GPIO.setup(keyer,GPIO.OUT)
cw = {
"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" : "----.", "." : ".-.-.-", "?" : "..--..", "/" : "-..-.",
"," : "--..--", "!" : "-.-.--", "'" : ".----.", "\"" : ".-..-.", "(" : "-.--.",
")" : "-.--.-", "&" : ".-...", ":" : "---...", ";" : "-.-.-.", "_" : "..--.-",
"=" : "-...-", "+" : ".-.-.", "-" : "-....-", "$" : "...-..-", "@" : ".--.-",
"#" : "...-.-"
}
configfn=str(expanduser("~"))+"/.keyer.conf"
def updhotsbut(*args):
global hotsend
print("should be fixing button...")
print("hotsend is "+str(hotsend))
if (hotsend == 1):
hots.config(text="Hot Send ON")
else:
hots.config(text="Hot Send OFF")
def hotstog(*args):
global hotsend
hotsend = int(1 - int(hotsend))
updhotsbut()
def hovertxt(txt):
hlabel.config(text=txt)
def hover(widg,txt):
widg.bind("<Enter>",lambda evt, tt=txt: hovertxt(tt))
widg.bind("<Leave>",lambda evt, tt="": hovertxt(tt))
def pttaction(val):
if (val == 1):
ptt_on()
if (val == 0):
ptt_off()
def starttuning(val):
global tuning
global tuning_idt
global keyer
# print ("val is "+str(val)+" and input is " +str(GPIO.input(keyer)))
if (GPIO.input(keyer) == 1 and val == 10):
# print ("should be canceling")
root.after_cancel(tuning_idt)
ptt_off()
return None
if (val > 1):
ptt_on()
val = val - 1
tuning_idt=root.after(1000,starttuning,val)
else:
root.after_cancel(tuning_idt)
ptt_off()
def startclock(*args):
global clock_id
putdate()
clock_id=root.after(500,startclock,'')
def putdate():
now = str(datetime.datetime.utcnow())
datelab.config(text=now[0:16]+" UTC")
if (now[18] in {'0','2','4','6','8'}):
datelab.config(bg='#00ff00')
else:
datelab.config(bg='#005500')
def numonly(val):
for ch in val:
if (not ch in ('0123456789')):
return False
return True
def alnumslashonly(val):
for ch in val:
if (not ch in ('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/')):
return False
return True
def cwonly(val):
global cw
for ch in val:
ch=ch.upper()
if (not ch in cw.keys() and ch != " " and ch != "<" and ch != ">"):
print ("BAD CHAR: ["+ch+"]")
return False
return True
def WriteCfg():
global configfn
global hotsend
fh=open(configfn,'w+')
fh.write('wpm='+str(iwpm.get())+"\n")
fh.write('mycall='+imycall.get()+"\n")
fh.write('hotsend='+str(hotsend)+"\n")
sx=root.geometry().replace('+',' ').split()[1]
sy=root.geometry().replace('+',' ').split()[2]
fh.write("geom=+"+sx+"+"+sy+"\n")
for kid in macroframe.winfo_children():
fh.write('macro='+str(kid['text'])+"\n")
print("Saved config to "+configfn)
def MACGRIDNEW():
mr = 0
mc = 0
gs=macroframe.grid_slaves()
for s in gs:
gi=s.grid_info()
gir=gi['row']
if (mr < gir):
mr = gir
for s in gs:
gi=s.grid_info()
gir=gi['row']
gic=gi['column']
if (gir == mr and mc < gic):
mc = gic
print ("max row is "+str(mr)+" and max col is "+str(mc))
if (mc == 5):
return mr+1, 1
else:
return mr, mc+1
def MACROREGRID():
macbuts=[]
gs=macroframe.grid_slaves()
for r in range (1,100):
for c in range (1,6):
for s in gs:
gi=s.grid_info()
rr=gi['row']
cc=gi['column']
if (r == rr and c == cc):
macbuts.append(s)
rr=1
cc=1
for m in macbuts:
m.grid(row=rr, column=cc)
cc=cc+1
if (cc > 5):
cc=1
rr=rr+1
def MAKEMACBUT(txt,rr,cc):
bb=Button(macroframe, text=txt)
bb.grid(row=rr,column=cc)
hover(bb,"Hit to send "+txt+", or right click to edit.")
bb.config(command=lambda widg=bb: RUNMACRO(widg))
bb.bind("<ButtonRelease-3>", lambda ebe, widg=bb: EDITBTN(widg))
return bb
def NEWMACRO(*args):
rr,cc=MACGRIDNEW()
bb=MAKEMACBUT('NEW MACRO',rr,cc)
EDITBTN(bb)
if (bb['text'] == 'NEW MACRO' or bb['text'] == ''):
bb.destroy()
def EDITBTN(widg):
newval = askstring('New Macro','Enter new macro. <C> is your call. <I> is other call.',initialvalue=widg['text'])
if (newval == ''):
widg.destroy()
MACROREGRID()
else:
widg.config(text=newval)
def EXPANDVARS(strinp):
s1=strinp.upper()\
.replace('<I>',iothercall.get().upper())\
.replace('<C>',imycall.get().upper())\
.replace('<AR>','+')\
.replace('<SK>','#')\
.replace('<KN>','(')\
.replace('<AS>','&')
s2=re.sub(r"<.*>?","",s1)
s3=s2.replace('<','')\
.replace('>','')
return s3
def RUNMACRO(widg):
global hotsend
# DOING IN MULTIPLE STEPS. SHUT UP
string=EXPANDVARS(widg['text'])
tempstr=cwinput.get()
cwinput.delete(0,END)
cwinput.insert(0,string)
QUEUE()
cwinput.delete(0,END)
cwinput.insert(0,tempstr)
if (hotsend == 1):
STARTSEND()
def ReadCfg():
global serport
global keyer
global geom
global configfn
global wpm
global mycall
global ee
global hotsend
if (not path.exists(configfn)):
root.iconify()
mycall=askstring("Call Sign","Please enter your call sign.").upper().strip()
wpm=askinteger("WPM","Enter a default words per minute for CW.",minvalue=5,maxvalue=50)
imycall.delete(0,END)
imycall.insert(0,mycall)
iwpm.delete(0,END)
iwpm.insert(0,wpm)
zz=Button(macroframe,\
text='SAMPLE MACRO')
zz.config(command=lambda widg = zz: RUNMACRO(widg))
zz.grid(row=0,column=1)
WriteCfg()
zz.destroy()
imycall.delete(0,END)
iwpm.delete(0,END)
if (path.exists(configfn)):
print ("Reading Config file now that it exists...")
rr = 1
cc = 1
fh=open(configfn,'r')
cfglines = fh.readlines()
fh.close()
for cfgline in cfglines:
items=cfgline.split('=')
if (items[0] == "hotsend"):
hotsend=int(items[1].strip())
if (items[0] == "wpm"):
wpm=items[1].strip()
if (items[0] == "mycall"):
mycall=items[1].strip()
if (items[0] == "geom"):
geom=items[1].strip()
if (items[0] == "macro"):
macrocontents=items[1].strip()
MAKEMACBUT(macrocontents,rr,cc)
cc = cc + 1
if (cc > 5):
rr = rr + 1
cc = 1
return True
else:
print ("No config file yet.")
return None
def Quitter(*args):
global keyer
GPIO.cleanup(keyer)
WriteCfg()
root.destroy()
def ptt_on():
global keyer
GPIO.output(keyer,1)
lcw.config(bg='#00ff00')
# root.update()
def ptt_off():
global keyer
lcw.config(bg='#005500')
# root.update()
GPIO.output(keyer,0)
def KEY(ch):
global cw
global keyer
global wpm
t = (1200.0/float(wpm))/1000.0
ditlength = t
dahlength = ditlength * 3
ch = ch.upper()
if (ch == " "):
True
else :
for dd in cw[ch]:
if (dd == "-"):
ptt_on()
sleep(dahlength)
ptt_off()
sleep(ditlength)
if (dd == "."):
ptt_on()
sleep(ditlength)
ptt_off()
sleep(ditlength)
root.update()
sleep (dahlength)
#root.update()
def BCLEAR():
cwinput.delete(0,END)
QCLEAR()
def QCLEAR():
global sending
sending = 0
queue.delete(0,END)
def QUEUE(*args):
if (queue.get() != ""):
queue.insert(END," ")
queue.insert(END,cwinput.get().upper())
cwinput.delete(0,END)
def STOP():
global sending
sending = 0
lcw.config(bg='#d9d9d9')
def STARTSEND():
global sending
QUEUE()
if (sending == 0):
sending = 1
SENDCW()
def SENDCW():
global sending
global wpm
if (sending == 1):
wpm=iwpm.get()
qq = queue.get()
qq = EXPANDVARS(qq)
if (qq == ""):
print ("Nothing in queue")
sending = 0
return None
key = qq[0]
qq = qq[1:]
queue.delete(0,END)
queue.insert(0,qq)
root.update()
KEY(key)
root.update()
if (queue.get() != ""):
SENDCW()
else:
sending = 0
lcw.config(bg='#d9d9d9')
root=Tk()
cwframe=Frame(root,borderwidth=2,relief="groove")
callsframe=Frame(root,borderwidth=2,relief="groove")
macroframe=Frame(root,borderwidth=2,relief="groove")
helpframe=Frame(root,borderwidth=2,relief="groove")
root.bind("<Escape>",Quitter)
root.bind("<Control-w>",Quitter)
root.protocol("WM_DELETE_WINDOW", Quitter)
root.title("GPIO Keyer")
macroframe.bind("<ButtonRelease-3>",NEWMACRO)
pttframe=Frame(cwframe)
# CW FRAME WIDGETS
lqueue = Label(cwframe, text="Queue:")
queue = Entry(cwframe, font="Courier", width=84, validate="key")
queue['validatecommand']=(queue.register(cwonly),'%P')
lcw = Label(cwframe, text="CW:")
cwinput = Entry(cwframe, font="Courier", width=40, validate="key")
cwinput['validatecommand']=(cwinput.register(cwonly),'%P')
cwinput.bind("<Return>",QUEUE)
cwinput.bind("<KP_Enter>",QUEUE)
lwpm = Label(cwframe,text="wpm:")
iwpm = Entry(cwframe, font = "Courier", width=2, validate="key")
iwpm['validatecommand']=(iwpm.register(numonly),'%P')
clr = Button(cwframe,text="clear queue", command=QCLEAR, width=8, fg="white",bg="blue")
clrb = Button(cwframe,text="clear both", command=BCLEAR, width=8, fg="white",bg="blue")
qcw = Button(cwframe,text="queue", command=QUEUE, width=8, bg="yellow")
bcw = Button(cwframe,text="GO", command=STARTSEND, width=8, bg="green")
scw = Button(cwframe,text="STOP", command=STOP, width=8, bg="red")
ptt = Button(pttframe,text="PTT", width=4)
tune = Button(pttframe,text="TUNE", width=5)
hots = Button(pttframe,text="Hot Send", width=8)
datelab = Label (cwframe, font="Helvetica 12 bold", text="YYYY-MM-DDDD HH:MM", width=23)
prosigns = Label(cwframe, font="Helvetica 10 bold", text="&=AS +=AR #=SK (=KN", width=23)
#HELP
hlabel=Label(helpframe,text="")
# CALLSIGN FRAME
lmycall = Label(callsframe, text="Your Station's Call:")
imycall = Entry(callsframe, width=13, validate="key", justify="center")
imycall['validatecommand']=(imycall.register(alnumslashonly),'%P')
lothercall = Label(callsframe, text="Other Station's Call:")
iothercall = Entry(callsframe, width=13, validate="key", justify="center")
iothercall['validatecommand']=(iothercall.register(alnumslashonly),'%P')
lnotes = Label(callsframe, text="<C> is replaced with your call. <I> is replaced with other station's call.")
ptt.bind('<Button-1>',lambda evt, val = 1: pttaction(val))
ptt.bind('<ButtonRelease-1>',lambda evt, val=0: pttaction(val))
tune.bind('<Button-1>',lambda evt, val = float(10): starttuning(val))
hots.bind('<Button-1>',hotstog)
# FRAMES LAYOUT
cwframe.grid(row=4,column=1,columnspan=4,sticky='EW')
callsframe.grid(row=5,column=1,columnspan=4,sticky='EW')
macroframe.grid(row=6,column=1,columnspan=4,sticky='EW')
helpframe.grid(row=8,column=1,columnspan=4,sticky='EW')
# HELP LABEL
hlabel.grid(row=1,column=1)
# CW KEYING
clrb.grid(row=1,column=1)
clr.grid(row=1,column=2)
qcw.grid(row=1,column=3)
bcw.grid(row=1,column=4)
scw.grid(row=1,column=5)
lcw.grid(row=1,column=6)
cwinput.grid(row=1,column=7, columnspan=2)
lwpm.grid(row=1,column=9)
iwpm.grid(row=1,column=10)
lqueue.grid(row=2,column=1)
queue.grid(row=2,column=2,columnspan=9)
prosigns.grid(row=3,column=1, columnspan=2)
datelab.grid(row=3,column=2, columnspan=5)
pttframe.grid(row=3,column=8,columnspan=2)
ptt.grid(row=1,column=1)
tune.grid(row=1,column=2)
hots.grid(row=1,column=3)
# CALLSIGNS
lmycall.grid(row=1,column=1)
imycall.grid(row=1,column=2)
lothercall.grid(row=1,column=3)
iothercall.grid(row=1,column=4)
lnotes.grid(row=1,column=5)
hover(imycall,"Enter YOUR call here and it will be used in macros as <C>.")
hover(iothercall,"Enter OTHER call here and it will be used in macros as <I>.")
hover(iwpm,"Enter words per minute to send here.")
hover(cwframe,"This is the CW sending area.")
hover(callsframe,"Call sign area.")
hover(macroframe,"Right click in this area between buttons to make a new button. Right click an already existing button to edit it.")
hover(helpframe,"This area is for helpful help message hovering.")
ReadCfg()
iwpm.insert(0,wpm)
imycall.insert(0,mycall)
root.geometry(geom)
startclock()
updhotsbut()
root.mainloop()