Javascript wrappers for HTS221 Sensor library

Dependencies:   HTS221

Dependents:   ST_SENSOR_JS

Files at this revision

API Documentation at this revision

Comitter:
akhtar.syedzeeshan@gmail.com
Date:
Tue Oct 10 11:51:26 2017 +0200
Child:
1:924fb53eb7f8
Commit message:
Initial Commit

Changed in this revision

HTS221.lib Show annotated file Show diff for this revision Revisions of this file
HTS221_JS-js.cpp Show annotated file Show diff for this revision Revisions of this file
HTS221_JS-js.h Show annotated file Show diff for this revision Revisions of this file
HTS221_JS.cpp Show annotated file Show diff for this revision Revisions of this file
HTS221_JS.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTS221.lib	Tue Oct 10 11:51:26 2017 +0200
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/ST/code/HTS221/#312ee2694a77
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTS221_JS-js.cpp	Tue Oct 10 11:51:26 2017 +0200
@@ -0,0 +1,209 @@
+/**
+ ******************************************************************************
+ * @file    HTS221_JS.cpp
+ * @author  ST
+ * @version V1.0.0
+ * @date    9 October 2017
+ * @brief   Implementation of an HTS221 Humidity and Temperature 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 "HTS221_JS.h"
+
+#include "mbed.h"
+
+/* Class Implementation ------------------------------------------------------*/
+
+/**
+ * HTS221_JS#destructor
+ *
+ * Called if/when the HTS221_JS is GC'ed.
+ */
+void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(HTS221_JS)(void *void_ptr) {
+    delete static_cast<HTS221_JS*>(void_ptr);
+}
+
+/**
+ * Type infomation of the native HTS221_JS pointer
+ *
+ * Set HTS221_JS#destructor as the free callback.
+ */
+static const jerry_object_native_info_t native_obj_type_info = {
+    .free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR(HTS221_JS)
+};
+
+
+/**
+ * HTS221_JS#get_temperature (native JavaScript method)
+ *
+ * @returns temperature
+ */
+DECLARE_CLASS_FUNCTION(HTS221_JS, get_temperature) {
+    CHECK_ARGUMENT_COUNT(HTS221_JS, get_temperature, (args_count == 0));
+ 
+    
+    // Unwrap native HTS221_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 HTS221_JS pointer");
+    }
+
+    HTS221_JS *native_ptr = static_cast<HTS221_JS*>(void_ptr);
+ 
+    // Get the result from the C++ API
+    float result = native_ptr->get_temperature();
+    
+    // Cast it back to JavaScript and return
+    return jerry_create_number(result);
+
+    //return jerry_create_string((unsigned char*)"5C");
+}
+
+
+/**
+ * HTS221_JS#get_humidity (native JavaScript method)
+ *
+ * @returns humidity
+ */
+DECLARE_CLASS_FUNCTION(HTS221_JS, get_humidity) {
+    CHECK_ARGUMENT_COUNT(HTS221_JS, get_humidity, (args_count == 0));
+ 
+    
+    // Unwrap native HTS221_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 HTS221_JS pointer");
+    }
+
+    HTS221_JS *native_ptr = static_cast<HTS221_JS*>(void_ptr);
+ 
+    // Get the result from the C++ API
+    float result = native_ptr->get_humidity();
+    
+    // Cast it back to JavaScript and return
+    return jerry_create_number(result);
+
+    //return jerry_create_string((unsigned char*)"5C");
+}
+
+/**
+ * HTS221_JS#led_on (native JavaScript method)
+ * @param pin DigitalOut pin to blink
+ */
+DECLARE_CLASS_FUNCTION(HTS221_JS, led_on) {
+    // Check that we have 1 argument, and that it's an object
+    CHECK_ARGUMENT_COUNT(HTS221_JS, led_on, (args_count == 1));
+    CHECK_ARGUMENT_TYPE_ALWAYS(HTS221_JS, led_on, 0, object);
+ 
+    // Unwrap native HTS221_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 HTS221_JS pointer");
+    }
+
+    HTS221_JS *native_ptr = static_cast<HTS221_JS*>(void_ptr);
+
+
+    // Cast the LED argument
+    void *led_void_ptr;
+    const jerry_object_native_info_t *led_type_ptr;
+    bool led_has_ptr = jerry_get_object_native_pointer(args[0], &led_void_ptr, &led_type_ptr);
+
+    if (!led_has_ptr) {
+        printf("Not a DigitalOut input!");
+        return jerry_create_error(JERRY_ERROR_TYPE,
+                                  (const jerry_char_t *) "Failed to get native DigitalOut pointer");
+    }
+
+    DigitalOut *pin_ptr = reinterpret_cast<DigitalOut*>(led_void_ptr);
+
+    // Call the function from the C++ API
+    native_ptr->led_on(*pin_ptr);
+ 
+    // Return
+    return jerry_create_undefined();
+}
+
+/**
+ * HTS221_JS (native JavaScript constructor)
+ *
+ * @returns a JavaScript object representing HTS221_JS.
+ */
+DECLARE_CLASS_CONSTRUCTOR(HTS221_JS) {
+    CHECK_ARGUMENT_COUNT(HTS221_JS, __constructor, args_count == 1);
+    CHECK_ARGUMENT_TYPE_ALWAYS(HTS221_JS, __constructor, 0, object);
+    
+    // Extract native DevI2C argument (objects are always pointers) 
+    void *devI2c_ptr;
+    const jerry_object_native_info_t *devI2c_type_ptr;
+    bool devI2c_has_ptr = jerry_get_object_native_pointer(args[0], &devI2c_ptr, &devI2c_type_ptr);
+
+    if (!devI2c_has_ptr) {
+        printf("Not a I2C input!");
+        return jerry_create_error(JERRY_ERROR_TYPE,
+                                  (const jerry_char_t *) "Failed to get native DigitalOut pointer");
+    }
+
+    DevI2C* devI2c = reinterpret_cast<DevI2C*>(devI2c_ptr);
+    
+
+    // Extract native HTS221_JS pointer (from this object) 
+    HTS221_JS *native_ptr = new HTS221_JS(*devI2c);
+
+    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, HTS221_JS, get_temperature);
+    ATTACH_CLASS_FUNCTION(js_object, HTS221_JS, get_humidity);
+    ATTACH_CLASS_FUNCTION(js_object, HTS221_JS, led_on);
+
+    return js_object;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTS221_JS-js.h	Tue Oct 10 11:51:26 2017 +0200
@@ -0,0 +1,58 @@
+/**
+ ******************************************************************************
+ * @file    HTS221_JS.cpp
+ * @author  ST
+ * @version V1.0.0
+ * @date    9 October 2017
+ * @brief   Implementation of an HTS221 Humidity and Temperature 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.
+ *
+ ******************************************************************************
+ */
+
+/* Prevent recursive inclusion -----------------------------------------------*/
+
+#ifndef _HTS221_JS_JS_H
+#define _HTS221_JS_JS_H
+ 
+/* Includes ------------------------------------------------------------------*/
+
+// This file contains all the macros
+#include "jerryscript-mbed-library-registry/wrap_tools.h"
+ 
+// Class constructor
+DECLARE_CLASS_CONSTRUCTOR(HTS221_JS);
+ 
+// Define a wrapper, we can load the wrapper in `main.cpp`.
+// This makes it possible to load libraries optionally.
+DECLARE_JS_WRAPPER_REGISTRATION (HTS221_JS_library) {
+    REGISTER_CLASS_CONSTRUCTOR(HTS221_JS);
+}
+ 
+#endif 
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTS221_JS.cpp	Tue Oct 10 11:51:26 2017 +0200
@@ -0,0 +1,178 @@
+/**
+ ******************************************************************************
+ * @file    HTS221_JS.cpp
+ * @author  ST
+ * @version V1.0.0
+ * @date    9 October 2017
+ * @brief   Implementation of an HTS221 Humidity and Temperature 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 "HTS221_JS.h"
+
+#include <stdlib.h>     /* atoi */
+#include "mbed.h"
+
+/* Helper function for printing floats & doubles */
+static char *print_double(char* str, double v, int decimalDigits=2)
+{
+	int i = 1;
+	int intPart, fractPart;
+	int len;
+	char *ptr;
+
+	/* prepare decimal digits multiplicator */
+	for (;decimalDigits!=0; i*=10, decimalDigits--);
+
+	/* calculate integer & fractinal parts */
+	intPart = (int)v;
+	fractPart = (int)((v-(double)(int)v)*i);
+
+	/* fill in integer part */
+	sprintf(str, "%i.", intPart);
+
+	/* prepare fill in of fractional part */
+	len = strlen(str);
+	ptr = &str[len];
+
+	/* fill in leading fractional zeros */
+	for (i/=10;i>1; i/=10, ptr++) {
+	if (fractPart >= i) {
+		break;
+	}
+	*ptr = '0';
+	}
+
+	/* fill in (rest of) fractional part */
+	sprintf(ptr, "%i", fractPart);
+
+	return str;
+}
+
+/* Class Implementation ------------------------------------------------------*/
+
+/** Constructor
+ * @brief     Initializing the component.
+ * @param i2c object of an helper class which handles the I2C peripheral
+ */
+HTS221_JS::HTS221_JS(DevI2C &i2c){
+	hum_temp = new HTS221Sensor(&i2c);
+	hum_temp->init(NULL);
+	hum_temp->enable();
+}
+
+/** Destructor
+ * @brief     Recycling the component.
+ *  Deletes the Sensor Object
+ */
+HTS221_JS::~HTS221_JS(){
+	if(hum_temp != NULL){
+		delete hum_temp;
+	}
+}
+
+/**
+ * @brief  Read ID address of HTS221
+ * @retval The ID of the Sensor
+ */
+uint8_t HTS221_JS::readID(){
+	uint8_t result;
+	hum_temp->read_id(&result);
+	return result;
+}
+
+/**
+ * @brief  Get the temperature reading from HTS221
+ * @retval Temperature value
+ */
+float HTS221_JS::get_temperature(){
+	float value;
+	hum_temp->get_temperature(&value);
+    return value;
+}
+
+/**
+ * @brief  Get the temperature reading from HTS221
+ * @retval Temperature value in string
+ */
+unsigned char *HTS221_JS::get_temperature_string(){
+	float value;
+	char buffer[32];
+	
+	hum_temp->get_temperature(&value);
+    print_double(buffer, value);
+	unsigned char *r = new unsigned char[6];
+	for(int i = 0; i < 6; i++){
+		r[i] = buffer[i];
+	}
+	r[5] = '\0';
+	return r;
+}
+
+/**
+ * @brief  Get the humidity reading from HTS221
+ * @retval Humidity value
+ */
+float HTS221_JS::get_humidity(){
+	float value;
+	hum_temp->get_humidity(&value);
+    return value;
+}
+
+/**
+ * @brief  Get the humidity reading from HTS221
+ * @retval Humidity value in string
+ */
+unsigned char *HTS221_JS::get_humidity_string(){
+	float value;
+	char buffer[32];
+	hum_temp->get_humidity(&value);
+    print_double(buffer, value);
+	unsigned char *r = new unsigned char[6];
+	for(int i = 0; i < 6; i++){
+		r[i] = buffer[i];
+	}
+	r[5] = '\0';
+	return r;
+}
+uint8_t HTS221_JS::give(){
+	return 43;
+}
+
+/* Simple main function */
+void HTS221_JS::print_sensor_info(){
+	uint8_t id;
+	hum_temp->read_id(&id);
+  	printf("HTS221  humidity & temperature    = 0x%X\r\n", id);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTS221_JS.h	Tue Oct 10 11:51:26 2017 +0200
@@ -0,0 +1,84 @@
+/**
+ ******************************************************************************
+ * @file    HTS221_JS.cpp
+ * @author  ST
+ * @version V1.0.0
+ * @date    9 October 2017
+ * @brief   Implementation of an HTS221 Humidity and Temperature 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.
+ *
+ ******************************************************************************
+ */
+
+
+/* Prevent recursive inclusion -----------------------------------------------*/
+
+#ifndef __HTS221_JS_H__
+#define __HTS221_JS_H__
+
+/* Includes ------------------------------------------------------------------*/
+
+#include <stdint.h>
+#include "mbed.h"
+#include "HTS221Sensor.h"
+
+/* Class Declaration ---------------------------------------------------------*/
+
+/**
+ * Abstract class of an HTS221 Humidity and Temperature sensor for Javascript.
+ */
+class HTS221_JS {
+private:
+    /* Helper classes. */
+    HTS221Sensor *hum_temp = NULL;
+
+public:
+    /* Constructors */
+    HTS221_JS(){ printf("calling empty constructor"); }
+    HTS221_JS(DevI2C &devI2c);
+    
+    /* Destructor */
+    ~HTS221_JS();
+    
+    /* Declarations */
+    uint8_t readID();
+    float get_temperature();
+    unsigned char *get_temperature_string();
+    float get_humidity();
+    unsigned char *get_humidity_string();
+    uint8_t give();
+    void print_sensor_info();
+    void led_on(DigitalOut &led) {
+        //printf("led status: %d\n", led);
+        led = 1;
+        //led = !led;
+    }
+};
+
+#endif
\ No newline at end of file