Javascript wrappers for HTS221 Sensor library

Dependencies:   HTS221

Dependents:   ST_SENSOR_JS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTS221_JS.cpp Source File

HTS221_JS.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    HTS221_JS.cpp
00004  * @author  ST
00005  * @version V1.0.0
00006  * @date    9 October 2017
00007  * @brief   Implementation of an HTS221 Humidity 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 "HTS221_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 /** Constructor
00086  * @brief   Initializing the component.
00087  * @param   devI2c object of an helper class which handles the DevI2C peripheral
00088  */
00089 HTS221_JS::HTS221_JS(DevI2C &devI2c){
00090     init(devI2c);
00091 }
00092 
00093 /** init
00094  * @brief   Initializing the component.
00095  * @param   devI2c object of an helper class which handles the DevI2C peripheral
00096  */
00097 void HTS221_JS::init(DevI2C &devI2c){
00098     hum_temp = new HTS221Sensor(&devI2c);
00099     hum_temp->init(NULL);
00100     hum_temp->enable();
00101 }
00102 
00103 /** Constructor
00104  * @brief   Initializing the component.
00105  * @param   devI2c object of an helper class which handles the DevI2C peripheral
00106  * @param   address
00107  * @param   drdy pin
00108  */
00109 HTS221_JS::HTS221_JS(DevI2C &devI2c, uint8_t address, PinName drdy_pin){
00110     init(devI2c, address, drdy_pin);
00111 }
00112 
00113 /** init
00114  * @brief   Initializing the component.
00115  * @param   devI2c object of an helper class which handles the DevI2C peripheral
00116  * @param   address
00117  * @param   drdy pin
00118  */
00119 void HTS221_JS::init(DevI2C &devI2c, uint8_t address, PinName drdy_pin){
00120     hum_temp = new HTS221Sensor(&devI2c, address, drdy_pin);
00121     hum_temp->init(NULL);
00122     hum_temp->enable();
00123 }
00124 
00125 /** Constructor
00126  * @brief   Initializing the component.
00127  * @param   spi object of an helper class which handles the SPI peripheral
00128  */
00129 HTS221_JS::HTS221_JS(SPI &spi){
00130     init(spi);
00131 }
00132 
00133 /** init
00134  * @brief   Initializing the component.
00135  * @param   spi object of an helper class which handles the SPI peripheral
00136  */
00137 void HTS221_JS::init(SPI &spi){
00138     hum_temp = new HTS221Sensor(&spi);
00139     hum_temp->init(NULL);
00140     hum_temp->enable();
00141 }
00142 
00143 /** Constructor
00144  * @brief   Initializing the component.
00145  * @param   spi object of an helper class which handles the SPI peripheral
00146  * @param   cs pin
00147  * @param   drdy pin
00148  */
00149 HTS221_JS::HTS221_JS(SPI &spi, PinName cs_pin, PinName drdy_pin){
00150     init(spi, cs_pin, drdy_pin);
00151 }
00152 
00153 /** init
00154  * @brief   Initializing the component.
00155  * @param   spi object of an helper class which handles the SPI peripheral
00156  * @param   cs pin
00157  * @param   drdy pin
00158  */
00159 void HTS221_JS::init(SPI &spi, PinName cs_pin, PinName drdy_pin){
00160     hum_temp = new HTS221Sensor(&spi, cs_pin, drdy_pin);
00161     hum_temp->init(NULL);
00162     hum_temp->enable();
00163 }
00164 
00165 /** Destructor
00166  * @brief   Recycle the component.
00167  *  Deletes the Sensor Object
00168  */
00169 HTS221_JS::~HTS221_JS(){
00170     if(hum_temp != NULL){
00171         delete hum_temp;
00172     }
00173 }
00174 
00175 /**
00176  * @brief   Read ID address of HTS221
00177  * @retval  The ID of the Sensor
00178  */
00179 uint8_t HTS221_JS::readID(){
00180     uint8_t result;
00181     hum_temp->read_id(&result);
00182     return result;
00183 }
00184 
00185 /**
00186  * @brief   Get the temperature reading from HTS221
00187  * @retval  Temperature value
00188  */
00189 float HTS221_JS::get_temperature(){
00190     float value;
00191     hum_temp->get_temperature(&value);
00192     return value;
00193 }
00194 
00195 /**
00196  * @brief   Get the temperature reading from HTS221
00197  * @retval  Temperature value in string
00198  */
00199 char *HTS221_JS::get_temperature_string(char *buffer){
00200     float value;
00201     hum_temp->get_temperature(&value);
00202     print_double(buffer, value);
00203     return buffer;
00204 }
00205 
00206 /**
00207  * @brief   Get the humidity reading from HTS221
00208  * @retval  Humidity value
00209  */
00210 float HTS221_JS::get_humidity(){
00211     float value;
00212     hum_temp->get_humidity(&value);
00213     return value;
00214 }
00215 
00216 /**
00217  * @brief   Get the humidity reading from HTS221
00218  * @retval  Humidity value in string
00219  */
00220 char *HTS221_JS::get_humidity_string(char *buffer){
00221     float value;
00222     hum_temp->get_humidity(&value);
00223     print_double(buffer, value);
00224     return buffer;
00225 }