• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "jerryscript-mbed-util/logging.h"
16 #include "jerryscript-mbed-library-registry/wrap_tools.h"
17 
18 #include "mbed.h"
19 
20 /**
21  * DigitalOut#destructor
22  *
23  * Called if/when the DigitalOut is GC'ed.
24  */
NAME_FOR_CLASS_NATIVE_DESTRUCTOR(DigitalOut)25 void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(DigitalOut)(void* void_ptr) {
26     delete static_cast<DigitalOut*>(void_ptr);
27 }
28 
29 /**
30  * Type infomation of the native DigitalOut pointer
31  *
32  * Set DigitalOut#destructor as the free callback.
33  */
34 static const jerry_object_native_info_t native_obj_type_info = {
35     .free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR(DigitalOut)
36 };
37 
38 /**
39  * DigitalOut#write (native JavaScript method)
40  *
41  * Writes a binary value to a DigitalOut.
42  *
43  * @param value 1 or 0, specifying whether the output pin is high or low,
44  *      respectively
45  * @returns undefined, or an error if invalid arguments are provided.
46  */
DECLARE_CLASS_FUNCTION(DigitalOut,write)47 DECLARE_CLASS_FUNCTION(DigitalOut, write) {
48     CHECK_ARGUMENT_COUNT(DigitalOut, write, (args_count == 1));
49     CHECK_ARGUMENT_TYPE_ALWAYS(DigitalOut, write, 0, number);
50 
51     // Extract native DigitalOut pointer
52     void* void_ptr;
53     bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &native_obj_type_info);
54 
55     if (!has_ptr) {
56         return jerry_create_error(JERRY_ERROR_TYPE,
57                                   (const jerry_char_t *) "Failed to get native DigitalOut pointer");
58     }
59 
60     DigitalOut* native_ptr = static_cast<DigitalOut*>(void_ptr);
61 
62     int arg0 = jerry_get_number_value(args[0]);
63     native_ptr->write(arg0);
64 
65     return jerry_create_undefined();
66 }
67 
68 /**
69  * DigitalOut#read (native JavaScript method)
70  *
71  * Reads the current status of a DigitalOut
72  *
73  * @returns 1 if the pin is currently high, or 0 if the pin is currently low.
74  */
DECLARE_CLASS_FUNCTION(DigitalOut,read)75 DECLARE_CLASS_FUNCTION(DigitalOut, read) {
76     CHECK_ARGUMENT_COUNT(DigitalOut, read, (args_count == 0));
77 
78     // Extract native DigitalOut pointer
79     void* void_ptr;
80     bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &native_obj_type_info);
81 
82     if (!has_ptr) {
83         return jerry_create_error(JERRY_ERROR_TYPE,
84                                   (const jerry_char_t *) "Failed to get native DigitalOut pointer");
85     }
86 
87     DigitalOut* native_ptr = static_cast<DigitalOut*>(void_ptr);
88 
89     int result = native_ptr->read();
90     return jerry_create_number(result);
91 }
92 
93 /**
94  * DigitalOut#is_connected (native JavaScript method)
95  *
96  * @returns 0 if the DigitalOut is set to NC, or 1 if it is connected to an
97  *  actual pin
98  */
DECLARE_CLASS_FUNCTION(DigitalOut,is_connected)99 DECLARE_CLASS_FUNCTION(DigitalOut, is_connected) {
100     CHECK_ARGUMENT_COUNT(DigitalOut, is_connected, (args_count == 0));
101 
102     // Extract native DigitalOut pointer
103     void* void_ptr;
104     bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &native_obj_type_info);
105 
106     if (!has_ptr) {
107         return jerry_create_error(JERRY_ERROR_TYPE,
108                                   (const jerry_char_t *) "Failed to get native DigitalOut pointer");
109     }
110 
111     DigitalOut* native_ptr = static_cast<DigitalOut*>(void_ptr);
112 
113     int result = native_ptr->is_connected();
114     return jerry_create_number(result);
115 }
116 
117 /**
118  * DigitalOut (native JavaScript constructor)
119  *
120  * @param pin_name mbed pin to connect the DigitalOut to.
121  * @param value (optional) Initial value of the DigitalOut.
122  * @returns a JavaScript object representing a DigitalOut.
123  */
DECLARE_CLASS_CONSTRUCTOR(DigitalOut)124 DECLARE_CLASS_CONSTRUCTOR(DigitalOut) {
125     CHECK_ARGUMENT_COUNT(DigitalOut, __constructor, (args_count == 1 || args_count == 2));
126     CHECK_ARGUMENT_TYPE_ALWAYS(DigitalOut, __constructor, 0, number);
127     CHECK_ARGUMENT_TYPE_ON_CONDITION(DigitalOut, __constructor, 1, number, (args_count == 2));
128 
129     DigitalOut* native_ptr;
130 
131     // Call correct overload of DigitalOut::DigitalOut depending on the
132     // arguments passed.
133     PinName pin_name = PinName(jerry_get_number_value(args[0]));
134 
135     switch (args_count) {
136         case 1:
137             native_ptr = new DigitalOut(pin_name);
138             break;
139         case 2:
140             int value = static_cast<int>(jerry_get_number_value(args[1]));
141             native_ptr = new DigitalOut(pin_name, value);
142             break;
143     }
144 
145     // create the jerryscript object
146     jerry_value_t js_object = jerry_create_object();
147     jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info);
148 
149     // attach methods
150     ATTACH_CLASS_FUNCTION(js_object, DigitalOut, write);
151     ATTACH_CLASS_FUNCTION(js_object, DigitalOut, read);
152     ATTACH_CLASS_FUNCTION(js_object, DigitalOut, is_connected);
153 
154     return js_object;
155 }
156