subsys/sensing/ shares sensors between clients. Its public API is
include/pbl/sensing/sensing.h. A sensor of the
sensor class has a single listener: the
sensing subsystem is that listener for every sensor it manages, and any
number of clients, each with its own type, rate and batch size, read the
sensor through it.
A client is a struct pbl_sensing_client, owned by the code using it:
sensor and type: what to read, e.g. ACCEL and PBL_SENSOR_ACCEL.
scale: the multiplier of the SI unit it reads frames in, a power of ten,
e.g. 1000 for mm/s².
frequency_mhz: the sampling frequency in millihertz, or 0 for no frames
(a client that only wants events).
batch: how many frames, at frequency_mhz, it accepts per update. 0 keeps
the sensor sampling at frequency_mhz but discards the frames.
events: the events it wants, a bit per enum pbl_sensor_event_type.
notify and event: its callbacks.
pbl_sensing_client_add() and pbl_sensing_client_remove() add and remove
it; pbl_sensing_client_set_frequency(), _set_batch() and _set_events()
change it later.
On every change the subsystem works out what the sensor must do:
Each type samples at the highest frequency its clients ask for.
The report latency is the shortest batch duration of the clients, batch
frames at frequency_mhz, rounded down to whole frames of the fastest
type. Every type sampling gets the same, and a type that stops leaves it
to the others, so a driver applying it to the whole device keeps to it.
With no client taking frames, the sensor reports no batches.
An event is enabled while any client wants it, and goes to every client that wants it.
pbl_sensing_suspend() stops the sensor, keeping the clients, and restores
what they want when resumed.
Batches are stored once per sensor and type, as the driver produced them,
with the channel descriptors and timestamps of the batch. The frames of a
batch whose driver finds them itself, such as tagged FIFO words, are packed
a stride apart, keeping only those of the type. Consecutive batches merge,
so a sensor reporting every frame does not pay for a record per frame.
The buffer holds at least CONFIG_SENSING_BUFFER_FRAMES frames, or twice
the FIFO depth, and is allocated while a client reads the type.
Each client has its own position in the buffer. pbl_sensing_read()
decodes frames to the SI unit times the client’s scale, in the device frame,
and decimates them to the client’s frequency with a phase accumulator: a 10 Hz client of a 25 Hz
sensor gets 2 frames in 5, evenly spread, and a 25 Hz client of a sensor
running at 26 Hz gets 25 frames in 26. A client that falls behind loses its
oldest frames, counted by pbl_sensing_lost(), and the others are not held
back.
A client calls pbl_sensing_arm() with a number of frames, and its
notify callback runs once a new batch brings that many. It is disarmed
during the call; it either reads the frames and arms again, or hands them
over and arms once they are consumed. Returning false keeps it armed, to try
again on the next batch.
pbl_sensing_latest() returns the newest frame without touching the
sensor, and pbl_sensing_count() counts the frames acquired.
Callbacks run on the high priority system work queue, from the sensor
listener, with the lock of the sensor’s clients held. All sensing functions
take that lock, and clients take it with pbl_sensing_lock() to serialize
their own state with it and may call back into the
subsystem from their callbacks, which must not block.
static bool prv_notify(struct pbl_sensing_client *client) {
int32_t mm_s2[5][3];
uint16_t n = pbl_sensing_read(client, &mm_s2[0][0], 5U, nullptr);
...
pbl_sensing_arm(client, 5U);
return true;
}
static struct pbl_sensing_client s_client = {
.sensor = ACCEL,
.type = PBL_SENSOR_ACCEL,
.scale = 1000,
.frequency_mhz = 25000,
.batch = 5,
.notify = prv_notify,
};
pbl_sensing_client_add(&s_client);
pbl_sensing_arm(&s_client, 5U);
Generated from PebbleOS 7416f78a1. This page is maintained in the pebbleos repository: docs/subsystems/sensing.md.