-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path__main__.py
51 lines (43 loc) · 1.37 KB
/
__main__.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
import os, sys, signal, time, json, re, binascii
import nfc
non_touchend = ('non-touchend' in sys.argv)
non_loop = ('non-loop' in sys.argv) or non_touchend
running = True
exiting = False
def sig_hup_handler(signo, frame):
global running
global exiting
if running:
running = False
os.kill(os.getpid(), signal.SIGINT)
exiting = True
def sig_chld_handler(signo, frame):
global running
running ^= True
if not(running):
os.kill(os.getpid(), signal.SIGINT)
signal.signal(signal.SIGHUP, sig_hup_handler)
signal.signal(signal.SIGCHLD, sig_chld_handler)
def stdout_json(data):
sys.stdout.write(json.dumps(data))
sys.stdout.write('\n')
sys.stdout.flush()
def on_connect(tag):
identifier = binascii.hexlify(tag.identifier)
match = re.findall(r'[0-9]', tag.type)
type = int(match[0] if match else 0)
stdout_json({'event':'touchstart', 'id':identifier, 'type':type})
return not(non_touchend)
if __name__ == '__main__':
while True:
if running:
with nfc.ContactlessFrontend('usb') as clf:
while clf.connect(rdwr={'on-connect': on_connect}):
stdout_json({'event':'touchend'})
if non_loop:
running = False
break
elif not(exiting):
time.sleep(0.1)
else:
break