Re: [sensors] API must allow Web devs to easily create fused sensors in JS with an API consistant with native sensors

Reopening this issue following a discussion with @slightlyoff during 
the W3C TAG review.

While the current API absolutely allows building fused sensor in 
application level code, it is not quite clear whether (or how) it 
would be possible to have such sensors be subclasses of the generic 
sensor API.

The idea here would be something along the following lines. For 
example, imagine you wanted to create a high-level pedometer sensor, 
that just provided step count. This would filter the output of the 
accelerometer or perhaps the gyroscope, and might do some sensor 
fusion between both.

JS implementation would look something like that:

```js
class Pedometer extends Sensor {
    constructor(options) {
        this.gyroscope = new Gyroscope();
        this.gyroscopeReadings = [];
    }
    start() {
        this.gyroscope.onchange = e => {
            this.gyroscopeReadings.push(e.reading);
            if (this.gyroscopeReadings.length > 8) {
                let stepcount = filter(this.gyroscopeReadings);
                this.gyroscopeReadings.length = 0;
                if (stepcount > 0) {
                    let r = new 
PedometerReading(this.reading.stepcount + stepcount);
                    this.reading = r;
                    this.dispatchEvent(new 
SensorReadingEvent("change", r));
                }
            }
        }
        return this.gyroscope.start();
    }
    filter() {
        // etc
    }
}
```

-- 
GitHub Notification of comment by tobie
Please view or discuss this issue at 
https://github.com/w3c/sensors/issues/42#issuecomment-212640560 using 
your GitHub account

Received on Wednesday, 20 April 2016 22:40:58 UTC