1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "k_api.h"
6 #include "py/builtin.h"
7 #include "py/mperrno.h"
8 #include "py/obj.h"
9 #include "py/runtime.h"
10 #include "ucamera_service.h"
11 #include "ulog/ulog.h"
12 #include "wifi_camera.h"
13
14 #define LOG_TAG "MOD_UCAMERA"
15
obj_init(size_t n_args,const mp_obj_t * args)16 STATIC mp_obj_t obj_init(size_t n_args, const mp_obj_t *args)
17 {
18 LOGD(LOG_TAG, "entern %s; n_args = %d;\n", __func__, n_args);
19 int ret = -1;
20 if (n_args < 2) {
21 LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__,
22 n_args);
23 return mp_const_none;
24 }
25
26 char *camera_type = (char *)mp_obj_str_get_str(args[0]);
27 char *camera_url = (char *)mp_obj_str_get_str(args[1]);
28 if (!strcmp(camera_type, "wifi")) {
29 ret = ucamera_service_init(WIFI_CAMERA_NAME);
30 } else {
31 ret = -1;
32 LOGE(LOG_TAG, "unsupport camera type %s;\n", __func__);
33 }
34
35 if (ret < 0) {
36 LOGD(LOG_TAG, "init failed %s;\n", __func__);
37 }
38 return mp_obj_new_int(ret);
39 }
40 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(ucamera_obj_init, 0, obj_init);
41
obj_saveFrameToJpeg(size_t n_args,const mp_obj_t * args)42 STATIC mp_obj_t obj_saveFrameToJpeg(size_t n_args, const mp_obj_t *args)
43 {
44 LOGD(LOG_TAG, "entern %s; n_args = %d;\n", __func__, n_args);
45 int ret = -1;
46 if (n_args < 1) {
47 LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__,
48 n_args);
49 return mp_const_none;
50 }
51
52 frame_buffer_t *frame = NULL;
53 char *path = (char *)mp_obj_str_get_str(args[0]);
54 /*get one camera frame*/
55 frame = ucamera_service_get_frame();
56 if (!frame) {
57 LOGE(LOG_TAG, "ucamera get frame fail\n");
58 return;
59 } else {
60 LOGD(LOG_TAG, "ucamera get frame OK!\n");
61 }
62
63 /*save frame to jpeg file*/
64 ret = ucamera_service_save_frame(frame, path);
65 if (ret < 0) {
66 LOGE(LOG_TAG, "save image fail\n");
67 return;
68 } else {
69 LOGD(LOG_TAG, "save image to %s successfully!\n", path);
70 }
71 return mp_obj_new_int(ret);
72 }
73 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(ucamera_obj_saveFrameToJpeg, 0,
74 obj_saveFrameToJpeg);
75
obj_uninit(size_t n_args,const mp_obj_t * args)76 STATIC mp_obj_t obj_uninit(size_t n_args, const mp_obj_t *args)
77 {
78 LOGD(LOG_TAG, "entern %s; n_args = %d;\n", __func__, n_args);
79 int ret = -1;
80
81 ret = ucamera_service_uninit();
82 if (ret < 0) {
83 LOGD(LOG_TAG, "init failed %s;\n", __func__);
84 }
85 return mp_obj_new_int(ret);
86 }
87 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(ucamera_obj_uninit, 0, obj_uninit);
88
89 STATIC const mp_rom_map_elem_t ucamera_locals_dict_table[] = {
90 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_UCamera) },
91 { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&ucamera_obj_init) },
92 { MP_ROM_QSTR(MP_QSTR_uninit), MP_ROM_PTR(&ucamera_obj_uninit) },
93 { MP_ROM_QSTR(MP_QSTR_saveFrameToJpeg),
94 MP_ROM_PTR(&ucamera_obj_saveFrameToJpeg) },
95 };
96
97 STATIC MP_DEFINE_CONST_DICT(ucamera_locals_dict, ucamera_locals_dict_table);
98
99 const mp_obj_module_t ucamera_module = {
100 .base = { &mp_type_module },
101 .globals = (mp_obj_dict_t *)&ucamera_locals_dict,
102 };
103