Javascript wrappers for LPS22HB Sensor library

Dependencies:   LPS22HB

Dependents:   ST_SENSOR_JS

Revision:
0:c256bbccd0d4
Child:
1:6b7b798e36ae
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LPS22HB_JS.cpp	Tue Oct 10 11:51:55 2017 +0200
@@ -0,0 +1,179 @@
+/**
+ ******************************************************************************
+ * @file    LPS22HB_JS.cpp
+ * @author  ST
+ * @version V1.0.0
+ * @date    9 October 2017
+ * @brief   Implementation of an LPS22HB Pressure 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 "LPS22HB_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
+ */
+LPS22HB_JS::LPS22HB_JS(DevI2C &i2c){
+	press_temp = new LPS22HBSensor(&i2c);
+	press_temp->init(NULL);
+	press_temp->enable();
+}
+
+/** Destructor
+ * @brief     Recycling the component.
+ *  Deletes the Sensor Object
+ */
+LPS22HB_JS::~LPS22HB_JS(){
+	if(press_temp != NULL){
+		delete press_temp;
+	}
+}
+
+/**
+ * @brief  Read ID address of LPS22HB
+ * @retval The ID of the Sensor
+ */
+uint8_t LPS22HB_JS::readID(){
+	uint8_t result;
+	press_temp->read_id(&result);
+	return result;
+}
+
+/**
+ * @brief  Get the temperature reading from LPS22HB
+ * @retval Temperature value
+ */
+float LPS22HB_JS::get_temperature(){
+	float value;
+	press_temp->get_temperature(&value);
+    return value;
+}
+
+/**
+ * @brief  Get the temperature reading from LPS22HB
+ * @retval Temperature value in string form
+ */
+unsigned char *LPS22HB_JS::get_temperature_string(){
+	float value;
+	char buffer[32];
+	
+	press_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 pressure reading from LPS22HB
+ * @retval pressure value
+ */
+float LPS22HB_JS::get_pressure(){
+	float value;
+	press_temp->get_pressure(&value);
+    return value;
+}
+
+
+/**
+ * @brief  Get the pressure reading from LPS22HB
+ * @retval pressure value in string form
+ */
+unsigned char *LPS22HB_JS::get_pressure_string(){
+	float value;
+	char buffer[32];
+	press_temp->get_pressure(&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 LPS22HB_JS::give(){
+	return 43;
+}
+
+/* Simple main function */
+void LPS22HB_JS::print_sensor_info(){
+	uint8_t id;
+	press_temp->read_id(&id);
+  	printf("LPS22HB  pressure & temperature    = 0x%X\r\n", id);
+}
\ No newline at end of file