-
Notifications
You must be signed in to change notification settings - Fork 1
/
serialFootPedalControl.py
121 lines (90 loc) · 3.99 KB
/
serialFootPedalControl.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
import serial #from library PySerial
import keyboard
import json
configFile = open("pedalConfig.json").read()
configData = json.loads(configFile)
numPedals = configData["numPedals"]
#press actions (order in array is index of pedal)
onKey = configData["onKey"]
onKeySHIFT = configData["onKeySHIFT"]
def PTTHook(a):
pass
def setPTTHook(input_fn):
global PTTHook
PTTHook = input_fn
shiftState = False
PTTLockedState = False
ser = serial.Serial(configData["port"], 9600)
while True:
code=int(ser.readline())
#key presses
#~~~~~~~~~~~~~~~~~~~ #when shift is OFF# ~~~~~~~~~~~~~~~~~~~~~~#
if not shiftState:
######### press events ##########
if code < numPedals:
if onKey[code] == "PTT":
if PTTLockedState:
PTTLockedState = False
print("Code: " + str(code) +" PTT UNLOCKED!")
else: # if unlocked
print("Code: " + str(code) +" PTT ON!")
PTTHook(True) # send PTT on
ser.write(bytes(b'o')) #turn LED on
elif onKey[code] == "SHIFT":
shiftState = True
print("Code: " + str(code) +" SHIFT ON!")
else:
keyboard.press(onKey[code])
print("Code: " + str(code) + " " + onKey[code])
######### release events ##########
else: #( when code >= numPedals)
if onKey[code-numPedals] == "PTT":
if PTTLockedState:
pass
#do nothing
else: #if unlocked
print("Code: " + str(code) +" PTT OFF!")
PTTHook(False) # send PTT off
ser.write(bytes(b'f'))
elif onKey[code-numPedals] == "SHIFT":
shiftState = False
print("Code: " + str(code) +" SHIFT OFF (already was)!")
else:
keyboard.release(onKey[code-numPedals])
keyboard.release(onKeySHIFT[code-numPedals]) #to make sure shifted keys are removed too
print("Code: " + str(code) + " " + onKey[code-numPedals] +" OFF")
print("(and) Code: shift of " + str(code) + " " + onKeySHIFT[code-numPedals] +" OFF")
#~~~~~~~~~~~~~~~~~~~ #when shift is ON# ~~~~~~~~~~~~~~~~~~~~~~#
else:
######### press events ##########
if code < numPedals:
if onKeySHIFT[code] == "PTT TOGGLE":
if PTTLockedState:
PTTLockedState = False
PTTHook(False) #send PTT off
print("Code: " + str(code) +" PTT UNLOCKED AND OFF!")
ser.write(bytes(b'f'))
else: #if unlocked
PTTLockedState = True
PTTHook(True) #send PTT ON
print("Code: " + str(code) +" PTT LOCKED ON!")
ser.write(bytes(b'o'))
elif onKeySHIFT[code] == "SHIFT":
shiftState = True
print("Code: " + str(code) +" SHIFT ON (already was)!")
else:
keyboard.press(onKeySHIFT[code])
print("Code: " + str(code) + " " + onKeySHIFT[code])
######### release events ##########
else: #(when code >= numPedals)
if onKeySHIFT[code - numPedals] == "PTT TOGGLE":
pass
#Nothing
elif onKeySHIFT[code - numPedals] == "SHIFT":
shiftState = False
print("Code: " + str(code) + " SHIFT OFF!")
else:
keyboard.release(onKey[code - numPedals])
keyboard.release(onKeySHIFT[code - numPedals]) #to make sure shifted keys are removed too
print("Code: " + str(code) + " " + onKeySHIFT[code - numPedals] +" OFF")
print("(and) Code: normal of" + " " + str(code) + onKey[code - numPedals] +" OFF")