@@ -161,39 +161,50 @@ def build_ui():
161161 image .set_src (image_dsc )
162162
163163
164- try :
165- # time.sleep(1) doesn't help
166- from camera import Camera , GrabMode , PixelFormat , FrameSize , GainCeiling
167- cam = Camera (
168- data_pins = [12 ,13 ,15 ,11 ,14 ,10 ,7 ,2 ],
169- vsync_pin = 6 ,
170- href_pin = 4 ,
171- sda_pin = 21 ,
172- scl_pin = 16 ,
173- pclk_pin = 9 ,
174- xclk_pin = 8 ,
175- xclk_freq = 20000000 ,
176- powerdown_pin = - 1 ,
177- reset_pin = - 1 ,
178- #pixel_format=PixelFormat.RGB565,
179- pixel_format = PixelFormat .GRAYSCALE ,
180- frame_size = FrameSize .R240X240 ,
181- grab_mode = GrabMode .LATEST
182- )
183- #cam.init() automatically done when creating the Camera()
184- #cam.reconfigure(frame_size=FrameSize.HVGA)
185- #frame_size=FrameSize.HVGA, # 480x320
186- #frame_size=FrameSize.QVGA, # 320x240
187- #frame_size=FrameSize.QQVGA # 160x120
188- cam .set_vflip (True )
164+ def init_cam ():
165+ try :
166+ # time.sleep(1) doesn't help
167+ from camera import Camera , GrabMode , PixelFormat , FrameSize , GainCeiling
168+ cam = Camera (
169+ data_pins = [12 ,13 ,15 ,11 ,14 ,10 ,7 ,2 ],
170+ vsync_pin = 6 ,
171+ href_pin = 4 ,
172+ sda_pin = 21 ,
173+ scl_pin = 16 ,
174+ pclk_pin = 9 ,
175+ xclk_pin = 8 ,
176+ xclk_freq = 20000000 ,
177+ powerdown_pin = - 1 ,
178+ reset_pin = - 1 ,
179+ #pixel_format=PixelFormat.RGB565,
180+ pixel_format = PixelFormat .GRAYSCALE ,
181+ frame_size = FrameSize .R240X240 ,
182+ grab_mode = GrabMode .LATEST
183+ )
184+ #cam.init() automatically done when creating the Camera()
185+ #cam.reconfigure(frame_size=FrameSize.HVGA)
186+ #frame_size=FrameSize.HVGA, # 480x320
187+ #frame_size=FrameSize.QVGA, # 320x240
188+ #frame_size=FrameSize.QQVGA # 160x120
189+ cam .set_vflip (True )
190+ return cam
191+ except Exception as e :
192+ print (f"init_cam exception: { e } " )
193+ return None
194+
195+
196+ cam = init_cam ()
197+ if not cam :
198+ print ("init cam failed, retrying..." )
199+ time .sleep (5 )
200+ cam = init_cam ()
201+
202+ if cam :
189203 build_ui ()
190204 while appscreen == lv .screen_active () and keepgoing is True :
191205 try_capture ()
192206 time .sleep_ms (100 ) # Allow for the MicroPython REPL to still work. Reducing it doesn't seem to affect the on-display FPS.
193207 print ("App backgrounded, deinitializing camera..." )
194- cam .deinit ()
195- show_launcher ()
196- except Exception as e :
197- print (f"Exception: { e } " )
198-
208+ cam .deinit ()
209+ show_launcher ()
199210
0 commit comments