forked from cnadler86/micropython-camera-API
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodcamera.h
More file actions
302 lines (263 loc) · 10.8 KB
/
Copy pathmodcamera.h
File metadata and controls
302 lines (263 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* The MIT License (MIT)
*
* Code based on circuitpython camera API by Jeff Epler
* Copyright (c) 2022 Jeff Epler for Adafruit Industries
* Adaptation to MicroPython by Christopher Nadler
* Copyright (c) 2024 Christopher Nadler
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_MODCAMERA_H
#define MICROPY_INCLUDED_MODCAMERA_H
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "py/mperrno.h"
#include "py/mphal.h" //TODO: Maybe we can add here MICROPY_PY_MACHINE_CAMERA (0) ?
#include "py/runtime.h"
#include "py/obj.h"
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
// ESP32-Camera specifics-> could go in separate header file, if this project starts implementing more ports.
#include "esp_camera.h"
#include "sensor.h"
#include "camera_pins.h"
#if defined (MICROPY_CAMERA_PIN_SIOD) && defined (MICROPY_CAMERA_PIN_SIOC) && defined (MICROPY_CAMERA_PIN_D0) && defined (MICROPY_CAMERA_PIN_D1) && defined (MICROPY_CAMERA_PIN_D2) && \
defined (MICROPY_CAMERA_PIN_D3) && defined (MICROPY_CAMERA_PIN_D4) && defined (MICROPY_CAMERA_PIN_D5) && defined (MICROPY_CAMERA_PIN_D6) && defined (MICROPY_CAMERA_PIN_D7) && \
defined (MICROPY_CAMERA_PIN_PCLK) && defined (MICROPY_CAMERA_PIN_VSYNC) && defined (MICROPY_CAMERA_PIN_HREF) && defined (MICROPY_CAMERA_PIN_XCLK)
#define MICROPY_CAMERA_ALL_REQ_PINS_DEFINED (1)
#endif
#ifndef MICROPY_CAMERA_PIN_PWDN
#define MICROPY_CAMERA_PIN_PWDN (-1)
#endif
#ifndef MICROPY_CAMERA_PIN_RESET
#define MICROPY_CAMERA_PIN_RESET (-1)
#endif
#ifndef MICROPY_CAMERA_XCLK_FREQ
#define MICROPY_CAMERA_XCLK_FREQ (20)
#endif
#if !defined (MICROPY_CAMERA_GRAB_MODE) && defined (CONFIG_IDF_TARGET_ESP32S3)
#define MICROPY_CAMERA_GRAB_MODE CAMERA_GRAB_LATEST
#elif !defined(MICROPY_CAMERA_GRAB_MODE)
#define MICROPY_CAMERA_GRAB_MODE CAMERA_GRAB_WHEN_EMPTY
#endif
#if !defined (MICROPY_CAMERA_FB_COUNT) && defined (CONFIG_IDF_TARGET_ESP32S3)
#define MICROPY_CAMERA_FB_COUNT (2)
#elif !defined(MICROPY_CAMERA_FB_COUNT)
#define MICROPY_CAMERA_FB_COUNT (1)
#endif
#ifndef MICROPY_CAMERA_DEFAULT_FRAME_SIZE
#define MICROPY_CAMERA_DEFAULT_FRAME_SIZE FRAMESIZE_QQVGA
#endif
#ifndef MICROPY_CAMERA_DEFAULT_PIXEL_FORMAT
#define MICROPY_CAMERA_DEFAULT_PIXEL_FORMAT PIXFORMAT_RGB565
#endif
#ifndef MICROPY_CAMERA_JPEG_QUALITY
#define MICROPY_CAMERA_JPEG_QUALITY (85)
#endif
typedef pixformat_t hal_camera_pixformat_t;
typedef framesize_t hal_camera_framesize_t;
typedef camera_grab_mode_t hal_camera_grabmode_t;
typedef gainceiling_t hal_camera_gainceiling_t;
typedef struct hal_camera_obj {
mp_obj_base_t base;
camera_config_t camera_config;
bool initialized;
camera_fb_t *captured_buffer;
} hal_camera_obj_t;
#endif // CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
typedef hal_camera_obj_t mp_camera_obj_t;
typedef hal_camera_pixformat_t mp_camera_pixformat_t;
typedef hal_camera_framesize_t mp_camera_framesize_t;
typedef hal_camera_grabmode_t mp_camera_grabmode_t;
typedef hal_camera_gainceiling_t mp_camera_gainceiling_t;
/**
* @brief Constructs the camera hardware abstraction layer.
* @details The Port-plattform shall define a default pwm-time source and also frame buffer location (no input)
*
* @param self Pointer to the camera object.
* @param data_pins Array of data pins.
* @param external_clock_pin External clock pin.
* @param pixel_clock_pin Pixel clock pin.
* @param vsync_pin VSYNC pin.
* @param href_pin HREF pin.
* @param powerdown_pin Power down pin.
* @param reset_pin Reset pin.
* @param sccb_sda_pin SCCB SDA pin.
* @param sccb_scl_pin SCCB SCL pin.
* @param xclk_freq_hz External clock frequency in Hz.
* @param pixel_format Pixel format.
* @param frame_size Frame size.
* @param jpeg_quality JPEG quality.
* @param fb_count Number of framebuffers.
* @param grab_mode Grab mode (single-shot/when-empty OR continous/latest)
*/
extern void mp_camera_hal_construct(
mp_camera_obj_t *self,
int8_t data_pins[8],
int8_t external_clock_pin,
int8_t pixel_clock_pin,
int8_t vsync_pin,
int8_t href_pin,
int8_t powerdown_pin,
int8_t reset_pin,
int8_t sccb_sda_pin,
int8_t sccb_scl_pin,
int8_t sccb_i2c_port,
int32_t xclk_freq_hz,
mp_camera_pixformat_t pixel_format,
mp_camera_framesize_t frame_size,
int8_t jpeg_quality,
int8_t fb_count,
mp_camera_grabmode_t grab_mode);
/**
* @brief Initializes the camera hardware abstraction layer. Will be called during API constructor
*
* @param self Pointer to the camera object.
*/
extern void mp_camera_hal_init(mp_camera_obj_t *self); //since we are not passing handles at construction, init() is used to create those handles
/**
* @brief Deinitializes the camera hardware abstraction layer.
*
* @param self Pointer to the camera object.
*/
extern void mp_camera_hal_deinit(mp_camera_obj_t *self);
/**
* @brief Returns true, if camera is initialized.
*
* @param self Pointer to the camera object.
*/
extern bool mp_camera_hal_initialized(mp_camera_obj_t *self);
/**
* @brief Reconfigures the camera hardware abstraction layer.
*
* @param self Pointer to the camera object.
* @param frame_size Frame size.
* @param pixel_format Pixel format.
* @param grab_mode Grab mode.
* @param fb_count Number of framebuffers.
*/
extern void mp_camera_hal_reconfigure(mp_camera_obj_t *self, mp_camera_framesize_t frame_size, mp_camera_pixformat_t pixel_format, mp_camera_grabmode_t grab_mode, mp_int_t fb_count);
/**
* @brief Captures an image and returns it as mp_obj_t (e.g. mp_obj_new_memoryview).
*
* @param self Pointer to the camera object.
* @return Captured image as micropython object.
*/
extern mp_obj_t mp_camera_hal_capture(mp_camera_obj_t *self);
/**
* @brief Returns true, if a frame is available.
*
* @param self Pointer to the camera object.
* @return True, if a frame is available.
*/
extern mp_obj_t mp_camera_hal_frame_available(mp_camera_obj_t *self);
/**
* @brief Frees the buffer of the camera object.
*
* @param self Pointer to the camera object.
*/
extern void mp_camera_hal_free_buffer(mp_camera_obj_t *self);
/**
* @brief Table mapping pixel formats API to their corresponding values at HAL.
* @details Needs to be defined in the port-specific implementation.
*/
extern const mp_rom_map_elem_t mp_camera_hal_pixel_format_table[9];
/**
* @brief Table mapping frame sizes API to their corresponding values at HAL.
* @details Needs to be defined in the port-specific implementation.
*/
extern const mp_rom_map_elem_t mp_camera_hal_frame_size_table[31];
/**
* @brief Table mapping gainceiling API to their corresponding values at HAL.
* @details Needs to be defined in the port-specific implementation.
*/
extern const mp_rom_map_elem_t mp_camera_hal_gainceiling_table[7];
/**
* @brief Table mapping grab mode API to their corresponding values at HAL.
* @details Needs to be defined in the port-specific implementation.
*/
extern const mp_rom_map_elem_t mp_camera_hal_grab_mode_table[2];
#define DECLARE_CAMERA_HAL_GET(type, name) \
extern type mp_camera_hal_get_##name(mp_camera_obj_t *self);
#define DECLARE_CAMERA_HAL_SET(type, name) \
extern void mp_camera_hal_set_##name(mp_camera_obj_t *self, type value);
/**
* @brief Helper functions to get and set sensor properties.
* @details The functions are used to get and set sensor properties in the camera object.
* Needs to be defined in the port-specific implementation.
*/
#define DECLARE_CAMERA_HAL_GETSET(type, name) \
DECLARE_CAMERA_HAL_GET(type, name) \
DECLARE_CAMERA_HAL_SET(type, name)
DECLARE_CAMERA_HAL_GETSET(bool, aec2)
DECLARE_CAMERA_HAL_GETSET(int, aec_value)
DECLARE_CAMERA_HAL_GETSET(int, ae_level)
DECLARE_CAMERA_HAL_GETSET(int, agc_gain)
DECLARE_CAMERA_HAL_GETSET(bool, awb_gain)
DECLARE_CAMERA_HAL_GETSET(int, brightness)
DECLARE_CAMERA_HAL_GETSET(bool, bpc)
DECLARE_CAMERA_HAL_GETSET(bool, colorbar)
DECLARE_CAMERA_HAL_GETSET(int, contrast)
DECLARE_CAMERA_HAL_GETSET(bool, dcw)
DECLARE_CAMERA_HAL_GETSET(int, denoise)
DECLARE_CAMERA_HAL_GETSET(bool, exposure_ctrl)
DECLARE_CAMERA_HAL_GETSET(mp_camera_gainceiling_t, gainceiling)
DECLARE_CAMERA_HAL_GETSET(bool, gain_ctrl)
DECLARE_CAMERA_HAL_GETSET(bool, hmirror)
DECLARE_CAMERA_HAL_GETSET(bool, lenc)
DECLARE_CAMERA_HAL_GETSET(int, quality)
DECLARE_CAMERA_HAL_GETSET(bool, raw_gma)
DECLARE_CAMERA_HAL_GETSET(int, saturation)
DECLARE_CAMERA_HAL_GETSET(int, sharpness)
DECLARE_CAMERA_HAL_GETSET(int, special_effect)
DECLARE_CAMERA_HAL_GETSET(bool, vflip)
DECLARE_CAMERA_HAL_GETSET(int, wb_mode)
DECLARE_CAMERA_HAL_GETSET(bool, whitebal)
DECLARE_CAMERA_HAL_GETSET(bool, wpc)
DECLARE_CAMERA_HAL_GET(int, address)
DECLARE_CAMERA_HAL_GET(int, fb_count)
DECLARE_CAMERA_HAL_GETSET(mp_camera_framesize_t, frame_size)
DECLARE_CAMERA_HAL_GET(camera_grab_mode_t, grab_mode)
DECLARE_CAMERA_HAL_GET(mp_camera_framesize_t, max_frame_size)
DECLARE_CAMERA_HAL_GET(mp_camera_pixformat_t, pixel_format)
DECLARE_CAMERA_HAL_GET(int, pixel_height)
DECLARE_CAMERA_HAL_GET(int, pixel_width)
DECLARE_CAMERA_HAL_GET(const char *, sensor_name)
DECLARE_CAMERA_HAL_GET(bool, supports_jpeg)
/**
* @brief Sets the raw resolution parameters including ROI (Region of Interest).
*
* @param self Pointer to the camera object.
* @param startX X start position.
* @param startY Y start position.
* @param endX X end position.
* @param endY Y end position.
* @param offsetX X offset.
* @param offsetY Y offset.
* @param totalX Total X size.
* @param totalY Total Y size.
* @param outputX Output X size.
* @param outputY Output Y size.
* @param scale Enable scaling.
* @param binning Enable binning.
* @return 0 on success, negative value on error.
*/
extern int mp_camera_hal_set_res_raw(mp_camera_obj_t *self, int startX, int startY, int endX, int endY, int offsetX, int offsetY, int totalX, int totalY, int outputX, int outputY, bool scale, bool binning);
#endif // MICROPY_INCLUDED_MODCAMERA_H