1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "aos_hal_adc.h"
6 #include "board_mgr.h"
7 #include "py/builtin.h"
8 #include "py/mperrno.h"
9 #include "py/obj.h"
10 #include "py/runtime.h"
11 #include "ulog/ulog.h"
12
13 #define LOG_TAG "DRIVER_ADC"
14
15 extern const mp_obj_type_t driver_adc_type;
16
17 // this is the actual C-structure for our new object
18 typedef struct {
19 // base represents some basic information, like type
20 mp_obj_base_t Base;
21 // a member created by us
22 char *ModuleName;
23 item_handle_t adc_handle;
24 } mp_adc_obj_t;
25
adc_obj_print(const mp_print_t * print,mp_obj_t self_in,mp_print_kind_t kind)26 void adc_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind)
27 {
28 mp_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
29 mp_printf(print, "ModuleName(%s)", self->ModuleName);
30 }
31
adc_obj_make_new(const mp_obj_type_t * type,size_t n_args,size_t n_kw,const mp_obj_t * args)32 STATIC mp_obj_t adc_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args)
33 {
34 mp_adc_obj_t *driver_obj = m_new_obj(mp_adc_obj_t);
35 if (!driver_obj) {
36 mp_raise_OSError(ENOMEM);
37 }
38 memset(driver_obj, 0, sizeof(mp_adc_obj_t));
39
40 driver_obj->Base.type = &driver_adc_type;
41 driver_obj->ModuleName = "adc";
42 driver_obj->adc_handle.handle = NULL;
43
44 return MP_OBJ_FROM_PTR(driver_obj);
45 }
46
adc_open(size_t n_args,const mp_obj_t * args)47 STATIC mp_obj_t adc_open(size_t n_args, const mp_obj_t *args)
48 {
49 int ret = -1;
50 if (n_args < 2) {
51 LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
52 return MP_OBJ_NEW_SMALL_INT (-MP_E2BIG);
53 }
54
55 mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
56 mp_adc_obj_t *driver_obj = (mp_adc_obj_t *)self;
57 if (driver_obj == NULL) {
58 LOGE(LOG_TAG, "driver_obj is NULL\n");
59 return MP_OBJ_NEW_SMALL_INT (-MP_EINVAL);
60 }
61
62 char *id = (char *)mp_obj_str_get_str(args[1]);
63 if (id == NULL) {
64 LOGE(LOG_TAG, "%s:illegal par id =%s;\n", __func__, id);
65 return MP_OBJ_NEW_SMALL_INT (-MP_EINVAL);
66 }
67
68 ret = py_board_mgr_init();
69 if (ret != 0) {
70 LOGE(LOG_TAG, "%s:py_board_mgr_init failed\n", __func__);
71 return MP_OBJ_NEW_SMALL_INT (-MP_ENOENT);
72 }
73
74 ret = py_board_attach_item(MODULE_ADC, id, &(driver_obj->adc_handle));
75 if (ret != 0) {
76 LOGE(LOG_TAG, "%s: py_board_attach_item failed ret = %d;\n", __func__, ret);
77 goto out;
78 }
79
80 adc_dev_t *adc_device = py_board_get_node_by_handle(MODULE_ADC, &(driver_obj->adc_handle));
81 if (NULL == adc_device) {
82 LOGE(LOG_TAG, "%s: py_board_get_node_by_handle failed;\n", __func__);
83 goto out;
84 }
85
86 ret = aos_hal_adc_init(adc_device);
87
88 out:
89 if (0 != ret) {
90 LOGE(LOG_TAG, "%s: adc open failed ret = %d;\n", __func__, ret);
91 py_board_disattach_item(MODULE_ADC, &(driver_obj->adc_handle));
92 }
93
94 return MP_OBJ_NEW_SMALL_INT(ret);
95 }
96 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(adc_open_obj, 2, adc_open);
97
adc_close(size_t n_args,const mp_obj_t * args)98 STATIC mp_obj_t adc_close(size_t n_args, const mp_obj_t *args)
99 {
100 int ret = -1;
101 adc_dev_t *adc_device = NULL;
102 if (n_args < 1) {
103 LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
104 return MP_OBJ_NEW_SMALL_INT (-MP_E2BIG);
105 }
106 mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
107 mp_adc_obj_t *driver_obj = (mp_adc_obj_t *)self;
108 if (driver_obj == NULL) {
109 LOGE(LOG_TAG, "driver_obj is NULL\n");
110 return MP_OBJ_NEW_SMALL_INT (-MP_EINVAL);
111 }
112
113 adc_device = py_board_get_node_by_handle(MODULE_ADC, &(driver_obj->adc_handle));
114 if (NULL == adc_device) {
115 LOGE(LOG_TAG, "%s: py_board_get_node_by_handle failed;\n", __func__);
116 return MP_OBJ_NEW_SMALL_INT (-MP_ENODEV);
117 }
118 ret = aos_hal_adc_finalize(adc_device);
119 py_board_disattach_item(MODULE_ADC, &(driver_obj->adc_handle));
120
121 return MP_OBJ_NEW_SMALL_INT(ret);
122 }
123 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(adc_close_obj, 1, adc_close);
124
adc_read(size_t n_args,const mp_obj_t * args)125 STATIC mp_obj_t adc_read(size_t n_args, const mp_obj_t *args)
126 {
127 adc_dev_t *adc_device = NULL;
128 if (n_args < 1) {
129 LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
130 return MP_OBJ_NEW_SMALL_INT(-MP_E2BIG);
131 }
132 mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
133 mp_adc_obj_t *driver_obj = (mp_adc_obj_t *)self;
134 if (driver_obj == NULL) {
135 LOGE(LOG_TAG, "driver_obj is NULL\n");
136 return MP_OBJ_NEW_SMALL_INT (-MP_EINVAL);
137 }
138 adc_device = py_board_get_node_by_handle(MODULE_ADC, &(driver_obj->adc_handle));
139 if (NULL == adc_device) {
140 LOGE(LOG_TAG, "%s: py_board_get_node_by_handle failed;\n", __func__);
141 return MP_OBJ_NEW_SMALL_INT (-MP_ENODEV);
142 }
143
144 int32_t adc_value = -1;
145 int32_t ret = aos_hal_adc_value_get(adc_device, &adc_value, 0);
146 if(ret != 0) {
147 return MP_OBJ_NEW_SMALL_INT(ret);
148 } else {
149 return MP_OBJ_NEW_SMALL_INT(adc_value);
150 }
151 }
152 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(adc_read_obj, 1, adc_read);
153
154 STATIC const mp_rom_map_elem_t adc_locals_dict_table[] = {
155 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ADC) },
156 { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&adc_open_obj) },
157 { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&adc_close_obj) },
158 { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&adc_read_obj) },
159 };
160
161 STATIC MP_DEFINE_CONST_DICT(adc_locals_dict, adc_locals_dict_table);
162
163 const mp_obj_type_t driver_adc_type = {
164 .base = { &mp_type_type },
165 .name = MP_QSTR_ADC,
166 .print = adc_obj_print,
167 .make_new = adc_obj_make_new,
168 .locals_dict = (mp_obj_dict_t *)&adc_locals_dict,
169 };
170