Skip to content

Commit f6f0386

Browse files
miggazElquezaescolar
authored andcommitted
samples: accel_polling: set sampling frequency when necessary
The accel_polling sample uses various sensor, but doesn't set a sampling rate. But some sensors (like st,lsm6dso) have a default sampling frequency of 0. So, depending on the sensor, the sample may not always work. There are two ways to fix this: either all drivers must set a valid sampling rate, or the sample shall at least try to set a value if there is none. We propose here the second approach, wich should allow the sample to work on more sensors out of the box. Signed-off-by: Miguel Gazquez <[email protected]>
1 parent c44c567 commit f6f0386

File tree

1 file changed

+24
-0
lines changed
  • samples/sensor/accel_polling/src

1 file changed

+24
-0
lines changed

samples/sensor/accel_polling/src/main.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,29 @@ static int print_accels(const struct device *dev)
5151
return 0;
5252
}
5353

54+
static int set_sampling_freq(const struct device *dev)
55+
{
56+
int ret;
57+
struct sensor_value odr;
58+
59+
ret = sensor_attr_get(dev, SENSOR_CHAN_ACCEL_XYZ, SENSOR_ATTR_SAMPLING_FREQUENCY, &odr);
60+
61+
/* If we don't get a frequency > 0, we set one */
62+
if (ret != 0 || (odr.val1 == 0 && odr.val2 == 0)) {
63+
odr.val1 = 100;
64+
odr.val2 = 0;
65+
66+
ret = sensor_attr_set(dev, SENSOR_CHAN_ACCEL_XYZ, SENSOR_ATTR_SAMPLING_FREQUENCY,
67+
&odr);
68+
69+
if (ret != 0) {
70+
printk("%s : failed to set sampling frequency\n", dev->name);
71+
}
72+
}
73+
74+
return 0;
75+
}
76+
5477
int main(void)
5578
{
5679
int ret;
@@ -60,6 +83,7 @@ int main(void)
6083
printk("sensor: device %s not ready.\n", sensors[i]->name);
6184
return 0;
6285
}
86+
set_sampling_freq(sensors[i]);
6387
}
6488

6589
#ifndef CONFIG_COVERAGE

0 commit comments

Comments
 (0)