-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
83 lines (60 loc) · 1.93 KB
/
code.py
File metadata and controls
83 lines (60 loc) · 1.93 KB
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
import time
import board
import digitalio
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
# Settings in SPR (Seconds Per Revolution)
run_speed = 0.99 # Anything lower adds SHIFT
stop_speed = 1.6 # Anything slower unpresses keys
# Setup keyboard
keyboard = Keyboard(usb_hid.devices)
# Setup switch input
cycle = digitalio.DigitalInOut(board.GP16)
cycle.direction = digitalio.Direction.INPUT
cycle.pull = digitalio.Pull.DOWN
# Setup LED
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
# Track last loop time
trigger_active = False
trigger_time = time.monotonic()
# Track keys
holdingW = False
holdingSHIFT = False
print('Activated!')
while True:
loop_time = time.monotonic()
rev_time = loop_time - trigger_time
# Circuit closed!
if cycle.value:
trigger_active = True
led.value = True
#print("Closed circuit! " + str(loop_time))
# Circuit broken! Start actions.
elif trigger_active == True:
trigger_active = False
led.value = False
trigger_time = loop_time
if not holdingSHIFT and rev_time <= run_speed:
holdingSHIFT = True
keyboard.press(Keycode.SHIFT)
print("Hold SHIFT")
if not holdingW:
holdingW = True
keyboard.press(Keycode.W)
print("Hold W")
print(" Rev time = " + str(rev_time))
# Timeout - full stop
if holdingW and rev_time >= stop_speed:
keyboard.release_all()
holdingW = False
holdingSHIFT = False
print("Stop All")
print(" Rev time = " + str(rev_time))
# Timeout - stop running
if holdingSHIFT and rev_time > run_speed:
keyboard.release(Keycode.SHIFT)
holdingSHIFT = False
print("Stop SHIFT")
print(" Rev time = " + str(rev_time))