Browse Source

working example for grabbing USB HID device

clementinecomputing 5 years ago
parent
commit
6973a1e27e
1 changed files with 51 additions and 0 deletions
  1. 51 0
      experiment/production/PIU-Notes.md

+ 51 - 0
experiment/production/PIU-Notes.md

@@ -177,6 +177,57 @@ for device in dev:
 
 
 The above has to be played around with but it looks like the seeds are there.
 The above has to be played around with but it looks like the seeds are there.
 
 
+---
+
+If you don't care about capturing multiple devices at once (within a single program), the following does an active poll:
+
+```
+#!/usr/bin/python3
+
+import os, sys, evdev, asyncio, os.path
+from evdev import InputDevice, categorize, ecodes
+
+dev_init = False
+dev = { }
+
+HID_NAME = 'HID c216:0180'
+
+evfns_n = 13
+evfns_bd = "/dev/input/"
+evfns = [ ]
+
+for x in range(evfns_n):
+  fn = evfns_bd + "event" + str(x)
+  if (os.path.exists(fn)):
+    evfns.append(fn)
+
+for evfn in evfns:
+  _dev = evdev.InputDevice(evfn)
+  print("device:", _dev.name)
+  if _dev.name == HID_NAME:
+    dev_init = True
+    dev = _dev
+    break
+
+if not dev_init:
+  print("could not find device, exiting")
+  sys.exit(0)
+
+print(dev)
+print(dev.capabilities())
+print(dev.capabilities(verbose=True))
+
+dev.grab()
+
+for event in dev.read_loop():
+  if event.type == ecodes.EV_KEY:
+    print(categorize(event))
+
+```
+
+Note that the `HID_NAME` is specific to the USB magstripe reader I had on hand.
+Multiple of the same could be attached and would need to be differentiated by thei `/dev/input/event*` endpoints.
+
 
 
 References
 References
 ---
 ---