1 /*
2 * Copyright (C) 2015-2019 Alibaba Group Holding Limited
3 */
4
5 #include <stdint.h>
6
7 #include "amp_config.h"
8 #include "amp_defines.h"
9 #include "aos_hal_dac.h"
10 #include "board_mgr.h"
11 #include "quickjs.h"
12 #include "quickjs_addon_common.h"
13 #include "aos_system.h"
14
15 #define MOD_STR "DAC"
16
17 static JSClassID js_dac_class_id;
18
native_dac_open(JSContext * ctx,JSValueConst this_val,int argc,JSValueConst * argv)19 static JSValue native_dac_open(JSContext *ctx, JSValueConst this_val,
20 int argc, JSValueConst *argv)
21 {
22 int8_t ret = -1;
23 item_handle_t dac_handle;
24 dac_handle.handle = NULL;
25 dac_dev_t *dac_device = NULL;
26
27 const char *id = JS_ToCString(ctx, argv[0]);
28 if (id == NULL) {
29 amp_error(MOD_STR, "get dac id fail!");
30 goto out;
31 }
32
33 ret = board_attach_item(MODULE_DAC, id, &dac_handle);
34 if (0 != ret) {
35 amp_error(MOD_STR, "board_attach_item fail!, id %s", id);
36 goto out;
37 }
38
39 amp_debug(MOD_STR, "dac handle:%u\n", dac_handle.handle);
40 dac_device = board_get_node_by_handle(MODULE_DAC, &dac_handle);
41 if (NULL == dac_device) {
42 amp_error(MOD_STR, "board_get_node_by_handle fail!\n");
43 goto out;
44 }
45 ret = aos_hal_dac_init(dac_device);
46 if (0 != ret) {
47 amp_error(MOD_STR, "aos_hal_dac_init fail!\n");
48 goto out;
49 }
50 ret = aos_hal_dac_start(dac_device, dac_device->port);
51 if (0 != ret) {
52 amp_error(MOD_STR, "hal_dac_start fail!\n");
53 }
54
55 out:
56 if (id != NULL) {
57 JS_FreeCString(ctx, id);
58 }
59 if (0 != ret) {
60 JS_SetContextOpaque(ctx, NULL);
61 board_disattach_item(MODULE_DAC, &dac_handle);
62 } else {
63 JSValue obj;
64 obj = JS_NewObjectClass(ctx, js_dac_class_id);
65 JS_SetOpaque(obj, (void *)dac_handle.handle);
66 return obj;
67 }
68 return JS_NewInt32(ctx, ret);
69 }
70
native_dac_setVol(JSContext * ctx,JSValueConst this_val,int argc,JSValueConst * argv)71 static JSValue native_dac_setVol(JSContext *ctx, JSValueConst this_val,
72 int argc, JSValueConst *argv)
73 {
74 int8_t ret = -1;
75 int8_t result = -1;
76 uint32_t voltage = 0;
77 item_handle_t dac_handle;
78 dac_dev_t *dac_device = NULL;
79
80 dac_handle.handle = JS_GetOpaque2(ctx, this_val, js_dac_class_id);
81 if (!dac_handle.handle) {
82 amp_warn(MOD_STR, "parameter must be handle");
83 goto out;
84 }
85
86 dac_device = board_get_node_by_handle(MODULE_DAC, &dac_handle);
87 if (NULL == dac_device) {
88 amp_error(MOD_STR, "board_get_node_by_handle fail!\n");
89 goto out;
90 }
91
92 JS_ToInt32(ctx, &voltage, argv[0]);
93
94 ret = aos_hal_dac_set_value(dac_device, dac_device->port, voltage);
95 if (-1 == ret) {
96 amp_error(MOD_STR, "dac set val fail!");
97 goto out;
98 }
99 result = 0;
100 out:
101 return JS_NewInt32(ctx, result);
102 }
103
native_dac_getVol(JSContext * ctx,JSValueConst this_val,int argc,JSValueConst * argv)104 static JSValue native_dac_getVol(JSContext *ctx, JSValueConst this_val,
105 int argc, JSValueConst *argv)
106 {
107 uint32_t voltage = 0;
108 item_handle_t dac_handle;
109 dac_dev_t *dac_device = NULL;
110
111 dac_handle.handle = JS_GetOpaque2(ctx, this_val, js_dac_class_id);
112 if (!dac_handle.handle) {
113 amp_warn(MOD_STR, "parameter must be handle");
114 goto out;
115 }
116
117 dac_device = board_get_node_by_handle(MODULE_DAC, &dac_handle);
118 if (NULL == dac_device) {
119 amp_error(MOD_STR, "board_get_node_by_handle fail!\n");
120 goto out;
121 }
122 voltage = (uint32_t)aos_hal_dac_get_value(dac_device, dac_device->port);
123 out:
124 JS_NewInt32(ctx, voltage);
125 }
126
native_dac_close(JSContext * ctx,JSValueConst this_val,int argc,JSValueConst * argv)127 static JSValue native_dac_close(JSContext *ctx, JSValueConst this_val,
128 int argc, JSValueConst *argv)
129 {
130 int8_t ret = -1;
131 item_handle_t dac_handle;
132 dac_dev_t *dac_device = NULL;
133
134 dac_handle.handle = JS_GetOpaque2(ctx, this_val, js_dac_class_id);
135 if (!dac_handle.handle) {
136 amp_warn(MOD_STR, "parameter must be handle");
137 goto out;
138 }
139
140 dac_device = board_get_node_by_handle(MODULE_DAC, &dac_handle);
141 if (NULL == dac_device) {
142 amp_error(MOD_STR, "board_get_node_by_handle fail!\n");
143 goto out;
144 }
145 aos_hal_dac_stop(dac_device, dac_device->port);
146 ret = aos_hal_dac_finalize(dac_device);
147 if (0 != ret) {
148 amp_error(MOD_STR, "aos_hal_dac_finalize fail!");
149 goto out;
150 }
151 board_disattach_item(MODULE_DAC, &dac_handle);
152
153 out:
154 return JS_NewInt32(ctx, ret);
155 }
156
157 static JSClassDef js_dac_class = {
158 "DAC",
159 };
160
161 static const JSCFunctionListEntry js_dac_funcs[] = {
162 JS_CFUNC_DEF("open", 1, native_dac_open),
163 JS_CFUNC_DEF("getVol", 0, native_dac_getVol),
164 JS_CFUNC_DEF("setVol", 1, native_dac_setVol),
165 JS_CFUNC_DEF("close", 0, native_dac_close),
166 };
167
js_dac_init(JSContext * ctx,JSModuleDef * m)168 static int js_dac_init(JSContext *ctx, JSModuleDef *m)
169 {
170 JSValue proto;
171
172 JS_NewClassID(&js_dac_class_id);
173
174 JS_NewClass(JS_GetRuntime(ctx), js_dac_class_id, &js_dac_class);
175 proto = JS_NewObject(ctx);
176 JS_SetPropertyFunctionList(ctx, proto, js_dac_funcs,
177 countof(js_dac_funcs));
178 JS_SetClassProto(ctx, js_dac_class_id, proto);
179
180 return JS_SetModuleExportList(ctx, m, js_dac_funcs,
181 countof(js_dac_funcs));
182 }
183
js_init_module_dac(JSContext * ctx,const char * module_name)184 JSModuleDef *js_init_module_dac(JSContext *ctx, const char *module_name)
185 {
186 JSModuleDef *m;
187 m = JS_NewCModule(ctx, module_name, js_dac_init);
188 if (!m)
189 return NULL;
190 JS_AddModuleExportList(ctx, m, js_dac_funcs, countof(js_dac_funcs));
191 return m;
192 }
193
module_dac_register(void)194 void module_dac_register(void)
195 {
196 amp_debug(MOD_STR, "module_dac_register");
197 JSContext *ctx = js_get_context();
198 aos_printf("module dac register\n");
199 js_init_module_dac(ctx, "DAC");
200 }
201