lit-robotics / libcamera-rs

Experimental Rust bindings for libcamera
Apache License 2.0
51 stars 16 forks source link

Initialize active camera object to get frame fast #35

Closed alexisgaziello closed 1 week ago

alexisgaziello commented 8 months ago

Hey,

I was wondering, what would be the correct way of initializing some sort of singleton for a camera object, that could be called to get frames very fast? Something like:

CAMERA: Camera = None // lazy optional struct initialized via initialize_camera?

fn initialize_camera() -> Result<(), String> {}  // to execute somewhere, at the beginning of the execution

fn capture() -> Result<Vec<u8>, String> {}  // to execute everytime we need a frame

Could someone put me on the right track for the definition of Camera? and where is the best place to read documentation that would help resolving this?

chemicstry commented 8 months ago

Hi,

I think the easiest way would be to put all the code from example (i.e. https://github.com/lit-robotics/libcamera-rs/blob/main/libcamera/examples/jpeg_capture.rs) into a thread and communicate over channels. You would need 2 channels, one to send a signal to capture and another to wait for response. The camera thread would loop on waiting for signal and then send an actual capture request to camera. Once request is completed, it would return the result over the second channel.

So your capture() function would just send a signal to a channel to start capture, and then wait on the second channel for the response.

Let me know if this is clear or if you need additional help.