-
Notifications
You must be signed in to change notification settings - Fork 0
/
specialtouch.py
97 lines (86 loc) · 2.94 KB
/
specialtouch.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
#!/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
# coordinates, where touch events cause action
# top y, top x, width, height
topy,topx,w,h = 0,450,574,139
# coordinates converted to ranges
hrange = range(topx, topx + w)
vrange = range(topy, topy + h)
# 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
X = -1
Y = -1
# grab screen events when it is off
if (scrstate == '1'):
if debug: print("screen is off, grabbing input")
dev.grab()
# function to toggle screen state and to grab input
def grabandscreentoggle(wantedstate):
if wantedstate == '1': # screen should be turned be off
# all input will be grabbed
dev.grab()
else:
dev.ungrab()
# actually toggling the screen by writing to file
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()
# reset X and Y when there was no touch
if BTNstate == 0:
X = -1
Y = -1
# get coordinates
if ecodes.bytype[absevent.event.type][absevent.event.code] == 'ABS_Y':
Y = absevent.event.value
if ecodes.bytype[absevent.event.type][absevent.event.code] == 'ABS_X':
X = absevent.event.value
# check if touch was detected and there are valid coordinates
if (BTNstate == 1) and (X > -1) and (Y > -1 ):
if debug: print(f'Touch at {X}/{Y} and screen state = {scrstate}'.format(X,Y,scrstate))
# act on touch events within the defined region when the screen is on
if (X in hrange) and (Y in vrange) and (scrstate == '0'):
# turn screen off and activate events grabbing
grabandscreentoggle('1')
# reset variables
BTNstate = "0"
X = -1
Y = -1
# debounce
time.sleep(.1)
# act on touch events anywhere when the screen is off
if (scrstate == '1'):
# turn screen on and let events pass
grabandscreentoggle('0')
# reset variables
BTNstate = "0"
X = -1
Y = -1
# debounce
time.sleep(.1)
# debounce
time.sleep(.1)