+(function (global, factory) {
if (typeof exports === 'undefined') {
factory(global.webduino || {});
} else {
module.exports = factory;
}
}(this, function (scope) {
'use strict';
var Module = scope.Module,
BoardEvent = scope.BoardEvent,
proto;
var SoilEvent = {
/**
* Fires when the value of humidity has changed.
*
* @event PhotocellEvent.MESSAGE
*/
MESSAGE: 'message'
};
/**
* The Soil class.
*
* @namespace webduino.module
* @class Soil
* @constructor
* @param {webduino.Board} board Board that the soil is attached to.
* @param {Integer} analogPinNumber The pin that soil is attached to.
* @extends webduino.Module
*/
function Soil(board, analogPinNumber) {
Module.call(this);
this._board = board;
this._pinNumber = Number(analogPinNumber);
this._messageHandler = onMessage.bind(this);
}
function formatter(val) {
val = Math.round(val * 10000) / 100;
return val;
}
function onMessage(event) {
var pin = event.pin;
if (this._pinNumber !== pin.analogNumber) {
return false;
}
this.emit(SoilEvent.MESSAGE, formatter(pin.value));
}
Soil.prototype = proto = Object.create(Module.prototype, {
constructor: {
value: Soil
},
/**
* The state indicating whether the module is scanning.
*
* @attribute state
* @type {String} `on` or `off`
*/
state: {
get: function() {
return this._state;
},
set: function(val) {
this._state = val;
}
}
});
/**
* Start detection.
*
* @method measure
* @param {Function} [callback] Callback after starting detection.
*/
/**
* Start detection.
*
* @method on
* @param {Function} [callback] Callback after starting detection.
* @deprecated `on()` is deprecated, use `measure()` instead.
*/
proto.measure = proto.on = function(callback) {
this._board.enableAnalogPin(this._pinNumber);
if (typeof callback !== 'function') {
callback = function() {};
}
this._callback = function(val) {
callback(val);
};
this._state = 'on';
this._board.on(BoardEvent.ANALOG_DATA, this._messageHandler);
this.addListener(SoilEvent.MESSAGE, this._callback);
};
/**
* Stop detection.
*
* @method off
*/
proto.off = function() {
this._state = 'off';
this._board.disableAnalogPin(this._pinNumber);
this._board.removeListener(BoardEvent.ANALOG_DATA, this._messageHandler);
this.removeListener(SoilEvent.MESSAGE, this._callback);
this._callback = null;
};
scope.module.Soil = Soil;
}));