Shares sensors between clients.
More...
|
| int | pbl_sensing_client_add (struct pbl_sensing_client *client) |
| | Add a client.
|
| |
| void | pbl_sensing_client_remove (struct pbl_sensing_client *client) |
| | Remove a client.
|
| |
| void | pbl_sensing_client_set_frequency (struct pbl_sensing_client *client, uint32_t frequency_mhz) |
| | Change the sampling frequency of a client.
|
| |
| void | pbl_sensing_client_set_batch (struct pbl_sensing_client *client, uint16_t batch) |
| | Change the batch size of a client.
|
| |
| void | pbl_sensing_client_set_events (struct pbl_sensing_client *client, uint8_t events) |
| | Change the events a client wants.
|
| |
| void | pbl_sensing_lock (const struct pbl_sensor *sensor) |
| | Take the lock of a sensor's clients.
|
| |
| void | pbl_sensing_unlock (const struct pbl_sensor *sensor) |
| | Release the lock of a sensor's clients.
|
| |
| uint32_t | pbl_sensing_sampling_frequency (const struct pbl_sensing_client *client) |
| | Get the frequency the sensor samples a client's type at.
|
| |
| void | pbl_sensing_arm (struct pbl_sensing_client *client, uint16_t frames) |
| | Notify the client once a number of frames are waiting.
|
| |
| uint32_t | pbl_sensing_available (struct pbl_sensing_client *client) |
| | Count the frames waiting for a client.
|
| |
| uint16_t | pbl_sensing_read (struct pbl_sensing_client *client, int32_t *out, uint16_t max, uint64_t *timestamp_us) |
| | Read the oldest frames waiting for a client.
|
| |
| void | pbl_sensing_discard (struct pbl_sensing_client *client) |
| | Discard the frames waiting for a client.
|
| |
| uint32_t | pbl_sensing_lost (struct pbl_sensing_client *client) |
| | Get and clear the number of frames a client lost by falling behind.
|
| |
| int | pbl_sensing_latest (const struct pbl_sensor *sensor, enum pbl_sensor_type type, uint32_t scale, int32_t *val, uint64_t *timestamp_us) |
| | Get the latest frame of a type, without accessing the sensor.
|
| |
| uint32_t | pbl_sensing_count (const struct pbl_sensor *sensor, enum pbl_sensor_type type) |
| | Count the frames of a type acquired since the first client read them.
|
| |
| void | pbl_sensing_suspend (const struct pbl_sensor *sensor, bool suspend) |
| | Suspend or resume a sensor.
|
| |
Shares sensors between clients.
A sensor of the sensor class has a single listener. The sensing subsystem is that listener for every sensor it manages, and shares the sensor between any number of clients, each asking for a channel type at its own sampling frequency and batch size:
- The sensor samples at the highest frequency any client asks for, and queues frames for as long as the client with the shortest batch duration allows.
- Batches are stored once, as the driver produced them, in a buffer per sensor and type. Each client has its own position in it, and reads frames decoded to the SI unit of the type times its scale, decimated to its own frequency with a phase accumulator, taking the first frame at or after each due time, so rates that do not divide the sampling rate are spread evenly. A client that falls behind loses its oldest frames without holding back the others.
- A client arms a threshold with pbl_sensing_arm() and is notified once that many frames are waiting, after which it is disarmed until armed again.
- Event detection, such as shakes, is enabled while any client wants the event, and events go to every client that wants them.
Clients' callbacks run on the high priority system work queue, from the sensor listener, with the lock of the sensor's clients held, as every sensing function takes it. They must not block, and may call back into the subsystem. Clients take the lock with pbl_sensing_lock() to serialize their own state with their callbacks.
int32_t mm_s2[5][3];
uint64_t t_us;
...
return true;
}
.scale = 1000,
.frequency_mhz = 25000,
.batch = 5,
.notify = prv_notify,
};
const struct pbl_sensor *const ACCEL
The board's accelerometer, which may also measure angular rate.
@ PBL_SENSOR_ACCEL
Acceleration, in m/s².
Definition sensor.h:118
void pbl_sensing_arm(struct pbl_sensing_client *client, uint16_t frames)
Notify the client once a number of frames are waiting.
int pbl_sensing_client_add(struct pbl_sensing_client *client)
Add a client.
uint16_t pbl_sensing_read(struct pbl_sensing_client *client, int32_t *out, uint16_t max, uint64_t *timestamp_us)
Read the oldest frames waiting for a client.
A client of a sensor.
Definition sensing.h:70
const struct pbl_sensor * sensor
Sensor.
Definition sensing.h:72
◆ pbl_sensing_arm()
Notify the client once a number of frames are waiting.
Does not notify for frames already waiting, only once a new batch brings the count up to frames.
- Parameters
-
| client | Client. |
| frames | Frames, 0 to disarm. |
◆ pbl_sensing_available()
Count the frames waiting for a client.
- Parameters
-
- Returns
- Frames, after decimation.
◆ pbl_sensing_client_add()
Add a client.
The client reads frames acquired from now on. The sensor is reconfigured for it, which may report queued batches to the other clients first.
- Parameters
-
| client | Client, with the fields up to user_data set, kept until removed. |
- Returns
- 0, -EINVAL for an invalid client or scale, -ENOTSUP if the sensor does not measure the type or detect the events, or -ENOMEM.
◆ pbl_sensing_client_remove()
Remove a client.
Once it returns, the client's callbacks are not called anymore. A client that was not added, or failed to, is ignored, as by the other functions taking a client.
- Parameters
-
◆ pbl_sensing_client_set_batch()
Change the batch size of a client.
- Parameters
-
| client | Client. |
| batch | Frames per update, 0 to discard the frames. |
◆ pbl_sensing_client_set_events()
Change the events a client wants.
- Parameters
-
| client | Client. |
| events | Events, a bit per enum pbl_sensor_event_type. |
◆ pbl_sensing_client_set_frequency()
| void pbl_sensing_client_set_frequency |
( |
struct pbl_sensing_client * |
client, |
|
|
uint32_t |
frequency_mhz |
|
) |
| |
Change the sampling frequency of a client.
- Parameters
-
| client | Client. |
| frequency_mhz | Sampling frequency in millihertz, 0 for no frames. |
◆ pbl_sensing_count()
Count the frames of a type acquired since the first client read them.
- Parameters
-
- Returns
- Frames, wrapping around.
◆ pbl_sensing_discard()
Discard the frames waiting for a client.
- Parameters
-
◆ pbl_sensing_latest()
| int pbl_sensing_latest |
( |
const struct pbl_sensor * |
sensor, |
|
|
enum pbl_sensor_type |
type, |
|
|
uint32_t |
scale, |
|
|
int32_t * |
val, |
|
|
uint64_t * |
timestamp_us |
|
) |
| |
Get the latest frame of a type, without accessing the sensor.
- Parameters
-
| sensor | Sensor. |
| type | Type. |
| scale | Multiplier of the SI unit, a power of ten up to 10^9. |
| [out] | val | Values in the SI unit times scale, in the device frame for vectors. |
| [out] | timestamp_us | Time of the frame in microseconds of uptime, or NULL. |
- Returns
- 0, -EINVAL for an invalid
scale, or -ENODATA if no client is reading frames of type, or none was acquired yet.
◆ pbl_sensing_lock()
| void pbl_sensing_lock |
( |
const struct pbl_sensor * |
sensor | ) |
|
Take the lock of a sensor's clients.
Every sensing function, and the callbacks of the sensor's clients, run with it held. The owner may take it again.
- Parameters
-
◆ pbl_sensing_lost()
Get and clear the number of frames a client lost by falling behind.
- Parameters
-
- Returns
- Frames lost, before decimation, since the last call.
◆ pbl_sensing_read()
| uint16_t pbl_sensing_read |
( |
struct pbl_sensing_client * |
client, |
|
|
int32_t * |
out, |
|
|
uint16_t |
max, |
|
|
uint64_t * |
timestamp_us |
|
) |
| |
Read the oldest frames waiting for a client.
- Parameters
-
| client | Client. |
| [out] | out | Values in the SI unit times the client's scale, pbl_sensor_num_values() per frame, in the device frame for vectors. |
| max | Maximum number of frames. |
| [out] | timestamp_us | Time of the first frame read in microseconds of uptime, or NULL. |
- Returns
- Number of frames read.
◆ pbl_sensing_sampling_frequency()
Get the frequency the sensor samples a client's type at.
- Parameters
-
- Returns
- Frequency in millihertz, 0 if not sampling.
◆ pbl_sensing_suspend()
| void pbl_sensing_suspend |
( |
const struct pbl_sensor * |
sensor, |
|
|
bool |
suspend |
|
) |
| |
Suspend or resume a sensor.
While suspended, the sensor neither samples nor detects events, and clients are kept but get nothing. Resuming restores what the clients want.
- Parameters
-
| sensor | Sensor. |
| suspend | True to suspend, false to resume. |
◆ pbl_sensing_unlock()
| void pbl_sensing_unlock |
( |
const struct pbl_sensor * |
sensor | ) |
|
Release the lock of a sensor's clients.
- Parameters
-