mount-matrix: move setup to a helper function

The logic to parse the mount matrix from udev is duplicated in 3
places. Move it to a common helper function.
This commit is contained in:
Daniel Drake
2019-03-21 15:08:17 +08:00
committed by hadess
parent 43fb8be70b
commit 83875cc4e9
5 changed files with 22 additions and 27 deletions

View File

@@ -24,6 +24,22 @@ static AccelVec3 id_matrix[3] = {
static char axis_names[] = "xyz"; static char axis_names[] = "xyz";
AccelVec3 *
setup_mount_matrix (GUdevDevice *device)
{
AccelVec3 *ret = NULL;
const char *mount_matrix;
mount_matrix = g_udev_device_get_property (device, "ACCEL_MOUNT_MATRIX");
if (!parse_mount_matrix (mount_matrix, &ret)) {
g_warning ("Invalid mount-matrix ('%s'), falling back to identity",
mount_matrix);
parse_mount_matrix (NULL, &ret);
}
return ret;
}
gboolean gboolean
parse_mount_matrix (const char *mtx, parse_mount_matrix (const char *mtx,
AccelVec3 *vecs[3]) AccelVec3 *vecs[3])

View File

@@ -8,6 +8,7 @@
*/ */
#include <glib.h> #include <glib.h>
#include <gudev/gudev.h>
typedef struct { typedef struct {
float x; float x;
@@ -15,6 +16,8 @@ typedef struct {
float z; float z;
} AccelVec3; } AccelVec3;
AccelVec3 *setup_mount_matrix (GUdevDevice *device);
gboolean parse_mount_matrix (const char *mtx, gboolean parse_mount_matrix (const char *mtx,
AccelVec3 *vecs[3]); AccelVec3 *vecs[3]);

View File

@@ -200,7 +200,6 @@ iio_buffer_accel_open (GUdevDevice *device,
gpointer user_data) gpointer user_data)
{ {
char *trigger_name; char *trigger_name;
const char *mount_matrix;
drv_data = g_new0 (DrvData, 1); drv_data = g_new0 (DrvData, 1);
@@ -218,13 +217,7 @@ iio_buffer_accel_open (GUdevDevice *device,
return FALSE; return FALSE;
} }
mount_matrix = g_udev_device_get_property (device, "ACCEL_MOUNT_MATRIX"); drv_data->mount_matrix = setup_mount_matrix (device);
if (!parse_mount_matrix (mount_matrix, &drv_data->mount_matrix)) {
g_warning ("Invalid mount-matrix ('%s'), falling back to identity",
mount_matrix);
parse_mount_matrix (NULL, &drv_data->mount_matrix);
}
drv_data->dev = g_object_ref (device); drv_data->dev = g_object_ref (device);
drv_data->dev_path = g_udev_device_get_device_file (device); drv_data->dev_path = g_udev_device_get_device_file (device);
drv_data->name = g_udev_device_get_property (device, "NAME"); drv_data->name = g_udev_device_get_property (device, "NAME");

View File

@@ -118,21 +118,12 @@ iio_poll_accel_open (GUdevDevice *device,
ReadingsUpdateFunc callback_func, ReadingsUpdateFunc callback_func,
gpointer user_data) gpointer user_data)
{ {
const char *mount_matrix;
iio_fixup_sampling_frequency (device); iio_fixup_sampling_frequency (device);
drv_data = g_new0 (DrvData, 1); drv_data = g_new0 (DrvData, 1);
drv_data->dev = g_object_ref (device); drv_data->dev = g_object_ref (device);
drv_data->name = g_udev_device_get_sysfs_attr (device, "name"); drv_data->name = g_udev_device_get_sysfs_attr (device, "name");
drv_data->mount_matrix = setup_mount_matrix (device);
mount_matrix = g_udev_device_get_property (device, "ACCEL_MOUNT_MATRIX");
if (!parse_mount_matrix (mount_matrix, &drv_data->mount_matrix)) {
g_warning ("Invalid mount-matrix ('%s'), falling back to identity",
mount_matrix);
parse_mount_matrix (NULL, &drv_data->mount_matrix);
}
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 = g_udev_device_get_sysfs_attr_as_double (device, "in_accel_scale");

View File

@@ -192,7 +192,6 @@ input_accel_open (GUdevDevice *device,
gpointer user_data) gpointer user_data)
{ {
const gchar * const subsystems[] = { "input", NULL }; const gchar * const subsystems[] = { "input", NULL };
const char *mount_matrix;
drv_data = g_new0 (DrvData, 1); drv_data = g_new0 (DrvData, 1);
drv_data->dev = g_object_ref (device); drv_data->dev = g_object_ref (device);
@@ -202,14 +201,7 @@ input_accel_open (GUdevDevice *device,
if (!drv_data->name) if (!drv_data->name)
drv_data->name = g_udev_device_get_property (device, "ID_MODEL"); drv_data->name = g_udev_device_get_property (device, "ID_MODEL");
drv_data->client = g_udev_client_new (subsystems); drv_data->client = g_udev_client_new (subsystems);
drv_data->mount_matrix = setup_mount_matrix (device);
mount_matrix = g_udev_device_get_property (device, "ACCEL_MOUNT_MATRIX");
if (!parse_mount_matrix (mount_matrix, &drv_data->mount_matrix)) {
g_warning ("Invalid mount-matrix ('%s'), falling back to identity",
mount_matrix);
parse_mount_matrix (NULL, &drv_data->mount_matrix);
}
drv_data->callback_func = callback_func; drv_data->callback_func = callback_func;
drv_data->user_data = user_data; drv_data->user_data = user_data;