Skip to content
This repository was archived by the owner on Jul 23, 2020. It is now read-only.

Commit 4dda433

Browse files
committed
light: Add support for "illuminance0" light sensors
That would be used in the tsl2772 and lm3533 kernel drivers, as well as devices with multiple channels, such as combined proximity and light sensors, which use "illuminance0" as the channel name instead of "illuminance". Closes: #290
1 parent 43e5ff7 commit 4dda433

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

data/80-iio-sensor-proxy.rules

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ SUBSYSTEM=="iio", TEST=="in_accel_x_raw", TEST=="in_accel_y_raw", TEST=="in_acce
99
SUBSYSTEM=="iio", TEST=="scan_elements/in_accel_x_en", TEST=="scan_elements/in_accel_y_en", TEST=="scan_elements/in_accel_z_en", ENV{IIO_SENSOR_PROXY_TYPE}="iio-buffer-accel"
1010
SUBSYSTEM=="iio", TEST=="scan_elements/in_rot_from_north_magnetic_tilt_comp_en", ENV{IIO_SENSOR_PROXY_TYPE}="iio-buffer-compass"
1111
SUBSYSTEM=="iio", TEST=="in_illuminance_input", ENV{IIO_SENSOR_PROXY_TYPE}="iio-poll-als"
12+
SUBSYSTEM=="iio", TEST=="in_illuminance0_input", ENV{IIO_SENSOR_PROXY_TYPE}="iio-poll-als"
1213
SUBSYSTEM=="iio", TEST=="in_illuminance_raw", ENV{IIO_SENSOR_PROXY_TYPE}="iio-poll-als"
1314
SUBSYSTEM=="iio", TEST=="scan_elements/in_intensity_both_en", ENV{IIO_SENSOR_PROXY_TYPE}="iio-buffer-als"
1415
SUBSYSTEM=="input", ENV{ID_INPUT_ACCELEROMETER}=="1", ENV{IIO_SENSOR_PROXY_TYPE}="input-accel"

src/drv-iio-poll-light.c

+40-16
Original file line numberDiff line numberDiff line change
@@ -71,23 +71,45 @@ light_changed (gpointer user_data)
7171
return G_SOURCE_CONTINUE;
7272
}
7373

74+
static char *
75+
get_illuminance_channel_path (GUdevDevice *device,
76+
const char *suffix)
77+
{
78+
const char *channels[] = {
79+
"in_illuminance",
80+
"in_illuminance0"
81+
};
82+
char *path = NULL;
83+
guint i;
84+
85+
for (i = 0; i < G_N_ELEMENTS (channels); i++) {
86+
path = g_strdup_printf ("%s/%s_%s",
87+
g_udev_device_get_sysfs_path (device),
88+
channels[i],
89+
suffix);
90+
if (g_file_test (path, G_FILE_TEST_EXISTS))
91+
return path;
92+
g_clear_pointer (&path, g_free);
93+
}
94+
return NULL;
95+
}
96+
7497
static guint
7598
get_interval (GUdevDevice *device)
7699
{
77-
gdouble time;
100+
gdouble time = DEFAULT_POLL_TIME;
78101
char *path, *contents;
79102

80-
path = g_build_filename (g_udev_device_get_sysfs_path (device),
81-
"in_illuminance_integration_time",
82-
NULL);
103+
path = get_illuminance_channel_path (device, "integration_time");
104+
if (!path)
105+
goto out;
83106
if (g_file_get_contents (path, &contents, NULL, NULL)) {
84107
time = g_ascii_strtod (contents, NULL);
85108
g_free (contents);
86-
} else {
87-
time = DEFAULT_POLL_TIME;
88109
}
89110
g_free (path);
90111

112+
out:
91113
return (time * 1000);
92114
}
93115

@@ -124,17 +146,19 @@ iio_poll_light_open (GUdevDevice *device,
124146
drv_data->user_data = user_data;
125147

126148
drv_data->interval = get_interval (device);
127-
drv_data->input_path = g_build_filename (g_udev_device_get_sysfs_path (device),
128-
"in_illuminance_input",
129-
NULL);
130-
if (!g_file_test (drv_data->input_path, G_FILE_TEST_EXISTS)) {
131-
g_free (drv_data->input_path);
132-
drv_data->input_path = g_build_filename (g_udev_device_get_sysfs_path (device),
133-
"in_illuminance_raw",
134-
NULL);
135-
}
149+
drv_data->input_path = get_illuminance_channel_path (device, "input");
150+
if (!drv_data->input_path)
151+
drv_data->input_path = get_illuminance_channel_path (device, "raw");
152+
if (!drv_data->input_path)
153+
return FALSE;
136154

137-
drv_data->scale = g_udev_device_get_sysfs_attr_as_double (device, "in_illuminance_scale");
155+
if (g_str_has_prefix (drv_data->input_path, "in_illuminance0")) {
156+
drv_data->scale = g_udev_device_get_sysfs_attr_as_double (device,
157+
"in_illuminance0_scale");
158+
} else {
159+
drv_data->scale = g_udev_device_get_sysfs_attr_as_double (device,
160+
"in_illuminance_scale");
161+
}
138162
if (drv_data->scale == 0.0)
139163
drv_data->scale = 1.0;
140164

0 commit comments

Comments
 (0)