Javascript wrappers for LPS22HB Sensor library

Dependencies:   LPS22HB

Dependents:   ST_SENSOR_JS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LPS22HB_JS.cpp Source File

LPS22HB_JS.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    LPS22HB_JS.cpp
00004  * @author  ST
00005  * @version V1.0.0
00006  * @date    9 October 2017
00007  * @brief   Implementation of an LPS22HB Pressure and Temperature sensor for use
00008  *          with Javascript.
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright notice,
00019  *      this list of conditions and the following disclaimer in the documentation
00020  *      and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022  *      may be used to endorse or promote products derived from this software
00023  *      without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  ******************************************************************************
00037  */
00038 
00039 
00040 /* Includes ------------------------------------------------------------------*/
00041 
00042 #include "LPS22HB_JS.h"
00043 
00044 #include <stdlib.h>     /* atoi */
00045 #include "mbed.h"
00046 
00047 /* Helper function for printing floats & doubles */
00048 static char *print_double(char* str, double v, int decimalDigits=2)
00049 {
00050     int i = 1;
00051     int intPart, fractPart;
00052     int len;
00053     char *ptr;
00054 
00055     /* prepare decimal digits multiplicator */
00056     for (;decimalDigits!=0; i*=10, decimalDigits--);
00057 
00058     /* calculate integer & fractinal parts */
00059     intPart = (int)v;
00060     fractPart = (int)((v-(double)(int)v)*i);
00061 
00062     /* fill in integer part */
00063     sprintf(str, "%i.", intPart);
00064 
00065     /* prepare fill in of fractional part */
00066     len = strlen(str);
00067     ptr = &str[len];
00068 
00069     /* fill in leading fractional zeros */
00070     for (i/=10;i>1; i/=10, ptr++) {
00071         if (fractPart >= i) {
00072             break;
00073         }
00074         *ptr = '0';
00075     }
00076 
00077     /* fill in (rest of) fractional part */
00078     sprintf(ptr, "%i", fractPart);
00079 
00080     return str;
00081 }
00082 
00083 /* Class Implementation ------------------------------------------------------*/
00084 
00085 
00086 /** Constructor
00087  * @brief   Initializing the component.
00088  * @param   DevI2c object of helper class which handles the DevI2C peripheral
00089  */
00090 LPS22HB_JS::LPS22HB_JS(DevI2C &devI2c){
00091     init(devI2c);
00092 }
00093 
00094 /** init
00095  * @brief   Initializing the component.
00096  * @param   DevI2c object of helper class which handles the DevI2C peripheral
00097  */
00098 void LPS22HB_JS::init(DevI2C &devI2c){
00099     press_temp = new LPS22HBSensor(&devI2c);
00100     press_temp->init(NULL);
00101     press_temp->enable();
00102 }
00103 
00104 /** Constructor
00105  * @brief   Initializing the component.
00106  * @param   DevI2c object of helper class which handles the DevI2C peripheral
00107  * @param   Address
00108  * @param   INT pin
00109  */
00110 LPS22HB_JS::LPS22HB_JS(DevI2C &devI2c, uint8_t address, PinName int_pin){
00111     init(devI2c, address, int_pin);
00112 }
00113 
00114 /** init
00115  * @brief   Initializing the component.
00116  * @param   DevI2c object of helper class which handles the DevI2C peripheral
00117  * @param   Address
00118  * @param   INT pin
00119  */
00120 void LPS22HB_JS::init(DevI2C &devI2c, uint8_t address, PinName int_pin){
00121     press_temp = new LPS22HBSensor(&devI2c, address, int_pin);
00122     press_temp->init(NULL);
00123     press_temp->enable();
00124 }
00125 
00126 /** Constructor
00127  * @brief   Initializing the component.
00128  * @param   SPI object of helper class which handles the SPI peripheral
00129  * @param   CS pin
00130  */
00131 LPS22HB_JS::LPS22HB_JS(SPI &spi, PinName cs_pin){
00132     init(spi, cs_pin);
00133 }
00134 
00135 /** init
00136  * @brief   Initializing the component.
00137  * @param   SPI object of helper class which handles the SPI peripheral
00138  * @param   CS pin
00139  */
00140 void LPS22HB_JS::init(SPI &spi, PinName cs_pin){
00141     press_temp = new LPS22HBSensor(&spi, cs_pin);
00142     press_temp->init(NULL);
00143     press_temp->enable();
00144 }
00145 
00146 /** Constructor
00147  * @brief   Initializing the component.
00148  * @param   SPI object of helper class which handles the SPI peripheral
00149  * @param   CS pin
00150  * @param   INT pin
00151  * @param   SPI type
00152  */
00153 LPS22HB_JS::LPS22HB_JS(SPI &spi, PinName cs_pin, PinName int_pin, int spi_type){
00154     init(spi, cs_pin, int_pin, spi_type);
00155 }
00156 
00157 /** init
00158  * @brief   Initializing the component.
00159  * @param   SPI object of helper class which handles the SPI peripheral
00160  * @param   CS pin
00161  * @param   INT pin
00162  * @param   SPI type
00163  */
00164 void LPS22HB_JS::init(SPI &spi, PinName cs_pin, PinName int_pin, int spi_type){
00165     press_temp = new LPS22HBSensor(&spi, cs_pin, int_pin, spi_type == 3? LPS22HBSensor::SPI3W: LPS22HBSensor::SPI4W);
00166     press_temp->init(NULL);
00167     press_temp->enable();
00168 }
00169 
00170 /** Destructor
00171  * @brief   Recycling the component. Deletes the Sensor Object
00172  */
00173 LPS22HB_JS::~LPS22HB_JS(){
00174     if(press_temp != NULL){
00175         delete press_temp;
00176     }
00177 }
00178 
00179 /** readID
00180  * @brief   Read ID address of LPS22HB
00181  * @retval  The ID of the Sensor
00182  */
00183 uint8_t LPS22HB_JS::readID(){
00184     uint8_t result;
00185     press_temp->read_id(&result);
00186     return result;
00187 }
00188 
00189 /** get_temperature
00190  * @brief   Gets the temperature reading from LPS22HB
00191  * @retval  Temperature value
00192  */
00193 float LPS22HB_JS::get_temperature(){
00194     float value;
00195     press_temp->get_temperature(&value);
00196     return value;
00197 }
00198 
00199 /** get_temperature_string
00200  * @brief   Gets the temperature reading from LPS22HB
00201  * @retval  Temperature value in string form
00202  */
00203 char *LPS22HB_JS::get_temperature_string(char *buffer){
00204     float value;
00205     press_temp->get_temperature(&value);
00206     print_double(buffer, value);
00207     return buffer;
00208 }
00209 
00210 /** get_pressure
00211  * @brief   Gets the pressure reading from LPS22HB
00212  * @retval  Pressure value
00213  */
00214 float LPS22HB_JS::get_pressure(){
00215     float value;
00216     press_temp->get_pressure(&value);
00217     return value;
00218 }
00219 
00220 /** get_pressure_string
00221  * @brief   Gets the pressure reading from LPS22HB
00222  * @retval  pressure value in string form
00223  */
00224 char *LPS22HB_JS::get_pressure_string(char *buffer){
00225     float value;
00226     press_temp->get_pressure(&value);
00227     print_double(buffer, value);
00228     return buffer;
00229 }