PebbleOS v4.47.0-512-g5285d4236
Loading...
Searching...
No Matches
Accelerometer

Accelerometer class. More...

Modules

 Simulator
 Simulated accelerometer, replaying the samples and events it is fed.
 

Data Structures

struct  pbl_accel_sample
 Accelerometer sample. More...
 
struct  AccelRawBatch
 Batch of raw samples in a driver-owned buffer. More...
 
struct  pbl_accel_event
 Accelerometer event. More...
 
struct  pbl_accel_listener
 Consumer of an accelerometer's samples and events. More...
 
struct  pbl_accel_ops
 Driver operations. More...
 
struct  pbl_accel_state
 Class runtime state. More...
 
struct  pbl_accel
 An accelerometer. More...
 
struct  AccelRawBatch.axis
 Per-axis source, indexed by IMUCoordinateAxis; encodes axis remapping and direction. More...
 

Macros

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

Enumerations

enum  pbl_accel_event_type { PBL_ACCEL_EVENT_SHAKE , PBL_ACCEL_EVENT_DOUBLE_TAP }
 Accelerometer event type. More...
 

Functions

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.
 

Variables

const struct pbl_accel *const ACCEL
 The board's accelerometer.
 

Detailed Description

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.

static void prv_samples(const struct pbl_accel *accel, const AccelRawBatch *batch,
void *user_data) {
...
}
static const struct pbl_accel_listener s_listener = {.samples = prv_samples};
uint32_t interval_us;
pbl_accel_set_sampling_interval(ACCEL, 40000, &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

Data Structure Documentation

◆ pbl_accel_sample

struct pbl_accel_sample

Accelerometer 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

struct 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

struct pbl_accel_event

Accelerometer event.

Data Fields
IMUCoordinateAxis axis Axis the event was detected on.
int32_t direction Direction along the axis: positive or negative.
enum pbl_accel_event_type type Type.

◆ pbl_accel_state

struct pbl_accel_state

Class runtime 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

struct pbl_accel

An accelerometer.

Data Fields
struct pbl_device dev Device.
const struct pbl_accel_ops * ops Driver operations.
struct pbl_accel_state * state Runtime state.

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

Macro Definition Documentation

◆ PBL_ACCEL_INIT

#define PBL_ACCEL_INIT (   sym,
  _name,
  _parent,
  _ops 
)
Value:
{ \
.dev = PBL_DEVICE_INIT_CLASS(sym, _name, pbl_accel_dev_init, _parent, &pbl_accel_class), \
.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
symSymbol of the accelerometer instance, with its state defined by PBL_ACCEL_STATE_DEFINE().
_nameName.
_parentParent device, such as the I2C bus, or NULL.
_opsDriver operations.

◆ PBL_ACCEL_STATE_DEFINE

#define PBL_ACCEL_STATE_DEFINE (   sym)
Value:
static struct pbl_accel_state sym##_accel_state = {.lock = PBL_MUTEX_INITIALIZER}
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
symSymbol of the accelerometer instance.

Enumeration Type Documentation

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

Function Documentation

◆ 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
devAccelerometer device.
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
accelAccelerometer.
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
accelAccelerometer.
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
accelAccelerometer.
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
accelAccelerometer.
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
accelAccelerometer.

◆ pbl_accel_peek()

int pbl_accel_peek ( const struct pbl_accel *  accel,
struct pbl_accel_sample *  sample 
)

Read the most recent sample.

The driver may report queued samples from within this call.

Parameters
accelAccelerometer.
[out]sampleMost recent sample.
Returns
0 on success, or a negative errno.

◆ pbl_accel_report_event()

void pbl_accel_report_event ( const struct pbl_accel *  accel,
const struct pbl_accel_event *  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
accelAccelerometer.
eventEvent.

◆ pbl_accel_report_sample()

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.

Same rules as pbl_accel_report_samples().

Parameters
accelAccelerometer.
sampleSample.

◆ pbl_accel_report_samples()

void pbl_accel_report_samples ( const struct pbl_accel *  accel,
const AccelRawBatch *  batch 
)

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
accelAccelerometer.
batchBatch, 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
accelAccelerometer.
onTrue 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()

int pbl_accel_set_listener ( const struct pbl_accel *  accel,
const struct pbl_accel_listener *  listener 
)

Set the listener of samples and events.

Parameters
accelAccelerometer.
listenerListener, 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
accelAccelerometer.
num_samplesMaximum number of samples to batch, at most pbl_accel_get_max_num_samples().
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
accelAccelerometer.
rotatedTrue 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
accelAccelerometer.
interval_usRequested sampling interval in microseconds.
[out]actual_usSampling 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
accelAccelerometer.
onTrue 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
accelAccelerometer.
highTrue 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
accelAccelerometer.
percentSensitivity 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
accelAccelerometer.

Variable Documentation

◆ ACCEL

const struct pbl_accel* const ACCEL
extern

The board's accelerometer.