-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpletouch_waveshare.py
More file actions
59 lines (49 loc) · 1.8 KB
/
Copy pathsimpletouch_waveshare.py
File metadata and controls
59 lines (49 loc) · 1.8 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
#!/usr/bin/python3
# package evdev must be installed
from evdev import InputDevice, ecodes, categorize
import subprocess
import time
# debug flag, no script output when False
debug = False
if debug: print('debug is on')
# touch screen device
dev = InputDevice('/dev/input/event3')
def screenstate():
r = subprocess.check_output('xset q | grep -q "Monitor is On" && echo -n on || echo -n off', shell=True, universal_newlines=True)
return r
scrstate = screenstate()
# init vars
BTNstate = 0
# function to toggle screen state
def screentoggle(wantedstate):
s = subprocess.check_output(f'xset dpms force {wantedstate}', shell=True)
# read events and categorize
for event in dev.read_loop():
absevent = categorize(event)
try:
if ecodes.bytype[absevent.event.type][absevent.event.code] == 'BTN_TOUCH':
BTNstate = absevent.event.value
# read screen state when touch is detected
if BTNstate == 1:
scrstate = screenstate()
if debug: print(f'Touch detected and screen state = {scrstate}')
# act on touch events within the defined region when the screen is on
if scrstate == 'on':
# turn screen off
screentoggle('off')
# reset variable
BTNstate = "0"
# debounce
time.sleep(.5)
# act on touch events anywhere when the screen is off
if (scrstate == 'off'):
# turn screen on and let events pass
screentoggle('on')
# reset variables
BTNstate = "0"
# debounce
time.sleep(.5)
except:
BTNstate = "0"
# debounce
time.sleep(.5)