Javascript wrappers for LSM6DSL Sensor library

Dependencies:   LSM6DSL

Dependents:   ST_SENSOR_JS

Revision:
0:7eb29414734d
Child:
1:7e5e76bf3efe
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LSM6DSL_JS.cpp	Tue Oct 10 11:52:24 2017 +0200
@@ -0,0 +1,195 @@
+/**
+ ******************************************************************************
+ * @file    LSM6DSL_JS.cpp
+ * @author  ST
+ * @version V1.0.0
+ * @date    9 October 2017
+ * @brief   Implementation of an LSM6DSL Accelerometer and gyroscope 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 "LSM6DSL_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;
+}
+
+/* Helper function for creating JSON for data */
+char *LSM6DSL_JS::make_json(char* str, int32_t *data, char *axes, int data_count)
+{
+	char *ptr;
+	ptr = str;
+	str[0] = 0;
+
+	for(int i = 0; i < data_count; i++){
+		//printf("len: %i\n", strlen(str));
+		if(i == 0){
+			sprintf(ptr, "{");
+			ptr = &str[strlen(str)];
+		}
+		else{
+			sprintf(ptr, ",");
+			ptr = &str[strlen(str)];
+		}
+		sprintf(ptr, "\"%c\":%i", axes[i], data[i]);
+		ptr = &str[strlen(str)];
+		
+	}
+	sprintf(ptr, "}");
+	return str;
+}
+
+/* Class Implementation ------------------------------------------------------*/
+
+/** Constructor
+ * @brief     Initiaze the component.
+ * @param i2c object of an helper class which handles the I2C peripheral
+ */
+LSM6DSL_JS::LSM6DSL_JS(DevI2C &i2c){
+	acc_gyro = new LSM6DSLSensor(&i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_HIGH,D4,D5);
+	acc_gyro->init(NULL);
+	acc_gyro->enable_x();
+	acc_gyro->enable_g();
+}
+
+/** Destructor
+ * @brief     Recycle the component.
+ *  Deletes the Sensor Object
+ */
+LSM6DSL_JS::~LSM6DSL_JS(){
+	if(acc_gyro != NULL){
+		delete acc_gyro;
+	}
+}
+
+/**
+ * @brief  Read ID address of LSM6DSL
+ * @retval The ID of the Sensor
+ */
+uint8_t LSM6DSL_JS::readID(){
+	uint8_t result;
+	acc_gyro->read_id(&result);
+	return result;
+}
+
+/**
+ * @brief  Get the accleremeter reading from LSM6DSL
+ * @retval Accleremeter value
+ */
+int32_t *LSM6DSL_JS::get_accelerometer_axes(){
+	int32_t axes[3];
+  	acc_gyro->get_x_axes(axes);
+    printf("LSM6DSL [acc/mg]:        %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
+	return axes;
+}
+
+/**
+ * @brief  Get the accleremeter reading from LSM6DSL
+ * @retval Accleremeter value in JSON string form
+ */
+
+unsigned char *LSM6DSL_JS::get_accelerometer_axes_json(){
+	int32_t axes[3];
+  	acc_gyro->get_x_axes(axes);
+    //printf("LSM6DSL [acc/mg]:        %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
+	
+	char axes_labels[3] = {'x', 'y', 'z'};
+	char *data = new char[128];
+	make_json(data, axes, axes_labels, 3);
+	
+	return (unsigned char *)data;
+}
+
+
+/**
+ * @brief  Get the gyroscope reading from LSM6DSL
+ * @retval Gyroscope value
+ */
+int32_t *LSM6DSL_JS::get_gyroscope_axes(){
+	int32_t axes[3];
+    acc_gyro->get_g_axes(axes);
+    printf("LSM6DSL [gyro/mdps]:     %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
+    return axes;
+}
+
+/**
+ * @brief  Get the gyroscope reading from LSM6DSL
+ * @retval Gyroscope value
+ */
+unsigned char *LSM6DSL_JS::get_gyroscope_axes_json(){
+	int32_t axes[3];
+    acc_gyro->get_g_axes(axes);
+    //printf("LSM6DSL [gyro/mdps]:     %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
+    
+	char axes_labels[3] = {'x', 'y', 'z'};
+	char *data = new char[128];
+	make_json(data, axes, axes_labels, 3);
+	
+	return (unsigned char *)data;
+}