1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 
5 #include "aos_hal_dac.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_DAC"
14 
15 extern const mp_obj_type_t driver_dac_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 dac_handle;
24 } mp_dac_obj_t;
25 
dac_obj_print(const mp_print_t * print,mp_obj_t self_in,mp_print_kind_t kind)26 void dac_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind)
27 {
28     LOGD(LOG_TAG, "entern %s;\n", __func__);
29     mp_dac_obj_t *self = MP_OBJ_TO_PTR(self_in);
30     mp_printf(print, "ModuleName(%s)", self->ModuleName);
31 }
32 
dac_obj_make_new(const mp_obj_type_t * type,size_t n_args,size_t n_kw,const mp_obj_t * args)33 STATIC mp_obj_t dac_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args)
34 {
35     LOGD(LOG_TAG, "entern  %s;\n", __func__);
36     mp_dac_obj_t *driver_obj = m_new_obj(mp_dac_obj_t);
37     if (!driver_obj) {
38         mp_raise_OSError(MP_EINVAL);
39     }
40 
41     driver_obj->Base.type = &driver_dac_type;
42     driver_obj->ModuleName = "dac";
43     driver_obj->dac_handle.handle = NULL;
44 
45     return MP_OBJ_FROM_PTR(driver_obj);
46 }
47 
obj_open(size_t n_args,const mp_obj_t * args)48 STATIC mp_obj_t obj_open(size_t n_args, const mp_obj_t *args)
49 {
50     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
51     int ret = -1;
52     dac_dev_t *dac_device = NULL;
53 
54     if (n_args < 2) {
55         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
56         return mp_const_none;
57     }
58     mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
59     mp_dac_obj_t *driver_obj = (mp_dac_obj_t *)self;
60     if (driver_obj == NULL) {
61         LOGE(LOG_TAG, "driver_obj is NULL\n");
62         return mp_const_none;
63     }
64 
65     char *id = (char *)mp_obj_str_get_str(args[1]);
66     LOGD(LOG_TAG, "%s: id =%s;\n", __func__, id);
67 
68     if (id == NULL) {
69         LOGE(LOG_TAG, "%s:illegal par id =%s;\n", __func__, id);
70         return mp_const_none;
71     }
72 
73     ret = py_board_mgr_init();
74     if (ret != 0) {
75         LOGE(LOG_TAG, "%s:py_board_mgr_init failed\n", __func__);
76         return mp_const_none;
77     }
78 
79     LOGD(LOG_TAG, "%s: py_board_mgr_init ret = %d;\n", __func__, ret);
80     ret = py_board_attach_item(MODULE_DAC, id, &(driver_obj->dac_handle));
81     if (ret != 0) {
82         LOGE(LOG_TAG, "%s: py_board_attach_item failed ret = %d;\n", __func__, ret);
83         goto out;
84     }
85 
86     dac_device = py_board_get_node_by_handle(MODULE_DAC, &(driver_obj->dac_handle));
87     if (NULL == dac_device) {
88         LOGE(LOG_TAG, "%s: py_board_get_node_by_handle failed;\n", __func__);
89         goto out;
90     }
91 
92     LOGD(LOG_TAG, "%s: port = %d;\n", __func__, dac_device->port);
93 #if 0
94     ret = hal_dac_init(dac_device);
95     if (ret != 0)
96     {
97         LOGE(LOG_TAG, "hal_dac_init failed\n");
98         goto out;
99     }
100 
101     ret = hal_dac_start(dac_device, dac_device->port);
102     if (ret != 0)
103     {
104         LOGE(LOG_TAG, "hal_dac_start failed\n");
105     }
106 #endif
107 out:
108     if (0 != ret) {
109         py_board_disattach_item(MODULE_DAC, &(driver_obj->dac_handle));
110     }
111 
112     LOGD(LOG_TAG, "%s:out\n", __func__);
113 
114     return MP_ROM_INT(ret);
115 }
116 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(dac_obj_open, 2, obj_open);
117 
obj_close(size_t n_args,const mp_obj_t * args)118 STATIC mp_obj_t obj_close(size_t n_args, const mp_obj_t *args)
119 {
120     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
121     int ret = -1;
122     dac_dev_t *dac_device = NULL;
123     if (n_args < 1) {
124         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
125         return mp_const_none;
126     }
127     mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
128     mp_dac_obj_t *driver_obj = (mp_dac_obj_t *)self;
129     if (driver_obj == NULL) {
130         LOGE(LOG_TAG, "driver_obj is NULL\n");
131         return mp_const_none;
132     }
133 
134     dac_device = py_board_get_node_by_handle(MODULE_DAC, &(driver_obj->dac_handle));
135     if (NULL == dac_device) {
136         LOGE(LOG_TAG, "%s: py_board_get_node_by_handle failed;\n", __func__);
137         return mp_const_none;
138     }
139 
140     // hal_dac_stop(dac_device, dac_device->port);
141     // ret = hal_dac_finalize(dac_device);
142     py_board_disattach_item(MODULE_DAC, &(driver_obj->dac_handle));
143     LOGD(LOG_TAG, "%s:out\n", __func__);
144 
145     return MP_ROM_INT(ret);
146 }
147 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(dac_obj_close, 1, obj_close);
148 
obj_getVol(size_t n_args,const mp_obj_t * args)149 STATIC mp_obj_t obj_getVol(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     dac_dev_t *dac_device = NULL;
154     if (n_args < 1) {
155         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
156         return mp_const_none;
157     }
158     mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
159     mp_dac_obj_t *driver_obj = (mp_dac_obj_t *)self;
160     if (driver_obj == NULL) {
161         LOGE(LOG_TAG, "driver_obj is NULL\n");
162         return mp_const_none;
163     }
164 
165     dac_device = py_board_get_node_by_handle(MODULE_DAC, &(driver_obj->dac_handle));
166     if (NULL == dac_device) {
167         LOGE(LOG_TAG, "%s: py_board_get_node_by_handle failed;\n", __func__);
168         return mp_const_none;
169     }
170 
171     // ret = (int)hal_dac_get_value(dac_device, dac_device->port);
172     LOGD(LOG_TAG, "%s:out\n", __func__);
173 
174     return MP_ROM_INT(ret);
175 }
176 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(dac_obj_getVol, 1, obj_getVol);
177 
obj_setVol(size_t n_args,const mp_obj_t * args)178 STATIC mp_obj_t obj_setVol(size_t n_args, const mp_obj_t *args)
179 {
180     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
181     int ret = -1;
182     uint32_t voltage = 0;
183     dac_dev_t *dac_device = NULL;
184     if (n_args < 2) {
185         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
186         return mp_const_none;
187     }
188     mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
189     mp_dac_obj_t *driver_obj = (mp_dac_obj_t *)self;
190     if (driver_obj == NULL) {
191         LOGE(LOG_TAG, "driver_obj is NULL\n");
192         return mp_const_none;
193     }
194 
195     dac_device = py_board_get_node_by_handle(MODULE_DAC, &(driver_obj->dac_handle));
196     if (NULL == dac_device) {
197         LOGE(LOG_TAG, "%s: py_board_get_node_by_handle failed;\n", __func__);
198         return mp_const_none;
199     }
200 
201     voltage = (uint32_t)mp_obj_get_int(args[1]);
202     // ret     = hal_dac_set_value(dac_device, dac_device->port, voltage);
203     LOGD(LOG_TAG, "%s:out\n", __func__);
204 
205     return MP_ROM_INT(ret);
206 }
207 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(dac_obj_setVol, 2, obj_setVol);
208 
209 STATIC const mp_rom_map_elem_t dac_locals_dict_table[] = {
210     { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_DAC) },
211     { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&dac_obj_open) },
212     { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&dac_obj_close) },
213     { MP_ROM_QSTR(MP_QSTR_getVol), MP_ROM_PTR(&dac_obj_getVol) },
214     { MP_ROM_QSTR(MP_QSTR_setVol), MP_ROM_PTR(&dac_obj_setVol) },
215 };
216 
217 STATIC MP_DEFINE_CONST_DICT(dac_locals_dict, dac_locals_dict_table);
218 
219 const mp_obj_type_t driver_dac_type = {
220     .base = { &mp_type_type },
221     .name = MP_QSTR_DAC,
222     .print = dac_obj_print,
223     .make_new = dac_obj_make_new,
224     .locals_dict = (mp_obj_dict_t *)&dac_locals_dict,
225 };
226