Skip to content

Commit 2944b43

Browse files
committed
Add Example 3 - Camera
Update HM01B0 with methods `open()`, `read()`, and `release()` Add HM01B0 camera setup to example boot.py
1 parent 8c1abbd commit 2944b43

File tree

3 files changed

+92
-5
lines changed

3 files changed

+92
-5
lines changed

drivers/camera/hm01b0_pio.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from machine import Pin, I2C
33
from ulab import numpy as np
44
from time import sleep_us
5-
import time
5+
import cv2
66

77
# Derived from:
88
# https://github.com/openmv/openmv/blob/5acf5baf92b4314a549bdd068138e5df6cc0bac7/drivers/sensors/hm01b0.c
@@ -289,10 +289,35 @@ def start_pio_dma(self):
289289
ctrl = dma_ctrl
290290
)
291291

292-
Pin(self.pin_vsync).irq(
293-
trigger = Pin.IRQ_FALLING,
294-
handler = lambda pin: self._vsync_handler()
295-
)
292+
def active(self, active = None):
293+
if active == None:
294+
return self.sm.active()
295+
296+
self.sm.active(active)
297+
298+
if active:
299+
Pin(self.pin_vsync).irq(
300+
trigger = Pin.IRQ_FALLING,
301+
handler = lambda pin: self._vsync_handler()
302+
)
303+
else:
304+
Pin(self.pin_vsync).irq(
305+
handler = None
306+
)
307+
308+
def open(self):
309+
self.active(True)
310+
311+
def release(self):
312+
self.active(False)
313+
314+
def read(self):
315+
"""
316+
Reads a frame from the camera.
317+
Returns:
318+
tuple: (success, frame)
319+
"""
320+
return (True, cv2.cvtColor(self.buffer, cv2.COLOR_BayerRG2BGR))
296321

297322
def _vsync_handler(self):
298323
# Disable DMA before reconfiguring it

examples/boot.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
# this based on your specific board and configuration
66
spi = machine.SPI(0)
77

8+
# Initialize I2C bus, assuming default pins on bus 0. You may need to adjust
9+
# this based on your specific board and configuration
10+
i2c = machine.I2C(0)
11+
812
# Initialize display, if available
913
try:
1014
# Import a display driver module. This example assumes the ST7789, which is
@@ -64,3 +68,22 @@
6468
# Clear the display, if the driver supports it
6569
if hasattr(display, 'clear'):
6670
display.clear()
71+
72+
# Initialize camera, if available
73+
try:
74+
# Import a camera driver module. This example assumes the HM01B0, which is
75+
# a popular camera module for embedded systems. This example uses a PIO
76+
# driver, which is a peripheral interface only available on Raspberry Pi RP2
77+
# processors
78+
import hm01b0_pio
79+
80+
# Create a camera object. This will depend on the camera driver you are
81+
# using, and you may need to adjust the parameters based on your specific
82+
# camera and board configuration
83+
camera = hm01b0_pio.HM01B0_PIO(i2c,
84+
pin_d0=12,
85+
pin_vsync=13,
86+
pin_hsync=14,
87+
pin_pclk=15)
88+
except ImportError:
89+
print("boot.py - Camera driver module not found, skipping camera initialization.")

examples/ex03_camera.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Import OpenCV
2+
import cv2
3+
4+
# Open a camera, similar to any other Python environment! In standard OpenCV,
5+
# you would use `cv2.VideoCapture(0)` or similar, and OpenCV would leverage the
6+
# host operating system to open a camera object and return it as a
7+
# `cv2.VideoCapture` object. However, we don't have that luxury in MicroPython,
8+
# so a camera driver is required instead. Any camera driver can be used, as long
9+
# as it implements the same methods as the standard OpenCV `cv2.VideoCapture`
10+
# class, such as `open()`, `read()`, and `release()`
11+
#
12+
# This example assumes a camera driver called `camera` has been initialized by a
13+
# `boot.py` script. See the example `boot.py` script for more details
14+
camera.open()
15+
16+
# Loop to continuously read frames from the camera and display them
17+
while True:
18+
# Read a frame from the camera, just like any other Python environment! It
19+
# returns a tuple, where the first element is a boolean indicating success,
20+
# and the second element is the frame (NumPy array) read from the camera
21+
success, frame = camera.read()
22+
23+
# Check if the frame was read successfully
24+
if success == False:
25+
print("Error reading frame from camera")
26+
break
27+
28+
# Display the frame
29+
cv2.imshow(display, frame)
30+
31+
# Check for key presses
32+
key = cv2.waitKey(1)
33+
34+
# If the 'q' key is pressed, exit the loop
35+
if key == ord('q'):
36+
break
37+
38+
# Release the camera, just like in any other Python environment!
39+
camera.release()

0 commit comments

Comments
 (0)