forked from tuxintrouble/sigbit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.py
78 lines (60 loc) · 2.3 KB
/
key.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
#!/usr/local/bin/python3
#
# This file is part of the SigBit project
# https://github.com/tuxintrouble/sigbit
# Author: Sebastian Stetter, DJ5SE
# License: GNU GENERAL PUBLIC LICENSE Version 3
#
# Hardware Abstraction Classes for interfacing with iambic keys
from util import ditlen
###Hardware Abstraction Classes###
# must implement the functions dit() and dah()
# which return True if Paddle is pressed
class GPIOKey():
"""GPIO based key implementation for ESP32 and micropython, takes pin numbers as arguments"""
def __init__(self,ditpin, dahpin):
import machine
import utime as time
self.debounce_factor = 0.001 #sec
self.last_time_dit = time.time()
self.last_time_dah = time.time()
self.ditpin = machine.Pin(ditpin, mode = machine.Pin.IN, pull = machine.Pin.PULL_UP)
self.dahpin = machine.Pin(dahpin, mode = machine.Pin.IN, pull = machine.Pin.PULL_UP)
def dit(self):
if self.last_time_dit + self.debounce_factor < time.time() and self.ditpin.value():
self.last_time_dit = time.time()
return True
else:
return False
def dah(self):
if self.last_time_dah + self.debounce_factor < time.time() and self.dahpin.value():
self.last_time_dah = time.time()
return True
else:
return False
class SerialKey():
"""Serial based key implementation for cPython, takes a port as argument (COM1,/dev/ttyUSB0") """
def __init__(self, port):
import serial,time, sys
self.debounce_factor = 0.001 # sec
try:
self.ser = serial.Serial(port)
except serial.serialutil.SerialException as err:
print(err)
sys.exit()
self.last_time_dit = time.time()
self.last_time_dah = time.time()
def dit(self):
import time
if self.last_time_dit + self.debounce_factor < time.time() and self.ser.getDSR():
self.last_time_dit = time.time()
return True
else:
return False
def dah(self):
import time
if self.last_time_dah + self.debounce_factor < time.time() and self.ser.getCTS():
self.last_time_dah = time.time()
return True
else:
return False