1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 #include "py/mperrno.h"
6 #include "py/obj.h"
7 #include "py/runtime.h"
8 #include "py/builtin.h"
9
10 #include "ulog/ulog.h"
11 #include "base/modules/c/include/WrapperIHaasDataInput.h"
12
13 #define LOG_TAG "DATAINPUT"
14
15 extern const mp_obj_type_t minicv_datainput_type;
16 // this is the actual C-structure for our new object
17 typedef struct
18 {
19 // base represents some basic information, like type
20 mp_obj_base_t Base;
21 // a member created by us
22 char *ModuleName;
23 DataInputType_t mDataInputType;
24 CameraNumber_t mCameraNum;
25 char *mFileName;
26 void *mInstance;
27 } mp_datainput_obj_t;
28
datainput_obj_print(const mp_print_t * print,mp_obj_t self_in,mp_print_kind_t kind)29 void datainput_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind)
30 {
31 LOGD(LOG_TAG, "entern %s;\n", __func__);
32 mp_datainput_obj_t *self = MP_OBJ_TO_PTR(self_in);
33 mp_printf(print, "ModuleName(%s)", self->ModuleName);
34 }
35
datainput_obj_make_new(const mp_obj_type_t * type,size_t n_args,size_t n_kw,const mp_obj_t * args)36 STATIC mp_obj_t datainput_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args)
37 {
38 LOGD(LOG_TAG, "entern %s;\n", __func__);
39 mp_datainput_obj_t* driver_obj = m_new_obj(mp_datainput_obj_t);
40 if (!driver_obj) {
41 mp_raise_OSError(MP_EINVAL);
42 }
43
44 driver_obj->Base.type = &minicv_datainput_type;
45 driver_obj->ModuleName = "minicv-DataInput";
46 driver_obj->mDataInputType = DATA_INPUT_SOURCE_NONE;
47 driver_obj->mCameraNum = DATA_INPUT_CAMERA_NUMBER_NONE;
48 driver_obj->mFileName = NULL;
49 driver_obj->mInstance = NULL;
50
51 return MP_OBJ_FROM_PTR(driver_obj);
52 }
53
obj_open(size_t n_args,const mp_obj_t * args)54 STATIC mp_obj_t obj_open(size_t n_args, const mp_obj_t *args)
55 {
56 LOGD(LOG_TAG, "entern %s; n_args = %d;\n", __func__, n_args);
57 int ret = -1;
58 void* instance = NULL;
59 if (n_args < 3)
60 {
61 LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
62 return mp_const_none;
63 }
64 mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
65 mp_datainput_obj_t* driver_obj = (mp_datainput_obj_t *)self;
66 if (driver_obj == NULL)
67 {
68 LOGE(LOG_TAG, "driver_obj is NULL\n");
69 return mp_const_none;
70 }
71
72 if (driver_obj->mInstance != NULL)
73 {
74 LOGE(LOG_TAG, "Module has been opened, please clode first\n");
75 return mp_const_none;
76 }
77
78 driver_obj->mDataInputType = (DataInputType_t)mp_obj_get_int(args[1]);
79 driver_obj->mFileName = (char *)mp_obj_str_get_str(args[2]);
80 LOGD(LOG_TAG, "%s:mDataInputType = %d;\n", __func__, driver_obj->mDataInputType);
81 LOGD(LOG_TAG, "%s:mFileName = %s;\n", __func__, driver_obj->mFileName);
82 instance = DataInputCreateInstance(driver_obj->mDataInputType);
83 driver_obj->mInstance = instance;
84 if (instance == NULL)
85 {
86 LOGE(LOG_TAG, "DataInputCreateInstance failed\n");
87 return mp_const_none;
88 }
89
90 ret = DataInputOpen(instance, driver_obj->mFileName);
91 if(ret)
92 {
93 LOGE(LOG_TAG, "\r\n=====datainput open failed===\r\n");
94 return mp_const_none;
95 }
96 return mp_const_none;
97 }
98 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(datainput_obj_open, 3, obj_open);
99
obj_openCamera(size_t n_args,const mp_obj_t * args)100 STATIC mp_obj_t obj_openCamera(size_t n_args, const mp_obj_t *args)
101 {
102 LOGD(LOG_TAG, "entern %s; n_args = %d;\n", __func__, n_args);
103 int ret = -1;
104 void* instance = NULL;
105 if (n_args < 3)
106 {
107 LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
108 return mp_const_none;
109 }
110 mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
111 mp_datainput_obj_t* driver_obj = (mp_datainput_obj_t *)self;
112 if (driver_obj == NULL)
113 {
114 LOGE(LOG_TAG, "driver_obj is NULL\n");
115 return mp_const_none;
116 }
117
118 if (driver_obj->mInstance != NULL)
119 {
120 LOGE(LOG_TAG, "Module has been opened, please clode first\n");
121 return mp_const_none;
122 }
123
124 driver_obj->mDataInputType = (DataInputType_t)mp_obj_get_int(args[1]);
125 driver_obj->mCameraNum = (CameraNumber_t)mp_obj_get_int(args[2]);
126 LOGD(LOG_TAG, "%s:mDataInputType = %d;\n", __func__, driver_obj->mDataInputType);
127 LOGD(LOG_TAG, "%s:mCameraNum = %d;\n", __func__, driver_obj->mCameraNum);
128 instance = DataInputCreateInstance(driver_obj->mDataInputType);
129 driver_obj->mInstance = instance;
130 if (instance == NULL)
131 {
132 LOGE(LOG_TAG, "DataInputCreateInstance failed\n");
133 return mp_const_none;
134 }
135
136 ret = DataInputOpen2(instance, driver_obj->mCameraNum);
137 if(ret)
138 {
139 LOGE(LOG_TAG, "\r\n=====datainput opencamera failed===\r\n");
140 return mp_const_none;
141 }
142
143 LOGD(LOG_TAG, "%s:out\n", __func__);
144
145 return mp_const_none;
146 }
147 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(datainput_obj_openCamera, 3, obj_openCamera);
148
obj_close(size_t n_args,const mp_obj_t * args)149 STATIC mp_obj_t obj_close(size_t n_args, const mp_obj_t *args)
150 {
151 LOGD(LOG_TAG, "entern %s; n_args = %d;\n", __func__, n_args);
152 int ret = -1;
153 void* instance = NULL;
154 if (n_args < 1)
155 {
156 LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
157 return mp_const_none;
158 }
159 mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
160 mp_datainput_obj_t* driver_obj = (mp_datainput_obj_t *)self;
161 if (driver_obj == NULL)
162 {
163 LOGE(LOG_TAG, "driver_obj is NULL\n");
164 return mp_const_none;
165 }
166
167 if (driver_obj->mInstance == NULL)
168 {
169 LOGE(LOG_TAG, "Module has not been opened, not need close\n");
170 return mp_const_none;
171 }
172
173 DataInputClose(driver_obj->mInstance);
174 driver_obj->mDataInputType = DATA_INPUT_SOURCE_NONE;
175 driver_obj->mCameraNum = DATA_INPUT_CAMERA_NUMBER_NONE;
176 driver_obj->mFileName = NULL;
177 driver_obj->mInstance = NULL;
178
179 LOGD(LOG_TAG, "%s:out\n", __func__);
180
181 return mp_const_none;
182 }
183 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(datainput_obj_close, 1, obj_close);
184
obj_checkDataReady(size_t n_args,const mp_obj_t * args)185 STATIC mp_obj_t obj_checkDataReady(size_t n_args, const mp_obj_t *args)
186 {
187 LOGD(LOG_TAG, "entern %s; n_args = %d;\n", __func__, n_args);
188 void* instance = NULL;
189 if (n_args < 1)
190 {
191 LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
192 return mp_const_none;
193 }
194 mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
195 mp_datainput_obj_t* driver_obj = (mp_datainput_obj_t *)self;
196 if (driver_obj == NULL)
197 {
198 LOGE(LOG_TAG, "driver_obj is NULL\n");
199 return mp_const_none;
200 }
201
202 if (driver_obj->mInstance == NULL)
203 {
204 LOGE(LOG_TAG, "Module has not been opened, please open first\n");
205 return mp_const_none;
206 }
207
208 int state = (int)DataInputCheckDataReady(driver_obj->mInstance);
209 mp_obj_t ret = mp_obj_new_bool(state);
210 LOGD(LOG_TAG, "%s:out state = %d;\n", __func__, state);
211
212 return MP_ROM_INT(&ret);
213 }
214 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(datainput_obj_checkDataReady, 1, obj_checkDataReady);
215
obj_requestData(size_t n_args,const mp_obj_t * args)216 STATIC mp_obj_t obj_requestData(size_t n_args, const mp_obj_t *args)
217 {
218 LOGD(LOG_TAG, "entern %s; n_args = %d;\n", __func__, n_args);
219 int ret = -1;
220 void* instance = NULL;
221 ImageBuffer_t *image = NULL;
222 if (n_args < 1)
223 {
224 LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
225 return mp_const_none;
226 }
227 mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
228 mp_datainput_obj_t* driver_obj = (mp_datainput_obj_t *)self;
229 if (driver_obj == NULL)
230 {
231 LOGE(LOG_TAG, "driver_obj is NULL\n");
232 return mp_const_none;
233 }
234 if (driver_obj->mInstance == NULL)
235 {
236 LOGE(LOG_TAG, "Module has not been opened, please open first\n");
237 return mp_const_none;
238 }
239
240 ret = DataInputRequestData(driver_obj->mInstance, &image, 1000);
241 if (ret != 0)
242 {
243 LOGE(LOG_TAG, "Request Data failed\n");
244 return mp_const_none;
245 }
246
247 LOGD(LOG_TAG, "%s:out image->address[0] = %p;image = %p;\n", __func__, image->address[0], image);
248
249 return MP_OBJ_FROM_PTR(image);
250 }
251 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(datainput_obj_requestData, 1, obj_requestData);
252
obj_releaseData(size_t n_args,const mp_obj_t * args)253 STATIC mp_obj_t obj_releaseData(size_t n_args, const mp_obj_t *args)
254 {
255 LOGD(LOG_TAG, "entern %s; n_args = %d;\n", __func__, n_args);
256 int ret = -1;
257 void* instance = NULL;
258 if (n_args < 2)
259 {
260 LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
261 return mp_const_none;
262 }
263 mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
264 mp_datainput_obj_t* driver_obj = (mp_datainput_obj_t *)self;
265 if (driver_obj == NULL)
266 {
267 LOGE(LOG_TAG, "driver_obj is NULL\n");
268 return mp_const_none;
269 }
270
271 ImageBuffer_t *image = (ImageBuffer_t *)MP_OBJ_TO_PTR(args[1]);
272 if (image == NULL)
273 {
274 LOGE(LOG_TAG, "image is NULL\n");
275 return mp_const_none;
276 }
277
278 ret = DataInputReleaseData(driver_obj->mInstance, image);
279 if (ret != 0)
280 {
281 LOGE(LOG_TAG, "Release Data failed\n");
282 return mp_const_none;
283 }
284
285 LOGD(LOG_TAG, "%s:out\n", __func__);
286
287 return mp_const_none;
288 }
289 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(datainput_obj_releaseData, 2, obj_releaseData);
290
291 STATIC const mp_rom_map_elem_t datainput_locals_dict_table[] = {
292 {MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_DataInput)},
293 {MP_OBJ_NEW_QSTR(MP_QSTR_DATA_INPUT_CAMERA_NUMBER_NONE), MP_ROM_INT(DATA_INPUT_CAMERA_NUMBER_NONE)},
294 {MP_OBJ_NEW_QSTR(MP_QSTR_DATA_INPUT_CAMERA_NUMBER_FRONT), MP_ROM_INT(DATA_INPUT_CAMERA_NUMBER_FRONT)},
295 {MP_OBJ_NEW_QSTR(MP_QSTR_DATA_INPUT_CAMERA_NUMBER_BACK), MP_ROM_INT(DATA_INPUT_CAMERA_NUMBER_BACK)},
296 {MP_OBJ_NEW_QSTR(MP_QSTR_DATA_INPUT_CAMERA_NUMBER_MAX), MP_ROM_INT(DATA_INPUT_CAMERA_NUMBER_MAX)},
297 {MP_OBJ_NEW_QSTR(MP_QSTR_DATA_INPUT_SOURCE_NONE),
298 MP_ROM_INT(DATA_INPUT_SOURCE_NONE)},
299 {MP_OBJ_NEW_QSTR(MP_QSTR_DATA_INPUT_SOURCE_IMAGE_BMP),
300 MP_ROM_INT(DATA_INPUT_SOURCE_IMAGE_BMP)},
301 {MP_OBJ_NEW_QSTR(MP_QSTR_DATA_INPUT_SOURCE_IMAGE_GIF),
302 MP_ROM_INT(DATA_INPUT_SOURCE_IMAGE_GIF)},
303 {MP_OBJ_NEW_QSTR(MP_QSTR_DATA_INPUT_SOURCE_IMAGE_PNG),
304 MP_ROM_INT(DATA_INPUT_SOURCE_IMAGE_PNG)},
305 {MP_OBJ_NEW_QSTR(MP_QSTR_DATA_INPUT_SOURCE_IMAGE_JPG),
306 MP_ROM_INT(DATA_INPUT_SOURCE_IMAGE_JPG)},
307 {MP_OBJ_NEW_QSTR(MP_QSTR_DATA_INPUT_SOURCE_VIDEO_FILE),
308 MP_ROM_INT(DATA_INPUT_SOURCE_VIDEO_FILE)},
309 {MP_OBJ_NEW_QSTR(MP_QSTR_DATA_INPUT_SOURCE_VIDEO_RTMP),
310 MP_ROM_INT(DATA_INPUT_SOURCE_VIDEO_RTMP)},
311 {MP_OBJ_NEW_QSTR(MP_QSTR_DATA_INPUT_SOURCE_VIDEO_RTSP),
312 MP_ROM_INT(DATA_INPUT_SOURCE_VIDEO_RTSP)},
313 {MP_OBJ_NEW_QSTR(MP_QSTR_DATA_INPUT_SOURCE_CAMERA_USB),
314 MP_ROM_INT(DATA_INPUT_SOURCE_CAMERA_USB)},
315 {MP_OBJ_NEW_QSTR(MP_QSTR_DATA_INPUT_SOURCE_CAMERA_LOCAL),
316 MP_ROM_INT(DATA_INPUT_SOURCE_CAMERA_LOCAL)},
317 {MP_OBJ_NEW_QSTR(MP_QSTR_DATA_INPUT_SOURCE_CAMERA_IP),
318 MP_ROM_INT(DATA_INPUT_SOURCE_CAMERA_IP)},
319 {MP_OBJ_NEW_QSTR(MP_QSTR_DATA_INPUT_SOURCE_MAX),
320 MP_ROM_INT(DATA_INPUT_SOURCE_MAX)},
321 {MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&datainput_obj_open)},
322 {MP_ROM_QSTR(MP_QSTR_openCamera), MP_ROM_PTR(&datainput_obj_openCamera)},
323 {MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&datainput_obj_close)},
324 {MP_ROM_QSTR(MP_QSTR_checkDataReady), MP_ROM_PTR(&datainput_obj_checkDataReady)},
325 {MP_ROM_QSTR(MP_QSTR_requestData), MP_ROM_PTR(&datainput_obj_requestData)},
326 {MP_ROM_QSTR(MP_QSTR_releaseData), MP_ROM_PTR(&datainput_obj_releaseData)},
327 };
328
329 STATIC MP_DEFINE_CONST_DICT(datainput_locals_dict, datainput_locals_dict_table);
330
331 const mp_obj_type_t minicv_datainput_type = {
332 .base = {&mp_type_type},
333 .name = MP_QSTR_DataInput,
334 .print = datainput_obj_print,
335 .make_new = datainput_obj_make_new,
336 .locals_dict = (mp_obj_dict_t *)&datainput_locals_dict,
337 };
338
339
340
341