forked from intel/zephyr.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzjs_grove_lcd_ipm.c
357 lines (282 loc) · 10 KB
/
zjs_grove_lcd_ipm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright (c) 2016-2018, Intel Corporation.
#ifdef BUILD_MODULE_GROVE_LCD
#ifndef QEMU_BUILD
// C includes
#include <string.h>
#ifndef ZJS_LINUX_BUILD
// Zephyr includes
#include <zephyr.h>
#endif
#include <device.h>
#include <display/grove_lcd.h>
#include <misc/util.h>
// ZJS includes
#include "zjs_ipm.h"
#include "zjs_util.h"
#define ZJS_GLCD_TIMEOUT_TICKS 5000
#define MAX_BUFFER_SIZE 256
static struct k_sem glcd_sem;
static jerry_value_t zjs_glcd_prototype;
static bool zjs_glcd_ipm_send_sync(zjs_ipm_message_t *send,
zjs_ipm_message_t *result)
{
send->id = MSG_ID_GLCD;
send->flags = 0 | MSG_SYNC_FLAG;
send->user_data = (void *)result;
send->error_code = ERROR_IPM_NONE;
if (zjs_ipm_send(MSG_ID_GLCD, send) != 0) {
ERR_PRINT("failed to send message\n");
return false;
}
// block until reply or timeout, we shouldn't see the ARC
// time out, if the ARC response comes back after it
// times out, it could pollute the result on the stack
if (k_sem_take(&glcd_sem, ZJS_GLCD_TIMEOUT_TICKS)) {
ERR_PRINT("FATAL ERROR, ipm timed out\n");
return false;
}
return true;
}
static jerry_value_t zjs_glcd_call_remote_function(zjs_ipm_message_t *send)
{
if (!send)
return zjs_error_context("invalid send message", 0, 0);
zjs_ipm_message_t reply;
bool success = zjs_glcd_ipm_send_sync(send, &reply);
if (!success) {
return zjs_error_context("ipm message failed or timed out!", 0, 0);
}
if (reply.error_code != ERROR_IPM_NONE) {
ERR_PRINT("error code: %u\n", (unsigned int)reply.error_code);
return zjs_error_context("error received", 0, 0);
}
u8_t value = reply.data.glcd.value;
return jerry_create_number(value);
}
// returns undefined instead of the value result
static jerry_value_t zjs_glcd_call_remote_ignore(zjs_ipm_message_t *send)
{
ZVAL rval = zjs_glcd_call_remote_function(send);
if (jerry_value_is_error(rval))
return rval;
return ZJS_UNDEFINED;
}
static void ipm_msg_receive_callback(void *context, u32_t id,
volatile void *data)
{
if (id != MSG_ID_GLCD)
return;
zjs_ipm_message_t *msg = (zjs_ipm_message_t *)(*(uintptr_t *)data);
if ((msg->flags & MSG_SYNC_FLAG) == MSG_SYNC_FLAG) {
zjs_ipm_message_t *result = (zjs_ipm_message_t *)msg->user_data;
// synchronous ipm, copy the results
if (result) {
*result = *msg;
}
// un-block sync api
k_sem_give(&glcd_sem);
} else {
// asynchronous ipm, should not get here
ZJS_ASSERT(false, "async message received");
}
}
static ZJS_DECL_FUNC(zjs_glcd_print)
{
// args: text
ZJS_VALIDATE_ARGS(Z_STRING);
jerry_size_t size = MAX_BUFFER_SIZE;
char *buffer = zjs_alloc_from_jstring(argv[0], &size);
if (!buffer) {
return zjs_error("cannot allocate buffer");
}
// send IPM message to the ARC side
zjs_ipm_message_t send;
send.type = TYPE_GLCD_PRINT;
send.data.glcd.buffer = buffer;
jerry_value_t result = zjs_glcd_call_remote_ignore(&send);
zjs_free(buffer);
return result;
}
static ZJS_DECL_FUNC(zjs_glcd_clear)
{
// send IPM message to the ARC side
zjs_ipm_message_t send;
// no input parameter to set
send.type = TYPE_GLCD_CLEAR;
return zjs_glcd_call_remote_function(&send);
}
static ZJS_DECL_FUNC(zjs_glcd_set_cursor_pos)
{
// args: column, row
ZJS_VALIDATE_ARGS(Z_NUMBER, Z_NUMBER);
// send IPM message to the ARC side
zjs_ipm_message_t send;
send.type = TYPE_GLCD_SET_CURSOR_POS;
send.data.glcd.col = (u8_t)jerry_get_number_value(argv[0]);
send.data.glcd.row = (u8_t)jerry_get_number_value(argv[1]);
return zjs_glcd_call_remote_ignore(&send);
}
static ZJS_DECL_FUNC(zjs_glcd_select_color)
{
// args: predefined color index
ZJS_VALIDATE_ARGS(Z_NUMBER);
// send IPM message to the ARC side
zjs_ipm_message_t send;
send.type = TYPE_GLCD_SELECT_COLOR;
send.data.glcd.value = (u8_t)jerry_get_number_value(argv[0]);
return zjs_glcd_call_remote_ignore(&send);
}
static ZJS_DECL_FUNC(zjs_glcd_set_color)
{
// args: red, green, blue
ZJS_VALIDATE_ARGS(Z_NUMBER, Z_NUMBER, Z_NUMBER);
// send IPM message to the ARC side
zjs_ipm_message_t send;
send.type = TYPE_GLCD_SET_COLOR;
send.data.glcd.color_r = (u8_t)jerry_get_number_value(argv[0]);
send.data.glcd.color_g = (u8_t)jerry_get_number_value(argv[1]);
send.data.glcd.color_b = (u8_t)jerry_get_number_value(argv[2]);
return zjs_glcd_call_remote_ignore(&send);
}
static ZJS_DECL_FUNC(zjs_glcd_set_function)
{
// args: predefined function number
ZJS_VALIDATE_ARGS(Z_NUMBER);
// send IPM message to the ARC side
zjs_ipm_message_t send;
send.type = TYPE_GLCD_SET_FUNCTION;
send.data.glcd.value = (u8_t)jerry_get_number_value(argv[0]);
return zjs_glcd_call_remote_ignore(&send);
}
static ZJS_DECL_FUNC(zjs_glcd_get_function)
{
// send IPM message to the ARC side
zjs_ipm_message_t send;
// no input parameter to set
send.type = TYPE_GLCD_GET_FUNCTION;
return zjs_glcd_call_remote_function(&send);
}
static ZJS_DECL_FUNC(zjs_glcd_set_display_state)
{
// args: predefined numeric constants
ZJS_VALIDATE_ARGS(Z_NUMBER);
// send IPM message to the ARC side
zjs_ipm_message_t send;
send.type = TYPE_GLCD_SET_DISPLAY_STATE;
send.data.glcd.value = (u8_t)jerry_get_number_value(argv[0]);
return zjs_glcd_call_remote_ignore(&send);
}
static ZJS_DECL_FUNC(zjs_glcd_get_display_state)
{
// send IPM message to the ARC side
zjs_ipm_message_t send;
// no input parameter to set
send.type = TYPE_GLCD_GET_DISPLAY_STATE;
return zjs_glcd_call_remote_function(&send);
}
static ZJS_DECL_FUNC(zjs_glcd_init)
{
// send IPM message to the ARC side
zjs_ipm_message_t send;
// no input parameter to set
send.type = TYPE_GLCD_INIT;
ZVAL result = zjs_glcd_call_remote_function(&send);
if (jerry_value_is_error(result)) {
return result;
}
// create the Grove LCD device object
jerry_value_t dev_obj = zjs_create_object();
jerry_set_prototype(dev_obj, zjs_glcd_prototype);
return dev_obj;
}
static void zjs_grove_lcd_cleanup(void *native)
{
jerry_release_value(zjs_glcd_prototype);
}
static const jerry_object_native_info_t grove_lcd_module_type_info = {
.free_cb = zjs_grove_lcd_cleanup
};
// Note. setInputState is not supported in Zephyr driver yet
// with right-to-left text flow (GLCD_IS_SHIFT_DECREMENT|GLCD_IS_ENTRY_RIGHT)
// and defaults to left-to-right only, so we don't support
// configuring input state until Zephyr implements this feature
static jerry_value_t zjs_grove_lcd_init()
{
zjs_ipm_init();
zjs_ipm_register_callback(MSG_ID_GLCD, ipm_msg_receive_callback);
k_sem_init(&glcd_sem, 0, 1);
zjs_native_func_t array[] = {
{ zjs_glcd_print, "print" },
{ zjs_glcd_clear, "clear" },
{ zjs_glcd_set_cursor_pos, "setCursorPos" },
{ zjs_glcd_select_color, "selectColor" },
{ zjs_glcd_set_color, "setColor" },
{ zjs_glcd_set_function, "setFunction" },
{ zjs_glcd_get_function, "getFunction" },
{ zjs_glcd_set_display_state, "setDisplayState" },
{ zjs_glcd_get_display_state, "getDisplayState" },
{ NULL, NULL }
};
zjs_glcd_prototype = zjs_create_object();
zjs_obj_add_functions(zjs_glcd_prototype, array);
// create global grove_lcd object
jerry_value_t glcd_obj = zjs_create_object();
zjs_obj_add_function(glcd_obj, "init", zjs_glcd_init);
// create object properties
jerry_value_t val;
// function flags
val = jerry_create_number(GLCD_FS_8BIT_MODE);
zjs_set_property(glcd_obj, "GLCD_FS_8BIT_MODE", val);
jerry_release_value(val);
val = jerry_create_number(GLCD_FS_ROWS_2);
zjs_set_property(glcd_obj, "GLCD_FS_ROWS_2", val);
jerry_release_value(val);
val = jerry_create_number(GLCD_FS_ROWS_1);
zjs_set_property(glcd_obj, "GLCD_FS_ROWS_1", val);
jerry_release_value(val);
val = jerry_create_number(GLCD_FS_DOT_SIZE_BIG);
zjs_set_property(glcd_obj, "GLCD_FS_DOT_SIZE_BIG", val);
jerry_release_value(val);
val = jerry_create_number(GLCD_FS_DOT_SIZE_LITTLE);
zjs_set_property(glcd_obj, "GLCD_FS_DOT_SIZE_LITTLE", val);
jerry_release_value(val);
// display state flags
val = jerry_create_number(GLCD_DS_DISPLAY_ON);
zjs_set_property(glcd_obj, "GLCD_DS_DISPLAY_ON", val);
jerry_release_value(val);
val = jerry_create_number(GLCD_DS_DISPLAY_OFF);
zjs_set_property(glcd_obj, "GLCD_DS_DISPLAY_OFF", val);
jerry_release_value(val);
val = jerry_create_number(GLCD_DS_CURSOR_ON);
zjs_set_property(glcd_obj, "GLCD_DS_CURSOR_ON", val);
jerry_release_value(val);
val = jerry_create_number(GLCD_DS_CURSOR_OFF);
zjs_set_property(glcd_obj, "GLCD_DS_CURSOR_OFF", val);
jerry_release_value(val);
val = jerry_create_number(GLCD_DS_BLINK_ON);
zjs_set_property(glcd_obj, "GLCD_DS_BLINK_ON", val);
jerry_release_value(val);
val = jerry_create_number(GLCD_DS_BLINK_OFF);
zjs_set_property(glcd_obj, "GLCD_DS_BLINK_OFF", val);
jerry_release_value(val);
// colors
val = jerry_create_number(GROVE_RGB_WHITE);
zjs_set_property(glcd_obj, "GROVE_RGB_WHITE", val);
jerry_release_value(val);
val = jerry_create_number(GROVE_RGB_RED);
zjs_set_property(glcd_obj, "GROVE_RGB_RED", val);
jerry_release_value(val);
val = jerry_create_number(GROVE_RGB_GREEN);
zjs_set_property(glcd_obj, "GROVE_RGB_GREEN", val);
jerry_release_value(val);
val = jerry_create_number(GROVE_RGB_BLUE);
zjs_set_property(glcd_obj, "GROVE_RGB_BLUE", val);
jerry_release_value(val);
// Set up cleanup function for when the object gets freed
jerry_set_object_native_pointer(glcd_obj, NULL,
&grove_lcd_module_type_info);
return glcd_obj;
}
JERRYX_NATIVE_MODULE(grove_lcd, zjs_grove_lcd_init)
#endif // QEMU_BUILD
#endif // BUILD_MODULE_GROVE_LCD