JFLib

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 SensorState what a sensor does now

    • Off = 0 -- nobody asks for it
    • Waiting = 1 -- started, no reading yet
    • Running = 2 -- readings arrive
    • Paused = 3 -- the app is in the background (or the system sleeps): stopped until it is back
    • Denied = 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 LocationState what the location does now

    • Off = 0 -- nobody asks for it
    • Searching = 1 -- started (or asking the user), no fix yet
    • Fix = 2 -- a fix arrived: location_fix
    • Paused = 3 -- the app is in the background: stopped until it is back
    • Denied = 4 -- the manifest (location) or the user refuses it, or the user took it back
    • Disabled = 5 -- the system's location switch is off
    • Unavailable = 6 -- no location service on this device or platform
  • struct Reading one reading of a sensor

    • v: [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 high
    • absolute: bool -- (rotation) its zero is north; false: only relative to where it started
  • struct Attitude the device's position in the room, degrees

    • pitch: f32 -- -90 .. 90: the top edge above the bottom edge (upright: 90)
    • roll: f32 -- -180 .. 180: the right edge below the left edge
    • side: 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 field
    • absolute: 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 FixSource where a fix comes from

    • Other = 0 -- the host did not say
    • Gps = 1 -- satellites
    • Network = 2 -- mobile cells and wireless networks
  • struct Fix one location fix

    • latitude: f64 -- degrees, north positive
    • longitude: f64 -- degrees, east positive
    • accuracy: Option<f32> -- metres (68 % of the fixes lie this close), when the provider says
    • altitude: Option<f64> -- metres above the WGS 84 ellipsoid, when the provider says
    • speed: Option<f32> -- metres per second, when the provider says
    • bearing: Option<f32> -- degrees clockwise from north, when the provider says
    • source: FixSource

impl Fix

  • fn age_ms(&self) -> u64 how old it is, ms (a fix of a script stays as old as the script says)

  • struct SensorWatch these sensors run while the guard lives; dropping it stops them

impl SensorWatch

  • fn mask(&self) -> u32 the SENSOR_* bits this guard keeps running (0: refused by the manifest)

  • fn watch(mask: u32) -> SensorWatch start these sensors (SENSOR_* bits) until the guard is dropped; a sensor this device lacks is not an error: its state() says Unavailable. Without sensors in the manifest: nothing runs, state() says Denied

  • fn state(bit: u32) -> SensorState what this sensor (one SENSOR_* bit) does now

  • fn 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 LocationWatch the location runs while the guard lives; dropping it stops it

  • fn location_watch() -> LocationWatch start the location until the guard is dropped; the first start may ask the user, so call it from a user action. Without location in the manifest: nothing runs, location_state() says Denied (an ask location line: the UI's question, UIC_PERMISSION, then call it again)

  • fn location_state() -> LocationState what the location does now

  • fn 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 news

  • fn 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 asked

  • fn 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 taken

  • fn 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 with fix (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

Source: docs/API.md in the JFLib repository.