22import webcam
33
44appscreen = lv .screen_active ()
5+ th .disable ()
56
67keepgoing = True
78keepliveqrdecoding = False
@@ -94,7 +95,7 @@ def qr_button_click(e):
9495 qr_label .set_text (lv .SYMBOL .EYE_OPEN )
9596
9697
97- def try_capture ():
98+ def try_capture_only_webcam ():
9899 global current_cam_buffer , image_dsc , image
99100 new_cam_buffer = webcam .capture_frame (cam ) # Returns memoryview
100101 if new_cam_buffer and len (new_cam_buffer ) == 240 * 240 :
@@ -106,7 +107,7 @@ def try_capture():
106107 else :
107108 print ("Invalid buffer size:" , len (new_cam_buffer ))
108109
109- def try_capture_old ():
110+ def try_capture ():
110111 global current_cam_buffer , image_dsc , image , use_webcam
111112 if use_webcam :
112113 new_cam_buffer = webcam .capture_frame (cam )
@@ -123,8 +124,8 @@ def try_capture_old():
123124 #current_cam_buffer = None # Clear reference to allow GC
124125 #image.invalidate() #does not work
125126 # Free the previous buffer (if any) after setting new data
126- # if current_cam_buffer is not None and not use_webcam:
127- # cam.free_buffer() # Free the old buffer
127+ if current_cam_buffer is not None and not use_webcam :
128+ cam .free_buffer () # Free the old buffer
128129 current_cam_buffer = new_cam_buffer # Store new buffer reference
129130
130131
@@ -178,7 +179,6 @@ def build_ui():
178179
179180def init_cam ():
180181 try :
181- # time.sleep(1) doesn't help
182182 from camera import Camera , GrabMode , PixelFormat , FrameSize , GainCeiling
183183 cam = Camera (
184184 data_pins = [12 ,13 ,15 ,11 ,14 ,10 ,7 ,2 ],
@@ -221,27 +221,15 @@ def init_cam():
221221 except Exception as e :
222222 print (f"camtest.py: webcam exception: { e } " )
223223
224- #time.sleep_ms(1000)
225- memview = webcam .capture_frame (cam ) # Returns memoryview
226- time .sleep_ms (1000 )
227- static_bytes_obj = bytes (memview )
228-
229- if cam or use_webcam :
224+ if cam :
230225 build_ui ()
231226 count = 0
232227 while appscreen == lv .screen_active () and keepgoing is True :
233- print (f"capture nr { count } " )
234- count += 1
235- #try_capture()
236- #webcam.recapture_frame(cam)
237- bytes_obj = bytes (memview )
238- print (f"got bytes: { len (bytes_obj )} " )
239- #image_dsc.data = bytes_obj
240- image_dsc .data = static_bytes_obj
241- time .sleep_ms (200 ) # Allow for the MicroPython REPL to still work. Reducing it doesn't seem to affect the on-display FPS.
242- # somehow, everything's fine until I tell LVGL to redraw the image:
243- #image.invalidate()
244- image .set_src (image_dsc )
228+ try_capture ()
229+ # Task handler needs to be updated from the same thread, otherwise it causes concurrency issues:
230+ lv .task_handler ()
231+ time .sleep_ms (5 )
232+ lv .tick_inc (5 )
245233 print ("App backgrounded, deinitializing camera..." )
246234 if use_webcam :
247235 webcam .deinit (cam ) # Deinitializes webcam
@@ -250,6 +238,7 @@ def init_cam():
250238else :
251239 print ("No camera found, exiting..." )
252240
241+ th .enable ()
253242show_launcher ()
254243
255244
0 commit comments