Accelerometer class.
More...
|
| | Simulator |
| | Simulated accelerometer, replaying the samples and events it is fed.
|
| |
|
| #define | PBL_ACCEL_STATE_DEFINE(sym) |
| | Define the class state of accelerometer sym, for the PBL_*_DEFINE() macro of a driver.
|
| |
| #define | PBL_ACCEL_INIT(sym, _name, _parent, _ops) |
| | Initializer for the struct pbl_accel of accelerometer sym.
|
| |
|
| int | pbl_accel_dev_init (const struct pbl_device *dev) |
| | Device init of every accelerometer: calls the driver's init with the lock held.
|
| |
| void | pbl_accel_lock (const struct pbl_accel *accel) |
| | Take the device lock.
|
| |
| void | pbl_accel_unlock (const struct pbl_accel *accel) |
| | Release the device lock.
|
| |
| int | pbl_accel_set_listener (const struct pbl_accel *accel, const struct pbl_accel_listener *listener) |
| | Set the listener of samples and events.
|
| |
| int | pbl_accel_set_rotated (const struct pbl_accel *accel, bool rotated) |
| | Set whether the axes are rotated by 180 degrees.
|
| |
| int | pbl_accel_set_sampling_interval (const struct pbl_accel *accel, uint32_t interval_us, uint32_t *actual_us) |
| | Set the sampling interval.
|
| |
| uint32_t | pbl_accel_get_sampling_interval (const struct pbl_accel *accel) |
| | Get the sampling interval.
|
| |
| int | pbl_accel_set_num_samples (const struct pbl_accel *accel, uint32_t num_samples) |
| | Set the maximum number of samples the driver may batch.
|
| |
| uint32_t | pbl_accel_get_max_num_samples (const struct pbl_accel *accel) |
| | Get the maximum number of samples the driver can batch.
|
| |
| int | pbl_accel_peek (const struct pbl_accel *accel, struct pbl_accel_sample *sample) |
| | Read the most recent sample.
|
| |
| int | pbl_accel_set_shake_detection (const struct pbl_accel *accel, bool on) |
| | Enable or disable shake detection.
|
| |
| bool | pbl_accel_get_shake_detection (const struct pbl_accel *accel) |
| | Check whether shake detection is enabled.
|
| |
| int | pbl_accel_set_double_tap_detection (const struct pbl_accel *accel, bool on) |
| | Enable or disable double tap detection.
|
| |
| bool | pbl_accel_get_double_tap_detection (const struct pbl_accel *accel) |
| | Check whether double tap detection is enabled.
|
| |
| int | pbl_accel_set_shake_sensitivity_high (const struct pbl_accel *accel, bool high) |
| | Select high or normal shake sensitivity.
|
| |
| int | pbl_accel_set_shake_sensitivity_percent (const struct pbl_accel *accel, uint8_t percent) |
| | Set the normal shake sensitivity.
|
| |
| void | pbl_accel_report_samples (const struct pbl_accel *accel, const AccelRawBatch *batch) |
| | Report a batch of samples to the listener.
|
| |
| void | pbl_accel_report_sample (const struct pbl_accel *accel, const struct pbl_accel_sample *sample) |
| | Report a single sample to the listener, as a one-sample batch.
|
| |
| void | pbl_accel_report_event (const struct pbl_accel *accel, const struct pbl_accel_event *event) |
| | Report an event to the listener.
|
| |
Accelerometer class.
An accelerometer is a struct pbl_accel device. Drivers are a thin layer over the hardware: they keep no sample buffers and know nothing about their consumers. They report samples, shakes and double taps to the class, which hands them to the listener a consumer registered with pbl_accel_set_listener(), the accelerometer service in practice. Hardware state such as FIFO modes is hidden: the consumer states its requirements (sampling interval, batch size) and the driver picks the hardware configuration.
Every call into the driver, and every report out of it, runs with the device lock held, a recursive mutex. The listener runs with it held too, so it may call back into the device, and a consumer that takes the lock around its own state is serialized with the driver without a second lock to order against it.
void *user_data) {
...
}
uint32_t interval_us;
An accelerometer.
Definition accel.h:168
Batch of raw samples in a driver-owned buffer.
Definition accel.h:64
int pbl_accel_set_sampling_interval(const struct pbl_accel *accel, uint32_t interval_us, uint32_t *actual_us)
Set the sampling interval.
int pbl_accel_set_num_samples(const struct pbl_accel *accel, uint32_t num_samples)
Set the maximum number of samples the driver may batch.
const struct pbl_accel *const ACCEL
The board's accelerometer.
uint32_t pbl_accel_get_max_num_samples(const struct pbl_accel *accel)
Get the maximum number of samples the driver can batch.
int pbl_accel_set_listener(const struct pbl_accel *accel, const struct pbl_accel_listener *listener)
Set the listener of samples and events.
#define PBL_MIN(a, b)
Get the smaller of two values.
Definition math.h:36
Consumer of an accelerometer's samples and events.
Definition accel.h:114
void(* samples)(const struct pbl_accel *accel, const AccelRawBatch *batch, void *user_data)
Optional.
Definition accel.h:116
◆ pbl_accel_sample
| Data Fields |
|
uint64_t |
timestamp_us |
Sample time in microseconds since the epoch, with no guaranteed precision. |
|
int16_t |
x |
Acceleration along the x axis, in milli-g. |
|
int16_t |
y |
Acceleration along the y axis, in milli-g. |
|
int16_t |
z |
Acceleration along the z axis, in milli-g. |
◆ AccelRawBatch
Batch of raw samples in a driver-owned buffer.
Handed to the listener so it can scale and copy the samples in one pass instead of taking a callback per sample.
| Data Fields |
|
struct AccelRawBatch.axis |
axis[3] |
Per-axis source, indexed by IMUCoordinateAxis; encodes axis remapping and direction. |
|
const uint8_t * |
data |
First raw sample, past any per-sample header. |
|
uint64_t |
first_timestamp_us |
Timestamp of the first sample, in microseconds since the epoch. |
|
uint16_t |
num_samples |
Number of samples in the buffer. |
|
uint32_t |
sampling_interval_us |
Interval between samples, in microseconds. |
|
int32_t |
scale_den |
Raw-count to milli-g denominator. |
|
int32_t |
scale_num |
Raw-count to milli-g numerator: mg = raw * scale_num / scale_den. |
|
uint8_t |
stride |
Bytes between consecutive samples, which lets the service skip e.g. FIFO tags.
|
◆ pbl_accel_event
◆ pbl_accel_state
| Data Fields |
|
bool |
double_tap_detection |
Double tap detection enabled. |
|
const struct pbl_accel_listener * |
listener |
Listener, or NULL. |
|
struct pbl_mutex |
lock |
Device lock. |
|
bool |
shake_detection |
Shake detection enabled. |
◆ pbl_accel
◆ AccelRawBatch.axis
| struct AccelRawBatch.axis |
Per-axis source, indexed by IMUCoordinateAxis; encodes axis remapping and direction.
| Data Fields |
|
uint8_t |
offset |
Byte offset of the little-endian int16 value within a sample. |
|
int8_t |
sign |
Sign applied to the value, +1 or -1. |
◆ PBL_ACCEL_INIT
| #define PBL_ACCEL_INIT |
( |
|
sym, |
|
|
|
_name, |
|
|
|
_parent, |
|
|
|
_ops |
|
) |
| |
Value: { \
.ops = (_ops), \
.state = &sym##_accel_state, \
}
int pbl_accel_dev_init(const struct pbl_device *dev)
Device init of every accelerometer: calls the driver's init with the lock held.
#define PBL_DEVICE_INIT_CLASS(sym, _name, _init, _parent, _cls)
Initializer for the struct pbl_device of device instance sym, in class _cls.
Definition device.h:153
Initializer for the struct pbl_accel of accelerometer sym.
- Parameters
-
| sym | Symbol of the accelerometer instance, with its state defined by PBL_ACCEL_STATE_DEFINE(). |
| _name | Name. |
| _parent | Parent device, such as the I2C bus, or NULL. |
| _ops | Driver operations. |
◆ PBL_ACCEL_STATE_DEFINE
| #define PBL_ACCEL_STATE_DEFINE |
( |
|
sym | ) |
|
Value:
struct pbl_mutex lock
Device lock.
Definition accel.h:158
Class runtime state.
Definition accel.h:156
#define PBL_DEVICE_STATE_DEFINE(sym)
Define the runtime state of device instance sym.
Definition device.h:132
#define PBL_MUTEX_INITIALIZER
Static initializer for an unlocked mutex.
Definition mutex.h:50
Define the class state of accelerometer sym, for the PBL_*_DEFINE() macro of a driver.
- Parameters
-
| sym | Symbol of the accelerometer instance. |
◆ pbl_accel_event_type
Accelerometer event type.
| Enumerator |
|---|
| PBL_ACCEL_EVENT_SHAKE | Shake, a motion above the shake threshold.
|
| PBL_ACCEL_EVENT_DOUBLE_TAP | Double tap on the watch body.
|
◆ pbl_accel_dev_init()
| int pbl_accel_dev_init |
( |
const struct pbl_device * |
dev | ) |
|
Device init of every accelerometer: calls the driver's init with the lock held.
- Parameters
-
- Returns
- 0 or a negative errno.
◆ pbl_accel_get_double_tap_detection()
| bool pbl_accel_get_double_tap_detection |
( |
const struct pbl_accel * |
accel | ) |
|
Check whether double tap detection is enabled.
- Parameters
-
- Returns
- True if double tap detection is enabled.
◆ pbl_accel_get_max_num_samples()
| uint32_t pbl_accel_get_max_num_samples |
( |
const struct pbl_accel * |
accel | ) |
|
Get the maximum number of samples the driver can batch.
- Parameters
-
- Returns
- Depth of the hardware FIFO, 0 if the device is not ready.
◆ pbl_accel_get_sampling_interval()
| uint32_t pbl_accel_get_sampling_interval |
( |
const struct pbl_accel * |
accel | ) |
|
Get the sampling interval.
- Parameters
-
- Returns
- Sampling interval in microseconds, 0 if the device is not ready.
◆ pbl_accel_get_shake_detection()
| bool pbl_accel_get_shake_detection |
( |
const struct pbl_accel * |
accel | ) |
|
Check whether shake detection is enabled.
- Parameters
-
- Returns
- True if shake detection is enabled.
◆ pbl_accel_lock()
| void pbl_accel_lock |
( |
const struct pbl_accel * |
accel | ) |
|
Take the device lock.
Recursive. A consumer takes it around its own state to serialize it with the driver.
- Parameters
-
◆ pbl_accel_peek()
Read the most recent sample.
The driver may report queued samples from within this call.
- Parameters
-
| accel | Accelerometer. |
| [out] | sample | Most recent sample. |
- Returns
- 0 on success, or a negative errno.
◆ pbl_accel_report_event()
Report an event to the listener.
Called by the driver from thread context with the device lock held, while detection of that event type is enabled.
- Parameters
-
| accel | Accelerometer. |
| event | Event. |
◆ pbl_accel_report_sample()
Report a single sample to the listener, as a one-sample batch.
Same rules as pbl_accel_report_samples().
- Parameters
-
| accel | Accelerometer. |
| sample | Sample. |
◆ pbl_accel_report_samples()
Report a batch of samples to the listener.
Called by the driver from thread context with the device lock held, samples in increasing time order.
- Parameters
-
| accel | Accelerometer. |
| batch | Batch, valid for the duration of the call. |
◆ pbl_accel_set_double_tap_detection()
| int pbl_accel_set_double_tap_detection |
( |
const struct pbl_accel * |
accel, |
|
|
bool |
on |
|
) |
| |
Enable or disable double tap detection.
- Parameters
-
| accel | Accelerometer. |
| on | True to enable, false to disable. |
- Returns
- 0 on success, -ENOTSUP if the driver has no double tap detection, or a negative errno.
◆ pbl_accel_set_listener()
Set the listener of samples and events.
- Parameters
-
| accel | Accelerometer. |
| listener | Listener, or NULL to remove it. |
- Returns
- 0 on success, or a negative errno.
◆ pbl_accel_set_num_samples()
| int pbl_accel_set_num_samples |
( |
const struct pbl_accel * |
accel, |
|
|
uint32_t |
num_samples |
|
) |
| |
Set the maximum number of samples the driver may batch.
- 0: the driver reports no samples.
- 1: the driver reports every sample as soon as it is acquired.
- n > 1: the driver may queue up to n samples and then report them at once. This is only an upper bound, which the driver can use for power saving.
When n is lowered below the number of samples already queued, the driver reports them before the new value takes effect, possibly from within this call.
- Parameters
-
- Returns
- 0 on success, or a negative errno.
◆ pbl_accel_set_rotated()
| int pbl_accel_set_rotated |
( |
const struct pbl_accel * |
accel, |
|
|
bool |
rotated |
|
) |
| |
Set whether the axes are rotated by 180 degrees.
- Parameters
-
| accel | Accelerometer. |
| rotated | True if the sensor is mounted rotated by 180 degrees. |
- Returns
- 0 on success, or a negative errno.
◆ pbl_accel_set_sampling_interval()
| int pbl_accel_set_sampling_interval |
( |
const struct pbl_accel * |
accel, |
|
|
uint32_t |
interval_us, |
|
|
uint32_t * |
actual_us |
|
) |
| |
Set the sampling interval.
The driver selects the longest supported interval that is equal to or shorter than the requested one, saturating at the shortest interval the hardware supports. The new interval takes effect immediately; the driver may flush queued samples first so that timestamps stay accurate.
- Parameters
-
| accel | Accelerometer. |
| interval_us | Requested sampling interval in microseconds. |
| [out] | actual_us | Sampling interval used, in microseconds, or NULL. |
- Returns
- 0 on success, or a negative errno.
◆ pbl_accel_set_shake_detection()
| int pbl_accel_set_shake_detection |
( |
const struct pbl_accel * |
accel, |
|
|
bool |
on |
|
) |
| |
Enable or disable shake detection.
- Parameters
-
| accel | Accelerometer. |
| on | True to enable, false to disable. |
- Returns
- 0 on success, -ENOTSUP if the driver has no shake detection, or a negative errno.
◆ pbl_accel_set_shake_sensitivity_high()
| int pbl_accel_set_shake_sensitivity_high |
( |
const struct pbl_accel * |
accel, |
|
|
bool |
high |
|
) |
| |
Select high or normal shake sensitivity.
In high sensitivity mode the detection threshold is at its minimum, so that any minor motion is reported as a shake; otherwise the threshold set by pbl_accel_set_shake_sensitivity_percent() applies. Does not enable shake detection.
- Parameters
-
| accel | Accelerometer. |
| high | True for high sensitivity, false for normal. |
- Returns
- 0 on success, or a negative errno.
◆ pbl_accel_set_shake_sensitivity_percent()
| int pbl_accel_set_shake_sensitivity_percent |
( |
const struct pbl_accel * |
accel, |
|
|
uint8_t |
percent |
|
) |
| |
Set the normal shake sensitivity.
Does not enable shake detection.
- Parameters
-
| accel | Accelerometer. |
| percent | Sensitivity from 0 (highest threshold, least sensitive) to 100 (lowest threshold, most sensitive). |
- Returns
- 0 on success, or a negative errno.
◆ pbl_accel_unlock()
| void pbl_accel_unlock |
( |
const struct pbl_accel * |
accel | ) |
|
Release the device lock.
- Parameters
-
◆ ACCEL
The board's accelerometer.