aboutsummaryrefslogtreecommitdiff
path: root/docs/sensors.md
blob: ef198b767f0b8980e947bc59435581097e2c5c33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
ZJS API for W3C Generic Sensors
==============================

* [Introduction](#introduction)
* [Web IDL](#web-idl)
* [API Documentation](#api-documentation)
* [Sample Apps](#sample-apps)

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
accelerometer and gyroscope. Since the W3C Sensor API is still a draft spec,
our implementation only provide a subset of the API and the API could be
slightly different, but we try to follow the latest spec as closely as possible.

The currently supported hardware are Arduino 101s with its built-in BMI160
accelerometer and gyroscope, also ambient light sensor that can be connected
using analog pin.

Web IDL
-------
This IDL provides an overview of the interface; see below for documentation of
specific API functions.

####Sensor Interface
```javascript
interface Sensor {
    readonly attribute SensorState state;   // The current state of Sensor object
    attribute double frequency;             // The frequency set
    void start();                           // Starts the sensor
    void stop();                            // Stops the sensor
    attribute ChangeCallback onchange;      // 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;  // The requested polling frequency, default is 20 if unset
};

enum SensorState {
    "unconnected",
    "activating",
    "activated",
    "idle",
    "errored"
};

interface SensorErrorEvent {
    attribute Error error;
};

callback ChangeCallback = void();
callback ActivateCallback = void();
callback ErrorCallback = void(SensorErrorEvent error);
```
####Accelerometer Interface
```javascript
[Constructor(optional AccelerometerOptions accelerometerOptions)]
interface Accelerometer : Sensor {
    attribute boolean includesGravity;
    readonly attribute double x;
    readonly attribute double y;
    readonly attribute double z;
};

dictionary AccelerometerOptions : SensorOptions  {
   boolean includeGravity = true;  // not supported, will throw an error if set
};
```
####GyroscopeSensor Interface
```javascript
[Constructor(optional SensorOptions sensorOptions)]
interface GyroscopeSensor : Sensor {
    attribute GyroscopeSensorReading reading;
};

interface GyroscopeSensorReading : SensorReading {
    readonly attribute double x;
    readonly attribute double y;
    readonly attribute double z;
};
```
####AmbientLightSensor Interface
```javascript
[Constructor(optional SensorOptions sensorOptions)]
interface AmbientLightSensor : Sensor {
    readonly attribute unsigned long pin;
    readonly attribute double illuminance;
};

dictionary AmbientLightSensorOptions : SensorOptions  {
    unsigned long pin;  // analog pin where the light is connected
};
```

API Documentation
-----------------

### onchange
`Sensor.onchange`

The onchange 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 this.[[state]] transitions from "activating" to "activated".

### onerror
`Sensor.onerror`

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

### start
`void Sensor.start()`

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

### stop
`void Sensor.stop()`

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

Sample Apps
-----------
* [Accelerometer sample](../samples/BMI160Accelerometer.js)
* [Gyroscope sample](../samples/BMI160Gyroscope.js)
* [Ambient Light sample](../samples/AmbientLight.js)