-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
1631 lines (1196 loc) · 50.5 KB
/
main.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
VERSION = "0.967 13.1.24" # ticker change to scolling display
#VERSION = "0.966 12.1.24" # remove deep sleep mode
#VERSION = "0.965 23.12.23" # ticker display and motd, new comand "d" decoder verbose on/off
#VERSION = "0.964 20.12.23" #speed controll command r,l,h
# 5.12.23 version 0.963 test ok :-)
# 4.12.23 version 0.962 weighting
# 2.12.23 version 0.961 wpm issu is solve
# 28.11.23 version 0.95
# 23.11.23 self.cq_liste and threshold_key in json file include
# 21.11.23 ready for new version MicroPython v1.21.0 on 2023-10-05
# bluetooth module gelöscht, lief mit der neuen version MicroPython v1.21.0 on 2023-10-05; fehler nicht gefunden??
#
# 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
# esp32 version 2.4.2023 text2cw break eingebaut
#
#
#
# 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 network, usocket, utime, ntptime
import ssd1306
import writer
# import framebuf
import freesans20 # FreeSans Font
import machine
from machine import TouchPad
from time import sleep
import esp32
import ubluetooth
import utime # utime is the micropython brother of time
import time
import ujson
class Ticker():
def __init__(self):
self.basis_string = ""
def clear_buffer(self):
self.basis_string = ""
def fifo_buffer(self, txt):
self.basis_string = self.basis_string + str(txt)
# Länge auf 10 Zeichen begrenzen, indem am Anfang abgeschnitten wird
if len(self.basis_string) >= 45:
self.basis_string = self.basis_string[-45:]
return self.basis_string
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()
print("connect")
elif event == 2: # _IRQ_CENTRAL_DISCONNECT:
# A central has disconnected from this peripheral.
self.advertiser()
self.disconnected()
print("disconnectec")
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()
print("read")
print(ble_msg)
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):
if is_ble_connected:
self.ble.gatts_notify(0, self.tx, data + '\n')
def advertiser(self):
name = bytes(self.name, 'UTF-8')
adv_data = self.ble.gap_advertise(100, bytearray('\x02\x01\x02', 'utf-8') + bytearray((len(name) + 1, 0x09),
'utf-8') + 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
class ESP32_BLE_pass():
def __init__(self, name):
pass
def send(self, data):
pass
##################################################
class CONSOLE_Print():
# only pint on console
def print_smal(self, data, inv):
if inv != 0:
print(f'\033[31m') # print invers "red"
print(data)
if inv != 0:
print(f'\033[0m')
def print_big(self, data, inv):
if inv != 0:
print(f'\033[31m')
print(data)
if inv != 0:
print(f'\033[0m')
def print_dark(self): # dark display
pass
def print_ticker_oled(self,txt,inv):
pass
def print_ticker_no_oled(self, data, inv):
if inv != 0:
print(f'\033[31m') # print invers "red"
print(data)
#ble.send(data)
if inv != 0:
print(f'\033[0m')
##################################################
class OLED_Print():
def __init__(self):
i2c = machine.I2C(1, scl=machine.Pin(18), sda=machine.Pin(19), freq=400000) # esp32
oled_width = 128
oled_height = 32
self.oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
self.oled.rotate(0)
def print_smal(self, data, inv):
if inv != 0:
print(f'\033[31m') # print invers "red"
print(data)
ble.send(data)
self.oled.fill(0)
self.oled.invert(inv)
self.oled.text(data, 0, 0)
self.oled.show()
if inv != 0:
print(f'\033[0m')
def print_dark(self): # dark display
self.oled.fill(0)
self.oled.show()
def print_big(self, data, inv):
if inv != 0:
print(f'\033[31m')
print(data)
ble.send(data)
self.oled.fill(0)
self.oled.invert(inv)
# oled.text("test",5,5)
font_writer = writer.Writer(self.oled, freesans20, False)
font_writer.set_textpos(5, 20)
font_writer.printstring(data)
self.oled.show()
if inv != 0:
print(f'\033[0m')
def print_ticker_oled_old(self,txt,inv):
self.oled.fill(0) # Lösche den Bildschirm
self.oled.rotate(0)
self.oled.invert(inv)
self.oled.text(tik.fifo_buffer(txt) ,1,1)
self.oled.show()
def print_ticker_oled(self,txt,inv):
buffer = tik.fifo_buffer(txt)
characters_per_line = 15
# Berechne die Anzahl der Zeilen basierend auf der Buffergröße
num_lines = (len(buffer) - 1) // characters_per_line + 1
self.oled.fill(0) # Lösche den Bildschirm
self.oled.rotate(0)
for i in range(num_lines):
start_index = i * characters_per_line
end_index = (i + 1) * characters_per_line
line_content = buffer[start_index:end_index]
self.oled.text(line_content ,1,i*10)
self.oled.show()
def print_ticker_no_oled(self, data, inv):
if inv != 0:
print(f'\033[31m') # print invers "red"
print(data)
ble.send(data)
if inv != 0:
print(f'\033[0m')
##################################################
class BLE_Print():
def print_smal(self, data, inv):
if inv != 0:
print(f'\033[31m') # print invers "red"
print(data)
ble.send(data)
if inv != 0:
print(f'\033[0m')
def print_big(self, data, inv):
if inv != 0:
print(f'\033[31m') # print invers "red"
print(data)
ble.send(data)
if inv != 0:
print(f'\033[0m')
def print_dark(self): # dark display
pass
def print_ticker_oled(self,txt,inv):
pass
def print_ticker_no_oled(self, data, inv):
if inv != 0:
print(f'\033[31m') # print invers "red"
print(data)
ble.send(data)
if inv != 0:
print(f'\033[0m')
#
# 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,verbose):
global decodings
if char in decodings:
return decodings[char]
else:
if verbose:
return '(' + char + '?)'
else:
return '?'
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
MAP('-.-.-', 'KA') # Spruchanfang
MAP('.-.-.', 'AR') # Spruchende
MAP('...-.-', 'SK') # Verkehrende
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
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, tcommand, twpm, led1, led2):
self.led1 = Pin(led1, Pin.OUT)
self.led2 = Pin(led2, Pin.OUT)
self.button_save = 0
self.button_short_command_save = 1
self.btimer = 0 # timer for debounce
self.comannd_state = 1 # im keyer mode
self.threshold_key_command = 201 # hard codet not im json file
self.state = 0
self.touchcommand = TouchPad(Pin(tcommand))
self.touchwpm = TouchPad(Pin(twpm))
#
# self.save_tfreq = cwt.tonfreq()
# self.ton_freq_command = cwt.tonfreq_command()
def state_key_command(self):
try:
self.touch_val = self.touchcommand.read()
# ggf. Fehler abfangen
except ValueError:
return (1)
if (self.touch_val <= self.threshold_key_command):
return (0)
return (1)
def state_key_short_command(self):
try:
self.touch_val1 = self.touchwpm.read()
except ValueError:
return (1)
if (self.touch_val1 <= self.threshold_key_command):
return (0)
return (1)
def button_press(self):
return (self.state_key_command())
def button_command_off(self):
# command and short command zurüchstezen auf 0
self.comannd_state = 0
self.short_c_state = 0 # short comannd state
self.led1(self.comannd_state)
self.led2(self.comannd_state)
iambic.char = "" # variablem zurücksetzen
iambic.word = ""
self.transmit_tune = 0 # dem tunemode sauber ausschalten
cw(0)
if iambic.tx_enable:
txopt.on()
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
oe.print_smal("Command:" + str(self.comannd_state), self.comannd_state)
beep(".")
if not self.comannd_state:
self.button_command_off()
if self.comannd_state:
# in command mode immer ton on
cwt.set2cton() # set command tone
cwt.onoff(True)
txopt.off()
else:
cwt.set2ton() # set side tone
cwt.onoff(iambic.sidetone_enable)
txopt.on()
print("tx_en:", iambic.tx_enable)
if iambic.tx_enable:
txopt.on()
return (self.button_save)
def button_state_short_command(self): # 24.11.23 new code
max_sate = 2 # in use (0,1,2)
# 1/0 wechsel der Taste abfragebń
if self.state_key_short_command() == 0 and self.button_short_command_save == 1: # 1 0 ->button is press
self.button_short_command_save = 0
elif self.state_key_short_command() == 1 and self.button_short_command_save == 0: # 0 0 ->button IS press
self.button_short_command_save = 1
print("WPM press 0")
# taste wurde gedrückt abhängig vom state aktion veranlassen
if self.short_c_state == max_sate:
self.short_c_state = 0
self.short_c_state += 1
if self.short_c_state == 1:
oe.print_big("cq text...", 1)
cb.comannd_state = 1
elif self.short_c_state == 2:
cw_time.set_wpm(iambic.wpm)
oe.print_big("WPM: " + str(iambic.wpm), 1)
cb.comannd_state = 1
elif self.short_c_state == 3:
oe.print_big("state 3", 1)
elif self.short_c_state == 3:
oe.print_big("state 3", 1)
class watch_ideal():
'''
20.11.2022 watch ideal time
# clear display when ideal
'''
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 = 1000
self.pwm_ton.freq(self.freq)
self.cwvolum = 300 # 30000 laut
self.on_off = 1
self.tone(0) # init now sound
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.set2ton()
self.pwm_ton.duty_u16(self.cwvolum)
else:
self.pwm_ton.duty_u16(1)
def tonec(self, on): # command
if on:
self.set2cton()
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) # Beep and TX
txopt.send(state)
def cw_beep(state): # only Beep
cwt.tonec(state)
class cw_timing(): # cw timing wird als eigene classe verwaltet, kann daher auch ohne IAMBIC class eingesetzt werden
def __init__(self, wpm=18,weight = 50,ration = 3):
self.wpm_t = wpm # wpmt local
self.weighting_t = weight
self.ratio_t = ration
self.DOTtime = 60.0 # wird durch calk_dit_time gesetzt
self.pDOTtime = 60.0 # wird durch calk_dit_time gesetzt
self.calk_dit_time()
# timing
def calk_dit_time(self): #aktive
#self.DOTtime_norm = 60 / (50 * self.wpm_t) # sekunden 20 wpm seems to be about 0.1 sec 0.06
# umrechnung DOTime auf Paris normiert ration_sign 1/3 weight_sign 50%
#(dithc, ditlc, dathc, sum) Paris
#(10, 28, 12, 50)
#self.paris_time = DOTtime * (1 / 100 * w * dith + 1 / 100 * (100 - w) * ditl + 1 / 100 * w * dah * r)
self.PARIS = 50
self.DOTtime_norm = 60.0 / self.wpm_t / self.PARIS * 1000 ## mili sekunden
self.paris_time = self.DOTtime_norm * (1 / 100 * 50 * 10 + 1 / 100 * (100 - 50) * 28 + 1 / 100 * 50 * 12/3 * 3)
#calulation of normalized dotime
self.nDOTtime = self.paris_time / (1 / 100 * self.weighting_t * 10 + 1 / 100 * (100 - self.weighting_t) * 28 + 1 / 100 * self.weighting_t * 12/3 * self.ratio_t)
self.DOTtime = self.nDOTtime / 50 * (self.weighting_t)
self.pDOTtime = self.nDOTtime / 50 * (100-self.weighting_t)
#print(f"DOTtime:{self.DOTtime}, {self.pDOTtime}")
return
def dit_time(self): #aktive
return self.DOTtime
def pdit_time(self): #pasive
return self.pDOTtime
def set_wpm(self, wpm):
self.wpm_t = wpm
self.calk_dit_time()
def get_ratio(self):
return self.ratio_t
def set_ratio(self, ratio):
self.ratio_t = ratio
self.calk_dit_time()
def get_weighting(self):
return self.weighting_t
def set_weighting(self, weighting):
self.weighting_t = weighting
self.calk_dit_time()
# 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)
c -> clock show time or uptime
d -> decoder help info toggle (on) (off) (-.-.--.-?) or ?
f -> adjust sidetone frequenz
v -> adjust sidetone volume 1-100
w -> adjust WPM (Word per minute)
r -> adjust ratio controll r ratio dit/da
l -> adjust weighting controll dit high low time
h -> Set weighting and dah to dit ratio to defaults
t -> tune mode, end with command mode
s -> save parameter to file
x -> exit Command mode
"""
def __init__(self, tdit, tdah):
# self.touchdit=TouchPad(Pin(32))
# self.touchdah=TouchPad(Pin(33))
self.touchdit = TouchPad(Pin(tdit))
self.touchdah = TouchPad(Pin(tdah))
self.dit = False;
self.dah = False
self.ktimer = 0
self.ktimer_end = 0
self.in_char = True
self.in_word = True
self.char = ""
self.word = ""
self.IDLE = 0; # value of state maschine
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_sidetone_volume = 0 # def for state machine
self.adj_ratio = 0
self.adj_weighting = 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.ratio = 3.0 # ration dit/dah
self.weighting = 50
self.cq = 0
# self.cq_liste =["","cq cq de dl2dbg dl2dbg bk","dl2dbg","cq cq test dl2dbg","cq","uli","cq cq"]
self.cq_liste = ["", "", "", "", "", "", ""]
self.tx_enable = 0
self.decoder_enable = 0
self.sidetone_enable = 1
self.sidetone_freq = 700 #
self.sidetone_volume = 10 # range 1,100 * 200 -> 2000 #30000 laut
self.threshold_key = 200 # threshold of the touch key
#
self.request = 0 # request of parameters
# --------------
self.iambic_data = {} # create date store
self.read_jsondata()
self.init_iambic_data()
print("..read button and from json-file")
self.wpm = self.iambic_data["wpm"]
self.ratio = self.iambic_data["ratio"]
self.weighting = self.iambic_data["weighting"]
cw_time.set_wpm(self.wpm) # class timing setzen
cw_time.set_ratio(self.ratio)
cw_time.set_weighting(self.weighting)
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):
print(">> read json")
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("ratio", self.ratio)
self.set_data("weighting", self.weighting)
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("decoder_enable", self.decoder_enable)
self.set_data("threshold_key", self.threshold_key)
self.set_data("cq_txt_liste", self.cq_liste)
self.write_data2file()
def print_parameter(self): # write new json file
oe.print_smal("---Parameter", 0)
oe.print_smal(VERSION, 0)
oe.print_smal("iambic_mode :" + str(self.iambic_mode), 0) # transmit
oe.print_smal("wpm :" + str(self.wpm), 0)
oe.print_smal("ratio :" + str(self.ratio), 0)
oe.print_smal("weighting :" + str(self.weighting), 0)
oe.print_smal("sidetone_enable :" + str(self.sidetone_enable), 0)
oe.print_smal("sidetone_freq :" + str(self.sidetone_freq), 0)
oe.print_smal("sidetone_volume :" + str(self.sidetone_volume), 0)
oe.print_smal("tx_enamble :" + str(self.tx_enable), 0)
oe.print_smal("decoder_enable :" + str(self.decoder_enable), 0)
oe.print_smal("threshold_key :" + str(self.threshold_key), 0)
oe.print_smal("cq_txt_liste :" + str(self.cq_liste), 0)
oe.print_smal("", 0)
def init_iambic_data(self): # is only use at the begin to create new json file
print("init read")
self.iambic_mode = self.iambic_data["iambic_mode"]
self.wpm = self.iambic_data["wpm"]
self.ratio = self.iambic_data["ratio"]
self.weighting = self.iambic_data["weighting"]
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.threshold_key = self.iambic_data["threshold_key"]
self.cq_liste = self.iambic_data["cq_txt_liste"]
# set extern Parameter
cw_time.set_wpm(self.wpm)
cw_time.set_ratio(self.ratio)
cw_time.set_weighting(self.weighting)
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)
if (self.touch_val <= self.threshold_key):
return (self.LOW)
def state_key_dah(self):
try:
self.touch_val = self.touchdah.read()
# ggf. Fehler abfangen
except ValueError:
return (self.HIGH)
if (self.touch_val <= self.threshold_key):
return (self.LOW)
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 print_request(self, txt):
oe.print_big(txt, cb.comannd_state)
text2beep(txt)
self.char = ""
def cycle(self):