Skip to content

Commit 2cfd27b

Browse files
committed
upate camera api to use static objects and add some guards to pin definitions
1 parent ab114fa commit 2cfd27b

File tree

2 files changed

+56
-29
lines changed

2 files changed

+56
-29
lines changed

src/camera_pins.h

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@
22
#ifndef MICROPY_CAMERA_MODEL_PINS_H
33
#define MICROPY_CAMERA_MODEL_PINS_H
44

5+
#ifdef __cplusplus
6+
extern "C" {
7+
#endif
8+
9+
#ifndef MICROPY_CAMERA_PIN_NONE
10+
#define MICROPY_CAMERA_PIN_NONE (-1)
11+
#endif
12+
513
#if defined(MICROPY_CAMERA_MODEL_WROVER_KIT)
6-
#define MICROPY_CAMERA_PIN_PWDN -1
7-
#define MICROPY_CAMERA_PIN_RESET -1
14+
#define MICROPY_CAMERA_PIN_PWDN MICROPY_CAMERA_PIN_NONE
15+
#define MICROPY_CAMERA_PIN_RESET MICROPY_CAMERA_PIN_NONE
816
#define MICROPY_CAMERA_PIN_XCLK 21
917
#define MICROPY_CAMERA_PIN_SIOD 26
1018
#define MICROPY_CAMERA_PIN_SIOC 27
@@ -22,8 +30,8 @@
2230
#define MICROPY_CAMERA_PIN_PCLK 22
2331

2432
#elif defined(MICROPY_CAMERA_MODEL_ESP_EYE)
25-
#define MICROPY_CAMERA_PIN_PWDN -1
26-
#define MICROPY_CAMERA_PIN_RESET -1
33+
#define MICROPY_CAMERA_PIN_PWDN MICROPY_CAMERA_PIN_NONE
34+
#define MICROPY_CAMERA_PIN_RESET MICROPY_CAMERA_PIN_NONE
2735
#define MICROPY_CAMERA_PIN_XCLK 4
2836
#define MICROPY_CAMERA_PIN_SIOD 18
2937
#define MICROPY_CAMERA_PIN_SIOC 23
@@ -41,7 +49,7 @@
4149
#define MICROPY_CAMERA_PIN_PCLK 25
4250

4351
#elif defined(MICROPY_CAMERA_MODEL_M5STACK_PSRAM) || defined(MICROPY_CAMERA_MODEL_M5STACK_UNITCAM)
44-
#define MICROPY_CAMERA_PIN_PWDN -1
52+
#define MICROPY_CAMERA_PIN_PWDN MICROPY_CAMERA_PIN_NONE
4553
#define MICROPY_CAMERA_PIN_RESET 15
4654
#define MICROPY_CAMERA_PIN_XCLK 27
4755
#define MICROPY_CAMERA_PIN_SIOD 25
@@ -60,7 +68,7 @@
6068
#define MICROPY_CAMERA_PIN_PCLK 21
6169

6270
#elif defined(MICROPY_CAMERA_MODEL_M5STACK_V2_PSRAM) || defined(MICROPY_CAMERA_MODEL_M5STACK_WIDE)
63-
#define MICROPY_CAMERA_PIN_PWDN -1
71+
#define MICROPY_CAMERA_PIN_PWDN MICROPY_CAMERA_PIN_NONE
6472
#define MICROPY_CAMERA_PIN_RESET 15
6573
#define MICROPY_CAMERA_PIN_XCLK 27
6674
#define MICROPY_CAMERA_PIN_SIOD 22
@@ -79,7 +87,7 @@
7987
#define MICROPY_CAMERA_PIN_PCLK 21
8088

8189
#elif defined(MICROPY_CAMERA_MODEL_M5STACK_ESP32CAM)
82-
#define MICROPY_CAMERA_PIN_PWDN -1
90+
#define MICROPY_CAMERA_PIN_PWDN MICROPY_CAMERA_PIN_NONE
8391
#define MICROPY_CAMERA_PIN_RESET 15
8492
#define MICROPY_CAMERA_PIN_XCLK 27
8593
#define MICROPY_CAMERA_PIN_SIOD 25
@@ -98,7 +106,7 @@
98106
#define MICROPY_CAMERA_PIN_PCLK 21
99107

100108
#elif defined(MICROPY_CAMERA_MODEL_M5STACK_CAMS3_UNIT)
101-
#define MICROPY_CAMERA_PIN_PWDN -1
109+
#define MICROPY_CAMERA_PIN_PWDN MICROPY_CAMERA_PIN_NONE
102110
#define MICROPY_CAMERA_PIN_RESET 21
103111
#define MICROPY_CAMERA_PIN_XCLK 11
104112
#define MICROPY_CAMERA_PIN_SIOD 17
@@ -117,8 +125,8 @@
117125
#define MICROPY_CAMERA_PIN_PCLK 12
118126

119127
#elif defined(MICROPY_CAMERA_MODEL_M5STACK_ATOM_S3R)
120-
#define MICROPY_CAMERA_PIN_PWDN -1 //needs to be low to run
121-
#define MICROPY_CAMERA_PIN_RESET -1
128+
#define MICROPY_CAMERA_PIN_PWDN MICROPY_CAMERA_PIN_NONE //needs to be low to run
129+
#define MICROPY_CAMERA_PIN_RESET MICROPY_CAMERA_PIN_NONE
122130
#define MICROPY_CAMERA_PIN_XCLK 21
123131
#define MICROPY_CAMERA_PIN_SIOD 12
124132
#define MICROPY_CAMERA_PIN_SIOC 9
@@ -137,7 +145,7 @@
137145

138146
#elif defined(MICROPY_CAMERA_MODEL_AI_THINKER)
139147
#define MICROPY_CAMERA_PIN_PWDN 32
140-
#define MICROPY_CAMERA_PIN_RESET -1
148+
#define MICROPY_CAMERA_PIN_RESET MICROPY_CAMERA_PIN_NONE
141149
#define MICROPY_CAMERA_PIN_XCLK 0
142150
#define MICROPY_CAMERA_PIN_SIOD 26
143151
#define MICROPY_CAMERA_PIN_SIOC 27
@@ -155,8 +163,8 @@
155163
#define MICROPY_CAMERA_PIN_PCLK 22
156164

157165
#elif defined(MICROPY_CAMERA_MODEL_XIAO_ESP32S3)
158-
#define MICROPY_CAMERA_PIN_PWDN -1
159-
#define MICROPY_CAMERA_PIN_RESET -1
166+
#define MICROPY_CAMERA_PIN_PWDN MICROPY_CAMERA_PIN_NONE
167+
#define MICROPY_CAMERA_PIN_RESET MICROPY_CAMERA_PIN_NONE
160168
#define MICROPY_CAMERA_PIN_XCLK 10
161169
#define MICROPY_CAMERA_PIN_SIOD 40
162170
#define MICROPY_CAMERA_PIN_SIOC 39
@@ -203,8 +211,8 @@
203211
#define MICROPY_CAMERA_PIN_PCLK 25
204212

205213
#elif defined(MICROPY_CAMERA_MODEL_ESP32S3_CAM_LCD)
206-
#define MICROPY_CAMERA_PIN_PWDN -1
207-
#define MICROPY_CAMERA_PIN_RESET -1
214+
#define MICROPY_CAMERA_PIN_PWDN MICROPY_CAMERA_PIN_NONE
215+
#define MICROPY_CAMERA_PIN_RESET MICROPY_CAMERA_PIN_NONE
208216
#define MICROPY_CAMERA_PIN_XCLK 40
209217
#define MICROPY_CAMERA_PIN_SIOD 17
210218
#define MICROPY_CAMERA_PIN_SIOC 18
@@ -222,8 +230,8 @@
222230
#define MICROPY_CAMERA_PIN_PCLK 11
223231

224232
#elif defined(MICROPY_CAMERA_MODEL_ESP32S3_EYE) || defined(MICROPY_CAMERA_MODEL_FREENOVE_ESP32S3_CAM)
225-
#define MICROPY_CAMERA_PIN_PWDN -1
226-
#define MICROPY_CAMERA_PIN_RESET -1
233+
#define MICROPY_CAMERA_PIN_PWDN MICROPY_CAMERA_PIN_NONE
234+
#define MICROPY_CAMERA_PIN_RESET MICROPY_CAMERA_PIN_NONE
227235
#define MICROPY_CAMERA_PIN_XCLK 15
228236
#define MICROPY_CAMERA_PIN_SIOD 4
229237
#define MICROPY_CAMERA_PIN_SIOC 5
@@ -241,8 +249,8 @@
241249
#define MICROPY_CAMERA_PIN_PCLK 13
242250

243251
#elif defined(MICROPY_CAMERA_MODEL_DFRobot_ESP32S3)
244-
#define MICROPY_CAMERA_PIN_PWDN -1
245-
#define MICROPY_CAMERA_PIN_RESET -1
252+
#define MICROPY_CAMERA_PIN_PWDN MICROPY_CAMERA_PIN_NONE
253+
#define MICROPY_CAMERA_PIN_RESET MICROPY_CAMERA_PIN_NONE
246254
#define MICROPY_CAMERA_PIN_XCLK 45
247255
#define MICROPY_CAMERA_PIN_SIOD 1
248256
#define MICROPY_CAMERA_PIN_SIOC 2
@@ -279,8 +287,8 @@
279287
#define MICROPY_CAMERA_PIN_PCLK 21
280288

281289
#elif defined(MICROPY_CAMERA_MODEL_TTGO_T_CAMERA_PLUS)
282-
#define MICROPY_CAMERA_PIN_PWDN -1
283-
#define MICROPY_CAMERA_PIN_RESET -1
290+
#define MICROPY_CAMERA_PIN_PWDN MICROPY_CAMERA_PIN_NONE
291+
#define MICROPY_CAMERA_PIN_RESET MICROPY_CAMERA_PIN_NONE
284292
#define MICROPY_CAMERA_PIN_XCLK 4
285293
#define MICROPY_CAMERA_PIN_SIOD 18
286294
#define MICROPY_CAMERA_PIN_SIOC 23
@@ -299,8 +307,8 @@
299307

300308
#elif defined(MICROPY_CAMERA_MODEL_NEW_ESPS3_RE1_0)
301309
// aliexpress board with label RE:1.0, uses slow 8MB QSPI PSRAM, only 4MB addressable
302-
#define MICROPY_CAMERA_PIN_PWDN -1
303-
#define MICROPY_CAMERA_PIN_RESET -1
310+
#define MICROPY_CAMERA_PIN_PWDN MICROPY_CAMERA_PIN_NONE
311+
#define MICROPY_CAMERA_PIN_RESET MICROPY_CAMERA_PIN_NONE
304312
#define MICROPY_CAMERA_PIN_XCLK 10
305313
#define MICROPY_CAMERA_PIN_SIOD 21
306314
#define MICROPY_CAMERA_PIN_SIOC 14
@@ -318,11 +326,11 @@
318326
#define MICROPY_CAMERA_PIN_PCLK 7
319327

320328
#elif defined(MICROPY_CAMERA_MODEL_XENOIONEX)
321-
#define MICROPY_CAMERA_PIN_PWDN -1
322-
#define MICROPY_CAMERA_PIN_RESET -1
329+
#define MICROPY_CAMERA_PIN_PWDN MICROPY_CAMERA_PIN_NONE
330+
#define MICROPY_CAMERA_PIN_RESET MICROPY_CAMERA_PIN_NONE
323331
#define MICROPY_CAMERA_PIN_XCLK 1 // Can use
324-
#define MICROPY_CAMERA_PIN_SIOD 8 // Can use other i2c SDA pin, set this to -1 | If not using i2c set to 8 or 47
325-
#define MICROPY_CAMERA_PIN_SIOC 9 // Can use other i2c SCL pin, set this to -1 | If not using i2c set to 9 or 21
332+
#define MICROPY_CAMERA_PIN_SIOD 8 // Can use other i2c SDA pin, set this to MICROPY_CAMERA_PIN_NONE | If not using i2c set to 8 or 47
333+
#define MICROPY_CAMERA_PIN_SIOC 9 // Can use other i2c SCL pin, set this to MICROPY_CAMERA_PIN_NONE | If not using i2c set to 9 or 21
326334

327335
#define MICROPY_CAMERA_PIN_D7 3 //D7
328336
#define MICROPY_CAMERA_PIN_D6 18 //D6
@@ -337,4 +345,9 @@
337345
#define MICROPY_CAMERA_PIN_PCLK 2
338346

339347
#endif // definition of camera pins for different boards
348+
349+
#ifdef __cplusplus
350+
}
351+
#endif
352+
340353
#endif // MICROPY_CAMERA_MODEL_PINS_H

