proximity: Use a margin around near_level
This makes sure we don't trigger too often on small movements around the near level.
This commit is contained in:

committed by
Bastien Nocera

parent
11778ab603
commit
1497d965a3
@@ -18,6 +18,9 @@
|
||||
|
||||
#define PROXIMITY_NEAR_LEVEL "PROXIMITY_NEAR_LEVEL"
|
||||
|
||||
#define PROXIMITY_WATER_MARK_LOW 0.9
|
||||
#define PROXIMITY_WATER_MARK_HIGH 1.1
|
||||
|
||||
typedef struct DrvData {
|
||||
guint timeout_id;
|
||||
ReadingsUpdateFunc callback_func;
|
||||
@@ -25,6 +28,7 @@ typedef struct DrvData {
|
||||
GUdevDevice *dev;
|
||||
const char *name;
|
||||
gint near_level;
|
||||
gint last_level;
|
||||
} DrvData;
|
||||
|
||||
static DrvData *drv_data = NULL;
|
||||
@@ -54,11 +58,15 @@ poll_proximity (gpointer user_data)
|
||||
DrvData *data = user_data;
|
||||
ProximityReadings readings;
|
||||
gint prox;
|
||||
gdouble near_level = data->near_level;
|
||||
|
||||
/* g_udev_device_get_sysfs_attr_as_int does not update when there's no event */
|
||||
prox = sysfs_get_int (data->dev, "in_proximity_raw");
|
||||
readings.is_near = (prox > data->near_level) ? PROXIMITY_NEAR_TRUE : PROXIMITY_NEAR_FALSE;
|
||||
g_debug ("Proximity read from IIO on '%s': %d/%d, near: %d", data->name, prox, data->near_level, readings.is_near);
|
||||
/* Use a margin so we don't trigger too often */
|
||||
near_level *= (data->last_level > near_level) ? PROXIMITY_WATER_MARK_LOW : PROXIMITY_WATER_MARK_HIGH;
|
||||
readings.is_near = (prox > near_level) ? PROXIMITY_NEAR_TRUE : PROXIMITY_NEAR_FALSE;
|
||||
g_debug ("Proximity read from IIO on '%s': %d/%f, near: %d", data->name, prox, near_level, readings.is_near);
|
||||
data->last_level = prox;
|
||||
|
||||
drv_data->callback_func (&iio_poll_proximity, (gpointer) &readings, drv_data->user_data);
|
||||
|
||||
|
Reference in New Issue
Block a user