Repository navigation
Expand file tree
/
Copy pathrotaryEncoderListener.py
More file actions
executable file
·47 lines (42 loc) · 1.92 KB
/
Copy pathrotaryEncoderListener.py
File metadata and controls
executable file
·47 lines (42 loc) · 1.92 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
import evdev
import select
import json
import os.path
import requests
from settings import volumeIncrement, apiEndpoint
# set this up for the live listener
devices = [evdev.InputDevice(fn) for fn in evdev.list_devices()]
devices = {dev.fd: dev for dev in devices}
try:
r = requests.get(f"{apiEndpoint}ready")
if (r.text != "True"): print("An error occured while notifying the server that we're monitoring the rotary encoder.")
except:
print("Failed to connect to the webserver. Maybe it's not running?")
# And kick off our infinite loop
def main():
done = False
while not done:
r, w, x = select.select(devices, [], [])
for fd in r:
for event in devices[fd].read():
event = evdev.util.categorize(event)
if isinstance(event, evdev.events.RelEvent):
if (event.event.value > 0):
try:
r = requests.get(f"{apiEndpoint}volume/+{str(event.event.value * volumeIncrement)}")
except:
print("Failed to connect to the webserver. Maybe it's not running?")
else:
try:
r = requests.get(f"{apiEndpoint}volume/{str(event.event.value * volumeIncrement)}")
except:
print("Failed to connect to the webserver. Maybe it's not running?")
elif isinstance(event, evdev.events.KeyEvent):
if event.keycode == "KEY_ENTER" and event.keystate == event.key_up:
try:
r = requests.get(f"{apiEndpoint}timer/bop")
if (int(r.text) < 0): print("An error occured setting the volume")
except:
print("Failed to connect to the webserver. Maybe it's not running?")
if __name__ == '__main__':
main()