-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpletouch.py
More file actions
61 lines (53 loc) · 1.83 KB
/
simpletouch.py
File metadata and controls
61 lines (53 loc) · 1.83 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
#!/usr/bin/python3
# needs to be run with sudo rights
# package evdev must be installed globally:
# sudo -H pip3 install evdev
from evdev import InputDevice, ecodes, categorize
import time
# debug flag, no script output when False
debug = True
if debug: print('debug is on')
# touch screen device
dev = InputDevice('/dev/input/event0')
# screen state, "0" is on; "1" is off, only root can write
screenstatefile = '/sys/class/backlight/rpi_backlight/bl_power'
# read screen state at start
f = open(screenstatefile, 'r')
scrstate = f.read().rstrip('\n')
f.close()
# init vars
BTNstate = 0
# function to toggle screen state
def screentoggle(wantedstate):
f = open(screenstatefile, 'w')
f.write(wantedstate)
f.close()
# read events and categorize
for event in dev.read_loop():
absevent = categorize(event)
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:
f = open(screenstatefile, 'r')
scrstate = f.read().rstrip('\n')
f.close()
if debug: print(f'Touch detected and screen state = {scrstate}'.format(scrstate))
# act on touch events within the defined region when the screen is on
if scrstate == '0':
# turn screen off
screentoggle('1')
# reset variable
BTNstate = "0"
# debounce
time.sleep(.1)
# act on touch events anywhere when the screen is off
if (scrstate == '1'):
# turn screen on and let events pass
screentoggle('0')
# reset variables
BTNstate = "0"
# debounce
time.sleep(.1)
# debounce
time.sleep(.1)