1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation 2425bb815Sopenharmony_ci * 3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License. 5425bb815Sopenharmony_ci * You may obtain a copy of the License at 6425bb815Sopenharmony_ci * 7425bb815Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8425bb815Sopenharmony_ci * 9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS 11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and 13425bb815Sopenharmony_ci * limitations under the License. 14425bb815Sopenharmony_ci */ 15425bb815Sopenharmony_ci#include "jerryscript-mbed-util/logging.h" 16425bb815Sopenharmony_ci#include "jerryscript-mbed-library-registry/wrap_tools.h" 17425bb815Sopenharmony_ci 18425bb815Sopenharmony_ci#include "mbed.h" 19425bb815Sopenharmony_ci 20425bb815Sopenharmony_ci/** 21425bb815Sopenharmony_ci * PwmOut#destructor 22425bb815Sopenharmony_ci * 23425bb815Sopenharmony_ci * Called if/when the PwmOut is GC'ed. 24425bb815Sopenharmony_ci */ 25425bb815Sopenharmony_civoid NAME_FOR_CLASS_NATIVE_DESTRUCTOR(PwmOut)(void* void_ptr) { 26425bb815Sopenharmony_ci delete static_cast<PwmOut*>(void_ptr); 27425bb815Sopenharmony_ci} 28425bb815Sopenharmony_ci 29425bb815Sopenharmony_ci/** 30425bb815Sopenharmony_ci * Type infomation of the native PwmOut pointer 31425bb815Sopenharmony_ci * 32425bb815Sopenharmony_ci * Set PwmOut#destructor as the free callback. 33425bb815Sopenharmony_ci */ 34425bb815Sopenharmony_cistatic const jerry_object_native_info_t native_obj_type_info = { 35425bb815Sopenharmony_ci .free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR(PwmOut) 36425bb815Sopenharmony_ci}; 37425bb815Sopenharmony_ci 38425bb815Sopenharmony_ci/** 39425bb815Sopenharmony_ci * PwmOut#write (native JavaScript method) 40425bb815Sopenharmony_ci * 41425bb815Sopenharmony_ci * Set the ouput duty-cycle, specified as a percentage (float) 42425bb815Sopenharmony_ci * 43425bb815Sopenharmony_ci * @param value A floating-point value representing the output duty-cycle, 44425bb815Sopenharmony_ci * specified as a percentage. The value should lie between 45425bb815Sopenharmony_ci * 0.0 (representing on 0%) and 1.0 (representing on 100%). 46425bb815Sopenharmony_ci * Values outside this range will be saturated to 0.0f or 1.0f 47425bb815Sopenharmony_ci * @returns undefined 48425bb815Sopenharmony_ci */ 49425bb815Sopenharmony_ciDECLARE_CLASS_FUNCTION(PwmOut, write) { 50425bb815Sopenharmony_ci CHECK_ARGUMENT_COUNT(PwmOut, write, (args_count == 1)); 51425bb815Sopenharmony_ci CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, write, 0, number); 52425bb815Sopenharmony_ci 53425bb815Sopenharmony_ci // Extract native PwmOut pointer 54425bb815Sopenharmony_ci void* void_ptr; 55425bb815Sopenharmony_ci bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &native_obj_type_info); 56425bb815Sopenharmony_ci 57425bb815Sopenharmony_ci if (!has_ptr) { 58425bb815Sopenharmony_ci return jerry_create_error(JERRY_ERROR_TYPE, 59425bb815Sopenharmony_ci (const jerry_char_t *) "Failed to get native PwmOut pointer"); 60425bb815Sopenharmony_ci } 61425bb815Sopenharmony_ci 62425bb815Sopenharmony_ci PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr); 63425bb815Sopenharmony_ci 64425bb815Sopenharmony_ci double arg0 = jerry_get_number_value(args[0]); 65425bb815Sopenharmony_ci native_ptr->write(static_cast<float>(arg0)); 66425bb815Sopenharmony_ci 67425bb815Sopenharmony_ci return jerry_create_undefined(); 68425bb815Sopenharmony_ci} 69425bb815Sopenharmony_ci 70425bb815Sopenharmony_ci/** 71425bb815Sopenharmony_ci * PwmOut#read (native JavaScript method) 72425bb815Sopenharmony_ci * 73425bb815Sopenharmony_ci * Return the current output duty-cycle setting, measured as a percentage (float) 74425bb815Sopenharmony_ci * 75425bb815Sopenharmony_ci * @returns 76425bb815Sopenharmony_ci * A floating-point value representing the current duty-cycle being output on the pin, 77425bb815Sopenharmony_ci * measured as a percentage. The returned value will lie between 78425bb815Sopenharmony_ci * 0.0 (representing on 0%) and 1.0 (representing on 100%). 79425bb815Sopenharmony_ci * 80425bb815Sopenharmony_ci * @note 81425bb815Sopenharmony_ci * This value may not match exactly the value set by a previous <write>. 82425bb815Sopenharmony_ci */ 83425bb815Sopenharmony_ciDECLARE_CLASS_FUNCTION(PwmOut, read) { 84425bb815Sopenharmony_ci CHECK_ARGUMENT_COUNT(PwmOut, read, (args_count == 0)); 85425bb815Sopenharmony_ci 86425bb815Sopenharmony_ci // Extract native PwmOut pointer 87425bb815Sopenharmony_ci void* void_ptr; 88425bb815Sopenharmony_ci bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &native_obj_type_info); 89425bb815Sopenharmony_ci 90425bb815Sopenharmony_ci if (!has_ptr) { 91425bb815Sopenharmony_ci return jerry_create_error(JERRY_ERROR_TYPE, 92425bb815Sopenharmony_ci (const jerry_char_t *) "Failed to get native PwmOut pointer"); 93425bb815Sopenharmony_ci } 94425bb815Sopenharmony_ci 95425bb815Sopenharmony_ci PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr); 96425bb815Sopenharmony_ci 97425bb815Sopenharmony_ci float result = native_ptr->read(); 98425bb815Sopenharmony_ci return jerry_create_number(result); 99425bb815Sopenharmony_ci} 100425bb815Sopenharmony_ci 101425bb815Sopenharmony_ci/** 102425bb815Sopenharmony_ci * PwmOut#period (native JavaScript method) 103425bb815Sopenharmony_ci * 104425bb815Sopenharmony_ci * Set the PWM period, specified in seconds (float), keeping the duty cycle the same. 105425bb815Sopenharmony_ci * 106425bb815Sopenharmony_ci * @note 107425bb815Sopenharmony_ci * The resolution is currently in microseconds; periods smaller than this 108425bb815Sopenharmony_ci * will be set to zero. 109425bb815Sopenharmony_ci */ 110425bb815Sopenharmony_ciDECLARE_CLASS_FUNCTION(PwmOut, period) { 111425bb815Sopenharmony_ci CHECK_ARGUMENT_COUNT(PwmOut, period, (args_count == 1)); 112425bb815Sopenharmony_ci CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, period, 0, number); 113425bb815Sopenharmony_ci 114425bb815Sopenharmony_ci // Extract native PwmOut pointer 115425bb815Sopenharmony_ci void* void_ptr; 116425bb815Sopenharmony_ci bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &native_obj_type_info); 117425bb815Sopenharmony_ci 118425bb815Sopenharmony_ci if (!has_ptr) { 119425bb815Sopenharmony_ci return jerry_create_error(JERRY_ERROR_TYPE, 120425bb815Sopenharmony_ci (const jerry_char_t *) "Failed to get native PwmOut pointer"); 121425bb815Sopenharmony_ci } 122425bb815Sopenharmony_ci 123425bb815Sopenharmony_ci PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr); 124425bb815Sopenharmony_ci 125425bb815Sopenharmony_ci double arg0 = jerry_get_number_value(args[0]); 126425bb815Sopenharmony_ci native_ptr->period(static_cast<float>(arg0)); 127425bb815Sopenharmony_ci 128425bb815Sopenharmony_ci return jerry_create_undefined(); 129425bb815Sopenharmony_ci} 130425bb815Sopenharmony_ci 131425bb815Sopenharmony_ci/** 132425bb815Sopenharmony_ci * PwmOut#period_ms (native JavaScript method) 133425bb815Sopenharmony_ci * 134425bb815Sopenharmony_ci * Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same. 135425bb815Sopenharmony_ci */ 136425bb815Sopenharmony_ciDECLARE_CLASS_FUNCTION(PwmOut, period_ms) { 137425bb815Sopenharmony_ci CHECK_ARGUMENT_COUNT(PwmOut, period_ms, (args_count == 1)); 138425bb815Sopenharmony_ci CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, period_ms, 0, number); 139425bb815Sopenharmony_ci 140425bb815Sopenharmony_ci // Extract native PwmOut pointer 141425bb815Sopenharmony_ci void* void_ptr; 142425bb815Sopenharmony_ci bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &native_obj_type_info); 143425bb815Sopenharmony_ci 144425bb815Sopenharmony_ci if (!has_ptr) { 145425bb815Sopenharmony_ci return jerry_create_error(JERRY_ERROR_TYPE, 146425bb815Sopenharmony_ci (const jerry_char_t *) "Failed to get native PwmOut pointer"); 147425bb815Sopenharmony_ci } 148425bb815Sopenharmony_ci 149425bb815Sopenharmony_ci PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr); 150425bb815Sopenharmony_ci 151425bb815Sopenharmony_ci double arg0 = jerry_get_number_value(args[0]); 152425bb815Sopenharmony_ci native_ptr->period_ms(static_cast<int>(arg0)); 153425bb815Sopenharmony_ci 154425bb815Sopenharmony_ci return jerry_create_undefined(); 155425bb815Sopenharmony_ci} 156425bb815Sopenharmony_ci 157425bb815Sopenharmony_ci/** 158425bb815Sopenharmony_ci * PwmOut#period_us (native JavaScript method) 159425bb815Sopenharmony_ci * 160425bb815Sopenharmony_ci * Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same. 161425bb815Sopenharmony_ci */ 162425bb815Sopenharmony_ciDECLARE_CLASS_FUNCTION(PwmOut, period_us) { 163425bb815Sopenharmony_ci CHECK_ARGUMENT_COUNT(PwmOut, period_us, (args_count == 1)); 164425bb815Sopenharmony_ci CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, period_us, 0, number); 165425bb815Sopenharmony_ci 166425bb815Sopenharmony_ci // Extract native PwmOut pointer 167425bb815Sopenharmony_ci void* void_ptr; 168425bb815Sopenharmony_ci bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &native_obj_type_info); 169425bb815Sopenharmony_ci 170425bb815Sopenharmony_ci if (!has_ptr) { 171425bb815Sopenharmony_ci return jerry_create_error(JERRY_ERROR_TYPE, 172425bb815Sopenharmony_ci (const jerry_char_t *) "Failed to get native PwmOut pointer"); 173425bb815Sopenharmony_ci } 174425bb815Sopenharmony_ci 175425bb815Sopenharmony_ci PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr); 176425bb815Sopenharmony_ci 177425bb815Sopenharmony_ci double arg0 = jerry_get_number_value(args[0]); 178425bb815Sopenharmony_ci native_ptr->period_us(static_cast<int>(arg0)); 179425bb815Sopenharmony_ci 180425bb815Sopenharmony_ci return jerry_create_undefined(); 181425bb815Sopenharmony_ci} 182425bb815Sopenharmony_ci 183425bb815Sopenharmony_ci/** 184425bb815Sopenharmony_ci * PwmOut#pulsewidth (native JavaScript method) 185425bb815Sopenharmony_ci * 186425bb815Sopenharmony_ci * Set the PWM pulsewidth, specified in seconds (float), keeping the period the same. 187425bb815Sopenharmony_ci */ 188425bb815Sopenharmony_ciDECLARE_CLASS_FUNCTION(PwmOut, pulsewidth) { 189425bb815Sopenharmony_ci CHECK_ARGUMENT_COUNT(PwmOut, pulsewidth, (args_count == 1)); 190425bb815Sopenharmony_ci CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, pulsewidth, 0, number); 191425bb815Sopenharmony_ci 192425bb815Sopenharmony_ci // Extract native PwmOut pointer 193425bb815Sopenharmony_ci void* void_ptr; 194425bb815Sopenharmony_ci bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &native_obj_type_info); 195425bb815Sopenharmony_ci 196425bb815Sopenharmony_ci if (!has_ptr) { 197425bb815Sopenharmony_ci return jerry_create_error(JERRY_ERROR_TYPE, 198425bb815Sopenharmony_ci (const jerry_char_t *) "Failed to get native PwmOut pointer"); 199425bb815Sopenharmony_ci } 200425bb815Sopenharmony_ci 201425bb815Sopenharmony_ci PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr); 202425bb815Sopenharmony_ci 203425bb815Sopenharmony_ci double arg0 = jerry_get_number_value(args[0]); 204425bb815Sopenharmony_ci native_ptr->pulsewidth(static_cast<float>(arg0)); 205425bb815Sopenharmony_ci 206425bb815Sopenharmony_ci return jerry_create_undefined(); 207425bb815Sopenharmony_ci} 208425bb815Sopenharmony_ci 209425bb815Sopenharmony_ci/** 210425bb815Sopenharmony_ci * PwmOut#pulsewidth_ms (native JavaScript method) 211425bb815Sopenharmony_ci * 212425bb815Sopenharmony_ci * Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same. 213425bb815Sopenharmony_ci */ 214425bb815Sopenharmony_ciDECLARE_CLASS_FUNCTION(PwmOut, pulsewidth_ms) { 215425bb815Sopenharmony_ci CHECK_ARGUMENT_COUNT(PwmOut, pulsewidth_ms, (args_count == 1)); 216425bb815Sopenharmony_ci CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, pulsewidth_ms, 0, number); 217425bb815Sopenharmony_ci 218425bb815Sopenharmony_ci // Extract native PwmOut pointer 219425bb815Sopenharmony_ci void* void_ptr; 220425bb815Sopenharmony_ci bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &native_obj_type_info); 221425bb815Sopenharmony_ci 222425bb815Sopenharmony_ci if (!has_ptr) { 223425bb815Sopenharmony_ci return jerry_create_error(JERRY_ERROR_TYPE, 224425bb815Sopenharmony_ci (const jerry_char_t *) "Failed to get native PwmOut pointer"); 225425bb815Sopenharmony_ci } 226425bb815Sopenharmony_ci 227425bb815Sopenharmony_ci PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr); 228425bb815Sopenharmony_ci 229425bb815Sopenharmony_ci double arg0 = jerry_get_number_value(args[0]); 230425bb815Sopenharmony_ci native_ptr->pulsewidth_ms(static_cast<int>(arg0)); 231425bb815Sopenharmony_ci 232425bb815Sopenharmony_ci return jerry_create_undefined(); 233425bb815Sopenharmony_ci} 234425bb815Sopenharmony_ci 235425bb815Sopenharmony_ci/** 236425bb815Sopenharmony_ci * PwmOut#pulsewidth_us (native JavaScript method) 237425bb815Sopenharmony_ci * 238425bb815Sopenharmony_ci * Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same. 239425bb815Sopenharmony_ci */ 240425bb815Sopenharmony_ciDECLARE_CLASS_FUNCTION(PwmOut, pulsewidth_us) { 241425bb815Sopenharmony_ci CHECK_ARGUMENT_COUNT(PwmOut, pulsewidth_us, (args_count == 1)); 242425bb815Sopenharmony_ci CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, pulsewidth_us, 0, number); 243425bb815Sopenharmony_ci 244425bb815Sopenharmony_ci // Extract native PwmOut pointer 245425bb815Sopenharmony_ci void* void_ptr; 246425bb815Sopenharmony_ci bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &native_obj_type_info); 247425bb815Sopenharmony_ci 248425bb815Sopenharmony_ci if (!has_ptr) { 249425bb815Sopenharmony_ci return jerry_create_error(JERRY_ERROR_TYPE, 250425bb815Sopenharmony_ci (const jerry_char_t *) "Failed to get native PwmOut pointer"); 251425bb815Sopenharmony_ci } 252425bb815Sopenharmony_ci 253425bb815Sopenharmony_ci PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr); 254425bb815Sopenharmony_ci 255425bb815Sopenharmony_ci double arg0 = jerry_get_number_value(args[0]); 256425bb815Sopenharmony_ci native_ptr->pulsewidth_us(static_cast<int>(arg0)); 257425bb815Sopenharmony_ci 258425bb815Sopenharmony_ci return jerry_create_undefined(); 259425bb815Sopenharmony_ci} 260425bb815Sopenharmony_ci 261425bb815Sopenharmony_ci/** 262425bb815Sopenharmony_ci * PwmOut (native JavaScript constructor) 263425bb815Sopenharmony_ci * 264425bb815Sopenharmony_ci * @param pin_name mbed pin to connect the PwmOut to. 265425bb815Sopenharmony_ci * @returns a JavaScript object representing a PwmOut. 266425bb815Sopenharmony_ci */ 267425bb815Sopenharmony_ciDECLARE_CLASS_CONSTRUCTOR(PwmOut) { 268425bb815Sopenharmony_ci CHECK_ARGUMENT_COUNT(PwmOut, __constructor, args_count == 1); 269425bb815Sopenharmony_ci CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, __constructor, 0, number); 270425bb815Sopenharmony_ci 271425bb815Sopenharmony_ci PinName pin_name = PinName(jerry_get_number_value(args[0])); 272425bb815Sopenharmony_ci 273425bb815Sopenharmony_ci // Create the native object 274425bb815Sopenharmony_ci PwmOut* native_ptr = new PwmOut(pin_name); 275425bb815Sopenharmony_ci 276425bb815Sopenharmony_ci // create the jerryscript object 277425bb815Sopenharmony_ci jerry_value_t js_object = jerry_create_object(); 278425bb815Sopenharmony_ci jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info); 279425bb815Sopenharmony_ci 280425bb815Sopenharmony_ci // attach methods 281425bb815Sopenharmony_ci ATTACH_CLASS_FUNCTION(js_object, PwmOut, write); 282425bb815Sopenharmony_ci ATTACH_CLASS_FUNCTION(js_object, PwmOut, read); 283425bb815Sopenharmony_ci ATTACH_CLASS_FUNCTION(js_object, PwmOut, period); 284425bb815Sopenharmony_ci ATTACH_CLASS_FUNCTION(js_object, PwmOut, period_ms); 285425bb815Sopenharmony_ci ATTACH_CLASS_FUNCTION(js_object, PwmOut, period_us); 286425bb815Sopenharmony_ci ATTACH_CLASS_FUNCTION(js_object, PwmOut, pulsewidth); 287425bb815Sopenharmony_ci ATTACH_CLASS_FUNCTION(js_object, PwmOut, pulsewidth_ms); 288425bb815Sopenharmony_ci ATTACH_CLASS_FUNCTION(js_object, PwmOut, pulsewidth_us); 289425bb815Sopenharmony_ci 290425bb815Sopenharmony_ci return js_object; 291425bb815Sopenharmony_ci} 292