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 * PwmOut#destructor
22 *
23 * Called if/when the PwmOut is GC'ed.
24 */
NAME_FOR_CLASS_NATIVE_DESTRUCTOR(PwmOut)25 void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(PwmOut)(void* void_ptr) {
26 delete static_cast<PwmOut*>(void_ptr);
27 }
28
29 /**
30 * Type infomation of the native PwmOut pointer
31 *
32 * Set PwmOut#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(PwmOut)
36 };
37
38 /**
39 * PwmOut#write (native JavaScript method)
40 *
41 * Set the ouput duty-cycle, specified as a percentage (float)
42 *
43 * @param value A floating-point value representing the output duty-cycle,
44 * specified as a percentage. The value should lie between
45 * 0.0 (representing on 0%) and 1.0 (representing on 100%).
46 * Values outside this range will be saturated to 0.0f or 1.0f
47 * @returns undefined
48 */
DECLARE_CLASS_FUNCTION(PwmOut,write)49 DECLARE_CLASS_FUNCTION(PwmOut, write) {
50 CHECK_ARGUMENT_COUNT(PwmOut, write, (args_count == 1));
51 CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, write, 0, number);
52
53 // Extract native PwmOut pointer
54 void* void_ptr;
55 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &native_obj_type_info);
56
57 if (!has_ptr) {
58 return jerry_create_error(JERRY_ERROR_TYPE,
59 (const jerry_char_t *) "Failed to get native PwmOut pointer");
60 }
61
62 PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr);
63
64 double arg0 = jerry_get_number_value(args[0]);
65 native_ptr->write(static_cast<float>(arg0));
66
67 return jerry_create_undefined();
68 }
69
70 /**
71 * PwmOut#read (native JavaScript method)
72 *
73 * Return the current output duty-cycle setting, measured as a percentage (float)
74 *
75 * @returns
76 * A floating-point value representing the current duty-cycle being output on the pin,
77 * measured as a percentage. The returned value will lie between
78 * 0.0 (representing on 0%) and 1.0 (representing on 100%).
79 *
80 * @note
81 * This value may not match exactly the value set by a previous <write>.
82 */
DECLARE_CLASS_FUNCTION(PwmOut,read)83 DECLARE_CLASS_FUNCTION(PwmOut, read) {
84 CHECK_ARGUMENT_COUNT(PwmOut, read, (args_count == 0));
85
86 // Extract native PwmOut pointer
87 void* void_ptr;
88 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &native_obj_type_info);
89
90 if (!has_ptr) {
91 return jerry_create_error(JERRY_ERROR_TYPE,
92 (const jerry_char_t *) "Failed to get native PwmOut pointer");
93 }
94
95 PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr);
96
97 float result = native_ptr->read();
98 return jerry_create_number(result);
99 }
100
101 /**
102 * PwmOut#period (native JavaScript method)
103 *
104 * Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
105 *
106 * @note
107 * The resolution is currently in microseconds; periods smaller than this
108 * will be set to zero.
109 */
DECLARE_CLASS_FUNCTION(PwmOut,period)110 DECLARE_CLASS_FUNCTION(PwmOut, period) {
111 CHECK_ARGUMENT_COUNT(PwmOut, period, (args_count == 1));
112 CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, period, 0, number);
113
114 // Extract native PwmOut pointer
115 void* void_ptr;
116 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &native_obj_type_info);
117
118 if (!has_ptr) {
119 return jerry_create_error(JERRY_ERROR_TYPE,
120 (const jerry_char_t *) "Failed to get native PwmOut pointer");
121 }
122
123 PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr);
124
125 double arg0 = jerry_get_number_value(args[0]);
126 native_ptr->period(static_cast<float>(arg0));
127
128 return jerry_create_undefined();
129 }
130
131 /**
132 * PwmOut#period_ms (native JavaScript method)
133 *
134 * Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same.
135 */
DECLARE_CLASS_FUNCTION(PwmOut,period_ms)136 DECLARE_CLASS_FUNCTION(PwmOut, period_ms) {
137 CHECK_ARGUMENT_COUNT(PwmOut, period_ms, (args_count == 1));
138 CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, period_ms, 0, number);
139
140 // Extract native PwmOut pointer
141 void* void_ptr;
142 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &native_obj_type_info);
143
144 if (!has_ptr) {
145 return jerry_create_error(JERRY_ERROR_TYPE,
146 (const jerry_char_t *) "Failed to get native PwmOut pointer");
147 }
148
149 PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr);
150
151 double arg0 = jerry_get_number_value(args[0]);
152 native_ptr->period_ms(static_cast<int>(arg0));
153
154 return jerry_create_undefined();
155 }
156
157 /**
158 * PwmOut#period_us (native JavaScript method)
159 *
160 * Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same.
161 */
DECLARE_CLASS_FUNCTION(PwmOut,period_us)162 DECLARE_CLASS_FUNCTION(PwmOut, period_us) {
163 CHECK_ARGUMENT_COUNT(PwmOut, period_us, (args_count == 1));
164 CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, period_us, 0, number);
165
166 // Extract native PwmOut pointer
167 void* void_ptr;
168 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &native_obj_type_info);
169
170 if (!has_ptr) {
171 return jerry_create_error(JERRY_ERROR_TYPE,
172 (const jerry_char_t *) "Failed to get native PwmOut pointer");
173 }
174
175 PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr);
176
177 double arg0 = jerry_get_number_value(args[0]);
178 native_ptr->period_us(static_cast<int>(arg0));
179
180 return jerry_create_undefined();
181 }
182
183 /**
184 * PwmOut#pulsewidth (native JavaScript method)
185 *
186 * Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
187 */
DECLARE_CLASS_FUNCTION(PwmOut,pulsewidth)188 DECLARE_CLASS_FUNCTION(PwmOut, pulsewidth) {
189 CHECK_ARGUMENT_COUNT(PwmOut, pulsewidth, (args_count == 1));
190 CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, pulsewidth, 0, number);
191
192 // Extract native PwmOut pointer
193 void* void_ptr;
194 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &native_obj_type_info);
195
196 if (!has_ptr) {
197 return jerry_create_error(JERRY_ERROR_TYPE,
198 (const jerry_char_t *) "Failed to get native PwmOut pointer");
199 }
200
201 PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr);
202
203 double arg0 = jerry_get_number_value(args[0]);
204 native_ptr->pulsewidth(static_cast<float>(arg0));
205
206 return jerry_create_undefined();
207 }
208
209 /**
210 * PwmOut#pulsewidth_ms (native JavaScript method)
211 *
212 * Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same.
213 */
DECLARE_CLASS_FUNCTION(PwmOut,pulsewidth_ms)214 DECLARE_CLASS_FUNCTION(PwmOut, pulsewidth_ms) {
215 CHECK_ARGUMENT_COUNT(PwmOut, pulsewidth_ms, (args_count == 1));
216 CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, pulsewidth_ms, 0, number);
217
218 // Extract native PwmOut pointer
219 void* void_ptr;
220 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &native_obj_type_info);
221
222 if (!has_ptr) {
223 return jerry_create_error(JERRY_ERROR_TYPE,
224 (const jerry_char_t *) "Failed to get native PwmOut pointer");
225 }
226
227 PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr);
228
229 double arg0 = jerry_get_number_value(args[0]);
230 native_ptr->pulsewidth_ms(static_cast<int>(arg0));
231
232 return jerry_create_undefined();
233 }
234
235 /**
236 * PwmOut#pulsewidth_us (native JavaScript method)
237 *
238 * Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same.
239 */
DECLARE_CLASS_FUNCTION(PwmOut,pulsewidth_us)240 DECLARE_CLASS_FUNCTION(PwmOut, pulsewidth_us) {
241 CHECK_ARGUMENT_COUNT(PwmOut, pulsewidth_us, (args_count == 1));
242 CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, pulsewidth_us, 0, number);
243
244 // Extract native PwmOut pointer
245 void* void_ptr;
246 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &native_obj_type_info);
247
248 if (!has_ptr) {
249 return jerry_create_error(JERRY_ERROR_TYPE,
250 (const jerry_char_t *) "Failed to get native PwmOut pointer");
251 }
252
253 PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr);
254
255 double arg0 = jerry_get_number_value(args[0]);
256 native_ptr->pulsewidth_us(static_cast<int>(arg0));
257
258 return jerry_create_undefined();
259 }
260
261 /**
262 * PwmOut (native JavaScript constructor)
263 *
264 * @param pin_name mbed pin to connect the PwmOut to.
265 * @returns a JavaScript object representing a PwmOut.
266 */
DECLARE_CLASS_CONSTRUCTOR(PwmOut)267 DECLARE_CLASS_CONSTRUCTOR(PwmOut) {
268 CHECK_ARGUMENT_COUNT(PwmOut, __constructor, args_count == 1);
269 CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, __constructor, 0, number);
270
271 PinName pin_name = PinName(jerry_get_number_value(args[0]));
272
273 // Create the native object
274 PwmOut* native_ptr = new PwmOut(pin_name);
275
276 // create the jerryscript object
277 jerry_value_t js_object = jerry_create_object();
278 jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info);
279
280 // attach methods
281 ATTACH_CLASS_FUNCTION(js_object, PwmOut, write);
282 ATTACH_CLASS_FUNCTION(js_object, PwmOut, read);
283 ATTACH_CLASS_FUNCTION(js_object, PwmOut, period);
284 ATTACH_CLASS_FUNCTION(js_object, PwmOut, period_ms);
285 ATTACH_CLASS_FUNCTION(js_object, PwmOut, period_us);
286 ATTACH_CLASS_FUNCTION(js_object, PwmOut, pulsewidth);
287 ATTACH_CLASS_FUNCTION(js_object, PwmOut, pulsewidth_ms);
288 ATTACH_CLASS_FUNCTION(js_object, PwmOut, pulsewidth_us);
289
290 return js_object;
291 }
292