-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
97 lines (71 loc) · 2.12 KB
/
code.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
import time
import analogio
import digitalio
import board
import math
import usb_hid
from adafruit_hid.mouse import Mouse
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
#INITIALIZE MOUSE
mouse = Mouse(usb_hid.devices)
#INITIALIZE KEYBOARD
keyboard = Keyboard(usb_hid.devices)
#PIN SETUP
potentiometer = analogio.AnalogIn(board.GP26)
sck = digitalio.DigitalInOut(board.GP14)
sck.direction = digitalio.Direction.OUTPUT
out = digitalio.DigitalInOut(board.GP15)
out.direction = digitalio.Direction.INPUT
#SLIDE POT SETUP
def get_potentiomer_value():
return(int(potentiometer.value * 100) // 65536)
last_value = get_potentiomer_value()
#BUTTON SETUP
button = digitalio.DigitalInOut(board.GP16)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP
#ADC SETUP
MAX_ADC_VALUE = (1 << 24) - 1 #16,777,215
#PRESSURE SENSOR SETUP
def read_adc():
result = 0
for i in range(25):
sck.value = True
time.sleep(0.001)
sck.value = False
time.sleep(0.001)
result = (result << 1) | out.value
return result
spacebar_held = False
#DEBOUNCE PARAMETERS
debounce_delay = 0.01
last_button_state = True
last_debounce_time = time.monotonic()
while True:
#READ BUTTON STATE
current_button_state = button.value
if current_button_state != last_button_state:
last_debounce_time = time.monotonic()
if(time.monotonic() - last_debounce_time) > debounce_delay:
if current_button_state == False:
if not spacebar_held:
keyboard.press(Keycode.SPACE)
spacebar_held = True
else:
if spacebar_held:
keyboard.release(Keycode.SPACE)
spacebar_held = False
last_button_state = current_button_state
#CHECK POT VALUE
current_value = get_potentiomer_value()
#MOVEMENT COMPARISON
movement = current_value - last_value
#MOVE MOUSE
if abs(movement) >2:
mouse.move(y=movement * 20)
last_value = current_value
#BREATH INPUT
adc_value = read_adc()
print("Raw ADC Value:", adc_value)
time.sleep(0.001)