[libcamera-devel,v2,14/14] py: Add hotplug-monitor.py
diff mbox series

Message ID 20220629070416.17550-15-tomi.valkeinen@ideasonboard.com
State Superseded
Headers show
Series
  • Python bindings event handling
Related show

Commit Message

Tomi Valkeinen June 29, 2022, 7:04 a.m. UTC
Add a simple example script which waits for camera hotplug events.

Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
---
 src/py/examples/hotplug-monitor.py | 38 ++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
 create mode 100644 src/py/examples/hotplug-monitor.py

Patch
diff mbox series

diff --git a/src/py/examples/hotplug-monitor.py b/src/py/examples/hotplug-monitor.py
new file mode 100644
index 00000000..787aa45a
--- /dev/null
+++ b/src/py/examples/hotplug-monitor.py
@@ -0,0 +1,38 @@ 
+#!/usr/bin/env python3
+
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (C) 2022, Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
+
+import libcamera as libcam
+import selectors
+import sys
+
+
+def main():
+    cm = libcam.CameraManager.singleton()
+
+    cm.camera_added = lambda c: print('Camera added:', c)
+    cm.camera_removed = lambda c: print('Camera removed:', c)
+
+    sel = selectors.DefaultSelector()
+    sel.register(cm.event_fd, selectors.EVENT_READ)
+
+    print('Waiting for camera hotplug events... (CTRL-C to exit)')
+
+    while True:
+        try:
+            events = sel.select()
+            if not events:
+                continue
+        except KeyboardInterrupt:
+            break
+
+        cm.dispatch_events()
+
+    cm.discard_events()
+
+    return 0
+
+
+if __name__ == '__main__':
+    sys.exit(main())