plat/sensors.rs#
Source: lib/plat/sensors.rs
sensors -- the device's motion sensors and its location for the app
watch(SENSOR_*) -> SensorWatch these sensors run while the guard lives (counted per sensor:
the last guard stops them); nothing runs without a guard.
Needs `sensors` in the manifest (else state() is Denied,
"PERMDENY sensors")
state(SENSOR_X) -> SensorState Off, Waiting (started, no reading yet), Running, Paused (the
app is in the background), Unavailable (this device has no
such sensor, or the platform none at all), Denied
read(SENSOR_X) -> Option<Reading> the last reading (kept while Paused, gone when stopped)
attitude() -> Option<Attitude> pitch, roll and heading from what runs: gravity (the
gravity sensor, the rotation estimate, the accelerometer, in
this order), heading from the rotation estimate, otherwise
computed from gravity and the magnetic field
attitude_of(gravity, rotation, magnet) -> Option<Attitude> the same from given values
location_watch() -> LocationWatch the location runs while the guard lives; the first start
may ask the user (call it from a user action: Android's
runtime permission, the browser's prompt). Needs `location`
in the manifest (else Denied, "PERMDENY location")
location_state() -> LocationState Off, Searching, Fix, Paused, Denied (the user or the
manifest refused), Disabled (the system's location switch
is off), Unavailable (no location service here)
location_fix() -> Option<Fix> the last fix: latitude, longitude, and what the provider
gave of accuracy, altitude, speed, bearing; age_ms()
take() -> (bits, location) (ui) what is new since the last call
suspend(on) (ui) the app went to the background / came back: everything
stops, Paused, and starts again without asking
New readings reach the UI thread like the host's other events: plat::wake(), then E_WAKE, and
the UI turns them into App::command(UIC_SENSOR, bits) and App::command(UIC_LOCATION, state);
nothing polls. A sensor's axes are the device's: x to the right of the screen, y to its top, z
out of it; accelerometer and gravity in m/s^2 (lying flat, screen up: z = +9.81), gyroscope in
rad/s, magnetic field in microtesla, the rotation estimate a unit quaternion (x, y, z, w) from
the device's axes to east, north, up (Reading::absolute: its zero is north; false: wherever the
device pointed when it started).
Android (android/GallerySensors.java, web_sensors.rs): SensorManager and LocationManager (GPS
and network providers, no Google Play services), ACCESS_FINE_LOCATION / ACCESS_COARSE_LOCATION
asked when location_watch starts. iOS (apple_ios_motion.rs, web_sensors.rs): Core Motion's device
motion and Core Location ("when in use", asked when location_watch starts). Browser (web/sensors.js, web_sensors.rs): DeviceMotionEvent,
DeviceOrientationEvent (Safari's requestPermission from the click), navigator.geolocation; no
magnetic field. Windows and Linux: everything is Unavailable (docs/ROADMAP.md names what a
native version would use). Scripts feed readings (`sensor ...`, `location ...`; lib/ui/auto.rs):
test_sensor, test_sensor_none, test_location.
/// acceleration including gravity, m/s^2
SENSOR_ACCEL: u32 = 1
/// gravity alone, m/s^2 (a virtual sensor of the system)
SENSOR_GRAVITY: u32 = 2
/// angular speed around x, y, z, rad/s
SENSOR_GYRO: u32 = 4
/// magnetic field, microtesla
SENSOR_MAGNET: u32 = 8
/// the rotation estimate: a unit quaternion from the device's axes to east, north, up
SENSOR_ROTATION: u32 = 16
/// every sensor
SENSOR_ALL: u32 = 31
enum SensorStatewhat a sensor does nowOff = 0-- nobody asks for itWaiting = 1-- started, no reading yetRunning = 2-- readings arrivePaused = 3-- the app is in the background (or the system sleeps): stopped until it is backDenied = 4-- the manifest refuses it (sensors) or the user did (Safari's question)Unavailable = 6-- this device has no such sensor, or the platform has none
enum LocationStatewhat the location does nowOff = 0-- nobody asks for itSearching = 1-- started (or asking the user), no fix yetFix = 2-- a fix arrived: location_fixPaused = 3-- the app is in the background: stopped until it is backDenied = 4-- the manifest (location) or the user refuses it, or the user took it backDisabled = 5-- the system's location switch is offUnavailable = 6-- no location service on this device or platform
struct Readingone reading of a sensorv: [f32; 4]-- x, y, z (the rotation estimate: x, y, z, w of the quaternion; else 0)accuracy: u8-- how far to trust it: 0 unreliable (a magnetometer asking for the figure eight), 1 low, 2 medium, 3 highabsolute: bool-- (rotation) its zero is north; false: only relative to where it started
struct Attitudethe device's position in the room, degreespitch: f32-- -90 .. 90: the top edge above the bottom edge (upright: 90)roll: f32-- -180 .. 180: the right edge below the left edgeside: f32-- -90 .. 90: the right edge above the left edge (a level's other axis beside pitch)heading: Option<f32>-- 0 .. 360 clockwise from north: where the device's top points, or its back when it is held upright; None: no rotation estimate and no magnetic fieldabsolute: bool-- heading's zero is north (false: where the estimate started)up: [f32; 3]-- the direction away from the earth in the device's axes (a unit vector)
enum FixSourcewhere a fix comes fromOther = 0-- the host did not sayGps = 1-- satellitesNetwork = 2-- mobile cells and wireless networks
struct Fixone location fixlatitude: f64-- degrees, north positivelongitude: f64-- degrees, east positiveaccuracy: Option<f32>-- metres (68 % of the fixes lie this close), when the provider saysaltitude: Option<f64>-- metres above the WGS 84 ellipsoid, when the provider saysspeed: Option<f32>-- metres per second, when the provider saysbearing: Option<f32>-- degrees clockwise from north, when the provider sayssource: FixSource
impl Fix
fn age_ms(&self) -> u64how old it is, ms (a fix of a script stays as old as the script says)struct SensorWatchthese sensors run while the guard lives; dropping it stops them
impl SensorWatch
fn mask(&self) -> u32the SENSOR_* bits this guard keeps running (0: refused by the manifest)fn watch(mask: u32) -> SensorWatchstart these sensors (SENSOR_* bits) until the guard is dropped; a sensor this device lacks is not an error: its state() says Unavailable. Withoutsensorsin the manifest: nothing runs, state() says Deniedfn state(bit: u32) -> SensorStatewhat this sensor (one SENSOR_* bit) does nowfn read(bit: u32) -> Option<Reading>the last reading of this sensor (one SENSOR_* bit)fn attitude() -> Option<Attitude>pitch, roll and heading from what runs now (see the module header)fn attitude_of(gravity: Option<[f32; 3]>, rotation: Option<Reading>, magnet: Option<[f32; 3]>) -> Option<Attitude>the attitude from a gravity vector (the accelerometer at rest: what points away from the earth), a rotation quaternion and a magnetic field, each in the device's axes; pitch and roll follow gravity when it is given, the rotation otherwise; heading follows the rotation, else gravity and the field. None: neither gravity nor rotation (or useless values)struct LocationWatchthe location runs while the guard lives; dropping it stops itfn location_watch() -> LocationWatchstart the location until the guard is dropped; the first start may ask the user, so call it from a user action. Withoutlocationin the manifest: nothing runs, location_state() says Denied (anask locationline: the UI's question, UIC_PERMISSION, then call it again)fn location_state() -> LocationStatewhat the location does nowfn location_fix() -> Option<Fix>the last fix (kept while Paused or Searching again; gone when stopped)fn take() -> (u32, bool)(ui) what is new since the last call: the sensors with news (a reading or another state), whether the location has newsfn suspend(on: bool)(ui) the app went to the background or the system to sleep (true): everything stops, Paused; back (false): it starts again, nobody is askedfn test_sensor(bit: u32, v: [f32; 4], accuracy: u8, absolute: bool) -> bool(scripts:sensor NAME ...) this sensor exists now; its reading arrives if it runs (a stopped sensor delivers nothing) -> it was takenfn test_sensor_none(mask: u32)(scripts:sensor none NAME) these sensors do not exist (any more)fn test_location(state: LocationState, fix: Option<(f64, f64, [f64; 4], u64)>)(scripts:location ...) the host's answer: a state (LocationState: Searching, Denied, Disabled, Unavailable), or Fix withfix(a fix of a script keeps its age)fn test_location_age(age_ms: u64)(scripts:location age MS) how old the script's fix is