[ 
https://issues.apache.org/jira/browse/MYNEWT-881?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16373451#comment-16373451
 ] 

ASF GitHub Bot commented on MYNEWT-881:
---------------------------------------

vrahane closed pull request #718: MYNEWT-881: SensorAPI: Add 
sensor_clear_high/low_thresh() API and LIS2DH12 set threshold cb fix
URL: https://github.com/apache/mynewt-core/pull/718
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/apps/sensors_test/src/main.c b/apps/sensors_test/src/main.c
index fe4a862d8..6b3af902f 100755
--- a/apps/sensors_test/src/main.c
+++ b/apps/sensors_test/src/main.c
@@ -36,6 +36,8 @@
 #include <id/id.h>
 #include <os/os_time.h>
 #include <defs/error.h>
+#include <sensor/accel.h>
+#include <sensor/sensor.h>
 
 #if MYNEWT_VAL(BNO055_CLI)
 #include <bno055/bno055.h>
@@ -80,6 +82,10 @@ static const oc_handler_t sensor_oic_handler = {
 /* Application-specified header. */
 #include "bleprph.h"
 
+struct sensor_type_traits stt;
+struct sensor_accel_data sad_low;
+struct sensor_accel_data sad_high;
+
 #if MYNEWT_VAL(SENSOR_OIC)
 static int sensor_oic_gap_event(struct ble_gap_event *event, void *arg);
 #endif
@@ -272,6 +278,9 @@ sensor_oic_gap_event(struct ble_gap_event *event, void *arg)
 
         oc_ble_coap_conn_del(event->disconnect.conn.conn_handle);
 
+        sensor_clear_low_thresh("lis2dh12_0", SENSOR_TYPE_ACCELEROMETER);
+        sensor_clear_high_thresh("lis2dh12_0", SENSOR_TYPE_ACCELEROMETER);
+
         /* Connection terminated; resume advertising. */
         sensor_oic_advertise();
         return 0;
@@ -525,6 +534,35 @@ main(int argc, char **argv)
     /* log reboot */
     reboot_start(hal_reset_cause());
 
+    stt = (struct sensor_type_traits) {
+        .stt_sensor_type     = SENSOR_TYPE_ACCELEROMETER,
+        .stt_low_thresh.sad = &sad_low,
+        .stt_high_thresh.sad = &sad_high,
+        .stt_algo            = SENSOR_THRESH_ALGO_WATERMARK
+    };
+
+    /* The thresholds are specified in m/s^2 */
+
+    sad_low = (struct sensor_accel_data) {
+        .sad_x = 0.6712,
+        .sad_x_is_valid = 1,
+        .sad_y = 0.6712,
+        .sad_y_is_valid = 1,
+        .sad_z = 0.6712,
+        .sad_z_is_valid = 1
+    };
+
+    sad_high = (struct sensor_accel_data) {
+        .sad_x = 0.6712,
+        .sad_x_is_valid = 1,
+        .sad_y = 0.6712,
+        .sad_y_is_valid = 1,
+        .sad_z = 0.6712,
+        .sad_z_is_valid = 1
+    };
+
+    sensor_set_thresh("lis2dh12_0", &stt);
+
     /*
      * As the last thing, process events from default event queue.
      */
diff --git a/hw/drivers/sensors/lis2dh12/include/lis2dh12/lis2dh12.h 
b/hw/drivers/sensors/lis2dh12/include/lis2dh12/lis2dh12.h
index c853484b7..08a1c7f02 100644
--- a/hw/drivers/sensors/lis2dh12/include/lis2dh12/lis2dh12.h
+++ b/hw/drivers/sensors/lis2dh12/include/lis2dh12/lis2dh12.h
@@ -294,6 +294,24 @@ lis2dh12_set_int1_duration(struct sensor_itf *itf, uint8_t 
dur);
 int
 lis2dh12_set_int2_duration(struct sensor_itf *itf, uint8_t dur);
 
+/**
+ * Disable interrupt 1
+ *
+ * @param the sensor interface
+ * @return 0 on success, non-zero on failure
+ */
+int
+lis2dh12_disable_int1(struct sensor_itf *itf);
+
+/**
+ * Disable interrupt 2
+ *
+ * @param the sensor interface
+ * @return 0 on success, non-zero on failure
+ */
+int
+lis2dh12_disable_int2(struct sensor_itf *itf);
+
 /**
  * Set high pass filter cfg
  *
diff --git a/hw/drivers/sensors/lis2dh12/src/lis2dh12.c 
b/hw/drivers/sensors/lis2dh12/src/lis2dh12.c
index e0849c838..bb2640c61 100644
--- a/hw/drivers/sensors/lis2dh12/src/lis2dh12.c
+++ b/hw/drivers/sensors/lis2dh12/src/lis2dh12.c
@@ -70,11 +70,19 @@ static int lis2dh12_sensor_get_config(struct sensor *, 
sensor_type_t,
 static int
 lis2dh12_sensor_set_trigger_thresh(struct sensor *, sensor_type_t,
                                    struct sensor_type_traits *);
+static int
+lis2dh12_sensor_clear_low_thresh(struct sensor *, sensor_type_t);
+
+static int
+lis2dh12_sensor_clear_high_thresh(struct sensor *, sensor_type_t);
+
 static const struct sensor_driver g_lis2dh12_sensor_driver = {
     .sd_read = lis2dh12_sensor_read,
     .sd_get_config = lis2dh12_sensor_get_config,
     /* Setting trigger threshold is optional */
-    .sd_set_trigger_thresh = lis2dh12_sensor_set_trigger_thresh
+    .sd_set_trigger_thresh = lis2dh12_sensor_set_trigger_thresh,
+    .sd_clear_low_trigger_thresh = lis2dh12_sensor_clear_low_thresh,
+    .sd_clear_high_trigger_thresh = lis2dh12_sensor_clear_high_thresh
 };
 
 /**
@@ -1242,6 +1250,60 @@ lis2dh12_set_int2_pin_cfg(struct sensor_itf *itf, 
uint8_t cfg)
     return lis2dh12_writelen(itf, LIS2DH12_REG_CTRL_REG6, &cfg, 1);
 }
 
+/**
+ * Disable interrupt 1
+ *
+ * @param the sensor interface
+ * @return 0 on success, non-zero on failure
+ */
+int
+lis2dh12_disable_int1(struct sensor_itf *itf)
+{
+    uint8_t reg;
+    int rc;
+
+    reg = 0;
+
+    rc = lis2dh12_clear_int1(itf);
+    if (rc) {
+        goto err;
+    }
+
+    os_time_delay((OS_TICKS_PER_SEC * 20)/1000 + 1);
+
+    rc = lis2dh12_writelen(itf, LIS2DH12_REG_INT1_CFG, &reg, 1);
+
+err:
+    return rc;
+}
+
+/**
+ * Disable interrupt 2
+ *
+ * @param the sensor interface
+ * @return 0 on success, non-zero on failure
+ */
+int
+lis2dh12_disable_int2(struct sensor_itf *itf)
+{
+    uint8_t reg;
+    int rc;
+
+    reg = 0;
+
+    rc = lis2dh12_clear_int2(itf);
+    if (rc) {
+        goto err;
+    }
+
+    os_time_delay((OS_TICKS_PER_SEC * 20)/1000 + 1);
+
+    rc = lis2dh12_writelen(itf, LIS2DH12_REG_INT2_CFG, &reg, 1);
+
+err:
+    return rc;
+}
+
 /**
  * Enable interrupt 1
  *
@@ -1255,49 +1317,66 @@ lis2dh12_enable_int1(struct sensor_itf *itf, uint8_t 
*reg)
 }
 
 /**
- * IRQ handler for int1 for low threshold
+ * IRQ handler for interrupts for high/low threshold
  *
  * @param arg
  */
 static void
