Javascript wrappers for LSM6DSL Sensor library

Dependencies:   LSM6DSL

Dependents:   ST_SENSOR_JS

Committer:
akhtar.syedzeeshan@gmail.com
Date:
Mon Oct 23 16:41:14 2017 +0200
Revision:
3:87ac9fb0d287
Parent:
1:7e5e76bf3efe
Child:
4:0eb0cded8861
Changed DevI2C to I2C in javascript. Currently casting I2C to DevI2C while using the driver

Who changed what in which revision?

UserRevisionLine numberNew contents of line
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 1 /**
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 2 ******************************************************************************
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 3 * @file LSM6DSL_JS.cpp
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 4 * @author ST
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 5 * @version V1.0.0
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 6 * @date 9 October 2017
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 7 * @brief Implementation of an LSM6DSL Accelerometer and gyroscope sensor for
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 8 * use with Javascript.
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 9 ******************************************************************************
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 10 * @attention
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 11 *
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 12 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 13 *
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 14 * Redistribution and use in source and binary forms, with or without modification,
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 15 * are permitted provided that the following conditions are met:
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 16 * 1. Redistributions of source code must retain the above copyright notice,
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 17 * this list of conditions and the following disclaimer.
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 19 * this list of conditions and the following disclaimer in the documentation
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 20 * and/or other materials provided with the distribution.
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 22 * may be used to endorse or promote products derived from this software
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 23 * without specific prior written permission.
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 24 *
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 35 *
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 36 ******************************************************************************
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 37 */
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 38
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 39
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 40 /* Includes ------------------------------------------------------------------*/
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 41
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 42 #include "LSM6DSL_JS.h"
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 43
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 44 #include <stdlib.h> /* atoi */
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 45 #include "mbed.h"
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 46
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 47 /* Helper function for printing floats & doubles */
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 48 static char *print_double(char* str, double v, int decimalDigits=2)
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 49 {
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 50 int i = 1;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 51 int intPart, fractPart;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 52 int len;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 53 char *ptr;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 54
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 55 /* prepare decimal digits multiplicator */
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 56 for (;decimalDigits!=0; i*=10, decimalDigits--);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 57
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 58 /* calculate integer & fractinal parts */
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 59 intPart = (int)v;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 60 fractPart = (int)((v-(double)(int)v)*i);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 61
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 62 /* fill in integer part */
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 63 sprintf(str, "%i.", intPart);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 64
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 65 /* prepare fill in of fractional part */
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 66 len = strlen(str);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 67 ptr = &str[len];
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 68
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 69 /* fill in leading fractional zeros */
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 70 for (i/=10;i>1; i/=10, ptr++) {
akhtar.syedzeeshan@gmail.com 3:87ac9fb0d287 71 if (fractPart >= i) {
akhtar.syedzeeshan@gmail.com 3:87ac9fb0d287 72 break;
akhtar.syedzeeshan@gmail.com 3:87ac9fb0d287 73 }
akhtar.syedzeeshan@gmail.com 3:87ac9fb0d287 74 *ptr = '0';
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 75 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 76
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 77 /* fill in (rest of) fractional part */
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 78 sprintf(ptr, "%i", fractPart);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 79
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 80 return str;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 81 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 82
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 83 /* Helper function for creating JSON for data */
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 84 char *LSM6DSL_JS::make_json(char* str, int32_t *data, char *axes, int data_count)
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 85 {
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 86 char *ptr;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 87 ptr = str;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 88 str[0] = 0;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 89
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 90 for(int i = 0; i < data_count; i++){
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 91 //printf("len: %i\n", strlen(str));
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 92 if(i == 0){
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 93 sprintf(ptr, "{");
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 94 ptr = &str[strlen(str)];
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 95 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 96 else{
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 97 sprintf(ptr, ",");
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 98 ptr = &str[strlen(str)];
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 99 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 100 sprintf(ptr, "\"%c\":%i", axes[i], data[i]);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 101 ptr = &str[strlen(str)];
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 102
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 103 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 104 sprintf(ptr, "}");
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 105 return str;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 106 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 107
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 108 /* Class Implementation ------------------------------------------------------*/
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 109
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 110 /** Constructor
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 111 * @brief Initiaze the component.
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 112 * @param i2c object of an helper class which handles the I2C peripheral
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 113 */
akhtar.syedzeeshan@gmail.com 3:87ac9fb0d287 114 LSM6DSL_JS::LSM6DSL_JS(I2C &i2c){
akhtar.syedzeeshan@gmail.com 3:87ac9fb0d287 115 acc_gyro = new LSM6DSLSensor(reinterpret_cast<DevI2C*>(&i2c), LSM6DSL_ACC_GYRO_I2C_ADDRESS_HIGH,D4,D5);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 116 acc_gyro->init(NULL);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 117 acc_gyro->enable_x();
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 118 acc_gyro->enable_g();
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 119 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 120
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 121 /** Destructor
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 122 * @brief Recycle the component.
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 123 * Deletes the Sensor Object
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 124 */
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 125 LSM6DSL_JS::~LSM6DSL_JS(){
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 126 if(acc_gyro != NULL){
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 127 delete acc_gyro;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 128 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 129 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 130
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 131 /**
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 132 * @brief Read ID address of LSM6DSL
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 133 * @retval The ID of the Sensor
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 134 */
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 135 uint8_t LSM6DSL_JS::readID(){
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 136 uint8_t result;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 137 acc_gyro->read_id(&result);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 138 return result;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 139 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 140
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 141 /**
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 142 * @brief Get the accleremeter reading from LSM6DSL
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 143 * @retval Accleremeter value
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 144 */
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 145 int32_t *LSM6DSL_JS::get_accelerometer_axes(){
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 146 int32_t axes[3];
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 147 acc_gyro->get_x_axes(axes);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 148 printf("LSM6DSL [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 149 return axes;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 150 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 151
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 152 /**
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 153 * @brief Get the accleremeter reading from LSM6DSL
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 154 * @retval Accleremeter value in JSON string form
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 155 */
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 156
akhtar.syedzeeshan@gmail.com 1:7e5e76bf3efe 157 char *LSM6DSL_JS::get_accelerometer_axes_json(char *data){
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 158 int32_t axes[3];
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 159 acc_gyro->get_x_axes(axes);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 160 //printf("LSM6DSL [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 161
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 162 char axes_labels[3] = {'x', 'y', 'z'};
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 163 make_json(data, axes, axes_labels, 3);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 164
akhtar.syedzeeshan@gmail.com 1:7e5e76bf3efe 165 return data;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 166 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 167
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 168
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 169 /**
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 170 * @brief Get the gyroscope reading from LSM6DSL
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 171 * @retval Gyroscope value
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 172 */
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 173 int32_t *LSM6DSL_JS::get_gyroscope_axes(){
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 174 int32_t axes[3];
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 175 acc_gyro->get_g_axes(axes);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 176 printf("LSM6DSL [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 177 return axes;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 178 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 179
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 180 /**
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 181 * @brief Get the gyroscope reading from LSM6DSL
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 182 * @retval Gyroscope value
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 183 */
akhtar.syedzeeshan@gmail.com 1:7e5e76bf3efe 184 char *LSM6DSL_JS::get_gyroscope_axes_json(char * data){
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 185 int32_t axes[3];
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 186 acc_gyro->get_g_axes(axes);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 187 //printf("LSM6DSL [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 188
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 189 char axes_labels[3] = {'x', 'y', 'z'};
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 190 make_json(data, axes, axes_labels, 3);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 191
akhtar.syedzeeshan@gmail.com 1:7e5e76bf3efe 192 return data;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 193 }