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

Tomi Valkeinen tomi.valkeinen at ideasonboard.com
Fri Aug 19 08:56:43 CEST 2022


On 18/08/2022 18:49, Jacopo Mondi wrote:
> Hi Tomi
> 
> 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/

Right... Copy-pasting... =)

>> +                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)
>> +
>>           if req.status != libcam.Request.Status.Complete:
>>               raise Exception('{}: Request failed: {}'.format(ctx.id, req.status))
>>
>> @@ -297,6 +308,9 @@ class CaptureState:
>>               ctx.camera.queue_request(req)
>>               ctx.reqs_queued += 1
>>
>> +    def __disconnect_handler(self, cam):
>> +        print(f'Camera {cam} disconnected')
>> +
> 
> Why this has a separate handler while the other ones that simply print
> a message do not ?

You know, I have no idea... I'll just move the print along with the others.

> Nit apart:
> Reviewed-by: Jacopo Mondi <jacopo at jmondi.org>

Thanks!

  Tomi


More information about the libcamera-devel mailing list