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 "amp_hal_keypad.h"
10 #include "aos_system.h"
11 #include "amp_task.h"
12 #include "board_mgr.h"
13 #include "be_inl.h"
14 
15 #define MOD_STR "Keypad"
16 
17 
18 static int keypad_js_cb_ref;
19 
20 typedef struct {
21 	keypad_code_t code;
22 	int value;
23 } keypad_notify_t;
24 
keypad_notify_callback(void * pdata)25 static int keypad_notify_callback(void *pdata)
26 {
27 	duk_context *ctx;
28 	keypad_notify_t *param = (keypad_notify_t *)pdata;
29 	if (!param)
30 		return -1;
31     ctx = be_get_context();
32     be_push_ref(ctx, keypad_js_cb_ref);
33     duk_push_int(ctx, param->code);
34 	duk_push_int(ctx, param->value);
35     if (duk_pcall(ctx, 2) != DUK_EXEC_SUCCESS) {
36         amp_console("%s", duk_safe_to_stacktrace(ctx, -1));
37     }
38     duk_pop(ctx);
39     duk_gc(ctx, 0);
40 	aos_free(param);
41 	return 0;
42 }
43 
keypad_event_callback(keypad_code_t code,int value)44 static int keypad_event_callback(keypad_code_t code, int value)
45 {
46 	keypad_notify_t *param = aos_malloc(sizeof(keypad_notify_t));
47 	if (!param)
48 		return -1;
49 	param->code = code;
50 	param->value = value;
51 	amp_task_schedule_call(keypad_notify_callback, param);
52 	return 0;
53 }
54 
native_keypad_on(duk_context * ctx)55 static duk_ret_t native_keypad_on(duk_context *ctx)
56 {
57     int ret = -1;
58 
59     if (!duk_is_function(ctx, 0)) {
60         amp_warn(MOD_STR, "parameter must be (function)");
61         goto out;
62     }
63     duk_dup(ctx, 0);
64 	keypad_js_cb_ref = be_ref(ctx);
65 
66     ret = amp_hal_keypad_event_register(keypad_event_callback);
67     if (ret < 0) {
68         amp_error(MOD_STR, "amp_hal_keypad_event_register fail!");
69         goto out;
70     }
71     ret = 0;
72 
73 out:
74     duk_push_int(ctx, ret);
75     return 1;
76 }
77 
78 
native_keypad_open(duk_context * ctx)79 static duk_ret_t native_keypad_open(duk_context *ctx)
80 {
81 	//amp_hal_keypad_init();
82     duk_push_int(ctx, 0);
83     return 1;
84 }
85 
native_keypad_close(duk_context * ctx)86 static duk_ret_t native_keypad_close(duk_context *ctx)
87 {
88 	//amp_hal_keypad_finalize();
89     duk_push_int(ctx, 0);
90     return 1;
91 }
92 
module_keypad_register(void)93 void module_keypad_register(void)
94 {
95     amp_debug(MOD_STR, "module_keypad_register");
96     duk_context *ctx = be_get_context();
97 
98     duk_push_object(ctx);
99 
100     duk_push_c_function(ctx, native_keypad_open, 1);
101     duk_put_prop_string(ctx, -2, "open");
102 
103     duk_push_c_function(ctx, native_keypad_on, 1);
104     duk_put_prop_string(ctx, -2, "on");
105 
106     duk_push_c_function(ctx, native_keypad_close, 1);
107     duk_put_prop_string(ctx, -2, "close");
108 
109     duk_put_prop_string(ctx, -2, "Keypad");
110 }
111 
112