ST Expansion SW Team / LPS22HB_JS

Dependencies:   LPS22HB

Dependents:   ST_SENSOR_JS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LPS22HB_JS-js.cpp Source File

LPS22HB_JS-js.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    LPS22HB_JS-js.cpp
00004  * @author  ST
00005  * @version V1.0.0
00006  * @date    9 October 2017
00007  * @brief   Implementation of an LPS22HB Pressure and Temperature sensor for use
00008  *          with Javascript.
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; 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 "LPS22HB_JS.h"
00047 
00048 #include "mbed.h"
00049 
00050 #ifdef TARGET_SENSOR_TILE
00051 mbed::Serial pc2((PinName)0x2C, (PinName)0x32);
00052 #define printf(...) pc2.printf(__VA_ARGS__)
00053 #endif
00054 
00055 /* Class Implementation ------------------------------------------------------*/
00056 
00057 /**
00058  * LPS22HB_JS#destructor
00059  * Called if/when the LPS22HB_JS is GC'ed.
00060  */
00061 void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(LPS22HB_JS)(void *void_ptr) {
00062     delete static_cast<LPS22HB_JS*>(void_ptr);
00063 }
00064 
00065  
00066 /**
00067  * Type infomation of the native LPS22HB_JS pointer
00068  * Sets LPS22HB_JS#destructor as the free callback.
00069  */
00070 static const jerry_object_native_info_t native_obj_type_info = {
00071     .free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR(LPS22HB_JS)
00072 };
00073 
00074 /**
00075  * LPS22HB_JS#init_spi (native JavaScript method)
00076  * @brief Initializes the sensor using SPI interface
00077  * @param SPI object of helper class which handles the SPI peripheral
00078  * @param CS pin
00079  * @param INT pin
00080  * @param SPI type
00081  */
00082 DECLARE_CLASS_FUNCTION(LPS22HB_JS, init_spi) {
00083     CHECK_ARGUMENT_COUNT(LPS22HB_JS, init_spi, (args_count == 2 || args_count == 4));
00084     CHECK_ARGUMENT_TYPE_ALWAYS(LPS22HB_JS, init_spi, 0, object);
00085     CHECK_ARGUMENT_TYPE_ALWAYS(LPS22HB_JS, init_spi, 1, number);
00086     CHECK_ARGUMENT_TYPE_ON_CONDITION(LPS22HB_JS, init_spi, 2, number, args_count == 4);
00087     CHECK_ARGUMENT_TYPE_ON_CONDITION(LPS22HB_JS, init_spi, 3, number, args_count == 4);
00088 
00089     // Unwrap native LPS22HB_JS object
00090     void *void_ptr;
00091     const jerry_object_native_info_t *type_ptr;
00092     bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr);
00093 
00094     if (!has_ptr || type_ptr != &native_obj_type_info) {
00095         return jerry_create_error(JERRY_ERROR_TYPE,
00096                                   (const jerry_char_t *) "Failed to get native LPS22HB_JS pointer");
00097     }
00098 
00099     LPS22HB_JS *native_ptr = static_cast<LPS22HB_JS*>(void_ptr);
00100  
00101     // Unwrap arguments
00102     void *spi_ptr;
00103     const jerry_object_native_info_t *spi_type_ptr;
00104     bool spi_has_ptr = jerry_get_object_native_pointer(args[0], &spi_ptr, &spi_type_ptr);
00105 
00106     // Check if we have the spi pointer
00107     if (!spi_has_ptr) {
00108         printf("Not a SPI input!");
00109         return jerry_create_error(JERRY_ERROR_TYPE,
00110                                   (const jerry_char_t *) "Failed to get native DigitalOut pointer");
00111     }
00112 
00113     // Cast the argument to C++
00114     SPI* spi = reinterpret_cast<SPI*>(spi_ptr);
00115     int cs_pin = jerry_get_number_value(args[1]);
00116     
00117     // Call the native function
00118     if(args_count == 2){
00119         native_ptr->init(*spi, (PinName)cs_pin);
00120     }
00121     else if(args_count == 4){
00122         int int_pin = jerry_get_number_value(args[2]);
00123         int spi_type = jerry_get_number_value(args[3]);
00124         
00125         /* To read string
00126         jerry_size_t req_sz = jerry_get_string_size(args[3]);
00127         jerry_char_t spi_type[req_sz];
00128         jerry_string_to_char_buffer(args[3], spi_type, req_sz);
00129         */
00130         native_ptr->init(*spi, (PinName)cs_pin, (PinName)int_pin, spi_type);
00131     }
00132 
00133     return jerry_create_number(0);
00134 }
00135 
00136 
00137 /**
00138  * LPS22HB_JS#init_i2c (native JavaScript method)
00139  * @brief   Initializes the sensor using I2C interface
00140  * @param   DevI2c object of helper class which handles the DevI2C peripheral
00141  * @param   Address
00142  * @param   INT pin
00143  */
00144 DECLARE_CLASS_FUNCTION(LPS22HB_JS, init_i2c) {
00145     CHECK_ARGUMENT_COUNT(LPS22HB_JS, init_i2c, (args_count == 1 || args_count == 3));
00146     CHECK_ARGUMENT_TYPE_ALWAYS(LPS22HB_JS, init_i2c, 0, object);
00147     CHECK_ARGUMENT_TYPE_ON_CONDITION(LPS22HB_JS, init_i2c, 1, number, args_count == 3);
00148     CHECK_ARGUMENT_TYPE_ON_CONDITION(LPS22HB_JS, init_i2c, 2, number, args_count == 3);
00149     
00150     // Unwrap native LPS22HB_JS object
00151     void *void_ptr;
00152     const jerry_object_native_info_t *type_ptr;
00153     bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr);
00154 
00155     if (!has_ptr || type_ptr != &native_obj_type_info) {
00156         return jerry_create_error(JERRY_ERROR_TYPE,
00157                                   (const jerry_char_t *) "Failed to get native LPS22HB_JS pointer");
00158     }
00159 
00160     LPS22HB_JS *native_ptr = static_cast<LPS22HB_JS*>(void_ptr);
00161  
00162     // Unwrap arguments
00163     void *i2c_ptr;
00164     const jerry_object_native_info_t *i2c_type_ptr;
00165     bool i2c_has_ptr = jerry_get_object_native_pointer(args[0], &i2c_ptr, &i2c_type_ptr);
00166 
00167     // Check if we have the i2c pointer
00168     if (!i2c_has_ptr) {
00169         printf("Not a I2C input!");
00170         return jerry_create_error(JERRY_ERROR_TYPE,
00171                                   (const jerry_char_t *) "Failed to get native DigitalOut pointer");
00172     }
00173 
00174     // Cast the argument to C++
00175     DevI2C* i2c = reinterpret_cast<DevI2C*>(i2c_ptr);
00176     
00177     // Call the native function
00178     if(args_count == 1){
00179         native_ptr->init(*i2c);
00180     }
00181     else if(args_count == 3){
00182         int address = jerry_get_number_value(args[1]);
00183         int int_pin = jerry_get_number_value(args[2]);
00184         native_ptr->init(*i2c, (uint8_t) address, (PinName) int_pin);
00185     }
00186     
00187     return jerry_create_number(0);
00188 }
00189 
00190 /**
00191  * LPS22HB_JS#get_temperature (native JavaScript method)
00192  * @brief   Gets the temperature reading
00193  * @returns Temperature
00194  */
00195 DECLARE_CLASS_FUNCTION(LPS22HB_JS, get_temperature) {
00196     CHECK_ARGUMENT_COUNT(LPS22HB_JS, get_temperature, (args_count == 0));
00197  
00198     
00199     // Unwrap native LPS22HB_JS object
00200     void *void_ptr;
00201     const jerry_object_native_info_t *type_ptr;
00202     bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr);
00203 
00204     if (!has_ptr || type_ptr != &native_obj_type_info) {
00205         return jerry_create_error(JERRY_ERROR_TYPE,
00206                                   (const jerry_char_t *) "Failed to get native LPS22HB_JS pointer");
00207     }
00208 
00209     LPS22HB_JS *native_ptr = static_cast<LPS22HB_JS*>(void_ptr);
00210  
00211     // Get the result from the C++ API
00212     //float result = native_ptr->get_temperature();
00213     
00214     char * result = new char[128];
00215     result = native_ptr->get_temperature_string(result);
00216     
00217     //pc.printf("Temperature: %s", result);
00218 
00219     // Cast it back to JavaScript
00220     jerry_value_t out = jerry_create_string((unsigned char *)result);
00221     
00222     //printf("temp: %s\n", result);
00223    
00224     // Recycle the result from function
00225     delete result;
00226 
00227     // Return the output
00228     return out;
00229 }
00230 
00231 
00232 /**
00233  * LPS22HB_JS#get_temperature_string (native JavaScript method)
00234  * @brief   Gets temperature reading in string form
00235  * @returns Temperature in string
00236  */
00237 DECLARE_CLASS_FUNCTION(LPS22HB_JS, get_temperature_string) {
00238     CHECK_ARGUMENT_COUNT(LPS22HB_JS, get_temperature_string, (args_count == 0));
00239  
00240     
00241     // Unwrap native LPS22HB_JS object
00242     void *void_ptr;
00243     const jerry_object_native_info_t *type_ptr;
00244     bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr);
00245 
00246     if (!has_ptr || type_ptr != &native_obj_type_info) {
00247         return jerry_create_error(JERRY_ERROR_TYPE,
00248                                   (const jerry_char_t *) "Failed to get native LPS22HB_JS pointer");
00249     }
00250 
00251     LPS22HB_JS *native_ptr = static_cast<LPS22HB_JS*>(void_ptr);
00252  
00253     // Get the result from the C++ API
00254     char * result = new char[128];
00255     result = native_ptr->get_temperature_string(result);
00256     
00257     //pc.printf("Temperature: %s", result);
00258 
00259     // Cast it back to JavaScript
00260     jerry_value_t out = jerry_create_string((unsigned char *)result);
00261     
00262     //printf("temp: %s\n", result);
00263    
00264     // Recycle the result from function
00265     delete result;
00266 
00267     // Return the output
00268     return out;
00269 }
00270 
00271 /**
00272  * LPS22HB_JS#get_pressure (native JavaScript method)
00273  * @brief   Gets the pressure reading
00274  * @returns Pressure
00275  */
00276 DECLARE_CLASS_FUNCTION(LPS22HB_JS, get_pressure) {
00277     CHECK_ARGUMENT_COUNT(LPS22HB_JS, get_pressure, (args_count == 0));
00278  
00279     // Unwrap native LPS22HB_JS object
00280     void *void_ptr;
00281     const jerry_object_native_info_t *type_ptr;
00282     bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr);
00283 
00284     if (!has_ptr || type_ptr != &native_obj_type_info) {
00285         return jerry_create_error(JERRY_ERROR_TYPE,
00286                                   (const jerry_char_t *) "Failed to get native LPS22HB_JS pointer");
00287     }
00288 
00289     LPS22HB_JS *native_ptr = static_cast<LPS22HB_JS*>(void_ptr);
00290  
00291     // Get the result from the C++ API
00292     float result = native_ptr->get_pressure();
00293     
00294     //printf("pressure: %f\n", result);
00295    
00296     // Cast it back to JavaScript and return
00297     return jerry_create_number(result);
00298 }
00299 
00300 
00301 /**
00302  * LPS22HB_JS#get_pressure_string (native JavaScript method)
00303  * @brief   Gets the pressure reading in string form
00304  * @returns Pressure
00305  */
00306 DECLARE_CLASS_FUNCTION(LPS22HB_JS, get_pressure_string) {
00307     CHECK_ARGUMENT_COUNT(LPS22HB_JS, get_pressure_string, (args_count == 0));
00308  
00309     
00310     // Unwrap native LPS22HB_JS object
00311     void *void_ptr;
00312     const jerry_object_native_info_t *type_ptr;
00313     bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr);
00314 
00315     if (!has_ptr || type_ptr != &native_obj_type_info) {
00316         return jerry_create_error(JERRY_ERROR_TYPE,
00317                                   (const jerry_char_t *) "Failed to get native LPS22HB_JS pointer");
00318     }
00319 
00320     LPS22HB_JS *native_ptr = static_cast<LPS22HB_JS*>(void_ptr);
00321  
00322     char * result = new char[128];
00323     result = native_ptr->get_pressure_string(result);
00324     
00325     // Cast it back to JavaScript
00326     jerry_value_t out = jerry_create_string((unsigned char *)result);
00327     
00328     //printf("pressure: %s\n", result);
00329    
00330     // Recycle the result from function
00331     delete result;
00332 
00333     // Return the output
00334     return out;
00335 }
00336 
00337 /**
00338  * LPS22HB_JS (native JavaScript constructor)
00339  * @brief   Constructor for Javascript wrapper
00340  * @returns a JavaScript object representing LPS22HB_JS.
00341  */
00342 DECLARE_CLASS_CONSTRUCTOR(LPS22HB_JS) {
00343     CHECK_ARGUMENT_COUNT(LPS22HB_JS, __constructor, args_count == 0);
00344     //pc.printf("constructor called!\n");
00345     
00346     // Extract native LPS22HB_JS pointer (from this object) 
00347     LPS22HB_JS *native_ptr = new LPS22HB_JS();
00348 
00349     jerry_value_t js_object = jerry_create_object();
00350     jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info);
00351 
00352     // attach methods
00353     ATTACH_CLASS_FUNCTION(js_object, LPS22HB_JS, init_spi);
00354     ATTACH_CLASS_FUNCTION(js_object, LPS22HB_JS, init_i2c);
00355     ATTACH_CLASS_FUNCTION(js_object, LPS22HB_JS, get_temperature);
00356     ATTACH_CLASS_FUNCTION(js_object, LPS22HB_JS, get_temperature_string);
00357     ATTACH_CLASS_FUNCTION(js_object, LPS22HB_JS, get_pressure);
00358     ATTACH_CLASS_FUNCTION(js_object, LPS22HB_JS, get_pressure_string);
00359     
00360     return js_object;
00361 }