Javascript wrappers for HTS221 Sensor library
Dependencies: HTS221
Diff: HTS221_JS.cpp
- Revision:
- 0:89911ffe212d
- Child:
- 1:924fb53eb7f8
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HTS221_JS.cpp Tue Oct 10 11:51:26 2017 +0200
@@ -0,0 +1,178 @@
+/**
+ ******************************************************************************
+ * @file HTS221_JS.cpp
+ * @author ST
+ * @version V1.0.0
+ * @date 9 October 2017
+ * @brief Implementation of an HTS221 Humidity and Temperature sensor for use
+ * with Javascript.
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>© 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 "HTS221_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;
+}
+
+/* Class Implementation ------------------------------------------------------*/
+
+/** Constructor
+ * @brief Initializing the component.
+ * @param i2c object of an helper class which handles the I2C peripheral
+ */
+HTS221_JS::HTS221_JS(DevI2C &i2c){
+ hum_temp = new HTS221Sensor(&i2c);
+ hum_temp->init(NULL);
+ hum_temp->enable();
+}
+
+/** Destructor
+ * @brief Recycling the component.
+ * Deletes the Sensor Object
+ */
+HTS221_JS::~HTS221_JS(){
+ if(hum_temp != NULL){
+ delete hum_temp;
+ }
+}
+
+/**
+ * @brief Read ID address of HTS221
+ * @retval The ID of the Sensor
+ */
+uint8_t HTS221_JS::readID(){
+ uint8_t result;
+ hum_temp->read_id(&result);
+ return result;
+}
+
+/**
+ * @brief Get the temperature reading from HTS221
+ * @retval Temperature value
+ */
+float HTS221_JS::get_temperature(){
+ float value;
+ hum_temp->get_temperature(&value);
+ return value;
+}
+
+/**
+ * @brief Get the temperature reading from HTS221
+ * @retval Temperature value in string
+ */
+unsigned char *HTS221_JS::get_temperature_string(){
+ float value;
+ char buffer[32];
+
+ hum_temp->get_temperature(&value);
+ print_double(buffer, value);
+ unsigned char *r = new unsigned char[6];
+ for(int i = 0; i < 6; i++){
+ r[i] = buffer[i];
+ }
+ r[5] = '\0';
+ return r;
+}
+
+/**
+ * @brief Get the humidity reading from HTS221
+ * @retval Humidity value
+ */
+float HTS221_JS::get_humidity(){
+ float value;
+ hum_temp->get_humidity(&value);
+ return value;
+}
+
+/**
+ * @brief Get the humidity reading from HTS221
+ * @retval Humidity value in string
+ */
+unsigned char *HTS221_JS::get_humidity_string(){
+ float value;
+ char buffer[32];
+ hum_temp->get_humidity(&value);
+ print_double(buffer, value);
+ unsigned char *r = new unsigned char[6];
+ for(int i = 0; i < 6; i++){
+ r[i] = buffer[i];
+ }
+ r[5] = '\0';
+ return r;
+}
+uint8_t HTS221_JS::give(){
+ return 43;
+}
+
+/* Simple main function */
+void HTS221_JS::print_sensor_info(){
+ uint8_t id;
+ hum_temp->read_id(&id);
+ printf("HTS221 humidity & temperature = 0x%X\r\n", id);
+}
\ No newline at end of file