Skip to content

Commit d7b98f2

Browse files
waveshare_esp32_s3_touch_lcd_2: use generic CameraManager
1 parent bfd3804 commit d7b98f2

3 files changed

Lines changed: 80 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Builtin Apps:
77

88
Frameworks:
99
- SDCard: add support for SDIO/SD/MMC mode
10-
- CameraManager: add init and deinit callbacks
10+
- CameraManager and CameraActivity: work fully camera-agnostic
1111

1212
OS:
1313
- Scale MicroPythonOS boot logo down if necessary

internal_filesystem/lib/mpos/board/matouch_esp32_s3_2_8.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def io0_interrupt_handler(pin):
112112
from mpos import CameraManager
113113

114114
def init_cam(width, height, colormode):
115+
toreturn = None
115116
try:
116117
from camera import Camera, GrabMode, PixelFormat, FrameSize, GainCeiling
117118

@@ -121,7 +122,6 @@ def init_cam(width, height, colormode):
121122

122123
# Try to initialize, with one retry for I2C poweroff issue
123124
max_attempts = 3
124-
toreturn = None
125125
for attempt in range(max_attempts):
126126
try:
127127
cam = Camera(
@@ -160,22 +160,20 @@ def init_cam(width, height, colormode):
160160
InputManager.unregister_indev(indev)
161161
print("input disabled")
162162
except Exception as e:
163-
print(f"disabling indev got exception: {e}")
163+
print(f"init_cam: disabling indev got exception: {e}")
164164

165-
return toreturn
166165
except Exception as e:
167166
print(f"init_cam exception: {e}")
168-
return None
167+
168+
return toreturn
169169

170170
def deinit_cam(cam):
171171
cam.deinit()
172172
# Power off, otherwise it keeps using a lot of current
173173
try:
174174
from machine import Pin, I2C
175175
i2c = I2C(1, scl=Pin(38), sda=Pin(39)) # Adjust pins and frequency
176-
#devices = i2c.scan()
177-
#print([hex(addr) for addr in devices]) # finds it on 60 = 0x3C after init
178-
camera_addr = 0x3C # for OV5640
176+
camera_addr = 0x3C # for OV3660
179177
reg_addr = 0x3008
180178
reg_high = (reg_addr >> 8) & 0xFF # 0x30
181179
reg_low = reg_addr & 0xFF # 0x08
@@ -219,7 +217,8 @@ def apply_cam_settings(cam_obj, prefs):
219217
vendor="OmniVision",
220218
init=init_cam,
221219
deinit=deinit_cam,
222-
capture=capture_cam
220+
capture=capture_cam,
221+
apply_settings=apply_cam_settings
223222
))
224223

225224
print("matouch_esp32_s3_2_8.py finished")

internal_filesystem/lib/mpos/board/waveshare_esp32_s3_touch_lcd_2.py

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,6 @@ def adc_to_voltage(adc_value):
112112
except Exception as e:
113113
print(f"Warning: powering off camera got exception: {e}")
114114

115-
# === AUDIO HARDWARE: Waveshare board has no buzzer or I2S audio so no need to initialize.
116-
117-
# === LED HARDWARE ===
118-
# Note: Waveshare board has no NeoPixel LEDs
119-
# LightsManager will not be initialized (functions will return False)
120-
121115
# === SENSOR HARDWARE ===
122116
from mpos import SensorManager
123117

@@ -128,11 +122,82 @@ def adc_to_voltage(adc_value):
128122
# === CAMERA HARDWARE ===
129123
from mpos import CameraManager
130124

125+
def init_cam(width, height, colormode):
126+
toreturn = None
127+
try:
128+
from camera import Camera, GrabMode, PixelFormat, FrameSize, GainCeiling
129+
130+
# Map resolution to FrameSize enum using CameraManager
131+
frame_size = CameraManager.resolution_to_framesize(width, height)
132+
print(f"init_internal_cam: Using FrameSize {frame_size} for {width}x{height}")
133+
134+
# Try to initialize, with one retry for I2C poweroff issue
135+
max_attempts = 3
136+
for attempt in range(max_attempts):
137+
try:
138+
cam = Camera(
139+
data_pins=[12,13,15,11,14,10,7,2],
140+
vsync_pin=6,
141+
href_pin=4,
142+
sda_pin=21,
143+
scl_pin=16,
144+
pclk_pin=9,
145+
xclk_pin=8,
146+
xclk_freq=20000000,
147+
powerdown_pin=-1,
148+
reset_pin=-1,
149+
pixel_format=PixelFormat.RGB565 if self.colormode else PixelFormat.GRAYSCALE,
150+
frame_size=frame_size,
151+
#grab_mode=GrabMode.WHEN_EMPTY,
152+
grab_mode=GrabMode.LATEST,
153+
fb_count=1
154+
)
155+
cam.set_vflip(True)
156+
toreturn=cam
157+
break
158+
except Exception as e:
159+
if attempt < max_attempts-1:
160+
print(f"init_cam attempt {attempt} failed: {e}, retrying...")
161+
else:
162+
print(f"init_cam final exception: {e}")
163+
break
164+
except Exception as e:
165+
print(f"init_cam exception: {e}")
166+
167+
return toreturn
168+
169+
def deinit_cam(cam):
170+
cam.deinit()
171+
# Power off, otherwise it keeps using a lot of current
172+
try:
173+
from machine import Pin, I2C
174+
i2c = I2C(1, scl=Pin(16), sda=Pin(21)) # Adjust pins and frequency
175+
camera_addr = 0x3C # for OV5640
176+
reg_addr = 0x3008
177+
reg_high = (reg_addr >> 8) & 0xFF # 0x30
178+
reg_low = reg_addr & 0xFF # 0x08
179+
power_off_command = 0x42 # Power off command
180+
i2c.writeto(camera_addr, bytes([reg_high, reg_low, power_off_command]))
181+
except Exception as e:
182+
print(f"Warning: powering off camera got exception: {e}")
183+
import time
184+
time.sleep_ms(100)
185+
186+
def capture_cam(cam_obj, colormode):
187+
return cam_obj.capture()
188+
189+
def apply_cam_settings(cam_obj, prefs):
190+
return CameraManager.ov_apply_camera_settings(cam_obj, prefs)
191+
131192
# Waveshare ESP32-S3-Touch-LCD-2 has OV5640 camera
132193
CameraManager.add_camera(CameraManager.Camera(
133194
lens_facing=CameraManager.CameraCharacteristics.LENS_FACING_BACK,
134195
name="OV5640",
135-
vendor="OmniVision"
196+
vendor="OmniVision",
197+
init=init_cam,
198+
deinit=deinit_cam,
199+
capture=capture_cam,
200+
apply_settings=apply_cam_settings
136201
))
137202

138203
print("waveshare_esp32_s3_touch_lcd_2.py finished")

0 commit comments

Comments
 (0)