Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: LSM303AGR
LSM303AGR_JS-js.cpp
00001 /** 00002 ****************************************************************************** 00003 * @file LSM303AGR_JS-js.cpp 00004 * @author ST 00005 * @version V1.0.0 00006 * @date 9 October 2017 00007 * @brief Implementation of an LSM303AGR Accelerometer and Magnetometer sensor for 00008 * use with Javascript. 00009 ****************************************************************************** 00010 * @attention 00011 * 00012 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> 00013 * 00014 * Redistribution and use in source and binary forms, with or without modification, 00015 * are permitted provided that the following conditions are met: 00016 * 1. Redistributions of source code must retain the above copyright notice, 00017 * this list of conditions and the following disclaimer. 00018 * 2. Redistributions in binary form must reproduce the above copyright notice, 00019 * this list of conditions and the following disclaimer in the documentation 00020 * and/or other materials provided with the distribution. 00021 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00022 * may be used to endorse or promote products derived from this software 00023 * without specific prior written permission. 00024 * 00025 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00026 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00028 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00029 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00030 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00031 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00032 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00033 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00034 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00035 * 00036 ****************************************************************************** 00037 */ 00038 00039 00040 /* Includes ------------------------------------------------------------------*/ 00041 00042 #include "jerryscript-mbed-util/logging.h" 00043 #include "jerryscript-mbed-library-registry/wrap_tools.h" 00044 00045 // Load the library that we'll wrap 00046 #include "LSM303AGR_JS.h" 00047 00048 #include "mbed.h" 00049 00050 #ifdef TARGET_SENSOR_TILE 00051 mbed::Serial pc3((PinName)0x2C, (PinName)0x32); 00052 #define printf(...) pc3.printf(__VA_ARGS__) 00053 #endif 00054 00055 00056 /* Class Implementation ------------------------------------------------------*/ 00057 00058 /** 00059 * LSM303AGR_JS#destructor 00060 * 00061 * Called if/when the LSM303AGR_JS is GC'ed. 00062 */ 00063 void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(LSM303AGR_JS)(void *void_ptr) { 00064 delete static_cast<LSM303AGR_JS*>(void_ptr); 00065 } 00066 00067 00068 /** 00069 * Type infomation of the native LSM303AGR_JS pointer 00070 * 00071 * Set LSM303AGR_JS#destructor as the free callback. 00072 */ 00073 static const jerry_object_native_info_t native_obj_type_info = { 00074 .free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR(LSM303AGR_JS) 00075 }; 00076 00077 00078 /** 00079 * LSM303AGR_JS#init_acc_spi (native JavaScript method) 00080 * @brief Initializes the sensor using SPI interface 00081 * @param SPI object of helper class which handles the SPI peripheral 00082 * @param CS pin 00083 * @param INT1 pin 00084 * @param INT2 pin 00085 * @param SPI type 00086 */ 00087 DECLARE_CLASS_FUNCTION(LSM303AGR_JS, init_acc_spi) { 00088 CHECK_ARGUMENT_COUNT(LSM303AGR_JS, init_acc_spi, (args_count == 2 || args_count == 4)); 00089 CHECK_ARGUMENT_TYPE_ALWAYS(LSM303AGR_JS, init_acc_spi, 0, object); 00090 CHECK_ARGUMENT_TYPE_ALWAYS(LSM303AGR_JS, init_acc_spi, 1, number); 00091 CHECK_ARGUMENT_TYPE_ON_CONDITION(LSM303AGR_JS, init_acc_spi, 2, number, args_count == 4); 00092 CHECK_ARGUMENT_TYPE_ON_CONDITION(LSM303AGR_JS, init_acc_spi, 3, number, args_count == 4); 00093 00094 // Unwrap native LSM303AGR_JS object 00095 void *void_ptr; 00096 const jerry_object_native_info_t *type_ptr; 00097 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr); 00098 00099 if (!has_ptr || type_ptr != &native_obj_type_info) { 00100 return jerry_create_error(JERRY_ERROR_TYPE, 00101 (const jerry_char_t *) "Failed to get native LSM303AGR_JS pointer"); 00102 } 00103 00104 LSM303AGR_JS *native_ptr = static_cast<LSM303AGR_JS*>(void_ptr); 00105 00106 // Unwrap arguments 00107 void *spi_ptr; 00108 const jerry_object_native_info_t *spi_type_ptr; 00109 bool spi_has_ptr = jerry_get_object_native_pointer(args[0], &spi_ptr, &spi_type_ptr); 00110 00111 // Check if we have the spi pointer 00112 if (!spi_has_ptr) { 00113 printf("Not a SPI input!"); 00114 return jerry_create_error(JERRY_ERROR_TYPE, 00115 (const jerry_char_t *) "Failed to get native DigitalOut pointer"); 00116 } 00117 00118 // Cast the argument to C++ 00119 SPI* spi = reinterpret_cast<SPI*>(spi_ptr); 00120 00121 int cs_pin = jerry_get_number_value(args[1]); 00122 00123 // Call the native function 00124 if(args_count == 2){ 00125 native_ptr->init_acc(*spi, (PinName)cs_pin); 00126 } 00127 else if(args_count == 4){ 00128 int int1_pin = jerry_get_number_value(args[2]); 00129 int int2_pin = jerry_get_number_value(args[3]); 00130 native_ptr->init_acc(*spi, (PinName)cs_pin, (PinName)int1_pin, (PinName)int2_pin); 00131 } 00132 00133 return jerry_create_number(0); 00134 } 00135 00136 /** 00137 * LSM303AGR_JS#init_acc_i2c (native JavaScript method) 00138 * @brief Initializes the sensor using I2C interface 00139 * @param DevI2c object of helper class which handles the DevI2C peripheral 00140 * @param INT1 pin 00141 * @param INT2 pin 00142 * @param Address 00143 */ 00144 DECLARE_CLASS_FUNCTION(LSM303AGR_JS, init_acc_i2c) { 00145 CHECK_ARGUMENT_COUNT(LSM303AGR_JS, init_acc_i2c, (args_count == 1 || args_count == 3 || args_count == 4)); 00146 CHECK_ARGUMENT_TYPE_ALWAYS(LSM303AGR_JS, init_acc_i2c, 0, object); 00147 CHECK_ARGUMENT_TYPE_ON_CONDITION(LSM303AGR_JS, init_acc_i2c, 1, number, args_count == 3); 00148 CHECK_ARGUMENT_TYPE_ON_CONDITION(LSM303AGR_JS, init_acc_i2c, 2, number, args_count == 3); 00149 CHECK_ARGUMENT_TYPE_ON_CONDITION(LSM303AGR_JS, init_acc_i2c, 3, number, args_count == 4); 00150 00151 // Unwrap native LSM303AGR_JS object 00152 void *void_ptr; 00153 const jerry_object_native_info_t *type_ptr; 00154 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr); 00155 00156 if (!has_ptr || type_ptr != &native_obj_type_info) { 00157 return jerry_create_error(JERRY_ERROR_TYPE, 00158 (const jerry_char_t *) "Failed to get native LSM303AGR_JS pointer"); 00159 } 00160 00161 LSM303AGR_JS *native_ptr = static_cast<LSM303AGR_JS*>(void_ptr); 00162 00163 // Unwrap arguments 00164 void *i2c_ptr; 00165 const jerry_object_native_info_t *i2c_type_ptr; 00166 bool i2c_has_ptr = jerry_get_object_native_pointer(args[0], &i2c_ptr, &i2c_type_ptr); 00167 00168 // Check if we have the i2c pointer 00169 if (!i2c_has_ptr) { 00170 printf("Not a I2C input!"); 00171 return jerry_create_error(JERRY_ERROR_TYPE, 00172 (const jerry_char_t *) "Failed to get native DigitalOut pointer"); 00173 } 00174 00175 // Cast the argument to C++ 00176 DevI2C* i2c = reinterpret_cast<DevI2C*>(i2c_ptr); 00177 00178 // Call the native function 00179 if(args_count == 1){ 00180 native_ptr->init_acc(*i2c); 00181 } 00182 else if(args_count == 3){ 00183 int int1_pin = jerry_get_number_value(args[1]); 00184 int int2_pin = jerry_get_number_value(args[2]); 00185 native_ptr->init_acc(*i2c, (PinName) int1_pin, (PinName) int2_pin); 00186 } 00187 else if(args_count == 4){ 00188 int int1_pin = jerry_get_number_value(args[1]); 00189 int int2_pin = jerry_get_number_value(args[2]); 00190 int address = jerry_get_number_value(args[3]); 00191 native_ptr->init_acc(*i2c, (PinName) int1_pin, (PinName) int2_pin, (uint8_t) address); 00192 } 00193 00194 return jerry_create_number(0); 00195 } 00196 00197 00198 /** 00199 * LSM303AGR_JS#init_mag_spi (native JavaScript method) 00200 * @brief Initializes the sensor using SPI interface 00201 * @param SPI object of helper class which handles the SPI peripheral 00202 * @param CS pin 00203 * @param INT1 pin 00204 * @param INT2 pin 00205 * @param SPI type 00206 */ 00207 DECLARE_CLASS_FUNCTION(LSM303AGR_JS, init_mag_spi) { 00208 CHECK_ARGUMENT_COUNT(LSM303AGR_JS, init_mag_spi, (args_count == 2 || args_count == 3)); 00209 CHECK_ARGUMENT_TYPE_ALWAYS(LSM303AGR_JS, init_mag_spi, 0, object); 00210 CHECK_ARGUMENT_TYPE_ALWAYS(LSM303AGR_JS, init_mag_spi, 1, number); 00211 CHECK_ARGUMENT_TYPE_ON_CONDITION(LSM303AGR_JS, init_mag_spi, 2, number, args_count == 3); 00212 00213 // Unwrap native LSM303AGR_JS object 00214 void *void_ptr; 00215 const jerry_object_native_info_t *type_ptr; 00216 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr); 00217 00218 if (!has_ptr || type_ptr != &native_obj_type_info) { 00219 return jerry_create_error(JERRY_ERROR_TYPE, 00220 (const jerry_char_t *) "Failed to get native LSM303AGR_JS pointer"); 00221 } 00222 00223 LSM303AGR_JS *native_ptr = static_cast<LSM303AGR_JS*>(void_ptr); 00224 00225 // Unwrap arguments 00226 void *spi_ptr; 00227 const jerry_object_native_info_t *spi_type_ptr; 00228 bool spi_has_ptr = jerry_get_object_native_pointer(args[0], &spi_ptr, &spi_type_ptr); 00229 00230 // Check if we have the spi pointer 00231 if (!spi_has_ptr) { 00232 printf("Not a SPI input!"); 00233 return jerry_create_error(JERRY_ERROR_TYPE, 00234 (const jerry_char_t *) "Failed to get native DigitalOut pointer"); 00235 } 00236 00237 // Cast the argument to C++ 00238 SPI* spi = reinterpret_cast<SPI*>(spi_ptr); 00239 00240 int cs_pin = jerry_get_number_value(args[1]); 00241 00242 // Call the native function 00243 if(args_count == 2){ 00244 native_ptr->init_mag(*spi, (PinName)cs_pin); 00245 } 00246 else if(args_count == 3){ 00247 int int_pin = jerry_get_number_value(args[2]); 00248 00249 native_ptr->init_mag(*spi, (PinName)cs_pin, (PinName)int_pin); 00250 } 00251 00252 return jerry_create_number(0); 00253 } 00254 00255 /** 00256 * LSM303AGR_JS#init_mag_i2c (native JavaScript method) 00257 * @brief Initializes the sensor using I2C interface 00258 * @param DevI2c object of helper class which handles the DevI2C peripheral 00259 * @param INT1 pin 00260 * @param INT2 pin 00261 * @param Address 00262 */ 00263 DECLARE_CLASS_FUNCTION(LSM303AGR_JS, init_mag_i2c) { 00264 CHECK_ARGUMENT_COUNT(LSM303AGR_JS, init_mag_i2c, (args_count == 1 || args_count == 2 || args_count == 3)); 00265 CHECK_ARGUMENT_TYPE_ALWAYS(LSM303AGR_JS, init_mag_i2c, 0, object); 00266 CHECK_ARGUMENT_TYPE_ON_CONDITION(LSM303AGR_JS, init_mag_i2c, 1, number, args_count == 2); 00267 CHECK_ARGUMENT_TYPE_ON_CONDITION(LSM303AGR_JS, init_mag_i2c, 2, number, args_count == 3); 00268 00269 // Unwrap native LSM303AGR_JS object 00270 void *void_ptr; 00271 const jerry_object_native_info_t *type_ptr; 00272 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr); 00273 00274 if (!has_ptr || type_ptr != &native_obj_type_info) { 00275 return jerry_create_error(JERRY_ERROR_TYPE, 00276 (const jerry_char_t *) "Failed to get native LSM303AGR_JS pointer"); 00277 } 00278 00279 LSM303AGR_JS *native_ptr = static_cast<LSM303AGR_JS*>(void_ptr); 00280 00281 // Unwrap arguments 00282 void *i2c_ptr; 00283 const jerry_object_native_info_t *i2c_type_ptr; 00284 bool i2c_has_ptr = jerry_get_object_native_pointer(args[0], &i2c_ptr, &i2c_type_ptr); 00285 00286 // Check if we have the i2c pointer 00287 if (!i2c_has_ptr) { 00288 printf("Not a I2C input!"); 00289 return jerry_create_error(JERRY_ERROR_TYPE, 00290 (const jerry_char_t *) "Failed to get native DigitalOut pointer"); 00291 } 00292 00293 // Cast the argument to C++ 00294 DevI2C* i2c = reinterpret_cast<DevI2C*>(i2c_ptr); 00295 00296 // Call the native function 00297 if(args_count == 1){ 00298 native_ptr->init_mag(*i2c); 00299 } 00300 else if(args_count == 2){ 00301 int int_pin = jerry_get_number_value(args[1]); 00302 native_ptr->init_mag(*i2c, (PinName) int_pin); 00303 } 00304 else if(args_count == 3){ 00305 int int_pin = jerry_get_number_value(args[1]); 00306 int address = jerry_get_number_value(args[3]); 00307 native_ptr->init_mag(*i2c, (PinName) int_pin, (uint8_t) address); 00308 } 00309 00310 return jerry_create_number(0); 00311 } 00312 00313 /** 00314 * LSM303AGR_JS#get_accelerometer_axes (native JavaScript method) 00315 * @brief Get Magnerometer reading 00316 * @returns accelerometer axes 00317 */ 00318 DECLARE_CLASS_FUNCTION(LSM303AGR_JS, get_accelerometer_axes) { 00319 CHECK_ARGUMENT_COUNT(LSM303AGR_JS, get_accelerometer_axes, (args_count == 0)); 00320 00321 00322 // Unwrap native LSM303AGR_JS object 00323 void *void_ptr; 00324 const jerry_object_native_info_t *type_ptr; 00325 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr); 00326 00327 if (!has_ptr || type_ptr != &native_obj_type_info) { 00328 return jerry_create_error(JERRY_ERROR_TYPE, 00329 (const jerry_char_t *) "Failed to get native LSM303AGR_JS pointer"); 00330 } 00331 00332 LSM303AGR_JS *native_ptr = static_cast<LSM303AGR_JS*>(void_ptr); 00333 00334 // Get the result from the C++ API 00335 char *result = new char[128]; 00336 native_ptr->get_accelerometer_axes_json(result); 00337 00338 // Cast it back to JavaScript 00339 jerry_value_t out = jerry_create_string((unsigned char *)result); 00340 00341 //printf("acc: %s\n", result); 00342 // Recycle the result from function 00343 delete result; 00344 00345 // Return the output 00346 return out; 00347 } 00348 00349 00350 /** 00351 * LSM303AGR_JS#get_magnetometer_axes (native JavaScript method) 00352 * @brief Get Magnerometer reading 00353 * @returns Magnetometer axes 00354 */ 00355 DECLARE_CLASS_FUNCTION(LSM303AGR_JS, get_magnetometer_axes) { 00356 CHECK_ARGUMENT_COUNT(LSM303AGR_JS, get_magnetometer_axes, (args_count == 0)); 00357 00358 00359 // Unwrap native LSM303AGR_JS object 00360 void *void_ptr; 00361 const jerry_object_native_info_t *type_ptr; 00362 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr); 00363 00364 if (!has_ptr || type_ptr != &native_obj_type_info) { 00365 return jerry_create_error(JERRY_ERROR_TYPE, 00366 (const jerry_char_t *) "Failed to get native LSM303AGR_JS pointer"); 00367 } 00368 00369 LSM303AGR_JS *native_ptr = static_cast<LSM303AGR_JS*>(void_ptr); 00370 00371 // Get the result from the C++ API 00372 char *result = new char[128]; 00373 native_ptr->get_magnetometer_axes_json(result); 00374 00375 // Cast it back to JavaScript 00376 jerry_value_t out = jerry_create_string((unsigned char *)result); 00377 00378 //printf("mag: %s\n", result); 00379 // Recycle the result from function 00380 delete result; 00381 00382 // Return the output 00383 return out; 00384 } 00385 00386 /** 00387 * LSM303AGR_JS (native JavaScript constructor) 00388 * @brief Constructor for Javascript wrapper 00389 * @returns a JavaScript object representing LSM303AGR_JS. 00390 */ 00391 DECLARE_CLASS_CONSTRUCTOR(LSM303AGR_JS) { 00392 CHECK_ARGUMENT_COUNT(LSM303AGR_JS, __constructor, args_count == 0); 00393 00394 // Extract native LSM303AGR_JS pointer (from this object) 00395 LSM303AGR_JS *native_ptr = new LSM303AGR_JS(); 00396 00397 jerry_value_t js_object = jerry_create_object(); 00398 jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info); 00399 00400 // attach methods 00401 ATTACH_CLASS_FUNCTION(js_object, LSM303AGR_JS, get_accelerometer_axes); 00402 ATTACH_CLASS_FUNCTION(js_object, LSM303AGR_JS, get_magnetometer_axes); 00403 ATTACH_CLASS_FUNCTION(js_object, LSM303AGR_JS, init_acc_i2c); 00404 ATTACH_CLASS_FUNCTION(js_object, LSM303AGR_JS, init_acc_spi); 00405 ATTACH_CLASS_FUNCTION(js_object, LSM303AGR_JS, init_mag_i2c); 00406 ATTACH_CLASS_FUNCTION(js_object, LSM303AGR_JS, init_mag_spi); 00407 00408 00409 00410 return js_object; 00411 }
Generated on Thu Jul 14 2022 02:47:33 by
