C+
Packages · View as Markdown

sensors

Accelerometer, gyroscope, magnetometer, barometer — one shape each.

[dependencies]
sensors = "*"
stdlib = "*"

Use cpc pm add . sensors to write the platform-specific dependency closure.

import "sensors/sensors" as sens;
import "stdlib/result" as result;

fn shook(s: sens::Sample, ctx: *u8) {
    if s.magnitude() > 15.0f64 { /* a shake */ }
}

match sens::updates(sens::Kind::Accelerometer, on_sample: shook) {
    result::Result::Ok(r) => { READINGS = r; }   // stops on drop
    result::Result::Err(e) => { }
}

Units are normalised

The platforms disagree and the difference is silent, so this package picks one set and converts:

kind unit note
Accelerometer m/s² includes gravity — flat on a table reads ~9.8, not 0
Gyroscope rad/s
Magnetometer µT uncalibrated; a compass needs more than this
Barometer hPa in x; y and z are 0

Apple reports acceleration in G; an app written against one platform and run on the other would be wrong by 9.8× with no error anywhere.

Coverage

macOS iOS Android Linux Windows
accelerometer, gyroscope, magnetometer hardware permitting Unavailable Unavailable
barometer ❌ no hardware ✅ needs NSMotionUsageDescription Unavailable Unavailable

Verified live on an iPad Pro M1 and an Android emulator, 2026-09-02.

A Mac without motion hardware, plus the current Linux and Windows backends, answers Unavailable. That is deliberately not Unsupported, which means "this build cannot ask at all".

The barometer needs NSMotionUsageDescription in the app's Info.plist on Apple — without it CMAltimeter starts, reports available, and delivers nothing at all. See docs/guide.md.

Tests

cd vendor/sensors && cpc test

Live paths need a device or emulator — playground/sensorprobe_android runs all four against adb emu sensor set.