main: Fix scale handling in IIO drivers

Our code expects 256 to corresponds to 1G acceleration.
IIO drivers give out a value that, when multiplied by the scale, gives
an acceleration in m/s².

Do the math to transform the IIO sensor's value to the scale we use
internally.
This commit is contained in:
Bastien Nocera
2014-11-20 00:17:10 +01:00
parent f1adea43f4
commit 126b758323
2 changed files with 10 additions and 2 deletions

View File

@@ -14,6 +14,10 @@
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
/* 1G (9.81m/s²) corresponds to "256"
* value x scale is in m/s² */
#define SCALE_TO_FF(scale) (scale * 256 / 9.81)
/** /**
* iio_channel_info - information about a given channel * iio_channel_info - information about a given channel
* @name: channel name * @name: channel name
@@ -507,7 +511,7 @@ process_scan_1 (char *data,
*ch_val = (int) val; *ch_val = (int) val;
*ch_present = TRUE; *ch_present = TRUE;
} }
*ch_val *= channels[k]->scale; *ch_val *= SCALE_TO_FF(channels[k]->scale);
break; break;
case 2: case 2:
case 8: case 8:

View File

@@ -14,6 +14,10 @@
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
/* 1G (9.81m/s²) corresponds to "256"
* value x scale is in m/s² */
#define SCALE_TO_FF(scale) (scale * 256 / 9.81)
typedef struct DrvData { typedef struct DrvData {
guint timeout_id; guint timeout_id;
ReadingsUpdateFunc callback_func; ReadingsUpdateFunc callback_func;
@@ -61,7 +65,7 @@ iio_poll_accel_open (GUdevDevice *device,
drv_data->callback_func = callback_func; drv_data->callback_func = callback_func;
drv_data->user_data = user_data; drv_data->user_data = user_data;
drv_data->scale = g_udev_device_get_sysfs_attr_as_double (device, "in_accel_scale"); drv_data->scale = SCALE_TO_FF(g_udev_device_get_sysfs_attr_as_double (device, "in_accel_scale"));
if (drv_data->scale == 0.0) if (drv_data->scale == 0.0)
drv_data->scale = 1.0; drv_data->scale = 1.0;