1 /*
2 * Copyright (C) 2015-2019 Alibaba Group Holding Limited
3 */
4
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 #include "amp_config.h"
10 #include "amp_defines.h"
11 #include "aos_hal_lcd.h"
12 #include "aos_system.h"
13 #include "amp_task.h"
14 #include "board_mgr.h"
15 #include "quickjs.h"
16 #include "quickjs_addon_common.h"
17
18 #define MOD_STR "LCD"
19
20 #define GPIO_IRQ_RISING_EDGE "rising"
21 #define GPIO_IRQ_FALLING_EDGE "falling"
22 #define GPIO_IRQ_BOTH_EDGE "both"
23
24 static uint16_t lcd_init_flag = 0;
25 static JSClassID js_lcd_class_id;
26
native_lcd_open(JSContext * ctx,JSValueConst this_val,int argc,JSValueConst * argv)27 static JSValue native_lcd_open(JSContext *ctx, JSValueConst this_val,
28 int argc, JSValueConst *argv)
29 {
30 int8_t ret = -1;
31
32 ret = aos_hal_lcd_init();
33 if (0 != ret) {
34 amp_error(MOD_STR, "aos_hal_lcd_init fail!");
35 }
36
37 return JS_NewInt32(ctx, ret);
38 }
39
native_lcd_close(JSContext * ctx,JSValueConst this_val,int argc,JSValueConst * argv)40 static JSValue native_lcd_close(JSContext *ctx, JSValueConst this_val,
41 int argc, JSValueConst *argv)
42 {
43 int32_t ret = -1;
44
45 ret = aos_hal_lcd_uninit();
46 if (0 != ret) {
47 amp_error(MOD_STR, "aos_hal_lcd_uninit fail!");
48 }
49
50 return JS_NewInt32(ctx, ret);
51 }
52
53
native_lcd_show(JSContext * ctx,JSValueConst this_val,int argc,JSValueConst * argv)54 static JSValue native_lcd_show(JSContext *ctx, JSValueConst this_val,
55 int argc, JSValueConst *argv)
56 {
57 int ret = -1;
58 uint8_t *buf;
59 uint32_t buf_len = 0;
60 int32_t x, y, w, h;
61 bool rotate;
62
63 if (argc < 6) {
64 amp_warn(MOD_STR, "parameter must be x, y, w, h, color");
65 goto out;
66 }
67
68 JS_ToInt32(ctx, &x, argv[0]);
69 JS_ToInt32(ctx, &y, argv[1]);
70 JS_ToInt32(ctx, &w, argv[2]);
71 JS_ToInt32(ctx, &h, argv[3]);
72
73 buf = JS_GetArrayBuffer(ctx, &buf_len, argv[4]);
74 if (!buf) {
75 amp_warn(MOD_STR, "parameter buffer is invalid, size: %d", buf_len);
76 goto out;
77 }
78
79 rotate = JS_ToBool(ctx, argv[5]);
80
81 ret = aos_hal_lcd_show(x, y, w, h, buf, rotate);
82 if (0 != ret) {
83 amp_error(MOD_STR, "aos_hal_lcd_fill fail!");
84 }
85
86 out:
87 return JS_NewInt32(ctx, ret);
88 }
89
native_lcd_fill(JSContext * ctx,JSValueConst this_val,int argc,JSValueConst * argv)90 static JSValue native_lcd_fill(JSContext *ctx, JSValueConst this_val,
91 int argc, JSValueConst *argv)
92 {
93 int ret = -1;
94 int32_t x, y, w, h;
95 uint32_t color;
96
97 if (argc < 5) {
98 amp_warn(MOD_STR, "parameter must be x, y, w, h, color");
99 goto out;
100 }
101
102 JS_ToInt32(ctx, &x, argv[0]);
103 JS_ToInt32(ctx, &y, argv[1]);
104 JS_ToInt32(ctx, &w, argv[2]);
105 JS_ToInt32(ctx, &h, argv[3]);
106 JS_ToUint32(ctx, &color, argv[4]);
107
108 ret = aos_hal_lcd_fill(x, y, w, h, color);
109 if (0 != ret) {
110 amp_error(MOD_STR, "aos_hal_lcd_fill fail!");
111 }
112
113 out:
114 return JS_NewInt32(ctx, ret);
115 }
116
117 static JSClassDef js_lcd_class = {
118 "LCD",
119 };
120
121 static const JSCFunctionListEntry js_lcd_funcs[] = {
122 JS_CFUNC_DEF("open", 0, native_lcd_open),
123 JS_CFUNC_DEF("show", 0, native_lcd_show),
124 JS_CFUNC_DEF("fill", 0, native_lcd_fill),
125 JS_CFUNC_DEF("close", 0, native_lcd_close),
126 };
127
js_lcd_init(JSContext * ctx,JSModuleDef * m)128 static int js_lcd_init(JSContext *ctx, JSModuleDef *m)
129 {
130 JSValue proto;
131
132 JS_NewClassID(&js_lcd_class_id);
133
134 JS_NewClass(JS_GetRuntime(ctx), js_lcd_class_id, &js_lcd_class);
135 proto = JS_NewObject(ctx);
136 JS_SetPropertyFunctionList(ctx, proto, js_lcd_funcs,
137 countof(js_lcd_funcs));
138 JS_SetClassProto(ctx, js_lcd_class_id, proto);
139
140 return JS_SetModuleExportList(ctx, m, js_lcd_funcs,
141 countof(js_lcd_funcs));
142 }
143
js_init_module_lcd(JSContext * ctx,const char * module_name)144 JSModuleDef *js_init_module_lcd(JSContext *ctx, const char *module_name)
145 {
146 JSModuleDef *m;
147 m = JS_NewCModule(ctx, module_name, js_lcd_init);
148 if (!m)
149 return NULL;
150 JS_AddModuleExportList(ctx, m, js_lcd_funcs, countof(js_lcd_funcs));
151 return m;
152 }
153
module_lcd_register(void)154 void module_lcd_register(void)
155 {
156 amp_debug(MOD_STR, "module_lcd_register");
157 JSContext *ctx = js_get_context();
158 aos_printf("module lcd register\n");
159 js_init_module_lcd(ctx, "lcd");
160 }
161