@@ -112,12 +112,6 @@ def adc_to_voltage(adc_value):
112112except 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 ===
122116from mpos import SensorManager
123117
@@ -128,11 +122,82 @@ def adc_to_voltage(adc_value):
128122# === CAMERA HARDWARE ===
129123from 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
132193CameraManager .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
138203print ("waveshare_esp32_s3_touch_lcd_2.py finished" )
0 commit comments