Skip to content

Commit 660668f

Browse files
committed
Add boot.py example to initialize display and mount SD card
Also update Example 1 to remove display driver dependency, now requires hardware to be initialized by boot.py
1 parent 918cb58 commit 660668f

File tree

2 files changed

+75
-23
lines changed

2 files changed

+75
-23
lines changed

examples/boot.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Import the machine module to access hardware features
2+
import machine
3+
4+
# Initialize SPI bus, assuming default pins on bus 0. You may need to adjust
5+
# this based on your specific board and configuration
6+
spi = machine.SPI(0)
7+
8+
# Initialize display, if available
9+
try:
10+
# Import a display driver module. This example assumes the ST7789, which is
11+
# a very popular display driver for embedded systems. Moreover, this example
12+
# uses an SPI-based driver, so it should work on any platform, but it's not
13+
# always the fastest option
14+
import st7789_spi
15+
16+
# Create a display object. This will depend on the display driver you are
17+
# using, and you may need to adjust the parameters based on your specific
18+
# display and board configuration
19+
display = st7789_spi.ST7789_SPI(width=240,
20+
height=320,
21+
spi=spi,
22+
pin_dc=16,
23+
pin_cs=17,
24+
rotation=1)
25+
except ImportError:
26+
print("boot.py - Display driver module not found, skipping display initialization.")
27+
28+
# Initialize SD card, if available
29+
try:
30+
# Import the SD card module. This is often not installed by default in
31+
# MicroPython, so you may need to install it manually. For example, you can
32+
# use `mpremote mip install sdcard`
33+
import sdcard
34+
35+
# This example assumes the SD card is on the same SPI bus as the display
36+
# with a different chip select pin. You may need to adjust this based on
37+
# your specific board and configuration
38+
sd_cs = machine.Pin(7, machine.Pin.OUT)
39+
sd = sdcard.SDCard(spi, sd_cs)
40+
41+
# Mount the SD card to the filesystem under the "/sd" directory, which makes
42+
# it accessible just like the normal MicroPython filesystem
43+
import uos
44+
vfs = uos.VfsFat(sd)
45+
uos.mount(vfs, "/sd")
46+
except ImportError:
47+
print("boot.py - sdcard module not found, skipping SD card initialization.")
48+
except OSError:
49+
print("boot.py - Failed to mount SD card, skipping SD card initialization.")
50+
51+
# Set the SPI bus baudrate (note - the sdcard module overrides the baudrate upon
52+
# initialization, so the baudrate should be set after that). It is recommended
53+
# to use the fastest baudrate supported by your board, display, and SD card to
54+
# minimize latency
55+
spi.init(baudrate=24_000_000)
56+
57+
# Clear the display to wipe any previous content. This is optional, but it's
58+
# recommended to ensure a clean slate
59+
if hasattr(display, 'clear'):
60+
display.clear()

examples/ex01_hello_opencv.py

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,6 @@
66
# implementation; ulab NumPy is a lightweight version of standard NumPy
77
from ulab import numpy as np
88

9-
# Standard OpenCV leverages the host operating system to display images, but we
10-
# don't have that luxury in MicroPython. Instead, we need to import a display
11-
# driver. Any display driver can be used, as long as it implements an `imshow()`
12-
# method that takes an NumPy array as input
13-
from st7789_spi import ST7789_SPI
14-
15-
# Create a display object. This will depend on the display driver you are using,
16-
# and you may need to adjust the parameters based on your specific display and
17-
# board configuration
18-
display = ST7789_SPI(width=240,
19-
height=320,
20-
spi_id=0,
21-
pin_cs=17,
22-
pin_dc=16,
23-
rotation=1)
24-
259
# Initialize an image (NumPy array) to be displayed, just like in any other
2610
# Python environment! Here we create a 240x320 pixel image with 3 color channels
2711
# (BGR order, like standard OpenCV) and a data type of `uint8` (you should
@@ -39,17 +23,25 @@
3923
# Note - Most OpenCV functions return the resulting image. It's redundant for
4024
# the drawing functions and often ignored, but if you call those functions from
4125
# the REPL without assigning it to a variable, the entire array will be printed.
42-
# To avoid this, you can simply re-assign the image, which has no effect other
43-
# than preventing the output from being printed
26+
# To avoid this, you can simply re-assign the image variable (for example,
27+
# `img = cv2.function(...)`)
4428

4529
# And the obligatory "Hello OpenCV" text! This time in red
4630
img = cv2.putText(img, "Hello OpenCV!", (50, 200), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
4731

4832
# Once we have an image ready to show, just call `cv2.imshow()`, almost like any
49-
# other Python environment! The only difference is that we need to pass the
50-
# display object we created earlier as the first argument, instead of a window
51-
# name string. Alternatively, you can call `display.imshow(img)` directly
52-
cv2.imshow(display, img)
33+
# other Python environment! However, there is one important difference:
34+
#
35+
# Standard OpenCV leverages the host operating system to display images in
36+
# windows, but we don't have that luxury in MicroPython. So there is an API
37+
# change to `cv2.imshow()` to accommodate this: instead of passing a window name
38+
# string as the first argument to `cv2.imshow()`, we pass a display driver. Any
39+
# display driver can be used, as long as it implements an `imshow()` method that
40+
# takes a NumPy array as input
41+
#
42+
# This example assumes a display driver called `display` has been initialized by
43+
# a `boot.py` script. See the example `boot.py` script for more details
44+
cv2.imshow(display, img) # Can alternatively call `display.imshow(img)`
5345

5446
# Standard OpenCV requires a call to `cv2.waitKey()` to process events and
5547
# actually display the image. However the display driver shows the image
@@ -61,4 +53,4 @@
6153
#
6254
# Note - Some MicroPython IDEs (like Thonny) don't actually send any key presses
6355
# until you hit Enter on your keyboard
64-
key = cv2.waitKey(1) # Not necessary to display image, can remove if desired
56+
key = cv2.waitKey(0) # Not necessary to display image, can remove if desired

0 commit comments

Comments
 (0)