Javascript wrappers for LSM6DSL Sensor library

Dependencies:   LSM6DSL

Dependents:   ST_SENSOR_JS

Committer:
akhtar.syedzeeshan@gmail.com
Date:
Wed Jan 17 10:56:16 2018 +0100
Revision:
6:97726929e01f
Parent:
5:4f29cb91aaef
Code fixes

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 6:97726929e01f 12 * <h2><center>&copy; COPYRIGHT(c) 2017 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 5:4f29cb91aaef 44 #include <stdlib.h> /* atof */
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 5:4f29cb91aaef 100 sprintf(ptr, "\"%c\":%i", axes[i], static_cast<int>(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 5:4f29cb91aaef 111 * @brief Initializing the component.
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 112 * @param DevI2c object of helper class which handles the DevI2C peripheral
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 113 */
akhtar.syedzeeshan@gmail.com 4:0eb0cded8861 114 LSM6DSL_JS::LSM6DSL_JS(DevI2C &devI2c){
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 115 init(devI2c);
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 116 }
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 117
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 118 /** init
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 119 * @brief Initializing the component.
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 120 * @param DevI2c object of helper class which handles the DevI2C peripheral
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 121 */
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 122 void LSM6DSL_JS::init(DevI2C &devI2c){
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 123 acc_gyro = new LSM6DSLSensor(&devI2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_HIGH, D4, D5);
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 124 acc_gyro->init(NULL);
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 125 acc_gyro->enable_x();
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 126 acc_gyro->enable_g();
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 127 }
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 128
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 129 /** Constructor
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 130 * @brief Initializing the component.
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 131 * @param DevI2c object of helper class which handles the DevI2C peripheral
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 132 * @param INT1 pin
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 133 * @param INT2 pin
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 134 */
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 135 LSM6DSL_JS::LSM6DSL_JS(DevI2C &devI2c, PinName int1_pin, PinName int2_pin){
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 136 init(devI2c, int1_pin, int2_pin);
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 137 }
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 138
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 139 /** init
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 140 * @brief Initializing the component.
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 141 * @param DevI2c object of helper class which handles the DevI2C peripheral
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 142 * @param INT1 pin
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 143 * @param INT2 pin
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 144 */
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 145 void LSM6DSL_JS::init(DevI2C &devI2c, PinName int1_pin, PinName int2_pin){
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 146 acc_gyro = new LSM6DSLSensor(&devI2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_HIGH, int1_pin, int2_pin);
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 147 acc_gyro->init(NULL);
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 148 acc_gyro->enable_x();
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 149 acc_gyro->enable_g();
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 150 }
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 151
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 152 /** Constructor
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 153 * @brief Initializing the component.
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 154 * @param DevI2c object of helper class which handles the DevI2C peripheral
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 155 * @param INT1 pin
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 156 * @param INT2 pin
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 157 * @param Address
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 158 */
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 159 LSM6DSL_JS::LSM6DSL_JS(DevI2C &devI2c, PinName int1_pin, PinName int2_pin, uint8_t address){
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 160 init(devI2c, int1_pin, int2_pin, address);
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 161 }
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 162
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 163 /** init
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 164 * @brief Initializing the component.
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 165 * @param DevI2c object of helper class which handles the DevI2C peripheral
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 166 * @param INT1 pin
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 167 * @param INT2 pin
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 168 * @param Address
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 169 */
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 170 void LSM6DSL_JS::init(DevI2C &devI2c, PinName int1_pin, PinName int2_pin, uint8_t address){
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 171 acc_gyro = new LSM6DSLSensor(&devI2c, address, int1_pin, int2_pin);
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 172 acc_gyro->init(NULL);
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 173 acc_gyro->enable_x();
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 174 acc_gyro->enable_g();
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 175 }
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 176
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 177 /** Constructor
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 178 * @brief Initializing the component.
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 179 * @param SPI object of helper class which handles the SPI peripheral
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 180 * @param CS pin
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 181 */
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 182 LSM6DSL_JS::LSM6DSL_JS(SPI &spi, PinName cs_pin){
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 183 init(spi, cs_pin);
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 184 }
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 185
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 186 /** init
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 187 * @brief Initializing the component.
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 188 * @param SPI object of helper class which handles the SPI peripheral
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 189 * @param CS pin
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 190 */
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 191 void LSM6DSL_JS::init(SPI &spi, PinName cs_pin){
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 192 //acc_gyro = new LSM6DSLSensor(&spi, PB_12, NC, PA_2, LSM6DSLSensor::SPI3W);
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 193 acc_gyro = new LSM6DSLSensor(&spi, cs_pin);
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 194 acc_gyro->init(NULL);
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 195 acc_gyro->enable_x();
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 196 acc_gyro->enable_g();
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 197 }
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 198
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 199 /** Constructor
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 200 * @brief Initializing the component.
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 201 * @param SPI object of helper class which handles the SPI peripheral
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 202 * @param CS pin
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 203 * @param INT1 pin
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 204 * @param INT2 pin
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 205 * @param SPI Type
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 206 */
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 207 LSM6DSL_JS::LSM6DSL_JS(SPI &spi, PinName cs_pin, PinName int1_pin, PinName int2_pin, int spi_type){
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 208 init(spi, cs_pin, int1_pin, int2_pin, spi_type);
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 209 }
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 210
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 211 /** init
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 212 * @brief Initializing the component.
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 213 * @param SPI object of helper class which handles the SPI peripheral
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 214 * @param CS pin
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 215 * @param INT1 pin
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 216 * @param INT2 pin
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 217 * @param SPI Type
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 218 */
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 219 void LSM6DSL_JS::init(SPI &spi, PinName cs_pin, PinName int1_pin, PinName int2_pin, int spi_type){
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 220 //acc_gyro = new LSM6DSLSensor(&spi, PB_12, NC, PA_2, LSM6DSLSensor::SPI3W);
akhtar.syedzeeshan@gmail.com 5:4f29cb91aaef 221 acc_gyro = new LSM6DSLSensor(&spi, cs_pin, int1_pin, int2_pin, spi_type == 3? LSM6DSLSensor::SPI3W: LSM6DSLSensor::SPI4W);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 222 acc_gyro->init(NULL);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 223 acc_gyro->enable_x();
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 224 acc_gyro->enable_g();
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 225 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 226
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 227 /** Destructor
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 228 * @brief Recycle the component.
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 229 * Deletes the Sensor Object
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 230 */
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 231 LSM6DSL_JS::~LSM6DSL_JS(){
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 232 if(acc_gyro != NULL){
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 233 delete acc_gyro;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 234 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 235 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 236
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 237 /**
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 238 * @brief Read ID address of LSM6DSL
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 239 * @retval The ID of the Sensor
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 240 */
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 241 uint8_t LSM6DSL_JS::readID(){
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 242 uint8_t result;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 243 acc_gyro->read_id(&result);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 244 return result;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 245 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 246
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 247 /**
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 248 * @brief Get the accleremeter reading from LSM6DSL
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 249 * @retval Accleremeter value
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 250 */
akhtar.syedzeeshan@gmail.com 4:0eb0cded8861 251 int32_t *LSM6DSL_JS::get_accelerometer_axes(int32_t *axes){
akhtar.syedzeeshan@gmail.com 4:0eb0cded8861 252 acc_gyro->get_x_axes(axes);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 253 printf("LSM6DSL [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 254 return axes;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 255 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 256
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 257 /**
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 258 * @brief Get the accleremeter reading from LSM6DSL
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 259 * @retval Accleremeter value in JSON string form
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 260 */
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 261
akhtar.syedzeeshan@gmail.com 1:7e5e76bf3efe 262 char *LSM6DSL_JS::get_accelerometer_axes_json(char *data){
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 263 int32_t axes[3];
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 264 acc_gyro->get_x_axes(axes);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 265 //printf("LSM6DSL [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 266
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 267 char axes_labels[3] = {'x', 'y', 'z'};
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 268 make_json(data, axes, axes_labels, 3);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 269
akhtar.syedzeeshan@gmail.com 1:7e5e76bf3efe 270 return data;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 271 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 272
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 273
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 274 /**
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 275 * @brief Get the gyroscope reading from LSM6DSL
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 276 * @retval Gyroscope value
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 277 */
akhtar.syedzeeshan@gmail.com 4:0eb0cded8861 278 int32_t *LSM6DSL_JS::get_gyroscope_axes(int32_t * axes){
akhtar.syedzeeshan@gmail.com 4:0eb0cded8861 279 acc_gyro->get_g_axes(axes);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 280 printf("LSM6DSL [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 281 return axes;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 282 }
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 283
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 284 /**
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 285 * @brief Get the gyroscope reading from LSM6DSL
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 286 * @retval Gyroscope value
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 287 */
akhtar.syedzeeshan@gmail.com 1:7e5e76bf3efe 288 char *LSM6DSL_JS::get_gyroscope_axes_json(char * data){
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 289 int32_t axes[3];
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 290 acc_gyro->get_g_axes(axes);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 291 //printf("LSM6DSL [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 292
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 293 char axes_labels[3] = {'x', 'y', 'z'};
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 294 make_json(data, axes, axes_labels, 3);
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 295
akhtar.syedzeeshan@gmail.com 1:7e5e76bf3efe 296 return data;
akhtar.syedzeeshan@gmail.com 0:7eb29414734d 297 }