Javascript wrappers for LSM6DSL Sensor library

Dependencies:   LSM6DSL

Dependents:   ST_SENSOR_JS

Committer:
akhtar.syedzeeshan@gmail.com
Date:
Wed Oct 25 13:59:25 2017 +0200
Revision:
4:0eb0cded8861
Parent:
3:87ac9fb0d287
Child:
5:4f29cb91aaef
Fully implemented DevI2C support

Who changed what in which revision?

UserRevisionLine numberNew contents of line
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 1 /**
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 2 ******************************************************************************
akhtar.syedzeeshan@gmail.com 2:ebd484d17a02 3 * @file LSM6DSL_JS-js.cpp
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 4 * @author ST
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 5 * @version V1.0.0
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 6 * @date 9 October 2017
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 7 * @brief Implementation of an LSM6DSL Accelerometer and gyroscope sensor for
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 8 * use with Javascript.
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 9 ******************************************************************************
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 10 * @attention
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 11 *
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 12 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 13 *
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 14 * Redistribution and use in source and binary forms, with or without modification,
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 15 * are permitted provided that the following conditions are met:
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 16 * 1. Redistributions of source code must retain the above copyright notice,
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 17 * this list of conditions and the following disclaimer.
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 19 * this list of conditions and the following disclaimer in the documentation
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 20 * and/or other materials provided with the distribution.
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 22 * may be used to endorse or promote products derived from this software
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 23 * without specific prior written permission.
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 24 *
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 35 *
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 36 ******************************************************************************
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 37 */
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 38
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 39
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 40 /* Includes ------------------------------------------------------------------*/
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 41
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 42 #include "jerryscript-mbed-util/logging.h"
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 43 #include "jerryscript-mbed-library-registry/wrap_tools.h"
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 44
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 45 // Load the library that we'll wrap
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 46 #include "LSM6DSL_JS.h"
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 47
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 48 #include "mbed.h"
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 49
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 50 /* Class Implementation ------------------------------------------------------*/
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 51
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 52 /**
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 53 * LSM6DSL_JS#destructor
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 54 *
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 55 * Called if/when the LSM6DSL_JS is GC'ed.
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 56 */
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 57 void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(LSM6DSL_JS)(void *void_ptr) {
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 58 delete static_cast<LSM6DSL_JS*>(void_ptr);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 59 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 60
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 61
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 62 /**
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 63 * Type infomation of the native LSM6DSL_JS pointer
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 64 *
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 65 * Set LSM6DSL_JS#destructor as the free callback.
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 66 */
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 67 static const jerry_object_native_info_t native_obj_type_info = {
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 68 .free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR(LSM6DSL_JS)
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 69 };
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 70
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 71
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 72 /**
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 73 * LSM6DSL_JS#get_accelerometer_axes (native JavaScript method)
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 74 *
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 75 * @returns accelerometer axes
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 76 */
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 77 DECLARE_CLASS_FUNCTION(LSM6DSL_JS, get_accelerometer_axes) {
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 78 CHECK_ARGUMENT_COUNT(LSM6DSL_JS, get_accelerometer_axes, (args_count == 0));
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 79
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 80
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 81 // Unwrap native LSM6DSL_JS object
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 82 void *void_ptr;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 83 const jerry_object_native_info_t *type_ptr;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 84 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 85
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 86 if (!has_ptr || type_ptr != &native_obj_type_info) {
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 87 return jerry_create_error(JERRY_ERROR_TYPE,
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 88 (const jerry_char_t *) "Failed to get native LSM6DSL_JS pointer");
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 89 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 90
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 91 LSM6DSL_JS *native_ptr = static_cast<LSM6DSL_JS*>(void_ptr);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 92
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 93 // Get the result from the C++ API
akhtar.syedzeeshan@gmail.com 1:7e5e76bf3efe 94 char *result = new char[128];
akhtar.syedzeeshan@gmail.com 1:7e5e76bf3efe 95 native_ptr->get_accelerometer_axes_json(result);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 96
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 97 // Cast it back to JavaScript
akhtar.syedzeeshan@gmail.com 1:7e5e76bf3efe 98 jerry_value_t out = jerry_create_string((unsigned char *)result);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 99
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 100 // Recycle the result from function
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 101 delete result;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 102
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 103 // Return the output
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 104 return out;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 105 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 106
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 107
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 108 /**
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 109 * LSM6DSL_JS#get_gyroscope_axes (native JavaScript method)
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 110 *
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 111 * @returns gyroscope axes
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 112 */
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 113 DECLARE_CLASS_FUNCTION(LSM6DSL_JS, get_gyroscope_axes) {
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 114 CHECK_ARGUMENT_COUNT(LSM6DSL_JS, get_gyroscope_axes, (args_count == 0));
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 115
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 116
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 117 // Unwrap native LSM6DSL_JS object
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 118 void *void_ptr;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 119 const jerry_object_native_info_t *type_ptr;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 120 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 121
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 122 if (!has_ptr || type_ptr != &native_obj_type_info) {
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 123 return jerry_create_error(JERRY_ERROR_TYPE,
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 124 (const jerry_char_t *) "Failed to get native LSM6DSL_JS pointer");
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 125 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 126
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 127 LSM6DSL_JS *native_ptr = static_cast<LSM6DSL_JS*>(void_ptr);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 128
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 129 // Get the result from the C++ API
akhtar.syedzeeshan@gmail.com 1:7e5e76bf3efe 130 char *result = new char[128];
akhtar.syedzeeshan@gmail.com 1:7e5e76bf3efe 131 native_ptr->get_gyroscope_axes_json(result);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 132
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 133 // Cast it back to JavaScript
akhtar.syedzeeshan@gmail.com 1:7e5e76bf3efe 134 jerry_value_t out = jerry_create_string((unsigned char *)result);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 135
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 136 // Recycle the result from function
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 137 delete result;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 138
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 139 // Return the output
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 140 return out;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 141 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 142
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 143 /**
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 144 * LSM6DSL_JS (native JavaScript constructor)
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 145 *
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 146 * @returns a JavaScript object representing LSM6DSL_JS.
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 147 */
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 148 DECLARE_CLASS_CONSTRUCTOR(LSM6DSL_JS) {
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 149 CHECK_ARGUMENT_COUNT(LSM6DSL_JS, __constructor, args_count == 1);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 150 CHECK_ARGUMENT_TYPE_ALWAYS(LSM6DSL_JS, __constructor, 0, object);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 151
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 152 // Unwrap native LSM6DSL_JS object
akhtar.syedzeeshan@gmail.com 4:0eb0cded8861 153 void *devI2c_ptr;
akhtar.syedzeeshan@gmail.com 4:0eb0cded8861 154 const jerry_object_native_info_t *devI2c_type_ptr;
akhtar.syedzeeshan@gmail.com 4:0eb0cded8861 155 bool devI2c_has_ptr = jerry_get_object_native_pointer(args[0], &devI2c_ptr, &devI2c_type_ptr);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 156
akhtar.syedzeeshan@gmail.com 4:0eb0cded8861 157 // Check if we have the devI2c pointer
akhtar.syedzeeshan@gmail.com 4:0eb0cded8861 158 if (!devI2c_has_ptr) {
akhtar.syedzeeshan@gmail.com 4:0eb0cded8861 159 printf("Not a DevI2C input!");
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 160 return jerry_create_error(JERRY_ERROR_TYPE,
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 161 (const jerry_char_t *) "Failed to get native DigitalOut pointer");
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 162 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 163
akhtar.syedzeeshan@gmail.com 1:7e5e76bf3efe 164 // Cast the argument to C++
akhtar.syedzeeshan@gmail.com 4:0eb0cded8861 165 DevI2C* devI2c = reinterpret_cast<DevI2C*>(devI2c_ptr);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 166
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 167 // Extract native LSM6DSL_JS pointer (from this object)
akhtar.syedzeeshan@gmail.com 4:0eb0cded8861 168 LSM6DSL_JS *native_ptr = new LSM6DSL_JS(*devI2c);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 169
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 170 jerry_value_t js_object = jerry_create_object();
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 171 jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 172
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 173 // attach methods
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 174 ATTACH_CLASS_FUNCTION(js_object, LSM6DSL_JS, get_accelerometer_axes);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 175 ATTACH_CLASS_FUNCTION(js_object, LSM6DSL_JS, get_gyroscope_axes);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 176
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 177 return js_object;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 178 }