Skip to content

Commit 7288501

Browse files
Finally fix it!
1 parent 771d041 commit 7288501

File tree

2 files changed

+16
-27
lines changed

2 files changed

+16
-27
lines changed

internal_filesystem/apps/com.example.camtest/assets/camtest.py

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import webcam
33

44
appscreen = lv.screen_active()
5+
th.disable()
56

67
keepgoing = True
78
keepliveqrdecoding = 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

179180
def 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():
250238
else:
251239
print("No camera found, exiting...")
252240

241+
th.enable()
253242
show_launcher()
254243

255244

internal_filesystem/apps/com.example.camtestnew/assets/camtestnew.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
cam = webcam.init("/dev/video0") # Initialize webcam with device path
1818
#memview = None
19-
memview = webcam.capture_frame(cam) # Returns memoryview
19+
#memview = webcam.capture_frame(cam) # Returns memoryview
2020
#time.sleep_ms(1000)
2121
#static_bytes_obj = bytes(memview)
2222

@@ -34,21 +34,21 @@
3434
"cf": lv.COLOR_FORMAT.L8
3535
},
3636
'data_size': width * height,
37-
'data': memview # Will be updated per frame
37+
'data': None # Will be updated per frame
3838
})
3939
image.set_src(image_dsc)
4040

4141
#memview = webcam.capture_frame(cam) # Returns memoryview
4242

43-
for i in range(300):
43+
for i in range(100):
4444
print(f"iteration {i}")
4545
webcam.recapture_frame(cam) #refresh memview
4646
#memview =
4747
#bytes_obj = bytes(memview)
4848
#print(f"got bytes: {len(bytes_obj)}")
4949
#image_dsc.data = bytes_obj
5050
#image_dsc.data = static_bytes_obj
51-
#image_dsc.data = webcam.capture_frame(cam) # Returns memoryview
51+
image_dsc.data = webcam.capture_frame(cam) # Returns memoryview
5252
image.set_src(image_dsc)
5353
#image.invalidate()
5454
lv.task_handler()

0 commit comments

Comments
 (0)