ribbons.actuators.led
Version:
LED as a Ribbons actuator.
62 lines (54 loc) • 1.45 kB
JavaScript
var Actuator = require('ribbons.actuator');
var util = require('util');
function Led (options) {
Actuator.call(this, options);
options = options || {};
this.pin = options.pin;
this.name = options.name || 'led';
}
util.inherits(Led, Actuator);
Led.prototype.start = function() {
this.platformLed = new this.platform.Pin(this.pin);
};
Led.prototype.on = function() {
this.platformLed.on();
this.info('on');
};
Led.prototype.off = function() {
this.platformLed.off();
this.info('off');
};
Led.prototype.pulse = function(duration) {
// turn off old pulse before starting new
this.stopPulse();
var duration = duration || 500;
this.info('pulse', 'duration ' + duration);
this.on();
var self = this;
this.timeoutId = setTimeout(function ledOff () {
self.off();
}, duration);
};
Led.prototype.stopPulse = function () {
this.info('stopPulse');
clearTimeout(this.timeoutId);
this.off();
};
Led.prototype.blink = function(freq) {
// in case it's a new frequency
// turn off old blinks before starting new
this.stopBlink();
var frequency = freq || 1;
this.info('blink', 'frequency ' + frequency);
var interval = 1000 / frequency;
var self = this;
this.intervalId = setInterval(function ledPulse () {
self.pulse(interval / 2);
}, interval);
};
Led.prototype.stopBlink = function() {
this.info('stopBlink');
clearInterval(this.intervalId);
this.off();
};
module.exports = Led;