PebbleOS v4.48.0-75-g7416f78a1
Loading...
Searching...
No Matches
Sensing

Shares sensors between clients. More...

Data Structures

struct  pbl_sensing_client
 A client of a sensor. More...
 

Functions

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.
 

Detailed Description

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:

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.

static bool prv_notify(struct pbl_sensing_client *client) {
int32_t mm_s2[5][3];
uint64_t t_us;
int n = pbl_sensing_read(client, &mm_s2[0][0], 5U, &t_us);
...
pbl_sensing_arm(client, 5U);
return true;
}
static struct pbl_sensing_client s_client = {
.scale = 1000,
.frequency_mhz = 25000,
.batch = 5,
.notify = prv_notify,
};
pbl_sensing_arm(&s_client, 5U);
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

Function Documentation

◆ pbl_sensing_arm()

void pbl_sensing_arm ( struct pbl_sensing_client *  client,
uint16_t  frames 
)

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
clientClient.
framesFrames, 0 to disarm.

◆ pbl_sensing_available()

uint32_t pbl_sensing_available ( struct pbl_sensing_client *  client)

Count the frames waiting for a client.

Parameters
clientClient.
Returns
Frames, after decimation.

◆ pbl_sensing_client_add()

int pbl_sensing_client_add ( struct pbl_sensing_client *  client)

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
clientClient, 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()

void pbl_sensing_client_remove ( struct pbl_sensing_client *  client)

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
clientClient.

◆ pbl_sensing_client_set_batch()

void pbl_sensing_client_set_batch ( struct pbl_sensing_client *  client,
uint16_t  batch 
)

Change the batch size of a client.

Parameters
clientClient.
batchFrames per update, 0 to discard the frames.

◆ pbl_sensing_client_set_events()

void pbl_sensing_client_set_events ( struct pbl_sensing_client *  client,
uint8_t  events 
)

Change the events a client wants.

Parameters
clientClient.
eventsEvents, 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
clientClient.
frequency_mhzSampling frequency in millihertz, 0 for no frames.

◆ pbl_sensing_count()

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.

Parameters
sensorSensor.
typeType.
Returns
Frames, wrapping around.

◆ pbl_sensing_discard()

void pbl_sensing_discard ( struct pbl_sensing_client *  client)

Discard the frames waiting for a client.

Parameters
clientClient.

◆ 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
sensorSensor.
typeType.
scaleMultiplier of the SI unit, a power of ten up to 10^9.
[out]valValues in the SI unit times scale, in the device frame for vectors.
[out]timestamp_usTime 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
sensorSensor.

◆ pbl_sensing_lost()

uint32_t pbl_sensing_lost ( struct pbl_sensing_client *  client)

Get and clear the number of frames a client lost by falling behind.

Parameters
clientClient.
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
clientClient.
[out]outValues in the SI unit times the client's scale, pbl_sensor_num_values() per frame, in the device frame for vectors.
maxMaximum number of frames.
[out]timestamp_usTime of the first frame read in microseconds of uptime, or NULL.
Returns
Number of frames read.

◆ pbl_sensing_sampling_frequency()

uint32_t pbl_sensing_sampling_frequency ( const struct pbl_sensing_client *  client)

Get the frequency the sensor samples a client's type at.

Parameters
clientClient.
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
sensorSensor.
suspendTrue 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
sensorSensor.