The SensorStream
class aggregates data from multiple sensors and provides an asynchronous stream of the combined sensor states.
This documentation provides a quick start guide for developers to understand and begin using the SensorStream
class. It includes examples to demonstrate how to instantiate the class, use the available options, and consume the sensor data stream.
Create a new SensorStream
instance with an array of sensor objects and an optional options object.
const sensorStream = new SensorStream(sensorArray, options);
sensorArray
: An array of sensors to be added to the stream.options
: An object with optional settings.delay
: The delay between stream iterations in milliseconds or'requestAnimationFrame'
for visual frame updates.accumulate
: A boolean to determine whether to accumulate state over time or to yield fresh state objects each iteration.
Adds a new sensor to the stream.
sensorStream.add(newSensor);
Removes an existing sensor from the stream.
sensorStream.remove(existingSensor);
Starts the sensor data stream.
sensorStream.start();
Stops the sensor data stream.
sensorStream.stop();
Create sensors and start streaming:
const motionSensor = new DeviceMotionSensor();
const orientationSensor = new DeviceOrientationSensor();
const sensorStream = new SensorStream([motionSensor, orientationSensor]);
sensorStream.start();
(async () => {
for await (const state of sensorStream) {
console.log(state);
}
})();
// Stream with a 100ms delay between iterations
const sensorStreamWithDelay = new SensorStream(sensorArray, { delay: 100 });
// Stream with state accumulation
const sensorStreamWithAccumulation = new SensorStream(sensorArray, { accumulate: true });
sensorStreamWithDelay.start();
(async () => {
for await (const state of sensorStreamWithDelay) {
console.log(state);
}
})();
Utilize the requestAnimationFrame
to synchronize with browser repaints, useful for visual updates:
const sensorStreamWithRAF = new SensorStream(sensorArray, { delay: 'requestAnimationFrame' });
sensorStreamWithRAF.start();
(async () => {
for await (const state of sensorStreamWithRAF) {
// Update your visualization here
}
})();
- When the
accumulate
option istrue
, the state object is built up over time with data from the sensors, with each sensor's latest data overwriting its previous state in the object. - When the
delay
option is set to'requestAnimationFrame'
, the stream yields new state data synchronized with the browser's repaint, making it ideal for animations or visual data representations.