diff --git a/src/drv-iio-poll-proximity.c b/src/drv-iio-poll-proximity.c index d06d21e..a4ac050 100644 --- a/src/drv-iio-poll-proximity.c +++ b/src/drv-iio-poll-proximity.c @@ -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);