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