[libcamera-devel] [PATCH v3 12/17] py: cam.py: Use new events support

Tomi Valkeinen tomi.valkeinen at ideasonboard.com
Fri Aug 19 09:03:12 CEST 2022


On 18/08/2022 23:59, Laurent Pinchart wrote:
> On Thu, Aug 18, 2022 at 05:49:22PM +0200, Jacopo Mondi wrote:
>> On Fri, Jul 01, 2022 at 11:45:16AM +0300, Tomi Valkeinen wrote:
>>> Convert cam.py to use the new event dispatching. In addition to handling
>>> the request-completed event, handle also disconnect, camera-added and
>>> camera-removed events (which only do a simple print).
>>>
>>> Signed-off-by: Tomi Valkeinen <tomi.valkeinen at ideasonboard.com>
>>> ---
>>>   src/py/cam/cam.py | 31 +++++++++++++++++++++++++------
>>>   1 file changed, 25 insertions(+), 6 deletions(-)
>>>
>>> diff --git a/src/py/cam/cam.py b/src/py/cam/cam.py
>>> index 6b6b678b..f19bad57 100755
>>> --- a/src/py/cam/cam.py
>>> +++ b/src/py/cam/cam.py
>>> @@ -230,11 +230,20 @@ class CaptureState:
>>>       # Called from renderer when there is a libcamera event
>>>       def event_handler(self):
>>>           try:
>>> -            reqs = self.cm.get_ready_requests()
>>> -
>>> -            for req in reqs:
>>> -                ctx = next(ctx for ctx in self.contexts if ctx.idx == req.cookie)
>>> -                self.__request_handler(ctx, req)
>>> +            evs = self.cm.get_events()
>>> +            for ev in evs:
>>> +                type = ev.type
>>> +
>>> +                if type == libcam.Event.Type.Undefined:
>>> +                    raise RuntimeError("Bad event type")
>>> +                elif type == libcam.Event.Type.CameraAdded:
>>> +                    print('Camera added:', ev.camera)
>>> +                elif type == libcam.Event.Type.CameraRemoved:
>>> +                    print('Camera added:', ev.camera)
>>
>> s/added/removed/
>>
>>> +                elif type == libcam.Event.Type.Disconnect:
>>> +                    self.__disconnect_handler(ev.camera)
>>> +                elif type == libcam.Event.Type.RequestCompleted:
>>> +                    self.__request_handler(ev.camera, ev.request)
>>>
>>>               running = any(ctx.reqs_completed < ctx.opt_capture for ctx in self.contexts)
>>>               return running
>>> @@ -242,7 +251,9 @@ class CaptureState:
>>>               traceback.print_exc()
>>>               return False
>>>
>>> -    def __request_handler(self, ctx, req):
>>> +    def __request_handler(self, cam, req):
>>> +        ctx = next(ctx for ctx in self.contexts if ctx.camera == cam)
> 
> Could the contexts list become a dictionary indexed by camera ?

It could, but I think it would result in more code and a "random" order 
for code execution (e.g. in which order do we start the cameras).

  Tomi


More information about the libcamera-devel mailing list