-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi2ts590.py
905 lines (820 loc) · 41.7 KB
/
midi2ts590.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
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
# midi2ts590 by Patrick EGLOFF aka TK5EP
#
#
# reads MIDI commands from a HERCULES DJcontrol compact controller
# each JOG, slider or button have a function assigned and sends KENWOOD remote commands to a TS590s via a COM port
# MIDI command are including the device number (0 to 4, depending on your setup), the MIDI status (depending on the kind of control), the control ID (depending on the device) and finally the control value
#
# v 0.1 15/02/17 first version
# v 0.2 15/01/2023 changed many small things, use of KwdCat library
# v 0.22 20/01/2023 added thread to poll radio and change mode/vfo accordingly on DJcontroller
# v 0.26 31/01/2023 added AF volunme control, and added AF vol and RF gain setting at startup
# v 0.29 03/02/23 added default AF vol, changed ini file with RTS & DTR
__Title = "Remote control for TS590 with DJcontrol Compact"
__Version = "0.29"
__VersionDate = "03/02/2023"
# import libraries
import configparser #https://docs.python.org/3/library/configparser.html
import os
import sys
import argparse
import math
from time import sleep
from os import environ # following 2 lines are to hide pygame welcome message
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'
import pygame.midi
from threading import Thread
## Import own libraries
from KwdCat import KwdCat
def ReadIniFile():
########################################
# Read the ini file and get settings
#
# some datas are INT so need to be converted with .getint !
########################################
try:
# read ini file
Config.read('midi2ts590.ini')
# check if Midi section exists
if Config.has_section('Midi'):
config.MidiDeviceIn = Config.getint('Midi','deviceIN') # get the device Midi IN, reads and converts in INT
config.MidiDeviceOut = Config.getint('Midi','deviceOUT') # get the device Midi OUT
else:
input("Midi section missing in config file. Please correct this !\nCTRL-C to exit")
sys.exit(1)
# check if default section exists
if Config.has_section('Default'):
config.RadioMode = Config.get('Default','mode')
config.RadioVFO = Config.get('Default','VFO')
config.RadioTuningStep = Config.getint('Default','tuningstep')
config.RadioSniff = Config.getint('Default','radiosniff')
config.AFvolume = Config.get('Default','afvolume')
else:
input("Default section missing in config file. Please correct this !\nCTRL-C to exit")
sys.exit(1)
# check if Radio section exists
if Config.has_section('Radio'):
config.RadioModel=Config.get('Radio','model')
config.RadioPort=Config.get('Radio','comport')
config.RadioBaudrate=Config.getint('Radio','baudrate') #reads and converts in INT
config.RadioBytesize=Config.getint('Radio','bytesize')
config.RadioStopbits=Config.getint('Radio','stopbits')
config.RadioParity=Config.get('Radio','parity')
config.RadioXonXoff=Config.getint('Radio','xonxoff')
config.RadioRtsCts=Config.getint('Radio','rtscts')
config.RadioDsrDtr=Config.getint('Radio','dsrdtr')
config.RadioRts=Config.getint('Radio','rts')
config.RadioDtr=Config.getint('Radio','dtr')
config.polltime=Config.getint('Radio','polltime')
config.RadioRxtimeout=Config.getint('Radio','rxtimeout')
config.RadioTxtimeout=Config.getint('Radio','txtimeout')
else:
input("Radio section missing in config file. Please correct this !\nCTRL-C to exit")
sys.exit(1)
# check if Commands section exists
if Config.has_section('Commands'):
config.Radiocmd1 = Config.get('Commands','cmd1')
config.Radiocmd2 = Config.get('Commands','cmd2')
config.Radiocmd3 = Config.get('Commands','cmd3')
#pass # do nothing
else:
input("Commands section missing in config file. Please correct this !\nCTRL-C to exit")
sys.exit(1)
# if an option is missing, raise an error
except configparser.NoOptionError:
input ("Missing option(s) in config file !\nPlease correct this or remove file to allow creating a default one !")
sys.exit(1)
else: # otherwise, we're happy
print ("Config file correctly read & parsed")
def CreateIniFile():
########################################
# create_midi2ts590_inifile
# in case the ini file does not exist, create one with default values
########################################
inifile = open('midi2ts590.ini','w')
# add default section
Config.add_section('Default')
Config.set('Default','# midi2ts590 config file')
Config.set('Default','# defaults values created by program')
Config.set('Default','# See manual for help in setting')
Config.set('Default','mode','USB')
Config.set('Default','VFO','A')
Config.set('Default','tuningstep','5')
Config.set('Default','radiosniff','0')
Config.set('Default','afvolume','0')
# add section Midi
Config.add_section('Midi')
# add settings
Config.set('Midi','deviceIN','1')
Config.set('Midi','deviceOUT','3')
# add section Radio
Config.add_section('Radio')
# add settings
Config.set('Radio','model','TS590s')
Config.set('Radio','comport','COM33')
Config.set('Radio','baudrate','57600')
Config.set('Radio','bytesize','8')
Config.set('Radio','stopbits','1')
Config.set('Radio','parity','N')
Config.set('Radio','xonxoff','0')
Config.set('Radio','rtscts','0')
Config.set('Radio','dsrdtr','0')
Config.set('Radio','rts','1')
Config.set('Radio','dtr','1')
Config.set('Radio','polltime','1000')
Config.set('Radio','rxtimeout','0')
Config.set('Radio','txtimeout','0')
# add section Commands
Config.add_section('Commands')
# add settings
Config.set('Commands','# put one or more KENWOOD commands (see manual) on each following line')
Config.set('Commands','# e.g cmd1 = VV;VX0; set VFO A=B and VOX OFF')
Config.set('Commands','# e.g cmd2 = rt0; will put RIT OFF')
Config.set('Commands','# these commands will be sent at startup')
Config.set('Commands','cmd1','VV')
Config.set('Commands','cmd2','')
Config.set('Commands','cmd3','')
# and lets write it out...
Config.write(inifile)
inifile.close()
# finally read the config file
ReadIniFile()
def ChangeMode(mode:str,ledonly = False):
################################
# change mode on radio and lights led under button
#
# input: mode: str must be 'CW', 'USB', 'LSB', 'FSK'
# ledonly: bool if we ONLY want to change the LED status NOT sending to radio
# by default ledonly False
################################
#global RadioMode
if mode == 'CW':
config.RadioMode = 'CW'
strCat = 'MD3' # MODE = CW
# switch on backlight
Midi_Out.write([ [[144,49,127],0], [[144,50,0],0], [[144,51,0],0] , [[144,52,0],0] ])
elif mode == 'LSB':
config.RadioMode = 'LSB'
strCat = 'MD1'
Midi_Out.write([ [[144,49,0],0] , [[144,50,0],0] , [[144,51,0],0] , [[144,52,127],0] ])
elif mode == 'USB':
config.RadioMode = 'USB'
strCat = 'MD2'
Midi_Out.write([ [[144,49,0],0] , [[144,50,0],0] , [[144,51,127],0] , [[144,52,0],0] ])
elif mode == 'FSK':
config.RadioMode = 'FSK'
strCat = 'MD6'
Midi_Out.write([ [[144,49,0],0] , [[144,50,127],0] , [[144,51,0],0] , [[144,52,0],0] ])
if ledonly == False: # if we only want to change the Leds and not send to radio
ts590.query(strCat,0)
def ChangeVFO(vfo:str,ledonly=False):
########################################
# put radio on VFO A or B
#
# input vfo:str must be 'A' or 'B'
########################################
if vfo == 'A':
strCat='FR0'
config.RadioVFO = 'A'
# switch on backlights
Midi_Out.write([ [[144,35,127],0] , [[144,34,0],0] , [[144,3,0],0] , [[144,4,0],0] ])
elif vfo == 'B':
config.RadioVFO = 'B'
strCat='FR1'
Midi_Out.write([[[144,34,127],0] , [[144,35,0],0] , [[144,3,0],0] , [[144,4,0],0] ])
if ledonly == False:
ts590.query(strCat,0)
def DJ_init():
##################
# init pygame
##################
pygame.midi.init()
def DJ_count()->int:
#############################################
# count the number of Midi devices connected
#############################################
MidiDeviceCount = pygame.midi.get_count()
print ("Number of Midi devices found =",MidiDeviceCount)
return MidiDeviceCount
def DJ_info():
#############################################
# lists all Midi deveices
#############################################
print("List of all available MIDI devices")
for i in range(pygame.midi.get_count()):
r = pygame.midi.get_device_info(i)
(interf, name, input, output, opened) = r
in_out = ""
if input:
in_out = "input"
if output:
in_out = "output"
# make a nice table of this
print ('nr: {: <3}Interface: {: <10} Name: {: <30} {: <8} Opened:{}'.format(i, str(interf,'UTF-8'), str(name,'UTF-8'), in_out, opened))
def DJ_initInput(device:int)->bool:
# check if the selected input device is correct
info = pygame.midi.get_device_info(device) # get infos about input device, returns (interf, name, input, output, opened)
#print (info)
#print (info[0])
if info == None: # if out of range, pygame class returns None
DJ_info() #show list of available devices
input("MIDI device number out of range\nCTRLC-C and correct this")
sys.exit(1)
elif info[4] == 1: # "opened" var in tuple returned, if 1 then device opened
DJ_info()
input ("Selected MIDI device already in use !\nCTRLC-C and correct this")
sys.exit(1)
elif info[2] != 1: # "input" var in tuple returned, if 1 then device is an input
DJ_info()
print("Selected device nr",device)
input ("This MIDI device is not an input !\nCTRLC-C and correct this")
sys.exit(1)
else:
if DEBUG:
print("MIDI input initialized",device)
return True
def DJ_initOutput(device:int)->bool:
# check if the selected output device is correct, same tests as above
info = pygame.midi.get_device_info(device)
#print (info)
if info == None:
DJ_info()
input("Device number out of range !\nCTRLC-C and correct this")
sys.exit(1)
elif info[4] == 1:
DJ_info()
input ("Selected MIDI device already in use !\nCTRLC-C and correct this")
sys.exit(1)
elif info[3] != 1:
DJ_info()
print("Selected device nr",device)
input("This MIDI device is not an output !\nCTRLC-C and correct this ")
sys.exit(1)
else:
if DEBUG:
print("MIDI output initialized",device)
return True
def DJ_LedsON():
Midi_Out.write([ [[144,1,127],0], [[144,2,127],0], [[144,3,127],0] , [[144,4,127],0] , [[144,33,127],0], [[144,34,127],0], [[144,35,127],0] , [[144,49,127],0], [[144,50,127],0], [[144,51,127],0] , [[144,52,127],0] , [[144,81,127],0], [[144,82,127],0], [[144,83,127],0] , [[144,43,127],0] , [[144,45,127],0] , [[144,48,127],0] ])
def DJ_LedsOFF():
Midi_Out.write([ [[144,1,0],0], [[144,2,0],0], [[144,3,0],0] , [[144,4,0],0] , [[144,33,0],0], [[144,34,0],0], [[144,35,0],0] , [[144,49,0],0], [[144,50,0],0], [[144,51,0],0] , [[144,52,0],0] , [[144,81,0],0], [[144,82,0],0], [[144,83,0],0] , [[144,43,0],0] , [[144,45,0],0] , [[144,48,0],0] ])
def DJ_LedsBlink(numTimes,period):
for i in range(0,numTimes): ## Run loop numTimes
DJ_LedsON()
sleep(period)
DJ_LedsOFF()
sleep(period)
def DJ_LedRECORD(state:int):
Midi_Out.write([[[0x90,0x2B,state],0]])
def DJ_LedAUTO(state:int):
MidiOut.write([[[0x90,0x2D,state],0]])
def DJ_LedDA_SYNC(state:int):
Midi_Out.write([[[0x90,0x23,state],0]])
def DJ_LedDA_CUE(state:int):
Midi_Out.write([[[0x90,0x22,state],0]])
def DJ_LedDA_PLAY(state:int):
Midi_Out.write([[[0x90,0x21,state],0]])
def DJ_LedDA_KP1(state:int):
Midi_Out.write([[[0x90,0x01,state],0]])
def DJ_LedDA_KP2(state:int):
Midi_Out.write([[[0x90,0x02,state],0]])
def DJ_LedDA_KP3(state:int):
Midi_Out.write([[[0x90,0x03,state],0]])
def DJ_LedDA_KP4(state:int):
Midi_Out.write([[[0x90,0x04,state],0]])
def DJ_LedDB_SYNC(state:int):
Midi_Out.write([[[0x90,0x53,state],0]])
def DJ_LedDB_CUE(state:int):
Midi_Out.write([[[0x90,0x52,state],0]])
def DJ_LedDB_PLAY(state:int):
Midi_Out.write([[[0x90,0x51,state],0]])
def DJ_LedMODE(state:int):
Midi_Out.write([[[0x90,0x30,state],0]])
def DJ_LedDB_KP1(state:int):
Midi_Out.write([[[0x90,0x31,state],0]])
def DJ_LedDB_KP2(state:int):
Midi_Out.write([[[0x90,0x32,state],0]])
def DJ_LedDB_KP3(state:int):
Midi_Out.write([[[0x90,0x33,state],0]])
def DJ_LedDB_KP4(state:int):
Midi_Out.write([[[0x90,0x34,state],0]])
def DJ_scan():
try:
event = Midi_In.read(10)[0]
data = event[0]
device = data[0]
status = data[1]
control = data[2]
value = data[3]
timestamp = event[1]
if DEBUG:
print ("Device:",device,"Status",status,"Control",control,"Value:",value,timestamp)
#detect rotation of jogs and pots
if device == 0xB0: # a pot/slider/jog has been turned
if status == 48: # JOG A activity detected
if DEBUG:
print("JOG_A turned")
if control < 64:
if config.RadioVFO == 'A':
ts590.VFOfreq(0,0,config.RadioTuningStep) # VFOfreq(0=VFOA,0=up,1=step)
if config.RadioVFO == 'B':
ts590.VFOfreq(1,0,config.RadioTuningStep) # VFOfreq(0=VFOA,0=up,1=step)
else:
if config.RadioVFO == 'A':
ts590.VFOfreq(0,1,config.RadioTuningStep)
if config.RadioVFO == 'B':
ts590.VFOfreq(1,1,config.RadioTuningStep)
elif status == 49: # JOG B activity detected
if DEBUG:
print("JOG_B turned")
if control < 64: # turned CW
ts590.RITUp() # send RIT up
else : # turned CCW
ts590.RITDown() # send RIT down
elif status == 54: # SLIDER activity detected
if DEBUG:
print(control*2)
strCat = format ("AG%04d"% ( control *2 )) # slider value is 0-127, RX VOLUME needs 0-255
ts590.query(strCat,0) # send to radio
elif status == 59 and config.RadioMode != 'CW' and config.RadioMode != 'FSK': # if DA_MEDIUM pot moved change SL command but NOT in CW
global oldsl
sl = math.floor(( control / 9.5)) # to get values from 0-13
if DEBUG:
print ("SL:",sl) # SL command 00-11
if sl != oldsl: # if the new value SL is different from previous one
strCat = format ("SL%02d"%sl) # format de CAT string
oldsl = sl # set the old value of SL
ts590.query(strCat,0) # send CAT command
elif status == 60 and config.RadioMode =='CW': # CW bandwidth 050-2500
global oldfwcw
fwcwval =[50,80,100,150,200,250,300,400,500,600,1000,1500,2000,2500] # these are the values for FW command is CW
fwcw = math.floor(control / 9.5) # to get values from 0-13
if fwcw != oldfwcw:
strCat = format ("FW%04d"% fwcwval[fwcw])
ts590.query(strCat,0)
oldfwcw = fwcw
if DEBUG:
print("CW FW:",strCat)
elif status == 60 and config.RadioMode =='FSK': #FSK bandwith 250-1500
global oldfwfsk
fwfskval = [250,500,1000,1500] # values for FW in FSK
fwfsk = math.floor(control / 41)
if fwfsk != oldfwfsk:
strCat= format ("FW%04d"% fwfskval[fwfsk])
ts590.query(strCat,0)
oldfwfsk = fwfsk
if DEBUG:
print("FSK FW:",strCat)
elif status == 63 and config.RadioMode != 'CW' and config.RadioMode != 'FSK':
global oldsh
sh = math.floor(control / 9.5) # SH command 00-13
if sh != oldsh:
strCat = format ("SH%02d"% sh)
ts590.query(strCat,0)
oldsh = sh
if DEBUG:
print("SH:",sh)
elif status == 64 and config.RadioMode =='CW': # CW only shift command IS
global oldis
istab =[300,350,400,450,500,550,600,650,700,750,800,850,900,950,1000] # IS values for CW
isval = math.floor(control/8.5)
if isval != oldis:
strCat = format ("IS %04d"%istab[isval])
ts590.query(strCat,0)
oldis = isval
if DEBUG:
print ("IS:",isval)
elif status == 57: # sets the power 005-100
out = math.floor(5 + (100 - 5) * control / 127 )
if DEBUG:
print ("%03d"%out)
strCat = format ("PC%03d"% out)
ts590.query(strCat,0)
elif status == 61:
out = control * 2
strCat = format ("RG%03d"%out)
ts590.query(strCat,0) # RG gain
if DEBUG:
print("RG:",out)
## check if buttonss have been pressed
if device == 144: # a key has been pressed
if status == 1 and control == 127: # DA_KP1 button pressed
if DEBUG:
print("DA_KP1")
ts590.query('TS1',0) # send TF-SET ON
DJ_LedDA_KP1(1) # light corresponding LED
elif status == 1 and control == 0: # if ket is released
if DEBUG:
print('DA_KP1 released')
ts590.query('TS0',0) # TF-SET OFF
DJ_LedDA_KP1(0) # LED off
elif status == 2: # DA_KP2 pressed
if control == 127 and config.RadioMode == 'CW':
if DEBUG:
print('DA_KP2 pressed')
ts590.query('CA1',0) # CW TUNE ON
DJ_LedDA_KP2(1)
else:
ts590.query('CA0',0) # CW TUNE OFF
DJ_LedDA_KP2(0)
elif status == 3: # DA_KP3 pressed
if control == 127:
if DEBUG:
print('DA_KP3 pressed')
ts590.query('FR0;FT1',0) # SPLIT A/B
DJ_LedDA_KP3(1) # LED on
DJ_LedDA_KP4(0) # all other mode LEDs off
DJ_LedDA_SYNC(0)
DJ_LedDA_CUE(0)
elif status == 4: #DA_KP4 pressed
if control == 127:
if DEBUG:
print('DA_KP3 pressed')
ts590.query('FR1;FT0',0) #SPLIT B/A
DJ_LedDA_KP3(0) # LED off
DJ_LedDA_KP4(1) # LED on
DJ_LedDA_SYNC(0) # LED VFO A off
DJ_LedDA_CUE(0) # LED VFO B off
elif status == 33: #VFO A=B
if control == 127:
if DEBUG:
print('DA_PLAY pressed')
ts590.query('VV',0) # send VV command
DJ_LedDA_PLAY(1)
else:
DJ_LedDA_PLAY(0)
elif status == 34: # DA_CUE key presses
if control == 127:
if DEBUG:
print('DA_CUE pressed')
ChangeVFO('B')
"""
ts590.query('FR1',0) # VFO A FR command
DJ_LedDA_SYNC(0)
DJ_LedDA_CUE(1) #DE_CUE LED on
DJ_LedDA_KP3(0) # LED off
DJ_LedDA_KP4(0) # LED off
"""
elif status == 35: # DA_SYNC presses
if control == 127:
if DEBUG:
print('DA_SYNC pressed')
ChangeVFO('A')
"""
ts590.query('FR0',0) # VFO B FR command
DJ_LedDA_SYNC(1)
DJ_LedDA_CUE(0)
DJ_LedDA_KP3(0)
DJ_LedDA_KP4(0)
"""
elif status == 43: # REC button
if control == 127: # pressed
if DEBUG:
print('REC pressed RadioIsON :',config.RadioIsON)
if config.RadioIsON == 1: # is the radio ON flag
ts590.RadioOnOff(0) # switch radio off
DJ_LedRECORD(0) # REC LED off
config.RadioIsON = 0 # swap flag
else: # radio is off
ts590.RadioOnOff(1) # turn it on
DJ_LedRECORD(1)
config.RadioIsON = 1
elif status == 49: # DA_KP1 button
if control == 127: # pressed
if DEBUG:
print('DB_KP1 pressed')
ChangeMode('CW') # change mode to CW
elif status == 50: # DA_KP2 button
if control == 127: # pressed
if DEBUG:
print('DB_KP2 pressed')
ChangeMode('FSK') # FSK mode
elif status == 51: # DA_KP3 presses
if control == 127:
if DEBUG:
print('DB_KP3 pressed')
ChangeMode('USB') # USB
elif status == 52: # DA_KP4
if control == 127:
if DEBUG:
print('DB_KP4 pressed')
ChangeMode('LSB') # LSB
elif status == 83: # DB_SYN pressed
if control == 127:
if DEBUG:
print('DB_SYNC pressed')
if config.RITisON == 0: # RIT ON/OFF toggle
ts590.query('RT1',0) # RT command
DJ_LedDB_SYNC(1)
config.RITisON = 1
elif config.RITisON == 1:
ts590.query('RT0',0)
DJ_LedDB_SYNC(0)
config.RITisON = 0
elif status == 82: # DB_CUE pressed
if control == 127:
if DEBUG:
print('DB_CUE pressed')
if config.XITisON == 0: # XIT toggle
ts590.query('XT1',0)
DJ_LedDB_CUE(1)
config.XITisON = 1
elif config.XITisON == 1:
ts590.query('XT0',0)
DJ_LedDB_CUE(0)
config.XITisON = 0
elif status == 81: # DB_PLAY pressed
if control == 127:
if DEBUG:
print('DB_PLAY pressed')
ts590.query('RC',0) # RC command RIT clear
DJ_LedDB_PLAY(1)
else:
DJ_LedDB_PLAY(0)
## add here more functions if needed
except:
print("Midi device read error")
def CheckRadioState():
#####################################
# TEST to check the radio state periodically
# if some changes are detected
# change the LEDs and flags accordingly
#
# some glitches due to state difference between polling time
# needs some work
#####################################
answerIF = ts590.query('IF',37) # read radio IF frame
if answerIF != None: # if we have a valid answer
mode = ts590.ReadCmdIF(answerIF)[5] # extract the mode
split = ts590.ReadCmdIF(answerIF)[7] # extract split
VFO = ts590.ReadCmdIF(answerIF)[6] # extract VFO
if DEBUG:
print("\nVariables in CheckRadioState:")
print(time.ctime()) # print time for checking
print("split:",split)
print ("VFO:",VFO)
print("Mode:",mode)
if 1<= int(mode) <= 9: #if we have a valid mode
modeStr = ts590.ConvertMode(int(mode)) # convert it to readable string
ChangeMode(modeStr,1)
# check VFO states
if split == '0': # split off
if VFO == '0': # if VFO A main
ChangeVFO('A',1)
elif VFO == '1':
ChangeVFO('B',1)
if split == '1': # split on
if VFO == '0': # VFO A is main
DJ_LedDA_KP3(1) # LED off
DJ_LedDA_KP4(0) # LED on
DJ_LedDA_SYNC(0) # LED VFO A off
DJ_LedDA_CUE(0) # LED VFO B off
config.radioVFO ='A'
else:
DJ_LedDA_KP3(0) # LED off
DJ_LedDA_KP4(1) # LED on
DJ_LedDA_SYNC(0) # LED VFO A off
DJ_LedDA_CUE(0) # LED VFO B off
config.radioVFO ='B'
else:
pass
def MakeDJequalRadio(rcvdatas):
#####################################
# Check the datas flow on the COM port between the radio and logging software
# if an answer to IF request is found, (most logging software rely on this)
# check if it is complete and change the LEDs on console and flags accordingly
#
# some glitches due to state difference between polling time
# may need some work 23/01/2023
#####################################
try:
start = rcvdatas.find("IF") # find the start of an IF in the received frame
if start != -1 and rcvdatas[start+37] == ";": # if found (!=1) and if at position 37 we've a ; separator
rcvdatas = rcvdatas[start:start+37] # we extract the IF frame
answerIF = ts590.ReadCmdIF(rcvdatas) # we extract all infos from the above frame
split = answerIF[7] # extract split
VFO = answerIF[6] # extract VFO
mode = answerIF[5] # extract the mode
modeStr = ts590.ConvertMode(int(mode)) # convert to readable mode
if DEBUG:
print("\nvariables in MakeDJequalRadio:")
print("rcvdatas:",rcvdatas)
print("split:",split,type(split))
print ("VFO:",VFO,type(VFO))
print("Mode:",mode,type(mode))
print("ModeStr:",modeStr,type(modeStr))
print("RadioMode",config.RadioMode,type(config.RadioMode))
print("RadioVFO",config.RadioVFO,type(config.RadioVFO))
#if 1<= int(mode) <= 9: #if we have a valid mode
if modeStr != config.RadioMode:
mode = ts590.ConvertMode(int(mode)) # convert it to readable string
ChangeMode(modeStr,0) # change only Leds, no CAT
# check VFO states
if split == '0': # split off
if VFO == '0': # if VFO A main
ChangeVFO('A',1)
if VFO == '1':
ChangeVFO('B',1)
if split == '1': # split on
if VFO == '0': # VFO A is main
DJ_LedDA_KP3(1) # LED off
DJ_LedDA_KP4(0) # LED on
DJ_LedDA_SYNC(0) # LED VFO A off
DJ_LedDA_CUE(0) # LED VFO B off
config.RadioVFO = 'A'
else:
DJ_LedDA_KP3(0) # LED off
DJ_LedDA_KP4(1) # LED on
DJ_LedDA_SYNC(0) # LED VFO A off
DJ_LedDA_CUE(0) # LED VFO B off
config.RadioVFO = 'B'
else:
pass
except:
print('Exception in MakeDJequalRadio')
def pollRadio(polltime):
# check periodically the radio state
global stop_thread # set a flag global variable needed to stop the thread
while True:
try:
sleep(polltime/1000) # to poll is ms set in config file
if DEBUG:
print("\nVariables in pollRadio:")
print(time.ctime()) # print time for checking
print("polltime:",config.polltime)
CheckRadioState()
if stop_thread: # if the flag is set and thread join
break # stop thread
except:
print("Exception in Pollradio thread")
pass
def SniffRadio(polltime):
global stop_thread # set a flag global variable needed to stop the thread
while True:
try:
if DEBUG:
print("\nVariables in pollRadio:")
print(time.ctime()) # print time for checking
print("polltime:",config.polltime)
sleep(polltime/1000) # to poll is ms set in config file
rcvdatas = ts590.read()
MakeDJequalRadio(rcvdatas)
if stop_thread: # if the flag is set and thread join
break # stop thread
except:
print("Exception in SniffRadio thread")
pass
###################################################
## MAIN
##
## reads configuration file or create it if not exist
## creates a connection to TS590 with the Kenwood library
## inits the DJcontroller and creates in/out objects
## polls the DJcontroller and sends command to TS590
###################################################
if __name__ == '__main__':
print ("\n%s - (c) Patrick EGLOFF aka TK5EP" %(__Title))
print ("Version %s %s made in Corsica :-) \n" % (__Version, __VersionDate) )
# global variables from a config file
import config # to create a set of global variables
oldfwcw = 0 # for memorizing mode changes
oldfwfsk = 0 #
oldsh = 0 #
oldis = 0 #
oldsl = 0 #
RadioMode = ''
RadioVFO = ''
received_datas = ''
RadioIsON = 0
RITisON = 0
XITisON = 0
#########################################
# create some optional arguments for startup
# -h : help
# -v : make verbose for debugging
# -p : show COM ports
# -m : show MIDI ports
#########################################
parser = argparse.ArgumentParser()
parser.add_argument("-m","--midi", help="show available MIDI dervices",action="store_true")
parser.add_argument("-c","--comports", help="show COM ports",action="store_true")
parser.add_argument("-v","--verbose", help="increase output verbosity",action="store_true")
args = parser.parse_args()
# init ini file parser
# allow_no_value=True -> to allow adding comments without value, so no trailing =
Config = configparser.ConfigParser(allow_no_value=True)
# use start option to set debug infos, like MIDI commands generated, CAT dialog, etc...
if args.verbose: # -v --verbose
DEBUG = True
else:
DEBUG= False
#DEBUG=True # to force the debugging
inifile = 'midi2ts590.ini' # check if ini file exists and read the settings
if os.path.isfile(inifile):
ReadIniFile()
else:
print('\nConfiguration file does not exist !\nCreating it in a few seconds, then edit it to your needs if something goes wrong.\n')
sleep(3)
CreateIniFile() # if not, create it
ts590 = KwdCat() # create instance of KwdCat the Kenwood CAT library
DJ_init() # init
if args.midi: # if optional -m or --midi argument at startup
DJ_info() # lists all available MIDI devices
if args.comports: # show list of COM ports if asked as option at startup
ts590.find_ports()
#if (ts590.open_port(port=config.RadioPort)): # test opening port with default values
config.RadioRxtimeout = config.RadioRxtimeout/1000 # to convert in ms
config.RadioTxtimeout = config.RadioTxtimeout/1000 # to convert in ms
# create object
if (ts590.open_port(port=config.RadioPort,baudrate=config.RadioBaudrate,bytesize=config.RadioBytesize,stopbits=config.RadioStopbits,xonxoff=config.RadioXonXoff,rtscts=config.RadioRtsCts,dsrdtr=config.RadioDsrDtr,parity=config.RadioParity,rts=config.RadioRts,dtr=config.RadioDtr,rxtimeout=config.RadioRxtimeout,txtimeout=config.RadioTxtimeout)):
print ('Radio model :',config.RadioModel,'on',ts590.serial.port)
print ('Baudrate=',ts590.serial.baudrate,'Bits=',ts590.serial.bytesize,'Stop=',ts590.serial.stopbits,'Parity=',ts590.serial.parity)
print ("Flow controls: XOn/XOff=",ts590.serial.xonxoff,"RTS/CTS=",ts590.serial.rtscts,"DSR/DTR=",ts590.serial.dsrdtr)
print ("Lines: RTS=",ts590.serial.rts,"DTR=",ts590.serial.dtr,"RXtimeout=",ts590.serial.timeout,"TXtimeout=",ts590.serial.write_timeout,"\n")
else:
print(config.RadioPort,"not available, busy or bad setting !")
print("Below are available ports, set one in the configuration file")
ts590.find_ports() # show a list of found comports
input('\nStopping. CTRL-C to stop')
sys.exit()
if (ts590.checkradio()): # check is radio is answering
print ("Radio communication OK\n")
else: # if no answer
ts590.close_port() # close port
input('\nCTRL-C to EXIT')
sys.exit(0)
try:
if DJ_initInput(config.MidiDeviceIn): # check if device is input, not busy
Midi_In = pygame.midi.Input(config.MidiDeviceIn) # open Midi input device
print('MIDI input device ready')
if DJ_initOutput(config.MidiDeviceOut): # check output device
Midi_Out = pygame.midi.Output(config.MidiDeviceOut) # open Midi output device
print('MIDI output device ready')
except:
print("\nMidi device error, device busy or not present ?")
input('\nCTRL-C to EXIT')
sys.exit()
DJ_LedsBlink(3,0.3) # some fancy animation at startup
ts590.query('PS1',0) # send radio ON
config.RadioIsON = 1 # flag for radio ON/OFF state
DJ_LedRECORD(1) # switch LED ON
ts590.query('RG255',0) # set RF gain at MAX
AFvolume = 'AG'+ config.AFvolume.rjust(4,'0') # send AF volume at default value
ts590.query(AFvolume,0)
ChangeMode(config.RadioMode) # switch radio to default mode
sleep(0.1)
ChangeVFO(config.RadioVFO) # put radio to default VFO
if config.Radiocmd1: # if the cmd1 is set in configuration file
ts590.query(config.Radiocmd1,0) # send to radio, 0 means no echo from radio needed
if config.Radiocmd2:
ts590.query(config.Radiocmd2,0)
if config.Radiocmd3:
ts590.query(config.Radiocmd3,0)
# inits for a little animation
animation = "|/-\\" # like a turning wheel
anicount = 0 # init animation counter position
if config.RadioSniff == 1: # polling the radio by sending IF regularely IF command
polling_daemon = Thread(target=pollRadio, args=(
config.polltime,), daemon=True, name='Poll Radio') # create a thread for the radio polling
polling_daemon.start() # start the thread
stop_thread = False # flag to stop the thread by calling it with join()
if config.RadioSniff == 2: # sniff the COM data flow and extract infos
sniffer_daemon = Thread(target=SniffRadio, args=(
config.polltime,), daemon=True, name='Sniff Radio') # create a thread for the radio sniffing
sniffer_daemon.start() # start the thread
stop_thread = False # flag to stop the thread by calling it with join()
print("\nFor a 'clean' stop of this software, use CTRL-C.")
#############################
# MAIN loop
#############################
while True:
try:
#print ("type",type(IFmode))
#ts590.query('AI0',3)
#ts590.query('FV',6)
#answerVV = ts590.query('VV',13)
#print(answerVV)
#answerFA = ts590.query('FA',13)
#print(ts590.ReadCmdFAFB(answerFA))
#xianswer=ts590.query('XI',17)
#print(ts590.ReadCmdXI(xianswer))
#print(ts590.ConvertMode(3))
#print(config.runningMode) # this is mode set on radio
#print(config.RadioMode) # this is mode set by sw
if Midi_In.poll(): # check if something is present on the MIDI input device
DJ_scan() # if yes check what it is
print(animation[anicount],end='\r') # show activity with a small animation at each midi device poll
anicount = (anicount + 1)%4 # next animation position, reset the number at 4 to avoid overflow
sleep(0.00001) # some delay in needed to not have CPU consumption !
except KeyboardInterrupt: # if we press CTRL-C to interrupt the program
if config.RadioSniff == 1:
stop_thread = True # set the flag to kill the thread
polling_daemon.join() # join the thread to stop it
if config.RadioSniff == 2:
stop_thread = True # set the flag to kill the thread
sniffer_daemon.join() # join the thread to stop it
ts590.close_port() # close radio port
pygame.midi.quit()
print('All threads killed, exiting in 2s')
sleep(2)
sys.exit()