-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.py
49 lines (38 loc) · 1.38 KB
/
reader.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
import subprocess
import signal
import evdev
class NFCReader:
singleton = None
def __init__(self, devPath):
self.devPath = devPath
class _NFCReader:
def __init__(self, devPath):
self.dev = evdev.InputDevice(devPath)
def get_card_uid(self):
buf = ''
for event in self.dev.read_loop():
if event.type == evdev.ecodes.EV_KEY:
k = evdev.events.KeyEvent(event)
if k.keystate == evdev.events.KeyEvent.key_down:
key = self.process_key(k.keycode)
if key == 'E':
yield buf
buf = ''
else:
buf += key
def process_key(self, keycode):
if keycode[:4] == 'KEY_':
if keycode[4:] == 'ENTER':
return 'E'
try:
int(keycode[4:])
except ValueError:
return '';
return keycode[4:]
def __enter__(self):
if NFCReader.singleton is not None:
raise Exception("Only one NFCReader instance allowed")
NFCReader.singleton = self._NFCReader(self.devPath)
return NFCReader.singleton
def __exit__(self, exc_type, exc_val, exc_tb):
NFCReader.singleton = None