Javascript wrappers for LPS22HB Sensor library
Dependencies: LPS22HB
LPS22HB_JS-js.cpp@5:e92dbbec30d7, 2017-10-31 (annotated)
- Committer:
- akhtar.syedzeeshan@gmail.com
- Date:
- Tue Oct 31 16:20:09 2017 +0100
- Revision:
- 5:e92dbbec30d7
- Parent:
- 4:c2cf5deba40f
- Child:
- 6:a480866ede7e
Implemented SPI support
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 1 | /** |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 2 | ****************************************************************************** |
akhtar.syedzeeshan@gmail.com | 2:5ed6416b6b4d | 3 | * @file LPS22HB_JS-js.cpp |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 4 | * @author ST |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 5 | * @version V1.0.0 |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 6 | * @date 9 October 2017 |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 7 | * @brief Implementation of an LPS22HB Pressure and Temperature sensor for use |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 8 | * with Javascript. |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 9 | ****************************************************************************** |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 10 | * @attention |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 11 | * |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 12 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 13 | * |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 14 | * Redistribution and use in source and binary forms, with or without modification, |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 15 | * are permitted provided that the following conditions are met: |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 17 | * this list of conditions and the following disclaimer. |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 19 | * this list of conditions and the following disclaimer in the documentation |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 20 | * and/or other materials provided with the distribution. |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 22 | * may be used to endorse or promote products derived from this software |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 23 | * without specific prior written permission. |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 24 | * |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 35 | * |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 36 | ****************************************************************************** |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 37 | */ |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 38 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 39 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 40 | /* Includes ------------------------------------------------------------------*/ |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 41 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 42 | #include "jerryscript-mbed-util/logging.h" |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 43 | #include "jerryscript-mbed-library-registry/wrap_tools.h" |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 44 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 45 | // Load the library that we'll wrap |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 46 | #include "LPS22HB_JS.h" |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 47 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 48 | #include "mbed.h" |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 49 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 50 | #ifdef TARGET_SENSOR_TILE |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 51 | mbed::Serial pc2((PinName)0x2C, (PinName)0x32); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 52 | #define printf(...) pc2.printf(__VA_ARGS__) |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 53 | #endif |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 54 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 55 | /* Class Implementation ------------------------------------------------------*/ |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 56 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 57 | /** |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 58 | * LPS22HB_JS#destructor |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 59 | * Called if/when the LPS22HB_JS is GC'ed. |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 60 | */ |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 61 | void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(LPS22HB_JS)(void *void_ptr) { |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 62 | delete static_cast<LPS22HB_JS*>(void_ptr); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 63 | } |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 64 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 65 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 66 | /** |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 67 | * Type infomation of the native LPS22HB_JS pointer |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 68 | * Sets LPS22HB_JS#destructor as the free callback. |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 69 | */ |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 70 | static const jerry_object_native_info_t native_obj_type_info = { |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 71 | .free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR(LPS22HB_JS) |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 72 | }; |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 73 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 74 | /** |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 75 | * LPS22HB_JS#init_spi (native JavaScript method) |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 76 | * @brief Initializes the sensor using SPI interface |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 77 | * @param SPI object of helper class which handles the SPI peripheral |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 78 | * @param CS pin |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 79 | * @param INT pin |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 80 | * @param SPI type |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 81 | */ |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 82 | DECLARE_CLASS_FUNCTION(LPS22HB_JS, init_spi) { |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 83 | CHECK_ARGUMENT_COUNT(LPS22HB_JS, init_spi, (args_count == 2 || args_count == 4)); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 84 | CHECK_ARGUMENT_TYPE_ALWAYS(LPS22HB_JS, init_spi, 0, object); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 85 | CHECK_ARGUMENT_TYPE_ALWAYS(LPS22HB_JS, init_spi, 1, number); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 86 | CHECK_ARGUMENT_TYPE_ON_CONDITION(LPS22HB_JS, init_spi, 2, number, args_count == 4); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 87 | CHECK_ARGUMENT_TYPE_ON_CONDITION(LPS22HB_JS, init_spi, 3, number, args_count == 4); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 88 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 89 | // Unwrap native LPS22HB_JS object |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 90 | void *void_ptr; |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 91 | const jerry_object_native_info_t *type_ptr; |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 92 | bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 93 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 94 | if (!has_ptr || type_ptr != &native_obj_type_info) { |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 95 | return jerry_create_error(JERRY_ERROR_TYPE, |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 96 | (const jerry_char_t *) "Failed to get native LPS22HB_JS pointer"); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 97 | } |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 98 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 99 | LPS22HB_JS *native_ptr = static_cast<LPS22HB_JS*>(void_ptr); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 100 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 101 | // Unwrap arguments |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 102 | void *spi_ptr; |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 103 | const jerry_object_native_info_t *spi_type_ptr; |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 104 | bool spi_has_ptr = jerry_get_object_native_pointer(args[0], &spi_ptr, &spi_type_ptr); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 105 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 106 | // Check if we have the spi pointer |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 107 | if (!spi_has_ptr) { |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 108 | printf("Not a SPI input!"); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 109 | return jerry_create_error(JERRY_ERROR_TYPE, |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 110 | (const jerry_char_t *) "Failed to get native DigitalOut pointer"); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 111 | } |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 112 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 113 | // Cast the argument to C++ |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 114 | SPI* spi = reinterpret_cast<SPI*>(spi_ptr); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 115 | int cs_pin = jerry_get_number_value(args[1]); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 116 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 117 | // Call the native function |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 118 | if(args_count == 2){ |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 119 | native_ptr->init(*spi, (PinName)cs_pin); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 120 | } |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 121 | else if(args_count == 4){ |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 122 | int int_pin = jerry_get_number_value(args[2]); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 123 | int spi_type = jerry_get_number_value(args[3]); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 124 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 125 | /* To read string |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 126 | jerry_size_t req_sz = jerry_get_string_size(args[3]); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 127 | jerry_char_t spi_type[req_sz]; |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 128 | jerry_string_to_char_buffer(args[3], spi_type, req_sz); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 129 | */ |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 130 | native_ptr->init(*spi, (PinName)cs_pin, (PinName)int_pin, spi_type); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 131 | } |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 132 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 133 | return jerry_create_number(0); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 134 | } |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 135 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 136 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 137 | /** |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 138 | * LPS22HB_JS#init_i2c (native JavaScript method) |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 139 | * @brief Initializes the sensor using I2C interface |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 140 | * @param DevI2c object of helper class which handles the DevI2C peripheral |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 141 | * @param Address |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 142 | * @param INT pin |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 143 | */ |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 144 | DECLARE_CLASS_FUNCTION(LPS22HB_JS, init_i2c) { |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 145 | CHECK_ARGUMENT_COUNT(LPS22HB_JS, init_i2c, (args_count == 1 || args_count == 3)); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 146 | CHECK_ARGUMENT_TYPE_ALWAYS(LPS22HB_JS, init_i2c, 0, object); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 147 | CHECK_ARGUMENT_TYPE_ON_CONDITION(LPS22HB_JS, init_i2c, 1, number, args_count == 3); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 148 | CHECK_ARGUMENT_TYPE_ON_CONDITION(LPS22HB_JS, init_i2c, 2, number, args_count == 3); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 149 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 150 | // Unwrap native LPS22HB_JS object |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 151 | void *void_ptr; |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 152 | const jerry_object_native_info_t *type_ptr; |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 153 | bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 154 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 155 | if (!has_ptr || type_ptr != &native_obj_type_info) { |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 156 | return jerry_create_error(JERRY_ERROR_TYPE, |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 157 | (const jerry_char_t *) "Failed to get native LPS22HB_JS pointer"); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 158 | } |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 159 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 160 | LPS22HB_JS *native_ptr = static_cast<LPS22HB_JS*>(void_ptr); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 161 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 162 | // Unwrap arguments |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 163 | void *i2c_ptr; |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 164 | const jerry_object_native_info_t *i2c_type_ptr; |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 165 | bool i2c_has_ptr = jerry_get_object_native_pointer(args[0], &i2c_ptr, &i2c_type_ptr); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 166 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 167 | // Check if we have the i2c pointer |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 168 | if (!i2c_has_ptr) { |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 169 | printf("Not a I2C input!"); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 170 | return jerry_create_error(JERRY_ERROR_TYPE, |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 171 | (const jerry_char_t *) "Failed to get native DigitalOut pointer"); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 172 | } |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 173 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 174 | // Cast the argument to C++ |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 175 | DevI2C* i2c = reinterpret_cast<DevI2C*>(i2c_ptr); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 176 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 177 | // Call the native function |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 178 | if(args_count == 1){ |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 179 | native_ptr->init(*i2c); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 180 | } |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 181 | else if(args_count == 3){ |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 182 | int address = jerry_get_number_value(args[1]); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 183 | int int_pin = jerry_get_number_value(args[2]); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 184 | native_ptr->init(*i2c, (uint8_t) address, (PinName) int_pin); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 185 | } |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 186 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 187 | return jerry_create_number(0); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 188 | } |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 189 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 190 | /** |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 191 | * LPS22HB_JS#get_temperature (native JavaScript method) |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 192 | * @brief Gets the temperature reading |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 193 | * @returns Temperature |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 194 | */ |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 195 | DECLARE_CLASS_FUNCTION(LPS22HB_JS, get_temperature) { |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 196 | CHECK_ARGUMENT_COUNT(LPS22HB_JS, get_temperature, (args_count == 0)); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 197 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 198 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 199 | // Unwrap native LPS22HB_JS object |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 200 | void *void_ptr; |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 201 | const jerry_object_native_info_t *type_ptr; |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 202 | bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 203 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 204 | if (!has_ptr || type_ptr != &native_obj_type_info) { |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 205 | return jerry_create_error(JERRY_ERROR_TYPE, |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 206 | (const jerry_char_t *) "Failed to get native LPS22HB_JS pointer"); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 207 | } |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 208 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 209 | LPS22HB_JS *native_ptr = static_cast<LPS22HB_JS*>(void_ptr); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 210 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 211 | // Get the result from the C++ API |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 212 | //float result = native_ptr->get_temperature(); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 213 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 214 | char * result = new char[128]; |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 215 | result = native_ptr->get_temperature_string(result); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 216 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 217 | //pc.printf("Temperature: %s", result); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 218 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 219 | // Cast it back to JavaScript |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 220 | jerry_value_t out = jerry_create_string((unsigned char *)result); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 221 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 222 | printf("temp: %s\n", result); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 223 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 224 | // Recycle the result from function |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 225 | delete result; |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 226 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 227 | // Return the output |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 228 | return out; |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 229 | } |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 230 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 231 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 232 | /** |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 233 | * LPS22HB_JS#get_temperature_string (native JavaScript method) |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 234 | * @brief Gets temperature reading in string form |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 235 | * @returns Temperature in string |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 236 | */ |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 237 | DECLARE_CLASS_FUNCTION(LPS22HB_JS, get_temperature_string) { |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 238 | CHECK_ARGUMENT_COUNT(LPS22HB_JS, get_temperature_string, (args_count == 0)); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 239 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 240 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 241 | // Unwrap native LPS22HB_JS object |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 242 | void *void_ptr; |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 243 | const jerry_object_native_info_t *type_ptr; |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 244 | bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 245 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 246 | if (!has_ptr || type_ptr != &native_obj_type_info) { |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 247 | return jerry_create_error(JERRY_ERROR_TYPE, |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 248 | (const jerry_char_t *) "Failed to get native LPS22HB_JS pointer"); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 249 | } |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 250 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 251 | LPS22HB_JS *native_ptr = static_cast<LPS22HB_JS*>(void_ptr); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 252 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 253 | // Get the result from the C++ API |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 254 | char * result = new char[128]; |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 255 | result = native_ptr->get_temperature_string(result); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 256 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 257 | //pc.printf("Temperature: %s", result); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 258 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 259 | // Cast it back to JavaScript |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 260 | jerry_value_t out = jerry_create_string((unsigned char *)result); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 261 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 262 | printf("temp: %s\n", result); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 263 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 264 | // Recycle the result from function |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 265 | delete result; |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 266 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 267 | // Return the output |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 268 | return out; |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 269 | } |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 270 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 271 | /** |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 272 | * LPS22HB_JS#get_pressure (native JavaScript method) |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 273 | * @brief Gets the pressure reading |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 274 | * @returns Pressure |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 275 | */ |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 276 | DECLARE_CLASS_FUNCTION(LPS22HB_JS, get_pressure) { |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 277 | CHECK_ARGUMENT_COUNT(LPS22HB_JS, get_pressure, (args_count == 0)); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 278 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 279 | // Unwrap native LPS22HB_JS object |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 280 | void *void_ptr; |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 281 | const jerry_object_native_info_t *type_ptr; |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 282 | bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 283 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 284 | if (!has_ptr || type_ptr != &native_obj_type_info) { |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 285 | return jerry_create_error(JERRY_ERROR_TYPE, |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 286 | (const jerry_char_t *) "Failed to get native LPS22HB_JS pointer"); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 287 | } |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 288 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 289 | LPS22HB_JS *native_ptr = static_cast<LPS22HB_JS*>(void_ptr); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 290 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 291 | // Get the result from the C++ API |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 292 | float result = native_ptr->get_pressure(); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 293 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 294 | printf("pressure: %f\n", result); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 295 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 296 | // Cast it back to JavaScript and return |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 297 | return jerry_create_number(result); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 298 | } |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 299 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 300 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 301 | /** |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 302 | * LPS22HB_JS#get_pressure_string (native JavaScript method) |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 303 | * @brief Gets the pressure reading in string form |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 304 | * @returns Pressure |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 305 | */ |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 306 | DECLARE_CLASS_FUNCTION(LPS22HB_JS, get_pressure_string) { |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 307 | CHECK_ARGUMENT_COUNT(LPS22HB_JS, get_pressure_string, (args_count == 0)); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 308 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 309 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 310 | // Unwrap native LPS22HB_JS object |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 311 | void *void_ptr; |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 312 | const jerry_object_native_info_t *type_ptr; |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 313 | bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 314 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 315 | if (!has_ptr || type_ptr != &native_obj_type_info) { |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 316 | return jerry_create_error(JERRY_ERROR_TYPE, |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 317 | (const jerry_char_t *) "Failed to get native LPS22HB_JS pointer"); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 318 | } |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 319 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 320 | LPS22HB_JS *native_ptr = static_cast<LPS22HB_JS*>(void_ptr); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 321 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 322 | char * result = new char[128]; |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 323 | result = native_ptr->get_pressure_string(result); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 324 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 325 | // Cast it back to JavaScript |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 326 | jerry_value_t out = jerry_create_string((unsigned char *)result); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 327 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 328 | printf("pressure: %s\n", result); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 329 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 330 | // Recycle the result from function |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 331 | delete result; |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 332 | |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 333 | // Return the output |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 334 | return out; |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 335 | } |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 336 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 337 | /** |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 338 | * LPS22HB_JS (native JavaScript constructor) |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 339 | * @brief Constructor for Javascript wrapper |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 340 | * @returns a JavaScript object representing LPS22HB_JS. |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 341 | */ |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 342 | DECLARE_CLASS_CONSTRUCTOR(LPS22HB_JS) { |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 343 | CHECK_ARGUMENT_COUNT(LPS22HB_JS, __constructor, args_count == 0); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 344 | //pc.printf("constructor called!\n"); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 345 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 346 | // Extract native LPS22HB_JS pointer (from this object) |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 347 | LPS22HB_JS *native_ptr = new LPS22HB_JS(); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 348 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 349 | jerry_value_t js_object = jerry_create_object(); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 350 | jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 351 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 352 | // attach methods |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 353 | ATTACH_CLASS_FUNCTION(js_object, LPS22HB_JS, init_spi); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 354 | ATTACH_CLASS_FUNCTION(js_object, LPS22HB_JS, init_i2c); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 355 | ATTACH_CLASS_FUNCTION(js_object, LPS22HB_JS, get_temperature); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 356 | ATTACH_CLASS_FUNCTION(js_object, LPS22HB_JS, get_temperature_string); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 357 | ATTACH_CLASS_FUNCTION(js_object, LPS22HB_JS, get_pressure); |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 358 | ATTACH_CLASS_FUNCTION(js_object, LPS22HB_JS, get_pressure_string); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 359 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 360 | return js_object; |
akhtar.syedzeeshan@gmail.com | 5:e92dbbec30d7 | 361 | } |