Module: j5e/sensor
Description
A module for working with generic sensor devices. It will emit events when the state of a sensor changes. The sensor module is generic in that it can be used to read any sensor type that uses an ADC input.
In electronics, an analog-to-digital converter is a system that converts an analog signal, such as a sound picked up by a microphone or light entering a digital camera, into a digital signal. An ADC may also provide an isolated measurement such as an electronic device that converts an analog input voltage or current to a digital number representing the magnitude of the voltage or current. Typically the digital output is a two's complement binary number that is proportional to the input, but there are other possibilities.Read more on Wikipedia
Requires
Class: Sensor
The Sensor class allows for input from sensor devices that connect to an ADC
Constructor
new Sensor(io)
Parameters
(required)
Description:
Pin identifier or IO Options (See instantiation)
Examples
// Using a pin number
import Sensor from "j5e/sensor";
const sensor = await new Sensor(12);
sensor.on("change", data => {
trace(data);
});
// Using a pin identifier
import Sensor from "j5e/sensor";
const sensor = await new Sensor("A1");
sensor.on("change", data => {
trace(data);
});
Properties
(read only)
(read only)
(read only)
(read only)
(read only)
(read only)
Methods
Sensor.configure
Configure a Sensor
sensor.configure(options);
Returns: The instance on which the method was called
Parameters
(required)
Description:
Device configuration options
Description:
Analog reference voltage
Default: 3.3
Description:
Wether the device is currently performing reads every
Default: true
Description:
Interval between readings in millseconds
Default: 100
Description:
Limit the output range
Description:
The input range of the sensor
Default: [0, N]
Description:
The output range for the sensor's value
Default: [0, N]
Description:
The minimum amount of change required to emit a "change" event
Default: 1
Example
// Passing in Cofiguration Options
import Sensor from "j5e/sensor";
const sensor = await new Sensor({
pin: 12
});
sensor.configure({
interval: 500
});
sensor.on("change", data => {
trace(data);
});
Sensor.enable
Enable a disabled sensor.
sensor.enable();
Returns: instance
Example
import Sensor from "j5e/sensor";
const sensor = await new Sensor(12);
sensor.disable();
// Wait 5 seconds and then take readings
timer.setTimeout(function() {
sensor.enable();
});
Sensor.disable
Disable an enabled sensor.
sensor.disable();
Returns: instance
Example
import Sensor from "j5e/sensor";
const sensor = await new Sensor(12);
// Take reading for 5 seconds and then stop
timer.setTimeout(function() {
sensor.disable();
});
Sensor.read
Synchronous read of a sensor.
sensor.read();
Returns: sensor value
Example
import Sensor from "j5e/sensor";
const sensor = await new Sensor(12);
let myValue = sensor.read();
Sensor.scale
scale/scaleTo Set a value scaling range
sensor.scale(low, high, low, high);
Returns: instance
Parameters
(required)
Description:
Lowerbound
(required)
Description:
Upperbound
Description:
Lowerbound
Example
import Sensor from "j5e/sensor";
const sensor = await new Sensor(12);
// Scale all future values to 8-bit range
sensor.scale([0, 255]);
Sensor.scaleTo
scaleTo Scales value to integer representation
sensor.scaleTo(low, low, high);
Returns: The scaled value
Parameters
(required)
Description:
An array containing a lower and upper bound
(required)
Description:
A number to use as a lower bound
(required)
Description:
A number to use as an upper bound
Example
import Sensor from "j5e/sensor";
const sensor = await new Sensor(12);
// Scale the returned value to 8-bit range
sensor.scaleTo([0, 255]);
Sensor.fscaleTo
fscaleTo Scales value to single precision float representation
sensor.fscaleTo(low, low, high);
Returns: The scaled value
Parameters
(required)
Description:
An array containing a lower and upper bound
(required)
Description:
A number to use as a lower bound
(required)
Description:
A number to use as an upper bound
Example
import Sensor from "j5e/sensor";
const sensor = await new Sensor(12);
// Scale the returned value to float between 0 and 1
sensor.fscaleTo([0, 1]);
Sensor.within
Fire a callback when the value is within a certain range
sensor.within(range, unit, callback);
Parameters
(required)
Description:
The upper and lower ends of the range to watch
(required)
Description:
The property to test
(required)
Description:
A callback to run when the event is fired.
Sensor.on
Create an event listener
sensor.on(event, listener);
Parameters
(required)
Description:
The name of the event to listen for
(required)
Description:
A callback to run when the event is fired.
Sensor.removeListener
Remove an event listener
sensor.removeListener(event, listener);
Parameters
(required)
Description:
The name of the event that we are removing a listener from
(required)
Description:
The callback that we are removing
Sensor.emit
Note: This method is not part of the public API, and is subject to change.
Emit an event
sensor.emit(event);
Parameters
(required)
Description:
The name of the event to emit
Sensor.once
Create an event listener that will only fire one time.
sensor.once(event, listener);
Parameters
(required)
Description:
The name of the event to listen for
(required)
Description:
A callback to run when the event is fired.