HTS221 relative humidity and temperature sensor library

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   X_NUCLEO_IKS01A3

Files at this revision

API Documentation at this revision

Comitter:
martlefebvre94
Date:
Tue Sep 17 08:40:01 2019 +0000
Parent:
5:ccf7f36492ae
Commit message:
Addition of functions for the configuration of the relative humidity and temperature sensor

Changed in this revision

HTS221Sensor.cpp Show annotated file Show diff for this revision Revisions of this file
HTS221Sensor.h Show annotated file Show diff for this revision Revisions of this file
diff -r ccf7f36492ae -r 7021a2a1ac72 HTS221Sensor.cpp
--- a/HTS221Sensor.cpp	Wed Jul 24 14:17:21 2019 +0000
+++ b/HTS221Sensor.cpp	Tue Sep 17 08:40:01 2019 +0000
@@ -250,6 +250,163 @@
     return 0;
 }
 
+/**
+ * @brief  Get HTS221 heater configuration
+ * @param  heater the pointer to the heater configuration
+ * @retval 0 in case of success, an error code otherwise
+ */
+int HTS221Sensor::get_heater(uint8_t *heater)
+{
+    HTS221_State_et heater_low_level;
+    
+    if (HTS221_Get_HeaterState((void *)this, &heater_low_level) == HTS221_ERROR) {
+        return 1;
+    }
+    
+    switch (heater_low_level) {
+        case HTS221_DISABLE:
+            *heater = 0;
+            break;
+        case HTS221_ENABLE:
+            *heater = 1;
+            break;
+        default:
+            return 1;
+    }
+    
+    return 0;
+}
+
+/**
+ * @brief  Set HTS221 heater configuration
+ * @param  heater the heater configuration to be set
+ * @retval 0 in case of success, an error code otherwise
+ */
+int HTS221Sensor::set_heater(uint8_t heater)
+{
+    HTS221_State_et new_heater;
+    
+    new_heater = (heater == 0)  ? HTS221_DISABLE
+                :               HTS221_ENABLE;
+    
+    if (HTS221_Set_HeaterState((void *)this, new_heater) == HTS221_ERROR) {
+        return 1;
+    }
+    
+    return 0;
+}
+
+/**
+ * @brief  Get HTS221 averaging configuration
+ * @param  avgh the pointer to the averaging for humidity, avgt the pointer to the averaging for temperature
+ * @retval 0 in case of success, an error code otherwise
+ */
+int HTS221Sensor::get_avg(float *avgh, float *avgt)
+{
+    HTS221_Avgh_et avgh_low_level;
+    HTS221_Avgt_et avgt_low_level;
+    
+    if (HTS221_Get_AvgHT((void *)this, &avgh_low_level, &avgt_low_level) == HTS221_ERROR) {
+        return 1;
+    }
+    
+    switch (avgh_low_level) {
+        case HTS221_AVGH_4:
+            *avgh = 4;
+            break;
+        case HTS221_AVGH_8:
+            *avgh = 8;
+            break;
+        case HTS221_AVGH_16:
+            *avgh = 16;
+            break;
+        case HTS221_AVGH_32:
+            *avgh = 32;
+            break;
+        case HTS221_AVGH_64:
+            *avgh = 64;
+            break;
+        case HTS221_AVGH_128:
+            *avgh = 128;
+            break;
+        case HTS221_AVGH_256:
+            *avgh = 256;
+            break;
+        case HTS221_AVGH_512:
+            *avgh = 512;
+            break;
+        default:
+            return 1;
+    }
+    
+    switch (avgt_low_level) {
+        case HTS221_AVGT_2:
+            *avgt = 2;
+            break;
+        case HTS221_AVGT_4:
+            *avgt = 4;
+            break;
+        case HTS221_AVGT_8:
+            *avgt = 8;
+            break;
+        case HTS221_AVGT_16:
+            *avgt = 16;
+            break;
+        case HTS221_AVGT_32:
+            *avgt = 32;
+            break;
+        case HTS221_AVGT_64:
+            *avgt = 64;
+            break;
+        case HTS221_AVGT_128:
+            *avgt = 128;
+            break;
+        case HTS221_AVGT_256:
+            *avgt = 256;
+            break;
+        default:
+            return 1;
+    }
+    
+    return 0;
+}
+
+/**
+ * @brief  Set HTS221 averaging configuration
+ * @param  avgh the averagingfor humidity to be set, avgt the averaging for temperature to be set
+ * @retval 0 in case of success, an error code otherwise
+ */
+int HTS221Sensor::set_avg(float avgh, float avgt)
+{
+    HTS221_Avgh_et new_avgh;
+    HTS221_Avgt_et new_avgt;
+    
+    new_avgh = (avgh == 4)      ? HTS221_AVGH_4
+              : (avgh == 8)     ? HTS221_AVGH_8
+              : (avgh == 16)    ? HTS221_AVGH_16
+              : (avgh == 32)    ? HTS221_AVGH_32
+              : (avgh == 64)    ? HTS221_AVGH_64
+              : (avgh == 128)   ? HTS221_AVGH_128
+              : (avgh == 256)   ? HTS221_AVGH_256
+              : (avgh == 512)   ? HTS221_AVGH_512
+              :                   HTS221_AVGH_4;
+    
+    new_avgt = (avgt == 2)      ? HTS221_AVGT_2
+              : (avgt == 4)     ? HTS221_AVGT_4
+              : (avgt == 8)     ? HTS221_AVGT_8
+              : (avgt == 16)    ? HTS221_AVGT_16
+              : (avgt == 32)    ? HTS221_AVGT_32
+              : (avgt == 64)    ? HTS221_AVGT_64
+              : (avgt == 128)   ? HTS221_AVGT_128
+              : (avgt == 256)   ? HTS221_AVGT_256
+              :                   HTS221_AVGT_2;
+    
+    if (HTS221_Set_AvgHT((void *)this, new_avgh, new_avgt) == HTS221_ERROR) {
+        return 1;
+    }
+    
+    return 0;
+}
 
 /**
  * @brief Read the data from register
diff -r ccf7f36492ae -r 7021a2a1ac72 HTS221Sensor.h
--- a/HTS221Sensor.h	Wed Jul 24 14:17:21 2019 +0000
+++ b/HTS221Sensor.h	Tue Sep 17 08:40:01 2019 +0000
@@ -69,6 +69,10 @@
     int reset(void);
     int get_odr(float *odr);
     int set_odr(float odr);
+    int get_heater(uint8_t *heater);
+    int set_heater(uint8_t heater);
+    int get_avg(float *avgh, float *avgt);
+    int set_avg(float avgh, float avgt);
     int read_reg(uint8_t reg, uint8_t *data);
     int write_reg(uint8_t reg, uint8_t data);
     /**