Javascript wrappers for LPS22HB Sensor library
Dependencies: LPS22HB
LPS22HB_JS-js.cpp@2:5ed6416b6b4d, 2017-10-12 (annotated)
- Committer:
- akhtar.syedzeeshan@gmail.com
- Date:
- Thu Oct 12 11:11:59 2017 +0200
- Revision:
- 2:5ed6416b6b4d
- Parent:
- 1:6b7b798e36ae
- Child:
- 3:3e19d8808491
Edited the info in source code
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 | 0:c256bbccd0d4 | 50 | /* Class Implementation ------------------------------------------------------*/ |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 51 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 52 | /** |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 53 | * LPS22HB_JS#destructor |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 54 | * |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 55 | * Called if/when the LPS22HB_JS is GC'ed. |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 56 | */ |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 57 | void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(LPS22HB_JS)(void *void_ptr) { |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 58 | delete static_cast<LPS22HB_JS*>(void_ptr); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 59 | } |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 60 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 61 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 62 | /** |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 63 | * Type infomation of the native LPS22HB_JS pointer |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 64 | * |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 65 | * Set LPS22HB_JS#destructor as the free callback. |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 66 | */ |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 67 | static const jerry_object_native_info_t native_obj_type_info = { |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 68 | .free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR(LPS22HB_JS) |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 69 | }; |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 70 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 71 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 72 | /** |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 73 | * LPS22HB_JS#get_temperature (native JavaScript method) |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 74 | * |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 75 | * @returns temperature |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 76 | */ |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 77 | DECLARE_CLASS_FUNCTION(LPS22HB_JS, get_temperature) { |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 78 | CHECK_ARGUMENT_COUNT(LPS22HB_JS, get_temperature, (args_count == 0)); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 79 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 80 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 81 | // Unwrap native LPS22HB_JS object |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 82 | void *void_ptr; |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 83 | const jerry_object_native_info_t *type_ptr; |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 84 | bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 85 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 86 | if (!has_ptr || type_ptr != &native_obj_type_info) { |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 87 | return jerry_create_error(JERRY_ERROR_TYPE, |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 88 | (const jerry_char_t *) "Failed to get native LPS22HB_JS pointer"); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 89 | } |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 90 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 91 | LPS22HB_JS *native_ptr = static_cast<LPS22HB_JS*>(void_ptr); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 92 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 93 | // Get the result from the C++ API |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 94 | float result = native_ptr->get_temperature(); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 95 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 96 | // Cast it back to JavaScript and return |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 97 | return jerry_create_number(result); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 98 | } |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 99 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 100 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 101 | /** |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 102 | * LPS22HB_JS#get_pressure (native JavaScript method) |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 103 | * |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 104 | * @returns pressure |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 105 | */ |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 106 | DECLARE_CLASS_FUNCTION(LPS22HB_JS, get_pressure) { |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 107 | CHECK_ARGUMENT_COUNT(LPS22HB_JS, get_pressure, (args_count == 0)); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 108 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 109 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 110 | // Unwrap native LPS22HB_JS object |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 111 | void *void_ptr; |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 112 | const jerry_object_native_info_t *type_ptr; |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 113 | bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 114 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 115 | if (!has_ptr || type_ptr != &native_obj_type_info) { |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 116 | return jerry_create_error(JERRY_ERROR_TYPE, |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 117 | (const jerry_char_t *) "Failed to get native LPS22HB_JS pointer"); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 118 | } |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 119 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 120 | LPS22HB_JS *native_ptr = static_cast<LPS22HB_JS*>(void_ptr); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 121 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 122 | // Get the result from the C++ API |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 123 | float result = native_ptr->get_pressure(); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 124 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 125 | // Cast it back to JavaScript and return |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 126 | return jerry_create_number(result); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 127 | } |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 128 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 129 | /** |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 130 | * LPS22HB_JS (native JavaScript constructor) |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 131 | * |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 132 | * @returns a JavaScript object representing LPS22HB_JS. |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 133 | */ |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 134 | DECLARE_CLASS_CONSTRUCTOR(LPS22HB_JS) { |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 135 | CHECK_ARGUMENT_COUNT(LPS22HB_JS, __constructor, args_count == 1); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 136 | CHECK_ARGUMENT_TYPE_ALWAYS(LPS22HB_JS, __constructor, 0, object); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 137 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 138 | // Unwrap native LPS22HB_JS object |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 139 | void *devI2c_ptr; |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 140 | const jerry_object_native_info_t *devI2c_type_ptr; |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 141 | bool devI2c_has_ptr = jerry_get_object_native_pointer(args[0], &devI2c_ptr, &devI2c_type_ptr); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 142 | |
akhtar.syedzeeshan@gmail.com | 1:6b7b798e36ae | 143 | // Check if we have the i2c pointer |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 144 | if (!devI2c_has_ptr) { |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 145 | printf("Not a I2C input!"); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 146 | return jerry_create_error(JERRY_ERROR_TYPE, |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 147 | (const jerry_char_t *) "Failed to get native DigitalOut pointer"); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 148 | } |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 149 | |
akhtar.syedzeeshan@gmail.com | 1:6b7b798e36ae | 150 | // Cast the argument to C++ |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 151 | DevI2C* devI2c = reinterpret_cast<DevI2C*>(devI2c_ptr); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 152 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 153 | // Extract native LPS22HB_JS pointer (from this object) |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 154 | LPS22HB_JS *native_ptr = new LPS22HB_JS(*devI2c); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 155 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 156 | jerry_value_t js_object = jerry_create_object(); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 157 | jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 158 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 159 | // attach methods |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 160 | ATTACH_CLASS_FUNCTION(js_object, LPS22HB_JS, get_temperature); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 161 | ATTACH_CLASS_FUNCTION(js_object, LPS22HB_JS, get_pressure); |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 162 | |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 163 | return js_object; |
akhtar.syedzeeshan@gmail.com | 0:c256bbccd0d4 | 164 | } |