src/modcamera_api.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
typedef struct mp_camera_obj_t mp_camera_obj;
3939
const mp_obj_type_t camera_type;
4040

41+
// Static camera object to ensure only one instance exists at a time
42+
static mp_camera_obj_t mp_camera_singleton = { .base = { NULL } };
43+
4144
//Constructor
4245
static mp_obj_t mp_camera_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
4346
enum { ARG_data_pins, ARG_pixel_clock_pin, ARG_vsync_pin, ARG_href_pin, ARG_sda_pin, ARG_scl_pin, ARG_xclock_pin, ARG_xclock_frequency, ARG_powerdown_pin, ARG_reset_pin, ARG_pixel_format, ARG_frame_size, ARG_jpeg_quality, ARG_fb_count, ARG_grab_mode, ARG_init, NUM_ARGS };
@@ -126,8 +129,19 @@ static mp_obj_t mp_camera_make_new(const mp_obj_type_t *type, size_t n_args, siz
126129
int8_t fb_count = args[ARG_fb_count].u_int;
127130
mp_camera_grabmode_t grab_mode = args[ARG_grab_mode].u_int;
128131

129-
mp_camera_obj_t *self = mp_obj_malloc_with_finaliser(mp_camera_obj_t, &camera_type);
130-
self->base.type = &camera_type;
132+
// Get static singleton object
133+
mp_camera_obj_t *self = &mp_camera_singleton;
134+
135+
// If camera was already initialized, deinit it first
136+
bool first_init = false;
137+
if (self->base.type == NULL) {
138+
// First time initialization
139+
self->base.type = &camera_type;
140+
first_init = true;
141+
} else {
142+
// Camera already exists, deinit it before reinitializing
143+
mp_camera_hal_deinit(self);
144+
}
131145

132146
mp_camera_hal_construct(self, data_pins, xclock_pin, pixel_clock_pin, vsync_pin, href_pin, powerdown_pin, reset_pin,
133147
sda_pin, scl_pin, xclock_frequency, pixel_format, frame_size, jpeg_quality, fb_count, grab_mode);

0 commit comments

Comments
 (0)