-lis2dh12_low_int1_irq_handler(void *arg)
+lis2dh12_int_irq_handler(void *arg)
 {
     sensor_mgr_put_read_evt(arg);
 }
 
 /**
- * IRQ handler for int2 for high threshold
+ * Clear the low threshold values and disable interrupt
  *
- * @param arg
+ * @param ptr to sensor
+ * @param the Sensor type
+ * @param Sensor type traits
+ *
+ * @return 0 on success, non-zero on failure
  */
-static void
-lis2dh12_high_int2_irq_handler(void *arg)
+static int
+lis2dh12_sensor_clear_low_thresh(struct sensor *sensor,
+                                 sensor_type_t type)
 {
-    sensor_mgr_put_read_evt(arg);
+    int rc;
+    struct sensor_itf *itf;
+
+    itf = SENSOR_GET_ITF(sensor);
+
+    if (type != SENSOR_TYPE_ACCELEROMETER) {
+        rc = SYS_EINVAL;
+        goto err;
+    }
+
+    rc = lis2dh12_disable_int1(itf);
+    if (rc) {
+        goto err;
+    }
+
+    hal_gpio_irq_release(itf->si_low_pin);
+
+    return 0;
+err:
+    return rc;
 }
 
-/* Set the trigger threshold values and enable interrupts
+/**
+ * Clear the high threshold values and disable interrupt
  *
  * @param ptr to sensor
  * @param the Sensor type
- * @param low threshold
- * @param high threshold
+ * @param Sensor type traits
  *
  * @return 0 on success, non-zero on failure
  */
 static int
