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

Magnetometer class. More...

Modules

 Simulator
 Simulated magnetometer, measuring the field pbl_mag_sim_set() sets.
 

Data Structures

struct  MagData
 Magnetometer sample, in milligauss, in the watch frame. More...
 
struct  pbl_mag_listener
 Magnetometer listener. More...
 
struct  pbl_mag_ops
 Magnetometer driver operations, called with the device lock held. More...
 
struct  pbl_mag_state
 Magnetometer runtime state. More...
 
struct  pbl_mag
 Magnetometer. More...
 

Macros

#define PBL_MAG_STATE_DEFINE(sym)
 Define the class state of a magnetometer.
 
#define PBL_MAG_INIT(sym, _name, _parent, _ops)
 Initialize a struct pbl_mag.
 

Functions

void pbl_mag_lock (const struct pbl_mag *mag)
 Take the device lock.
 
void pbl_mag_unlock (const struct pbl_mag *mag)
 Release the device lock.
 
int pbl_mag_set_listener (const struct pbl_mag *mag, const struct pbl_mag_listener *listener)
 Set the listener.
 
int pbl_mag_set_rotated (const struct pbl_mag *mag, bool rotated)
 Set whether the watch is mounted rotated by 180 degrees.
 
int pbl_mag_set_sampling_interval (const struct pbl_mag *mag, uint32_t interval_us, uint32_t *actual_us)
 Set the sampling interval.
 
uint32_t pbl_mag_get_sampling_interval (const struct pbl_mag *mag)
 Get the sampling interval.
 
int pbl_mag_peek (const struct pbl_mag *mag, MagData *sample)
 Read the most recent sample.
 
void pbl_mag_report_sample (const struct pbl_mag *mag, const MagData *sample)
 Report a new sample to the listener.
 

Variables

const struct pbl_mag *const MAG
 The board's magnetometer.
 

Detailed Description

Magnetometer class.

A magnetometer is a struct pbl_mag device. While sampling, the driver reports each new sample to the listener a consumer registered with pbl_mag_set_listener(), the compass service in practice. pbl_mag_peek() reads the most recent sample instead, for consumers that poll.

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.

static void prv_sample(const struct pbl_mag *mag, const MagData *sample, void *user_data) {
...
}
static const struct pbl_mag_listener s_listener = {.sample = prv_sample};
pbl_mag_set_listener(MAG, &s_listener);
Magnetometer.
Definition mag.h:82
Magnetometer sample, in milligauss, in the watch frame.
Definition mag.h:39
int pbl_mag_set_sampling_interval(const struct pbl_mag *mag, uint32_t interval_us, uint32_t *actual_us)
Set the sampling interval.
const struct pbl_mag *const MAG
The board's magnetometer.
int pbl_mag_set_listener(const struct pbl_mag *mag, const struct pbl_mag_listener *listener)
Set the listener.
Magnetometer listener.
Definition mag.h:51
void(* sample)(const struct pbl_mag *mag, const MagData *sample, void *user_data)
Optional.
Definition mag.h:53

Data Structure Documentation

◆ MagData

struct MagData

Magnetometer sample, in milligauss, in the watch frame.

Data Fields
int16_t x Magnetic field along the X axis.
int16_t y Magnetic field along the Y axis.
int16_t z Magnetic field along the Z axis.

◆ pbl_mag_state

struct pbl_mag_state

Magnetometer runtime state.

Data Fields
const struct pbl_mag_listener * listener Listener, or NULL.
struct pbl_mutex lock Device lock.
uint32_t sampling_interval_us Sampling interval in microseconds, 0 when not sampling.

◆ pbl_mag

struct pbl_mag

Magnetometer.

Data Fields
struct pbl_device dev Device.
const struct pbl_mag_ops * ops Driver operations.
struct pbl_mag_state * state Runtime state.

Macro Definition Documentation

◆ PBL_MAG_INIT

#define PBL_MAG_INIT (   sym,
  _name,
  _parent,
  _ops 
)
Value:
{ \
.dev = PBL_DEVICE_INIT_CLASS(sym, _name, pbl_mag_dev_init, _parent, &pbl_mag_class), \
.ops = (_ops), \
.state = &sym##_mag_state, \
}
#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

Initialize a struct pbl_mag.

Parameters
symSymbol of the instance, as passed to PBL_MAG_STATE_DEFINE().
_nameDevice name.
_parentParent device.
_opsDriver operations.

◆ PBL_MAG_STATE_DEFINE

#define PBL_MAG_STATE_DEFINE (   sym)
Value:
static struct pbl_mag_state sym##_mag_state = {.lock = PBL_MUTEX_INITIALIZER}
#define PBL_DEVICE_STATE_DEFINE(sym)
Define the runtime state of device instance sym.
Definition device.h:132
struct pbl_mutex lock
Device lock.
Definition mag.h:74
Magnetometer runtime state.
Definition mag.h:72
#define PBL_MUTEX_INITIALIZER
Static initializer for an unlocked mutex.
Definition mutex.h:50

Define the class state of a magnetometer.

Parameters
symSymbol of the instance.

Function Documentation

◆ pbl_mag_get_sampling_interval()

uint32_t pbl_mag_get_sampling_interval ( const struct pbl_mag *  mag)

Get the sampling interval.

Parameters
magMagnetometer.
Returns
Interval in microseconds, 0 when not sampling or not ready.

◆ pbl_mag_lock()

void pbl_mag_lock ( const struct pbl_mag *  mag)

Take the device lock.

Parameters
magMagnetometer.

◆ pbl_mag_peek()

int pbl_mag_peek ( const struct pbl_mag *  mag,
MagData *  sample 
)

Read the most recent sample.

Parameters
magMagnetometer.
[out]sampleSample.
Returns
0, -ENODEV if the magnetometer is not ready, -ENODATA when not sampling, or a negative errno.

◆ pbl_mag_report_sample()

void pbl_mag_report_sample ( const struct pbl_mag *  mag,
const MagData *  sample 
)

Report a new sample to the listener.

For drivers. Must be called with the device lock held.

Parameters
magMagnetometer.
sampleSample.

◆ pbl_mag_set_listener()

int pbl_mag_set_listener ( const struct pbl_mag *  mag,
const struct pbl_mag_listener *  listener 
)

Set the listener.

Parameters
magMagnetometer.
listenerListener, or NULL to remove it.
Returns
0.

◆ pbl_mag_set_rotated()

int pbl_mag_set_rotated ( const struct pbl_mag *  mag,
bool  rotated 
)

Set whether the watch is mounted rotated by 180 degrees.

When rotated, the X and Y axes of subsequent samples are inverted.

Parameters
magMagnetometer.
rotatedtrue if rotated.
Returns
0, -ENODEV if the magnetometer is not ready, or a negative errno.

◆ pbl_mag_set_sampling_interval()

int pbl_mag_set_sampling_interval ( const struct pbl_mag *  mag,
uint32_t  interval_us,
uint32_t *  actual_us 
)

Set the sampling interval.

Parameters
magMagnetometer.
interval_usRequested interval in microseconds, 0 to stop sampling.
[out]actual_usInterval in use, or NULL.
Returns
0, -ENODEV if the magnetometer is not ready, or a negative errno.

◆ pbl_mag_unlock()

void pbl_mag_unlock ( const struct pbl_mag *  mag)

Release the device lock.

Parameters
magMagnetometer.

Variable Documentation

◆ MAG

const struct pbl_mag* const MAG
extern

The board's magnetometer.