This program is designed for the ESP32-S3-EYE development kit.
Your project will use a specific set of CircuitPython libraries and the code.py file. To get everything you need, click on the Download Project Bundle link below, and uncompress the .zip file.
Drag the contents of the uncompressed bundle directory onto your Feather board's CIRCUITPY drive, replacing any existing files or directories with the same names, and adding any new ones that are necessary.
When CircuitPython restarts, the live camera view will be shown on the LCD. You can click the button marked "BOOT" to switch between live camera view and a color-bar test pattern. The test pattern is shown here. A small black sticker has been placed over the extremely bright green power LED.
# SPDX-FileCopyrightText: Copyright (c) 2022 Jeff Epler for Adafruit Industries # # SPDX-License-Identifier: MIT """ Use the built-in LCD as a viewfinder for the camera This example requires: * ESP32-S3-EYE development kit from Espressif To use: Copy the project bundle to CIRCUITPY. """ import struct import adafruit_ticks import board import displayio import espcamera import keypad button = keypad.Keys((board.BOOT,), value_when_pressed=False) cam = espcamera.Camera( data_pins=board.CAMERA_DATA, external_clock_pin=board.CAMERA_XCLK, pixel_clock_pin=board.CAMERA_PCLK, vsync_pin=board.CAMERA_VSYNC, href_pin=board.CAMERA_HREF, pixel_format=espcamera.PixelFormat.RGB565, frame_size=espcamera.FrameSize.R240X240, i2c=board.I2C(), external_clock_frequency=20_000_000, framebuffer_count=2, grab_mode=espcamera.GrabMode.WHEN_EMPTY) cam.vflip = True board.DISPLAY.auto_refresh = False display_bus = board.DISPLAY.bus display_bus.send(36, struct.pack(">hh", 0, 239)) display_bus.send(42, struct.pack(">hh", 0, 239)) display_bus.send(43, struct.pack(">hh", 0, 80+239)) t0 = adafruit_ticks.ticks_ms() while True: if (event := button.events.get()) and event.pressed: cam.colorbar = not cam.colorbar frame = cam.take(1) if isinstance(frame, displayio.Bitmap): display_bus.send(44, frame) t1 = adafruit_ticks.ticks_ms() fps = 1000 / adafruit_ticks.ticks_diff(t1, t0) print(f"{fps:3.1f}fps") # typically runs at about 25fps t0 = t1
How it works
Libraries
First, the code imports the necessary libraries. In particular:
- espcamera to interface with the camera
- displayio to use Bitmap objects
import struct import adafruit_ticks import board import displayio import espcamera import keypad
Test-pattern Toggle Button
The "Boot" button on this board can be used as a regular button with keypad.Keys
. The other 4 buttons share a single pin, so using them is more complicated.
button = keypad.Keys((board.BOOT,), value_when_pressed=False)
Camera
Next, the camera object is created. Because the LCD's size is 240x240, we use the 240x240 resolution mode, FrameSize.R240X240
when setting up the camera. Setting the vflip
property makes the LCD show the image right side up. Depending whether you want a mirror mode or not you can set the hmirror
property to True
.
cam = espcamera.Camera( data_pins=board.CAMERA_DATA, external_clock_pin=board.CAMERA_XCLK, pixel_clock_pin=board.CAMERA_PCLK, vsync_pin=board.CAMERA_VSYNC, href_pin=board.CAMERA_HREF, pixel_format=esp32_camera.PixelFormat.RGB565, frame_size=esp32_camera.FrameSize.R240X240, i2c=board.I2C(), external_clock_frequency=20_000_000, framebuffer_count=2, grab_mode=esp32_camera.GrabMode.WHEN_EMPTY) cam.vflip = True
Forever Loop to Display and check button
Whenever a "pressed" event arrives, toggle the "colorbar" property of the camera. It defaults to False
(off), so pressing BOOT once toggles it on and pressing BOOT again toggles it back off.
To make the display refresh as quickly as possible, we bypass displayio and send out our bitmap directly to the LCD. Just for bragging rights, we calculate the Frames Per Second (FPS) that we update the LCD. Refresh rates of 25FPS can be obtained with this code.
while True: if (event := button.events.get()) and event.pressed: cam.colorbar = not cam.colorbar frame = cam.take(1) if isinstance(frame, displayio.Bitmap): display_bus.send(44, frame) t1 = adafruit_ticks.ticks_ms() fps = 1000 / adafruit_ticks.ticks_diff(t1, t0) print(f"{fps:3.1f}fps") # typically runs at about 25fps t0 = t1
Text editor powered by tinymce.