Javascript wrappers for LSM6DSL Sensor library

Dependencies:   LSM6DSL

Dependents:   ST_SENSOR_JS

LSM6DSL_JS-js.cpp

Committer:
akhtar.syedzeeshan@gmail.com
Date:
2017-10-23
Revision:
3:87ac9fb0d287
Parent:
2:ebd484d17a02
Child:
4:0eb0cded8861

File content as of revision 3:87ac9fb0d287:

/**
 ******************************************************************************
 * @file    LSM6DSL_JS-js.cpp
 * @author  ST
 * @version V1.0.0
 * @date    9 October 2017
 * @brief   Implementation of an LSM6DSL Accelerometer and gyroscope sensor for
 *          use with Javascript.
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *   1. Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright notice,
 *      this list of conditions and the following disclaimer in the documentation
 *      and/or other materials provided with the distribution.
 *   3. Neither the name of STMicroelectronics nor the names of its contributors
 *      may be used to endorse or promote products derived from this software
 *      without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ******************************************************************************
 */


/* Includes ------------------------------------------------------------------*/

#include "jerryscript-mbed-util/logging.h"
#include "jerryscript-mbed-library-registry/wrap_tools.h"

// Load the library that we'll wrap
#include "LSM6DSL_JS.h"

#include "mbed.h"

/* Class Implementation ------------------------------------------------------*/

/**
 * LSM6DSL_JS#destructor
 *
 * Called if/when the LSM6DSL_JS is GC'ed.
 */
void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(LSM6DSL_JS)(void *void_ptr) {
    delete static_cast<LSM6DSL_JS*>(void_ptr);
}


/**
 * Type infomation of the native LSM6DSL_JS pointer
 *
 * Set LSM6DSL_JS#destructor as the free callback.
 */
static const jerry_object_native_info_t native_obj_type_info = {
    .free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR(LSM6DSL_JS)
};


/**
 * LSM6DSL_JS#get_accelerometer_axes (native JavaScript method)
 *
 * @returns accelerometer axes
 */
DECLARE_CLASS_FUNCTION(LSM6DSL_JS, get_accelerometer_axes) {
    CHECK_ARGUMENT_COUNT(LSM6DSL_JS, get_accelerometer_axes, (args_count == 0));
 
    
    // Unwrap native LSM6DSL_JS object
    void *void_ptr;
    const jerry_object_native_info_t *type_ptr;
    bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr);

    if (!has_ptr || type_ptr != &native_obj_type_info) {
        return jerry_create_error(JERRY_ERROR_TYPE,
                                  (const jerry_char_t *) "Failed to get native LSM6DSL_JS pointer");
    }

    LSM6DSL_JS *native_ptr = static_cast<LSM6DSL_JS*>(void_ptr);
 
    // Get the result from the C++ API
    char *result = new char[128];
    native_ptr->get_accelerometer_axes_json(result);
    
    // Cast it back to JavaScript
    jerry_value_t out = jerry_create_string((unsigned char *)result);
    
    // Recycle the result from function
    delete result;

    // Return the output
    return out;
}


/**
 * LSM6DSL_JS#get_gyroscope_axes (native JavaScript method)
 *
 * @returns gyroscope axes
 */
DECLARE_CLASS_FUNCTION(LSM6DSL_JS, get_gyroscope_axes) {
    CHECK_ARGUMENT_COUNT(LSM6DSL_JS, get_gyroscope_axes, (args_count == 0));
 
    
    // Unwrap native LSM6DSL_JS object
    void *void_ptr;
    const jerry_object_native_info_t *type_ptr;
    bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr);

    if (!has_ptr || type_ptr != &native_obj_type_info) {
        return jerry_create_error(JERRY_ERROR_TYPE,
                                  (const jerry_char_t *) "Failed to get native LSM6DSL_JS pointer");
    }

    LSM6DSL_JS *native_ptr = static_cast<LSM6DSL_JS*>(void_ptr);
 
    // Get the result from the C++ API
    char *result = new char[128];
    native_ptr->get_gyroscope_axes_json(result);
    
    // Cast it back to JavaScript
    jerry_value_t out = jerry_create_string((unsigned char *)result);
    
    // Recycle the result from function
    delete result;

    // Return the output
    return out;
}

/**
 * LSM6DSL_JS (native JavaScript constructor)
 *
 * @returns a JavaScript object representing LSM6DSL_JS.
 */
DECLARE_CLASS_CONSTRUCTOR(LSM6DSL_JS) {
    CHECK_ARGUMENT_COUNT(LSM6DSL_JS, __constructor, args_count == 1);
    CHECK_ARGUMENT_TYPE_ALWAYS(LSM6DSL_JS, __constructor, 0, object);
    
    // Unwrap native LSM6DSL_JS object
    void *i2c_ptr;
    const jerry_object_native_info_t *i2c_type_ptr;
    bool i2c_has_ptr = jerry_get_object_native_pointer(args[0], &i2c_ptr, &i2c_type_ptr);

    // Check if we have the i2c pointer
    if (!i2c_has_ptr) {
        printf("Not a I2C input!");
        return jerry_create_error(JERRY_ERROR_TYPE,
                                  (const jerry_char_t *) "Failed to get native DigitalOut pointer");
    }

    // Cast the argument to C++
    I2C* i2c = reinterpret_cast<I2C*>(i2c_ptr);

    // Extract native LSM6DSL_JS pointer (from this object) 
    LSM6DSL_JS *native_ptr = new LSM6DSL_JS(*i2c);

    jerry_value_t js_object = jerry_create_object();
    jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info);

    // attach methods
    ATTACH_CLASS_FUNCTION(js_object, LSM6DSL_JS, get_accelerometer_axes);
    ATTACH_CLASS_FUNCTION(js_object, LSM6DSL_JS, get_gyroscope_axes);
    
    return js_object;
}