@@ -18,7 +18,7 @@ use v4l::{Device, FourCC};
1818
1919/// Some (at least mine) IR cameras occasionally produce very dark frames which we ignore
2020const MAX_BRIGHTNESS_DECREASE : f32 = 24.0 ;
21- /// How many times we are allowed to fail getting a frame before we panic
21+ /// How many times we are allowed to fail getting a frame before we return an error
2222const MAX_FAILED_FRAMES_IN_ROW : u8 = 10 ;
2323
2424/// The type of a frame coming from the camera
@@ -79,7 +79,7 @@ pub struct Camera {
7979impl Camera {
8080 /// Creates a new Camera which can be used to get frames from the given device.
8181 ///
82- /// This is going to panic if a supported output pixel format cannot be found
82+ /// This is going to return an error if a supported output pixel format cannot be found
8383 pub fn new ( camera_path : & str ) -> Result < Self , Error > {
8484 trace ! ( "Creating Camera" ) ;
8585
@@ -120,17 +120,20 @@ impl Camera {
120120}
121121
122122/// Starts the camera loop
123- pub fn start ( frame : & Arc < Mutex < Option < Frame > > > , finished : & Arc < AtomicBool > , camera_path : & str ) {
123+ pub fn start (
124+ frame : & Arc < Mutex < Option < Frame > > > ,
125+ finished : & Arc < AtomicBool > ,
126+ camera_path : & str ,
127+ ) -> Result < ( ) , String > {
124128 let mut camera = match Camera :: new ( camera_path) {
125129 Ok ( c) => c,
126- Err ( e) => panic ! ( "Failed construct camera: {e}" ) ,
130+ Err ( e) => return Err ( format ! ( "Failed to create camera: {e}" ) ) ,
127131 } ;
128-
129132 let mut failed_frames_in_row = 0 ;
130133 let mut last_brightness = 255.0 ;
131134 loop {
132135 if finished. load ( Ordering :: SeqCst ) {
133- return ;
136+ return Ok ( ( ) ) ;
134137 }
135138
136139 let new_frame = match camera. get_frame ( ) {
@@ -139,10 +142,9 @@ pub fn start(frame: &Arc<Mutex<Option<Frame>>>, finished: &Arc<AtomicBool>, came
139142 error ! ( "Failed to get frame: {e}" ) ;
140143
141144 failed_frames_in_row += 1 ;
142- assert ! (
143- failed_frames_in_row < MAX_FAILED_FRAMES_IN_ROW ,
144- "Failed to get {MAX_FAILED_FRAMES_IN_ROW} frames in row: {e}"
145- ) ;
145+ if failed_frames_in_row < MAX_FAILED_FRAMES_IN_ROW {
146+ return Err ( String :: from ( "Failed to get too many frames in a row" ) ) ;
147+ }
146148 continue ;
147149 }
148150 } ;
0 commit comments