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 * AnalogIn#destructor
22 *
23 * Called if/when the AnalogIn is GC'ed.
24 */
NAME_FOR_CLASS_NATIVE_DESTRUCTOR(AnalogIn)25 void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(AnalogIn)(void* void_ptr) {
26 delete static_cast<AnalogIn*>(void_ptr);
27 }
28
29 /**
30 * Type infomation of the native AnalogIn pointer
31 *
32 * Set AnalogIn#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(AnalogIn)
36 };
37
38 /**
39 * AnalogIn#read (native JavaScript method)
40 *
41 * Read the input voltage, represented as a float in the range [0.0, 1.0]
42 *
43 * @returns A floating-point value representing the current input voltage, measured as a percentage
44 */
DECLARE_CLASS_FUNCTION(AnalogIn,read)45 DECLARE_CLASS_FUNCTION(AnalogIn, read) {
46 CHECK_ARGUMENT_COUNT(AnalogIn, read, (args_count == 0));
47
48 // Extract native AnalogIn pointer
49 void* void_ptr;
50 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &native_obj_type_info);
51
52 if (!has_ptr) {
53 return jerry_create_error(JERRY_ERROR_TYPE,
54 (const jerry_char_t *) "Failed to get native AnalogIn pointer");
55 }
56
57 AnalogIn* native_ptr = static_cast<AnalogIn*>(void_ptr);
58
59 float result = native_ptr->read();
60 return jerry_create_number(result);
61 }
62
63 /**
64 * AnalogIn#read_u16 (native JavaScript method)
65 *
66 * Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
67 *
68 * @returns 16-bit unsigned short representing the current input voltage, normalised to a 16-bit value
69 */
DECLARE_CLASS_FUNCTION(AnalogIn,read_u16)70 DECLARE_CLASS_FUNCTION(AnalogIn, read_u16) {
71 CHECK_ARGUMENT_COUNT(AnalogIn, read_u16, (args_count == 0));
72
73 // Extract native AnalogIn pointer
74 void* void_ptr;
75 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &native_obj_type_info);
76
77 if (!has_ptr) {
78 return jerry_create_error(JERRY_ERROR_TYPE,
79 (const jerry_char_t *) "Failed to get native AnalogIn pointer");
80 }
81
82 AnalogIn* native_ptr = static_cast<AnalogIn*>(void_ptr);
83
84 uint16_t result = native_ptr->read_u16();
85 return jerry_create_number(result);
86 }
87
88 /**
89 * AnalogIn (native JavaScript constructor)
90 *
91 * @param pin_name mbed pin to connect the AnalogIn to.
92 * @returns a JavaScript object representing a AnalogIn.
93 */
DECLARE_CLASS_CONSTRUCTOR(AnalogIn)94 DECLARE_CLASS_CONSTRUCTOR(AnalogIn) {
95 CHECK_ARGUMENT_COUNT(AnalogIn, __constructor, args_count == 1);
96 CHECK_ARGUMENT_TYPE_ALWAYS(AnalogIn, __constructor, 0, number);
97
98 PinName pin_name = PinName(jerry_get_number_value(args[0]));
99
100 // create native object
101 AnalogIn* native_ptr = new AnalogIn(pin_name);
102
103 // create the jerryscript object
104 jerry_value_t js_object = jerry_create_object();
105 jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info);
106
107 // attach methods
108 ATTACH_CLASS_FUNCTION(js_object, AnalogIn, read);
109 ATTACH_CLASS_FUNCTION(js_object, AnalogIn, read_u16);
110
111 return js_object;
112 }
113