[libcamera-devel,v4,09/15] py: Add hotplug-monitor.py
diff mbox series

Message ID 20230309142601.70556-10-tomi.valkeinen@ideasonboard.com
State Superseded
Headers show
Series
  • py: New python bindings event handling
Related show

Commit Message

Tomi Valkeinen March 9, 2023, 2:25 p.m. UTC
Add a simple example script which waits for camera hotplug events.

Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/py/examples/hotplug-monitor.py | 39 ++++++++++++++++++++++++++++++
 1 file changed, 39 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..5f42970c
--- /dev/null
+++ b/src/py/examples/hotplug-monitor.py
@@ -0,0 +1,39 @@ 
+#!/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()
+
+    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
+
+        events = cm.get_events()
+
+        for ev in events:
+            if ev.type == libcam.Event.Type.CameraAdded:
+                print('Camera added:', ev.camera)
+            elif ev.type == libcam.Event.Type.CameraRemoved:
+                print('Camera removed:', ev.camera)
+
+    return 0
+
+
+if __name__ == '__main__':
+    sys.exit(main())