Skip to content
This repository has been archived by the owner on Aug 5, 2022. It is now read-only.

Latest commit

 

History

History
115 lines (97 loc) · 4.44 KB

sensors.md

File metadata and controls

115 lines (97 loc) · 4.44 KB

ZJS API for W3C Generic Sensors

Introduction

ZJS Generic Sensor API implements the W3C Sensor API, and it's intended to provide a consistent API that allows apps to communicate with sensors like an accelerometer or gyroscope. Since the W3C Sensor API is still a draft spec, our implementation only provides a subset of the API -- and this API could be slightly different even though we try to follow the latest spec as closely as possible.

Note: The currently supported hardware is Arduino 101 that has a built-in BMI160 chip with accelerometer, gyroscope, and temperature sensors. The supported ambient light sensor is the Grove light sensor that comes with the Grove starter kit. The light sensor can be connected using an analog pin.

Web IDL

This IDL provides an overview of the interface; see below for documentation of specific API functions. We have a short document explaining ZJS WebIDL conventions.

Click to show WebIDL
interface Sensor {
    readonly attribute boolean activated;   // whether the sensor is activated or not
    readonly attribute boolean hasReading;  // whether the sensor has readings available
    readonly attribute double timestamp;    // timestamp of the latest reading in milliseconds
    attribute double frequency;             // sampling frequency in hertz
    void start();                           // starts the sensor
    void stop();                            // stops the sensor
    attribute ChangeCallback onreading;     // callback handler for change events
    attribute ActivateCallback onactivate;  // callback handler for activate events
    attribute ErrorCallback onerror;        // callback handler for error events
};

dictionary SensorOptions { double frequency; // desired frequency, default is 20 if unset };

interface SensorErrorEvent { attribute Error error; };

callback ChangeCallback = void(); callback ActivateCallback = void(); callback ErrorCallback = void(SensorErrorEvent error);

[Constructor(optional AccelerometerOptions accelerometerOptions)] interface Accelerometer : Sensor { readonly attribute double x; readonly attribute double y; readonly attribute double z; };

dictionary AccelerometerOptions : SensorOptions { string controller; // controller name, default to "bmi160" };

[Constructor(optional SensorOptions sensorOptions)] interface GyroscopeSensor : Sensor { readonly attribute double x; readonly attribute double y; readonly attribute double z; };

dictionary GyroscopeOptions : SensorOptions { string controller; // controller name, default to "bmi160" };

[Constructor(optional SensorOptions sensorOptions)] interface AmbientLightSensor : Sensor { readonly attribute unsigned long pin; readonly attribute double illuminance; };

dictionary AmbientLightSensorOptions : SensorOptions { string controller; // controller name, default to "ADC_0" unsigned long pin; // analog pin where the light is connected };

[Constructor(optional SensorOptions sensorOptions)] interface TemperatureSensor : Sensor { readonly attribute double celsius; };

dictionary TemperatureSensorOptions : SensorOptions { string controller; // controller name, default to "bmi160" };

Sensor API

onreading

Sensor.onreading

The onreading attribute is an EventHandler, which is called whenever a new reading is available.

onactivate

Sensor.onactivate

The onactivate attribute is an EventHandler which is called when the sensor is activated after calling start().

onerror

Sensor.onerror

The onactivate attribute is an EventHandler which is called whenever an exception cannot be handled synchronously.

sensor.start()

Starts the sensor instance, the sensor will get callback on onreading whenever there's a new reading available.

sensor.stop()

Stop the sensor instance, the sensor will stop reporting new readings.

Sample Apps