-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyer.py
1252 lines (911 loc) · 41.6 KB
/
keyer.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
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# esp32 Version 24.12.2022 save data in filesystem
# esp32 Version 25.07.2022 poti wpm
# esp32 Version 22.07.2022
# esp32 Version 11.07.2022
#
# Iambic keyer rp2040 include simple transceiver on 40m
#
# Copyright (C) 2022 dl2dbg
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser 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
# Lesser 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 <https://www.gnu.org/licenses/>.
from machine import Pin, Timer, PWM, ADC, I2C
import ssd1306
import writer
import framebuf
import freesans20
import machine
from machine import TouchPad
from machine import deepsleep
from time import sleep
import esp32
#from rp2 import PIO, StateMachine, asm_pio
import utime # utime is the micropython brother of time
import time
import ujson
import ubluetooth
ble_msg = ""
is_ble_connected = False
class ESP32_BLE():
def __init__(self, name):
# Create internal objects for the onboard LED
# blinking when no BLE device is connected
# stable ON when connected
self.led = Pin(2, Pin.OUT)
self.timer1 = Timer(0)
self.name = name
self.ble = ubluetooth.BLE()
self.ble.active(True)
self.disconnected()
self.ble.irq(self.ble_irq)
self.register()
self.advertiser()
def connected(self):
global is_ble_connected
is_ble_connected = True
self.led.value(1)
self.timer1.deinit()
def disconnected(self):
global is_ble_connected
is_ble_connected = False
self.timer1.init(period=100, mode=Timer.PERIODIC, callback=lambda t: self.led.value(not self.led.value()))
def ble_irq(self, event, data):
global ble_msg
if event == 1: #_IRQ_CENTRAL_CONNECT:
# A central has connected to this peripheral
self.connected()
elif event == 2: #_IRQ_CENTRAL_DISCONNECT:
# A central has disconnected from this peripheral.
self.advertiser()
self.disconnected()
elif event == 3: #_IRQ_GATTS_WRITE:
# A client has written to this characteristic or descriptor.
buffer = self.ble.gatts_read(self.rx)
ble_msg = buffer.decode('UTF-8').strip()
def register(self):
# Nordic UART Service (NUS)
NUS_UUID = '6E400001-B5A3-F393-E0A9-E50E24DCCA9E'
RX_UUID = '6E400002-B5A3-F393-E0A9-E50E24DCCA9E'
TX_UUID = '6E400003-B5A3-F393-E0A9-E50E24DCCA9E'
BLE_NUS = ubluetooth.UUID(NUS_UUID)
BLE_RX = (ubluetooth.UUID(RX_UUID), ubluetooth.FLAG_WRITE)
BLE_TX = (ubluetooth.UUID(TX_UUID), ubluetooth.FLAG_NOTIFY)
BLE_UART = (BLE_NUS, (BLE_TX, BLE_RX,))
SERVICES = (BLE_UART, )
((self.tx, self.rx,), ) = self.ble.gatts_register_services(SERVICES)
def send(self, data):
self.ble.gatts_notify(0, self.tx, data + '\n')
def advertiser(self):
name = bytes(self.name, 'UTF-8')
adv_data = bytearray('\x02\x01\x02') + bytearray((len(name) + 1, 0x09)) + name
self.ble.gap_advertise(100, adv_data)
print(adv_data)
print("\r\n")
# adv_data
# raw: 0x02010209094553503332424C45
# b'\x02\x01\x02\t\tESP32BLE'
#
# 0x02 - General discoverable mode
# 0x01 - AD Type = 0x01
# 0x02 - value = 0x02
# https://jimmywongiot.com/2019/08/13/advertising-payload-format-on-ble/
# https://docs.silabs.com/bluetooth/latest/general/adv-and-scanning/bluetooth-adv-data-basics
def print_ble(self,data,inv):
print(data)
oled.fill(0)
oled.invert(inv)
oled.text(data, 0, 0)
oled.show()
if is_ble_connected:
self.send(data)
def print_big(self,data,inv):
oled.fill(0)
#oled.text("test",5,5)
font_writer = writer.Writer(oled, freesans20,False)
font_writer.set_textpos(5,20)
font_writer.printstring(data)
oled.show()
ble = ESP32_BLE("ESP32BLE_CW")
#xiaoKey - a computer connected iambic keyer
# Copyright 2022 Mark Woodworth (AC9YW)
#https://github.com/MarkWoodworth/xiaokey/blob/master/code/code.py
# setup encode and decode
encodings = {}
def encode(char):
global encodings
if char in encodings:
return encodings[char]
elif char.lower() in encodings:
return encodings[char.lower()]
else:
return ''
decodings = {}
def decode(char):
global decodings
if char in decodings:
return decodings[char]
else:
return '('+char+'?)'
def MAP(pattern,letter):
decodings[pattern] = letter
encodings[letter ] = pattern
MAP('.-' ,'a') ; MAP('-...' ,'b') ; MAP('-.-.' ,'c') ; MAP('-..' ,'d') ; MAP('.' ,'e')
MAP('..-.' ,'f') ; MAP('--.' ,'g') ; MAP('....' ,'h') ; MAP('..' ,'i') ; MAP('.---' ,'j')
MAP('-.-' ,'k') ; MAP('.-..' ,'l') ; MAP('--' ,'m') ; MAP('-.' ,'n') ; MAP('---' ,'o')
MAP('.--.' ,'p') ; MAP('--.-' ,'q') ; MAP('.-.' ,'r') ; MAP('...' ,'s') ; MAP('-' ,'t')
MAP('..-' ,'u') ; MAP('...-' ,'v') ; MAP('.--' ,'w') ; MAP('-..-' ,'x') ; MAP('-.--' ,'y')
MAP('--..' ,'z')
MAP('.----','1') ; MAP('..---','2') ; MAP('...--','3') ; MAP('....-','4') ; MAP('.....','5')
MAP('-....','6') ; MAP('--...','7') ; MAP('---..','8') ; MAP('----.','9') ; MAP('-----','0')
MAP('.-.-.-','.') # period
MAP('--..--',',') # comma
MAP('..--..','?') # question mark
MAP('-...-', '=') # equals, also /BT separator
MAP('-....-','-') # hyphen
MAP('-..-.', '/') # forward slash
MAP('.--.-.','@') # at sign
class poti_adc():
'''
24.7.2022
simple pin read poti
#
'''
def __init__(self,poti_pin):
self.pot = ADC(Pin(poti_pin))
self.pot.atten(ADC.ATTN_11DB)
def wpm(self):
self.pot_value = self.pot.read()
return(self.pot_value//100+5)
class watch_ideal():
'''
20.11.2022 watch ideal time
'''
def __init__(self):
self.ideal_start = time.time()
def update(self):
self.ideal_start = time.time()
def diff(self):
return (time.time() - self.ideal_start)
class tx_opt():
'''
3.3.2022
simple pin on/off for tx optocopler
#
'''
def __init__(self,tx_pin):
self.tx_opt_pin = Pin(tx_pin,Pin.OUT)
self.on_off = 1
#self.tx_opt_pin = Pin(10, Pin.OUT)
def on(self):
self.on_off = 1
def off(self):
self.on_off = 0
def send(self,state):
if self.on_off == 1:
self.tx_opt_pin.value(state)
class command_button():
'''
6.3.2022 button for Command request
7.8.2022 touch key
'''
def __init__(self,pin_button,led1,led2):
#GPIO welcher als PWM Pin benutzt werden soll
#self.button = Pin(pin_button,Pin.IN,Pin.PULL_UP)
self.led1 = Pin(led1, Pin.OUT)
self.led2 = Pin(led2, Pin.OUT)
self.button_save = 0
self.button_save_wpm = 1
self.btimer = 0 # timer for debounce
self.comannd_state = 1 # im keyer mode
self.comannd_state_wpm = 0 # im keyer mode
self.comannd_state_wpm_cq = 0 # im keyer mode
self.touchcommand=TouchPad(Pin(27))
self.touchwpm=TouchPad(Pin(14))
#
# self.save_tfreq = cwt.tonfreq()
# self.ton_freq_command = cwt.tonfreq_command()
def state_key_command(self):
#print("..",self.touchcommand.read())
#print("..",self.touchwpm.read())
#sleep(0.5)
try:
self.touch_val = self.touchcommand.read()
# ggf. Fehler abfangen
except ValueError:
#print("..",self.touchcommand.read())
return(1)
#print("ValueError while reading touch_pin")
#print(self.touch_val)
if (self.touch_val <= 200):
#print("..",self.touchcommand.read())
return(0)
return(1)
#return(self.dit_key.value())
def state_key_wpm(self):
#print("..",self.touchwpm.read())
#sleep(0.5)
try:
self.touch_val1 = self.touchwpm.read()
# ggf. Fehler abfangen
except ValueError:
#print("..",self.touchcommand.read())
return(1)
#print("ValueError while reading touch_pin")
#print(self.touch_val)
if (self.touch_val1 <= 200):
#print("..",self.touchcommand.read())
return(0)
return(1)
#return(self.dit_key.value())
def button_press(self):
#print("command key: ",self.state_key_command())
return(self.state_key_command())
def button_command_off(self):
print("command off")
self.comannd_state = 0
self.led1(self.comannd_state)
self.led2(self.comannd_state)
ble.print_ble("command:off",self.comannd_state)
iambic.write_jsondata() # save parameter afer change
iambic.char = ""
iambic.word = ""
#text2cw("e")
def button_state(self):
if self.state_key_command() == 0 and self.button_save == 1: # 1 0 ->button is press
#utime.ticks_ms()
#self.btimer = utime.ticks_ms()
self.button_save = 0
elif self.state_key_command() == 1 and self.button_save == 0 : #0 0 ->button IS press
#elif self.state_key_command() == 1 and self.button_save == 0 and utime.ticks_ms() > self.btimer + 10 : #0 0 ->button IS press
self.button_save = 1
self.led1(not self.comannd_state)
self.comannd_state = not self.comannd_state
ble.print_ble("Command:"+str(self.comannd_state),self.comannd_state)
if not self.comannd_state:
self.button_command_off()
self.comannd_state_wpm = 0
if self.comannd_state:
# in command mode immer ton on
cwt.set2cton()
cwt.onoff(True)
else :
cwt.set2ton()
cwt.onoff(iambic.sidetone_enable)
#text2cw("e") #keine Peep am ende :-)
return(self.button_save)
def button_state_wpm(self):
if self.state_key_wpm() == 0 and self.button_save_wpm == 1: # 1 0 ->button is press
#utime.ticks_ms()
#self.btimer = utime.ticks_ms()
self.button_save_wpm = 0
print("WPM press 0")
elif self.state_key_wpm() == 1 and self.button_save_wpm == 0 : #0 0 ->button IS press
self.button_save_wpm = 1
print("WPM press 1")
self.comannd_state_wpm_cq = not self.comannd_state_wpm_cq # toggle zwischen wpm and cq call
print("WPM_CW",self.comannd_state_wpm_cq)
self.led2(not self.comannd_state)
#self.comannd_state_wpm = not self.comannd_state_wpm
self.comannd_state_wpm = 1
#self.comannd_state = 1
if self.comannd_state_wpm_cq :
#iambic.wpm = 18 # default wert bei tasten druck
cw_time.set_wpm(iambic.wpm)
ble.print_ble("Command_mode_wpm:"+str(self.comannd_state_wpm),1)
ble.print_big("WPM: "+str(iambic.wpm),1)
cb.comannd_state = 1
#text2cw("wpm") #ready
else:
#ble.print_ble("cq cq dl2dbg",1)
ble.print_big("cq",1)
return(self.button_save_wpm)
class watch_ideal():
'''
20.11.2022 watch ideal time
'''
def __init__(self):
self.ideal_start = time.time()
def update(self):
self.ideal_start = time.time()
def diff(self):
return time.time() - self.ideal_start
class cw_sound():
'''
6.3.2020 simple sound with pwm
'''
def __init__(self,pin=22):
#GPIO welcher als PWM Pin benutzt werden soll
self.pwm_ton = PWM(Pin(pin))
#eine Frequenz von 1000hz also 1kHz
self.freq = 600
self.Ton_freq_command = 1500
self.pwm_ton.freq(self.freq)
self.cwvolum = 300 #30000 laut
self.on_off = 1
def set_tonfreq(self,freq):
self.freq = freq
self.pwm_ton.freq(self.freq)##
def set2ton(self):
self.pwm_ton.freq(self.freq)
def set2cton(self):
self.pwm_ton.freq(self.Ton_freq_command)
def tonfreq(self):
return self.freq
def tonfreq_command(self):
return self.Ton_freq_command
def volume(self,volume):
print("set volume")
self.cwvolum = volume
def tone(self,on):
if self.on_off == 1:
if on:
self.pwm_ton.duty_u16(self.cwvolum)
else:
self.pwm_ton.duty_u16(1)
def onoff(self,state):
self.on_off = state
def cw(state):
cwt.tone(state)
txopt.send(state)
class cw_timing():
def __init__(self,wpm=18):
self.wpm = wpm
# timing
def dit_time(self):
self.PARIS = 50
return 60.0 / self.wpm / self.PARIS *1000 ## mili sekunden
def set_wpm(self, wpm):
self.wpm = wpm
# decode iambic b paddles
class Iambic:
"""
Command
a -> Iambic Mode A
b -> Iambic Mode B
m -> request Iambic Mode A/B
? -> request value of ...
/ -> schow value all
i -> TX_opt enable(on) disable(off)
o -> Sidetone toggle (on) (off)
f -> adjust sidetone frequenz
v -> adjust sidetone volume 1-100
q -> adjust qrg of tx
w -> adjust WPM (Word per minute)
t -> tune mode, end with command mode
s -> save parameter to file
x -> exit Command mode
tranceiver is toggle pin of rp2040
def tx_toggle():
wrap_target()
set(pins, 1) [1]
set(pins, 0) [1]
wrap()
"""
def __init__(self,dit_key,dah_key):
self.dit_key = Pin(dit_key,Pin.IN,Pin.PULL_UP)
self.dah_key = Pin(dah_key,Pin.IN,Pin.PULL_UP)
self.touchdit=TouchPad(Pin(32))
self.touchdah=TouchPad(Pin(33))
self.dit = False ; self.dah = False
self.ktimer = 0
self.ktimer_end = 0
self.in_char = True
self.in_word = True
self.deep_sleep = False
self.Idle_time = 2000000
self.deep_sleep_timer = utime.ticks_ms() + self.Idle_time
self.char = ""
self.word = ""
self.IDLE=0; self.CHK_DIT=1; self.CHK_DAH=2; self.KEYED_PREP=3; self.KEYED=4; self.INTER_ELEMENT=5
self.keyerState = self.IDLE
self.keyerControl = 0 #keyerControl = IAMBICB; // Or 0 for IAMBICA
# keyerControl bit definitions
self.DIT_L = 0x01 # Dit latch
self.DAH_L = 0x02 #Dah latch
self.DIT_PROC = 0x04 # Dit is being processed
self.PDLSWAP = 0x08 # 0 for normal, 1 for swap
#self.iambic_mode = 0x10 # 0 for Iambic A, 1 for Iambic B
self.LOW = 0
self.HIGH = 1
self.tune = 0 # transmit
self.transmit_tune= 0
self.adj_sidetone = 0
self.adj_wpm = 0
self.adj_qrg = 0
self.adj_sidetone_volume = 0
# this variable are default an will be overwrite with the json file
#
self.iambic_mode = 0x10 # 0 for Iambic A, 1 for Iambic B
self.wpm = 18
self.wpm_m = 0
self.cq = 0
self.cq_liste =["","cq cq dl2dbg","dl2dbg","cq cq test dl2dbg","cq","uli","cq cq"]
self.tx_enable = 0
self.txt_enable = 0
self.sidetone_enable = 1
self.sidetone_freq = 700 #
self.sidetone_volume = 10 # range 1,100 * 200 -> 2000 #30000 laut
#
self.f_liste =[7017540,7005250,7011390,7036050,7029870,7023700]
self.f_liste.sort()
self.qrg_marke = 0
self.qrg = self.f_liste[self.qrg_marke]
self.request = 0 # request of parameters
#--------------
self.iambic_data = {} # create date store
self.read_jsondata()
print("....read button.......default data")
if cb.button_press() == 0: # not press "0" -> json date read,and init, if "0" use factory setting
print("**** default data")
#self.read_jsondata()
self.init_iambic_data()
def set_data(self,key,value):
#self.iambic_data
self.key=key
self.value= value
self.iambic_data[self.key]=self.value
# print ('setting',menu_data)
def write_data2file(self):
#self.iambic_data
self.json_string = ujson.dumps(self.iambic_data)
with open('json_iambic.json', 'w') as outfile:
ujson.dump(self.json_string, outfile)
def read_jsondata(self):
with open('json_iambic.json') as json_file:
self.data = ujson.load(json_file)
self.iambic_data = ujson.loads(self.data)
def write_jsondata(self): # write new json file
print("write_jsondata")
self.set_data("iambic_mode",self.iambic_mode) # transmit
self.set_data("wpm",self.wpm)
self.set_data("sidetone_enable",self.sidetone_enable)
self.set_data("sidetone_freq",self.sidetone_freq)
self.set_data("sidetone_volume",self.sidetone_volume)
self.set_data("tx_enamble",self.tx_enable)
self.set_data("txt_emable",self.txt_enable)
self.write_data2file()
def print_parameter(self): # write new json file
#print("write_jsondata")
ble.print_ble("---Paramter",0)
ble.print_ble("iambic_mode :" + str(self.iambic_mode),0) # transmit
ble.print_ble("wpm :" +str(self.wpm),0)
ble.print_ble("sidetone_enable :" + str(self.sidetone_enable),0)
ble.print_ble("sidetone_freq :" +str(self.sidetone_freq),0)
ble.print_ble("sidetone_volume :" +str(self.sidetone_volume),0)
ble.print_ble("tx_enamble :" +str(self.tx_enable),0)
ble.print_ble("txt_emable :" +str(self.txt_enable),0)
ble.print_ble("",0)
def init_iambic_data(self): # is only use at the begin to create new json file
self.iambic_mode = self.iambic_data["iambic_mode"]
self.wpm = self.iambic_data["wpm"]
self.sidetone_enable = self.iambic_data["sidetone_enable"]
self.sidetone_freq = self.iambic_data["sidetone_freq"]
self.sidetone_volume = self.iambic_data["sidetone_volume"]
self.tx_enamble = self.iambic_data["tx_enamble"]
self.txt_emable = self.iambic_data["txt_emable"]
# set extern Parameter
cw_time.set_wpm(self.wpm)
cwt.set_tonfreq(self.sidetone_freq)
cwt.volume(self.sidetone_volume*200)
cwt.onoff(self.sidetone_enable)
#---------------
def state_key_dit(self):
try:
self.touch_val = self.touchdit.read()
# ggf. Fehler abfangen
except ValueError:
return(self.HIGH)
#print("ValueError while reading touch_pin")
#print(self.touch_val)
if (self.touch_val <= 200):
return(self.LOW)
#return(self.dit_key.value())
def state_key_dah(self):
try:
self.touch_val = self.touchdah.read()
# ggf. Fehler abfangen
except ValueError:
return(self.HIGH)
#print("ValueError while reading touch_pin")
#print(self.touch_val)
if (self.touch_val <= 200):
return(self.LOW)
#return(self.dit_key.value())
def update_PaddleLatch(self):
if (self.state_key_dit() == self.LOW):
self.keyerControl |= self.DIT_L
if (self.state_key_dah() == self.LOW):
self.keyerControl |= self.DAH_L
def cycle(self):
#utime.sleep(0.3)
cb.button_state() ## Comand button abfragen
cb.button_state_wpm() ## Comand button WPM abfragen
# clear display when ideal
if w_ideal.diff() >= 3 and cb.comannd_state == 0:
ble.print_big("",0)
w_ideal.update()
if cb.comannd_state == 1 : # "1" ->comand mode
if self.tune == 1: # begin tune
self.keyerSate = self.IDLE
if self.state_key_dah() == self.LOW: # transmit on
self.transmit_tune= 1
cw(1)
elif self.state_key_dit() == self.LOW: #transmit off
self.transmit_tune= 0
cw(0)
return
elif self.adj_sidetone == 1: # begin tune
self.keyerSate = self.IDLE
if self.state_key_dah() == self.LOW: # transmit on
if self.sidetone_freq > 2000:
text2cw("max")
else:
self.sidetone_freq =self.sidetone_freq +10
cwt.set_tonfreq(self.sidetone_freq) # change the Freq
ble.print_ble("sidetone_freq:"+str(self.sidetone_freq),cb.comannd_state)
play("-")
elif self.state_key_dit() == self.LOW: #transmit off
if self.sidetone_freq < 50:
text2cw("min")
else:
self.sidetone_freq = self.sidetone_freq -10
cwt.set_tonfreq(self.sidetone_freq)
ble.print_ble("sidetone_freq:"+str(self.sidetone_freq),cb.comannd_state)
play(".")
return
elif self.adj_sidetone_volume == 1: # adjust sidetone volume
self.keyerSate = self.IDLE
self.tx_enable = 1
#tx.on()
if self.state_key_dah() == self.LOW: # transmit on
if self.sidetone_volume >= 100:
text2cw("max")
else:
self.sidetone_volume = self.sidetone_volume + 1
cwt.volume(self.sidetone_volume*200)
ble.print_ble("sidetone:"+str(self.sidetone_volume*200),cb.comannd_state)
#print(self.sidetone_volume)
play("-")
elif self.state_key_dit() == self.LOW: #transmit off
if self.sidetone_volume <= 0:
text2cw("min")
else:
self.sidetone_volume = self.sidetone_volume - 1
cwt.volume(self.sidetone_volume*200)
ble.print_ble("sidetone:"+str(self.sidetone_volume*200),cb.comannd_state)
#print(self.sidetone_volume)
play(".")
return
elif self.adj_wpm == 1 or cb.comannd_state_wpm == 1 : # begin tune
self.keyerSate = self.IDLE
if cb.comannd_state_wpm == 1 : txopt.off()
if cb.comannd_state_wpm_cq :
# wpm mode
if self.state_key_dah() == self.LOW: # transmit on
self.wpm = self.wpm+1
cw_time.set_wpm(self.wpm)
ble.print_big("WPM: "+str(self.wpm),cb.comannd_state)
play(".")
elif self.state_key_dit() == self.LOW: #transmit off
if self.wpm >= 10:
self.wpm = self.wpm-1
cw_time.set_wpm(self.wpm)
ble.print_big("WPM: "+str(self.wpm),cb.comannd_state)
play(".")
else:
# cq mode
if self.state_key_dah() == self.LOW: # transmit on
if self.cq <= 4:
self.cq = self.cq+1
else:
self.cq = 0
ble.print_ble(""+ self.cq_liste[self.cq],cb.comannd_state)
play(".")
elif self.state_key_dit() == self.LOW: #transmit off
txopt.on()
text2cw(self.cq_liste[self.cq])
txopt.off()
play(".")
txopt.on()
return
elif self.adj_qrg == 1: # begin tune
self.keyerSate = self.IDLE
if self.state_key_dah() == self.LOW: # transmit on
if self.qrg_marke >= len(self.f_liste)-1:
text2cw("max")
else:
self.qrg_marke = self.qrg_marke + 1
#tx.set_freq(self.f_liste[self.qrg_marke])
print("....",self.f_liste[self.qrg_marke])
play("-")
elif self.state_key_dit() == self.LOW: #transmit off
if self.qrg_marke <= 0:
text2cw("min")
else:
self.qrg_marke = self.qrg_marke - 1
#tx.set_freq(self.f_liste[self.qrg_marke])
print("....",self.f_liste[self.qrg_marke])
play(".")
return
else: # wenn commad mode ende dann auch tune :-)
self.tune = 0
self.adj_qrg = 0
self.adj_sidetone = 0
self.adj_wpm = 0
self.adj_sidetone_volume = 0
self.request = 0
if self.keyerState == self.IDLE:
# Wait for direct or latched paddle press
#word grenze erkennen
if utime.ticks_ms() > (self.deep_sleep_timer + self.Idle_time):
if not self.deep_sleep:
self.deep_sleep = True
print(utime.ticks_ms(),self.deep_sleep_timer)
wake = Pin(14, mode = Pin.IN) #wpm Button
touch = TouchPad(wake)
touch.config(500)
self.touchdit.config(500)
esp32.wake_on_touch(True)
print("deep sleep")
if utime.ticks_ms() > (self.ktimer_end + cw_time.dit_time()*4.5):
if self.in_word:
self.in_word = False
#print(utime.ticks_ms(),self.ktimer_end)
#print(self.word)
ble.print_ble(self.word,0)
self.word = ""
if utime.ticks_ms() > (self.ktimer_end + cw_time.dit_time()*1.5):
if self.in_char:
self.in_char = False
#print(utime.ticks_ms(),self.ktimer_end)
#print("char",self.char)
self.deep_sleep = False
self.deep_sleep_timer = utime.ticks_ms() #timmer aktualisieren
self.word = self.word+decode(self.char)
#if cb.comannd_stare_wpm == 1:
if cb.comannd_state == 1: # "1" ->comand mode
Char = decode(self.char)
# comand mode ----------------
if Char == "i" : # TX enable(on) disable(off)
ble.print_ble("i tx enable",cb.comannd_state)
if self.request == 1:
if self.tx_enable :
text2cw("on")
else:
text2cw("off")
else:
self.tx_enable = not self.tx_enable
if self.tx_enable:
txopt.on()
ble.print_ble("tx_enable:on",cb.comannd_state)
text2cw("on")
else:
text2cw("off")
ble.print_ble("tx_enable:off",cb.comannd_state)
txopt.off()
#print("Transmit", self.tx_enable)
cb.button_command_off()
elif Char == "j" : # TX opt enable(on) disable(off)
ble.print_ble("j tx enable",cb.comannd_state)
if self.request == 1:
if self.txt_enable :
text2cw("on")
else:
text2cw("off")
else:
self.txt_enable = not self.txt_enable
if self.txt_enable:
#tx.on()
ble.print_ble("tx_op_enable:on",cb.comannd_state)
text2cw("on")
else:
text2cw("off")
ble.print_ble("tx_op_enable:off",cb.comannd_state)
#tx.off()
#print("Transmit", self.txt_enable)
cb.button_command_off()
elif Char == "o" : # TX enable(on) disable(off)
ble.print_ble("o sidetone on/off",cb.comannd_state)
if self.request == 1:
if self.sidetone_enable :
text2cw("on")
else:
text2cw("off")
else:
self.sidetone_enable = not self.sidetone_enable