Skip to main content

Crate day_part_sensors

Crate day_part_sensors 

Source
Expand description

day-part-sensors — a HEADLESS cross-platform motion-sensor API. No UI; any Rust code can depend on this crate and watch the device’s motion sensors through the platform’s NATIVE API.

use day_part_sensors::SensorKind;
let watch = day_part_sensors::watch(SensorKind::Accelerometer, |a| {
    println!("acceleration: {:.2} {:.2} {:.2} m/s²", a.x, a.y, a.z);
});
// Samples arrive until `watch` is dropped.

Platform selection is purely #[cfg(target_os)]/#[cfg(target_env)] (a sensor is an OS concern, not a widget-toolkit one): iOS uses CoreMotion, Android SensorManager (via a Java shim staged by day build), HarmonyOS the native libohsensor.so, Linux the Industrial I/O sysfs tree, and the web DeviceMotionEvent. macOS has no public motion-sensor API and Windows is a stub for now — both report no sensors at all.

§Why a stream

Every platform’s sensor API is already PUSH — SensorEventListener, CoreMotion handlers, OH_Sensor_Subscribe, devicemotion — so the older read() poll was an adapter in the wrong direction: each per-OS arm had to cache the newest event purely so a caller could ask for it. watch removes that inversion, and “no sample yet” stops being a poll artifact.

Delivery rate: the arms that cache a natively-pushed event are sampled at SAMPLE_MS; the pull-only arms (Linux sysfs, Windows) are read at the same cadence. A watch therefore delivers at a steady ~20 Hz rather than at the sensor’s own rate — plenty for a readout or a chart, and it keeps a fast sensor from flooding an app’s UI thread.

on_sample runs on an unspecified BACKGROUND thread (never the UI thread), so deliver into UI state with a day_reactive::Setter, exactly as day-part-http documents for its completions.

Structs§

SensorReading
One motion-sensor sample. Units are SI and depend on the SensorKind: m/s² for the accelerometer, rad/s for the gyroscope, µT for the magnetometer (iOS g’s and any platform quirks are normalized by the per-OS impls). Axis sign conventions are the platform’s own — e.g. a device lying face-up reads z ≈ +9.8 on Android but z ≈ -9.8 on iOS.
Watch
An active subscription. Dropping it stops delivery, and stops the underlying platform stream once the last watcher of that sensor is gone.

Enums§

SensorKind
Which motion sensor to query.

Constants§

SAMPLE_MS
How often a watch delivers, in milliseconds.

Functions§

is_available
Whether the device has the given sensor (and the platform an API for it). false also covers simulators/emulators without sensor passthrough and desktops without motion hardware.
watch
Subscribe to a sensor. Samples arrive on an unspecified BACKGROUND thread roughly every SAMPLE_MS until the returned Watch is dropped.
Day API ↩ Guides· daybrite.dev