Magnetometer class.
More...
|
| const struct pbl_mag *const | MAG |
| | The board's magnetometer.
|
| |
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) {
...
}
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
◆ 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
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
◆ PBL_MAG_INIT
| #define PBL_MAG_INIT |
( |
|
sym, |
|
|
|
_name, |
|
|
|
_parent, |
|
|
|
_ops |
|
) |
| |
Value: { \
.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
-
| sym | Symbol of the instance, as passed to PBL_MAG_STATE_DEFINE(). |
| _name | Device name. |
| _parent | Parent device. |
| _ops | Driver operations. |
◆ PBL_MAG_STATE_DEFINE
| #define PBL_MAG_STATE_DEFINE |
( |
|
sym | ) |
|
Value:
#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
-
| sym | Symbol of the instance. |
◆ pbl_mag_get_sampling_interval()
| uint32_t pbl_mag_get_sampling_interval |
( |
const struct pbl_mag * |
mag | ) |
|
Get the sampling interval.
- Parameters
-
- 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
-
◆ pbl_mag_peek()
Read the most recent sample.
- Parameters
-
| mag | Magnetometer. |
| [out] | sample | Sample. |
- 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
-
| mag | Magnetometer. |
| sample | Sample. |
◆ pbl_mag_set_listener()
Set the listener.
- Parameters
-
| mag | Magnetometer. |
| listener | Listener, 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
-
| mag | Magnetometer. |
| rotated | true 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
-
| mag | Magnetometer. |
| interval_us | Requested interval in microseconds, 0 to stop sampling. |
| [out] | actual_us | Interval 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
-
◆ MAG
The board's magnetometer.