-lis2dh12_sensor_set_trigger_thresh(struct sensor *sensor,
-                                   sensor_type_t type,
-                                   struct sensor_type_traits *stt)
+lis2dh12_sensor_clear_high_thresh(struct sensor *sensor,
+                                  sensor_type_t type)
 {
     int rc;
-    uint8_t tmp;
-    int16_t acc_mg;
-    uint8_t reg;
     struct sensor_itf *itf;
-    sensor_data_t low_thresh;
-    sensor_data_t high_thresh;
-    struct sensor_read_ev_ctx *srec;
 
     itf = SENSOR_GET_ITF(sensor);
 
@@ -1306,47 +1385,49 @@ lis2dh12_sensor_set_trigger_thresh(struct sensor 
*sensor,
         goto err;
     }
 
-    memcpy(&low_thresh, &stt->stt_low_thresh, sizeof(low_thresh));
-    memcpy(&high_thresh, &stt->stt_high_thresh, sizeof(high_thresh));
-
-    rc = lis2dh12_get_full_scale(itf, &tmp);
+    rc = lis2dh12_disable_int2(itf);
     if (rc) {
         goto err;
     }
 
-    if (tmp == LIS2DH12_FS_2G) {
-        tmp = 16;
-    } else if (tmp == LIS2DH12_FS_4G) {
-        tmp = 32;
-    } else if (tmp == LIS2DH12_FS_8G) {
-        tmp = 62;
-    } else if (tmp == LIS2DH12_FS_16G) {
-        tmp = 186;
-    } else {
-        rc = SYS_EINVAL;
-        goto err;
-    }
+    hal_gpio_irq_release(itf->si_high_pin);
+
+    return 0;
+err:
+    return rc;
+}
+
+static int
+lis2dh12_set_low_thresh(struct sensor_itf *itf,
+                        sensor_data_t low_thresh,
+                        uint8_t fs,
+                        struct sensor_type_traits *stt)
+{
+    int16_t acc_mg;
+    uint8_t reg;
+    int rc;
 
+    rc = 0;
     if (low_thresh.sad->sad_x_is_valid ||
         low_thresh.sad->sad_y_is_valid ||
         low_thresh.sad->sad_z_is_valid) {
 
         if (low_thresh.sad->sad_x_is_valid) {
             lis2dh12_calc_acc_mg(low_thresh.sad->sad_x, &acc_mg);
-            reg = acc_mg/tmp;
+            reg = acc_mg/fs;
         }
 
         if (low_thresh.sad->sad_y_is_valid) {
             lis2dh12_calc_acc_mg(low_thresh.sad->sad_y, &acc_mg);
-            if (reg > acc_mg/tmp) {
-                reg = acc_mg/tmp;
+            if (reg > acc_mg/fs) {
+                reg = acc_mg/fs;
             }
         }
 
         if (low_thresh.sad->sad_z_is_valid) {
             lis2dh12_calc_acc_mg(low_thresh.sad->sad_z, &acc_mg);
-            if (reg > acc_mg/tmp) {
-                reg = acc_mg/tmp;
+            if (reg > acc_mg/fs) {
+                reg = acc_mg/fs;
             }
         }
 
@@ -1369,12 +1450,10 @@ lis2dh12_sensor_set_trigger_thresh(struct sensor 
*sensor,
 
         os_time_delay((OS_TICKS_PER_SEC * 100)/1000 + 1);
 
-        srec = malloc(sizeof(struct sensor_read_ev_ctx));
-        srec->srec_sensor = sensor;
-        srec->srec_type = type;
+        hal_gpio_irq_release(itf->si_low_pin);
 
-        hal_gpio_irq_init(itf->si_low_pin, lis2dh12_low_int1_irq_handler, srec,
-                          HAL_GPIO_TRIG_FALLING, HAL_GPIO_PULL_NONE);
+        rc = hal_gpio_irq_init(itf->si_low_pin, lis2dh12_int_irq_handler, stt,
+                               HAL_GPIO_TRIG_FALLING, HAL_GPIO_PULL_NONE);
         if (rc) {
             goto err;
         }
@@ -1396,29 +1475,44 @@ lis2dh12_sensor_set_trigger_thresh(struct sensor 
*sensor,
         if (rc) {
             goto err;
         }
-
     }
 
+err:
+    hal_gpio_irq_release(itf->si_low_pin);
+    return rc;
+}
+
+static int
+lis2dh12_set_high_thresh(struct sensor_itf *itf,
+                         sensor_data_t high_thresh,
+                         uint8_t fs,
+                         struct sensor_type_traits *stt)
+{
+    int16_t acc_mg;
+    uint8_t reg;
+    int rc;
+
+    rc = 0;
     if (high_thresh.sad->sad_x_is_valid ||
         high_thresh.sad->sad_y_is_valid ||
         high_thresh.sad->sad_z_is_valid) {
 
         if (high_thresh.sad->sad_x_is_valid) {
             lis2dh12_calc_acc_mg(high_thresh.sad->sad_x, &acc_mg);
-            reg = acc_mg/tmp;
+            reg = acc_mg/fs;
         }
 
         if (high_thresh.sad->sad_y_is_valid) {
             lis2dh12_calc_acc_mg(high_thresh.sad->sad_y, &acc_mg);
-            if (reg < acc_mg/tmp) {
-                reg = acc_mg/tmp;
+            if (reg < acc_mg/fs) {
+                reg = acc_mg/fs;
             }
         }
 
         if (high_thresh.sad->sad_z_is_valid) {
             lis2dh12_calc_acc_mg(high_thresh.sad->sad_z, &acc_mg);
-            if (reg < acc_mg/tmp) {
-                reg = acc_mg/tmp;
+            if (reg < acc_mg/fs) {
+                reg = acc_mg/fs;
             }
         }
 
@@ -1441,12 +1535,10 @@ lis2dh12_sensor_set_trigger_thresh(struct sensor 
*sensor,
 
         os_time_delay((OS_TICKS_PER_SEC * 100)/1000 + 1);
 
-        srec = malloc(sizeof(struct sensor_read_ev_ctx));
-        srec->srec_sensor = sensor;
-        srec->srec_type = type;
+        hal_gpio_irq_release(itf->si_high_pin);
 
-        hal_gpio_irq_init(itf->si_high_pin, lis2dh12_high_int2_irq_handler, 
srec,
-                          HAL_GPIO_TRIG_FALLING, HAL_GPIO_PULL_NONE);
+        rc = hal_gpio_irq_init(itf->si_high_pin, lis2dh12_int_irq_handler, stt,
+                               HAL_GPIO_TRIG_FALLING, HAL_GPIO_PULL_NONE);
         if (rc) {
             goto err;
         }
@@ -1468,7 +1560,74 @@ lis2dh12_sensor_set_trigger_thresh(struct sensor *sensor,
         }
     }
 
+err:
+    hal_gpio_irq_release(itf->si_high_pin);
+    return rc;
+}
+
+
+/**
+ * Set the trigger threshold values and enable interrupts
+ *
+ * @param ptr to sensor
+ * @param the Sensor type
+ * @param Sensor type traits
+ *
+ * @return 0 on success, non-zero on failure
+ */
+static int
+lis2dh12_sensor_set_trigger_thresh(struct sensor *sensor,
+                                   sensor_type_t type,
+                                   struct sensor_type_traits *stt)
+{
+    int rc;
+    uint8_t fs;
+    struct sensor_itf *itf;
+    sensor_data_t low_thresh;
+    sensor_data_t high_thresh;
+
+    itf = SENSOR_GET_ITF(sensor);
+
+    if (type != SENSOR_TYPE_ACCELEROMETER) {
+        rc = SYS_EINVAL;
+        goto err;
+    }
+
+    memcpy(&low_thresh, &stt->stt_low_thresh, sizeof(low_thresh));
+    memcpy(&high_thresh, &stt->stt_high_thresh, sizeof(high_thresh));
+
+    rc = lis2dh12_get_full_scale(itf, &fs);
+    if (rc) {
+        goto err;
+    }
+
+    if (fs == LIS2DH12_FS_2G) {
+        fs = 16;
+    } else if (fs == LIS2DH12_FS_4G) {
+        fs = 32;
+    } else if (fs == LIS2DH12_FS_8G) {
+        fs = 62;
+    } else if (fs == LIS2DH12_FS_16G) {
+        fs = 186;
+    } else {
+        rc = SYS_EINVAL;
+        goto err;
+    }
+
+    /* Set low threshold and enable interrupt */
+    rc = lis2dh12_set_low_thresh(itf, low_thresh, fs, stt);
+    if (rc) {
+        goto err;
+    }
+
+    /* Set high threshold and enable interrupt */
+    rc = lis2dh12_set_high_thresh(itf, high_thresh, fs, stt);
+    if (rc) {
+        goto err;
+    }
+
     return 0;
+
 err:
     return rc;
 }
diff --git a/hw/sensor/include/sensor/sensor.h 
b/hw/sensor/include/sensor/sensor.h
index 787af7e31..23a32c084 100644
--- a/hw/sensor/include/sensor/sensor.h
+++ b/hw/sensor/include/sensor/sensor.h
@@ -308,6 +308,8 @@ struct sensor_type_traits {
     oc_resource_t *stt_oic_res;
 #endif
 
+    struct sensor *stt_sensor;
+
     /* Next item in the sensor traits list.  The head of this list is
      * contained within the sensor object.
      */
@@ -321,13 +323,6 @@ struct sensor_notify_ev_ctx {
     sensor_event_type_t snec_evtype;
 };
 
-struct sensor_read_ev_ctx {
-    /* The sensor for which the ev cb should be called */
-    struct sensor *srec_sensor;
-    /* The sensor type */
-    sensor_type_t srec_type;
-};
-
 /**
  * Read a single value from a sensor, given a specific sensor type
  * (e.g. SENSOR_TYPE_PROXIMITY).
@@ -382,6 +377,17 @@ typedef int (*sensor_set_config_func_t)(struct sensor *, 
void *);
 typedef int (*sensor_set_trigger_thresh_t)(struct sensor *, sensor_type_t,
                                            struct sensor_type_traits *stt);
 
+/**
+ * Clear the high/low threshold values for a specific sensor for the sensor
+ * type.
+ *
+ * @param ptr to the sensor
+ * @param type of sensor
+ *
+ * @return 0 on success, non-zero error code on failure.
+ */
+typedef int (*sensor_clear_trigger_thresh_t)(struct sensor *, sensor_type_t);
+
 /**
  * Set the notification expectation for a targeted set of events for the
  * specific sensor. After this function returns successfully, the implementer
@@ -422,6 +428,8 @@ struct sensor_driver {
     sensor_get_config_func_t sd_get_config;
     sensor_set_config_func_t sd_set_config;
     sensor_set_trigger_thresh_t sd_set_trigger_thresh;
+    sensor_clear_trigger_thresh_t sd_clear_low_trigger_thresh;
+    sensor_clear_trigger_thresh_t sd_clear_high_trigger_thresh;
     sensor_set_notification_t sd_set_notification;
     sensor_unset_notification_t sd_unset_notification;
     sensor_handle_interrupt_t sd_handle_interrupt;
@@ -554,7 +562,7 @@ void sensor_unlock(struct sensor *);
 int sensor_register_listener(struct sensor *, struct sensor_listener *);
 
 /**
- * Un-register a sensor listener. This allows a calling application to unset
+ * Un-register a sensor listener. This allows a calling application to clear
  * callbacks for a given sensor object.
  *
  * @param The sensor object
@@ -824,6 +832,28 @@ sensor_get_type_traits_byname(char *, struct 
sensor_type_traits **,
 int
 sensor_set_thresh(char *, struct sensor_type_traits *);
 
+/**
+ * Clears the low threshold for a sensor
+ *
+ * @param name of the sensor
+ * @param sensor type
+ *
+ * @return 0 on success, non-zero on failure
+ */
+int
+sensor_clear_low_thresh(char *, sensor_type_t);
+
+/**
+ * Clears the high threshold for a sensor
+ *
+ * @param name of the sensor
+ * @param sensor type
+ *
+ * @return 0 on success, non-zero on failure
+ */
+int
+sensor_clear_high_thresh(char *, sensor_type_t);
+
 /**
  * Set the watermark thresholds for a sensor
  *
diff --git a/hw/sensor/src/sensor.c b/hw/sensor/src/sensor.c
index 79384d06d..6df4d0273 100644
--- a/hw/sensor/src/sensor.c
+++ b/hw/sensor/src/sensor.c
@@ -158,7 +158,7 @@ sensor_mgr_insert(struct sensor *sensor)
 }
 
 /**
- * Remove a sensor type trait. This allows a calling application to unset
+ * Remove a sensor type trait. This allows a calling application to clear
  * sensortype trait for a given sensor object.
  *
  * @param The sensor object
@@ -203,6 +203,13 @@ sensor_insert_type_trait(struct sensor *sensor, struct 
sensor_type_traits *stt)
     struct sensor_type_traits *cursor, *prev;
     int rc;
 
+    if (!sensor) {
+        rc = SYS_EINVAL;
+        goto err;
+    }
+
+    stt->stt_sensor = sensor;
+
     rc = sensor_lock(sensor);
     if (rc != 0) {
         goto err;
@@ -1085,7 +1092,7 @@ sensor_unregister_notifier(struct sensor *sensor,
 
     if (sensor->s_funcs->sd_unset_notification) {
         rc = sensor->s_funcs->sd_unset_notification(sensor,
-                                                
notifier->sn_sensor_event_type);
+                                               notifier->sn_sensor_event_type);
     }
 
 done:
@@ -1187,10 +1194,10 @@ static void
 sensor_read_ev_cb(struct os_event *ev)
 {
     int rc;
-    struct sensor_read_ev_ctx *srec;
+    struct sensor_type_traits *stt;
 
-    srec = ev->ev_arg;
-    rc = sensor_read(srec->srec_sensor, srec->srec_type, NULL, NULL,
+    stt = ev->ev_arg;
+    rc = sensor_read(stt->stt_sensor, stt->stt_sensor_type, NULL, NULL,
                      OS_TIMEOUT_NEVER);
     assert(rc == 0);
 }
@@ -1892,6 +1899,7 @@ sensor_set_thresh(char *devname, struct 
sensor_type_traits *stt)
         stt_tmp->stt_low_thresh = stt->stt_low_thresh;
         stt_tmp->stt_high_thresh = stt->stt_high_thresh;
         stt_tmp->stt_algo = stt->stt_algo;
+        stt_tmp->stt_sensor = sensor;
         sensor_unlock(sensor);
     } else {
         rc = SYS_EINVAL;
@@ -1900,15 +1908,105 @@ sensor_set_thresh(char *devname, struct 
sensor_type_traits *stt)
 
     sensor_set_trigger_cmp_algo(sensor, stt_tmp);
 
+    rc = sensor_lock(sensor);
+    if (rc) {
+        goto err;
+    }
+
     if (sensor->s_funcs->sd_set_trigger_thresh) {
         rc = sensor->s_funcs->sd_set_trigger_thresh(sensor,
                                                     stt_tmp->stt_sensor_type,
                                                     stt_tmp);
         if (rc) {
+            sensor_unlock(sensor);
+            goto err;
+        }
+    }
+
+    sensor_unlock(sensor);
+
+    return 0;
+err:
+    return rc;
+}
+
+/**
+ * Clear the low threshold for a sensor
+ *
+ * @param name of the sensor
+ * @param sensor type
+ *
+ * @return 0 on success, non-zero on failure
+ */
+int
+sensor_clear_low_thresh(char *devname, sensor_type_t type)
+{
+    struct sensor *sensor;
+    struct sensor_type_traits *stt_tmp;
+    int rc;
+
+    sensor = sensor_get_type_traits_byname(devname, &stt_tmp, type);
+    if (!sensor || !stt_tmp) {
+        rc = SYS_EINVAL;
+        goto err;
+    }
+
+    rc = sensor_lock(sensor);
+    if (rc) {
+        goto err;
+    }
+
+    if (sensor->s_funcs->sd_clear_low_trigger_thresh) {
+        rc = sensor->s_funcs->sd_clear_low_trigger_thresh(sensor, type);
+        if (rc) {
+            sensor_unlock(sensor);
+            goto err;
+        }
+    }
+
+    sensor_unlock(sensor);
+
+    return 0;
+err:
+    return rc;
+}
+
+/**
+ * Clear the high threshold for a sensor
+ *
+ * @param name of the sensor
+ * @param sensor type
+ *
+ * @return 0 on success, non-zero on failure
+ */
+int
+sensor_clear_high_thresh(char *devname, sensor_type_t type)
+{
+    struct sensor *sensor;
+    struct sensor_type_traits *stt_tmp;
+    int rc;
+
+    sensor = sensor_get_type_traits_byname(devname, &stt_tmp, type);
+    if (!sensor || !stt_tmp) {
+        rc = SYS_EINVAL;
+        goto err;
+    }
+
+    rc = sensor_lock(sensor);
+    if (rc) {
+        goto err;
+    }
+
+    if (sensor->s_funcs->sd_clear_high_trigger_thresh) {
+        rc = sensor->s_funcs->sd_clear_high_trigger_thresh(sensor, type);
+        if (rc) {
+            sensor_unlock(sensor);
             goto err;
         }
     }
 
+    sensor_unlock(sensor);
+
     return 0;
 err:
     return rc;


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> SensorAPI: Add sensor_unset_thresh() API and LIS2DH12 set threshold cb fix
> --------------------------------------------------------------------------
>
>                 Key: MYNEWT-881
>                 URL: https://issues.apache.org/jira/browse/MYNEWT-881
>             Project: Mynewt
>          Issue Type: Improvement
>      Security Level: Public(Viewable by anyone) 
>          Components: Drivers, Sensor Framework
>    Affects Versions: v1_3_0_rel
>         Environment: ruuvitag-revb2
>            Reporter: Vipul Rahane
>            Assignee: Vipul Rahane
>            Priority: Major
>              Labels: core
>             Fix For: v1_4_0_rel
>
>
> There are two issues for calling the lis2dh12_sensor_set_trigger_thresh() :
> 1. gpio irq was not being released before doing a re-initialization
>  2. srec was malloced and calling the function multiple times would have 
> malloced additional memory without freeing it.
> Solution:
>  1. Release the gpio irq
>  2. Remove the malloc and add a ptr for the sensor in each type trait so that 
> a sensor read could be performed in the irq handler.
> Also, sensor_unset_thresh() API was added to unset thresholds 
> triggers/interrupts.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to