This sample walks every device of the sensor class with pbl_sensor_next(), and reads
each type it measures once with pbl_sensor_read(). PBL_SENSOR_SPEC() of
just the type asks for the whole channel: the X, Y and Z of a vector, in the
device frame, or the value of a scalar. Values come in SI units times a
multiplier the sample picks so that each prints with 3 decimals: mm/s² for
m/s², nT for µT, Pa for kPa.
It enables CONFIG_SENSOR and no role, so it gets the driver of every
sensor the board’s devicetree enables, whether the firmware uses it or not,
and runs on boards with any mix of them. On the emulator it reads the
simulated accelerometer, die temperature sensor and magnetometer, the last
enabled in its boards/qemu_emery.conf: the emulator boards leave it off,
having no magnetometer.
It then turns the console prompt on, with the sensor command of the
shell, CONFIG_SENSOR_SHELL, to drive the
rest of the class by hand: attributes and the values they take, events, a
flush, and the batches and events of a sensor printed as they come.
> sensor list
0: lsm6dso +shake
accelerometer x 0
accelerometer y 0
accelerometer z 0
> sensor avail 0 accelerometer sampling-frequency
12500 26000 52000 104000 208000
> sensor stream 0 on
> sensor attr 0 accelerometer sampling-frequency 26000
26000
> sensor attr 0 accelerometer report-latency 500000
499993
accelerometer: 13 frames 38461 us apart, at 5046966 us: x=1.710 y=3.998 z=-8.867 m/s2
accelerometer: 13 frames 38483 us apart, at 5548095 us: x=1.721 y=4.105 z=-8.816 m/s2
> sensor event 0 shake on
shake: +z at 6714569 us
> sensor attr 0 accelerometer sampling-frequency 0
0
> sensor stream 0 off
pbl samples samples/drivers/sensor
every board
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <pbl/console/console.h>
#include <pbl/drivers/sensor.h>
static const char *const s_types[PBL_SENSOR_TYPE_COUNT] = {
[PBL_SENSOR_ACCEL] = "accelerometer",
[PBL_SENSOR_ANGL_VEL] = "gyroscope",
[PBL_SENSOR_MAGN] = "magnetometer",
[PBL_SENSOR_TEMP] = "thermometer",
[PBL_SENSOR_PRESSURE] = "barometer"
};
/* Values are asked for at 1000 times the unit printed, which gets 3 decimals. */
static const struct {
const char *unit;
uint32_t scale;
} s_units[PBL_SENSOR_TYPE_COUNT] = {
[PBL_SENSOR_ACCEL] = {"m/s2", 1000U},
[PBL_SENSOR_ANGL_VEL] = {"rad/s", 1000U},
[PBL_SENSOR_MAGN] = {"uT", 1000000000U},
[PBL_SENSOR_TEMP] = {"C", 1000U},
[PBL_SENSOR_PRESSURE] = {"kPa", 1U}
};
static const char *prv_milli(char *buf, size_t size, int32_t val) {
(void)snprintf(buf, size, "%s%u.%03u", val < 0 ? "-" : "", (unsigned int)(llabs(val) / 1000),
(unsigned int)(llabs(val) % 1000));
return buf;
}
static void prv_print_values(const char *name, int type, const int32_t *val) {
char x[16], y[16], z[16];
if (pbl_sensor_num_values(PBL_SENSOR_SPEC(type)) == 3U) {
printf("%s: %s: x=%s y=%s z=%s %s\n", name, s_types[type], prv_milli(x, sizeof(x), val[0]),
prv_milli(y, sizeof(y), val[1]), prv_milli(z, sizeof(z), val[2]), s_units[type].unit);
} else {
printf("%s: %s: %s %s\n", name, s_types[type], prv_milli(x, sizeof(x), val[0]),
s_units[type].unit);
}
}
static void prv_read(const struct pbl_sensor *sensor, enum pbl_sensor_type type) {
const char *name = pbl_sensor_get_name(sensor);
int32_t val[3];
int ret;
/* No modifier: the whole vector of a vector type, the plain channel of a scalar one. */
ret = pbl_sensor_read(sensor, PBL_SENSOR_SPEC(type), s_units[type].scale, val, nullptr);
/* A saturated value is still a value. */
if (ret != 0 && ret != -ERANGE) {
fprintf(stderr, "%s: %s: read failed (%d)\n", name, s_types[type], ret);
return;
}
prv_print_values(name, type, val);
}
int main(void) {
const struct pbl_sensor *sensor = nullptr;
unsigned int count = 0U;
/* Every sensor the board describes, whether a role points at it or not. */
while ((sensor = pbl_sensor_next(sensor)) != nullptr) {
count++;
if (!pbl_sensor_is_ready(sensor)) {
printf("%s: not ready\n", pbl_sensor_get_name(sensor));
continue;
}
for (int t = 0; t < PBL_SENSOR_TYPE_COUNT; t++) {
if (pbl_sensor_has_type(sensor, t)) {
prv_read(sensor, t);
}
}
}
printf("%u sensors\n", count);
/* The sensor shell command does the rest by hand. */
pbl_console_enable_prompt();
printf("Sample done\n");
return 0;
}
\w+: (accelerometer|gyroscope|magnetometer|thermometer|barometer):
\d+ sensors
Sample done
Generated from PebbleOS d278268fe. This page is maintained in the pebbleos repository: docs/samples/drivers/sensor.md.