Skip to content

Instantly share code, notes, and snippets.

@wwwins
Last active September 20, 2018 05:42
Show Gist options
  • Select an option

  • Save wwwins/2178f8a2a8095c630edc191ab6d0cd24 to your computer and use it in GitHub Desktop.

Select an option

Save wwwins/2178f8a2a8095c630edc191ab6d0cd24 to your computer and use it in GitHub Desktop.
raspberry pi + usb rfid card reader
# ScanCardReader.py
# -*- coding: utf-8 -*-
# scanning usb rfid card reader.
import evdev
from evdev import InputDevice, categorize, ecodes
from select import select
input = evdev.InputDevice('/dev/input/event0')
scancodes = {
# Scancode: ASCIICode
0: None, 1: u'ESC', 2: u'1', 3: u'2', 4: u'3', 5: u'4', 6: u'5', 7: u'6', 8: u'7', 9: u'8',
10: u'9', 11: u'0', 12: u'-', 13: u'=', 14: u'BKSP', 15: u'TAB', 16: u'Q', 17: u'W', 18: u'E', 19: u'R',
20: u'T', 21: u'Y', 22: u'U', 23: u'I', 24: u'O', 25: u'P', 26: u'[', 27: u']', 28: u'CRLF', 29: u'LCTRL',
30: u'A', 31: u'S', 32: u'D', 33: u'F', 34: u'G', 35: u'H', 36: u'J', 37: u'K', 38: u'L', 39: u';',
40: u'"', 41: u'`', 42: u'LSHFT', 43: u'\\', 44: u'Z', 45: u'X', 46: u'C', 47: u'V', 48: u'B', 49: u'N',
50: u'M', 51: u',', 52: u'.', 53: u'/', 54: u'RSHFT', 56: u'LALT', 100: u'RALT'
}
# not working...
# issue: 一段時間沒使用,再次感應時,第一次會沒有反應
# https://github.com/gvalkov/python-evdev/issues/32
# https://github.com/gvalkov/python-evdev/issues/15
buf = ''
while True:
r, w, x = select([input], [], [], 0.1)
if r:
for event in input.read():
if event.type == evdev.ecodes.EV_KEY and event.value == 1:
data = evdev.categorize(event)
key_lookup = scancodes.get(data.scancode) or 'UNKNOWN:{}'.format(data.scancode)
if (key_lookup == 'CRLF'):
print('scan:{}'.format(buf))
buf = ''
else:
buf = buf + key_lookup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment