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

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

Commit Message

Tomi Valkeinen July 1, 2022, 8:45 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 | 39 ++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
 create mode 100644 src/py/examples/hotplug-monitor.py

Comments

Laurent Pinchart Aug. 18, 2022, 9:06 p.m. UTC | #1
Hi Tomi,

Thank you for the patch.

On Fri, Jul 01, 2022 at 11:45:21AM +0300, Tomi Valkeinen wrote:
> 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
> 
> 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())

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())