1 /* Copyright JS Foundation and other contributors, http://js.foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "application.h"
17 #include "jerryscript.h"
18
19 SYSTEM_MODE (MANUAL);
20
21 /**
22 * Set led value
23 */
24 static jerry_value_t
set_led(const jerry_value_t func_value,const jerry_value_t this_value,const jerry_value_t * args_p,const jerry_length_t args_cnt)25 set_led (const jerry_value_t func_value, /**< function object */
26 const jerry_value_t this_value, /**< this arg */
27 const jerry_value_t *args_p, /**< function arguments */
28 const jerry_length_t args_cnt) /**< number of function arguments */
29 {
30 if (args_cnt != 2)
31 {
32 Serial.println ("Wrong arguments count in 'test.setLed' function.");
33 return jerry_create_boolean (false);
34 }
35
36 int ledPin = jerry_get_number_value (args_p[0]);
37 bool value = jerry_get_boolean_value (args_p[1]);
38
39 pinMode (ledPin, OUTPUT);
40 digitalWrite (ledPin, value);
41
42 return jerry_create_boolean (true);
43 } /* set_led */
44
45 /**
46 * Delay function
47 */
48 static jerry_value_t
js_delay(const jerry_value_t func_value,const jerry_value_t this_value,const jerry_value_t * args_p,const jerry_length_t args_cnt)49 js_delay (const jerry_value_t func_value, /**< function object */
50 const jerry_value_t this_value, /**< this arg */
51 const jerry_value_t *args_p, /**< function arguments */
52 const jerry_length_t args_cnt) /**< number of function arguments */
53 {
54 if (args_cnt != 1)
55 {
56 Serial.println ("Wrong arguments count in 'test.delay' function.");
57 return jerry_create_boolean (false);
58 }
59
60 int millisec = jerry_get_number_value (args_p[0]);
61
62 delay (millisec);
63
64 return jerry_create_boolean (true);
65 } /* js_delay */
66
67 /*
68 * Init available js functions
69 */
70 static void
init_jerry()71 init_jerry ()
72 {
73 jerry_init (JERRY_INIT_EMPTY);
74
75 /* Create an empty JS object */
76 jerry_value_t object = jerry_create_object ();
77
78 jerry_value_t func_obj;
79 jerry_value_t prop_name;
80
81 func_obj = jerry_create_external_function (set_led);
82 prop_name = jerry_create_string ((const jerry_char_t *) "setLed");
83 jerry_release_value (jerry_set_property (object, prop_name, func_obj));
84 jerry_release_value (prop_name);
85 jerry_release_value (func_obj);
86
87 func_obj = jerry_create_external_function (js_delay);
88 prop_name = jerry_create_string ((const jerry_char_t *) "delay");
89 jerry_release_value (jerry_set_property (object, prop_name, func_obj));
90 jerry_release_value (prop_name);
91 jerry_release_value (func_obj);
92
93 /* Wrap the JS object (not empty anymore) into a jerry api value */
94 jerry_value_t global_object = jerry_get_global_object ();
95
96 /* Add the JS object to the global context */
97 prop_name = jerry_create_string ((const jerry_char_t *) "test");
98 jerry_release_value (jerry_set_property (global_object, prop_name, object));
99 jerry_release_value (prop_name);
100 jerry_release_value (object);
101 jerry_release_value (global_object);
102 } /* init_jerry */
103
104 /**
105 * Jerryscript simple test
106 */
107 static void
test_jerry()108 test_jerry ()
109 {
110 const jerry_char_t script[] = " \
111 test.setLed(7, true); \
112 test.delay(250); \
113 test.setLed(7, false); \
114 test.delay(250); \
115 ";
116
117 jerry_value_t eval_ret = jerry_eval (script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
118
119 /* Free JavaScript value, returned by eval */
120 jerry_release_value (eval_ret);
121 } /* test_jerry */
122
123 /**
124 * Setup code for particle firmware
125 */
126 void
setup()127 setup ()
128 {
129 Serial.begin (9600);
130 delay (2000);
131 Serial.println ("Beginning Listening mode test!");
132 } /* setup */
133
134 /**
135 * Loop code for particle firmware
136 */
137 void
loop()138 loop ()
139 {
140 init_jerry ();
141
142 /* Turn on and off the D7 LED */
143 test_jerry ();
144
145 jerry_cleanup ();
146 } /* loop */
147