Skip to content

Instantly share code, notes, and snippets.

@mauricesvay
Created October 19, 2012 20:13
Show Gist options
  • Save mauricesvay/3920415 to your computer and use it in GitHub Desktop.
Save mauricesvay/3920415 to your computer and use it in GitHub Desktop.
Controlling cheap USB LED notifiers with nodejs and node-hid
/* Controlling cheap USB LED notifiers with node-hid */
var Notifier = function(hid) {
this.hid = hid;
this.colors = ['OFF', 'BLUE', 'RED', 'GREEN', 'CYAN', 'PURPLE', 'YELLOW', 'WHITE'];
};
Notifier.prototype.write = function(arBytes) {
this.hid.write(arBytes);
};
Notifier.prototype.color = function(value) {
var color;
if (typeof value == 'number') {
color = value;
}
if (typeof value == 'string') {
color = this.colors.indexOf(value);
}
this.write([ color, 0, 0, 0, 0, 255, 0, 0 ] );
return this;
};
Notifier.prototype.off = function() {
this.color(Notifier.OFF);
return this;
};
Notifier.prototype.play = function(seq, delay, loop, i) {
var self = this;
var color;
if (typeof loop === 'undefined') {
loop = false;
}
if (typeof i === 'undefined') {
i = 0;
}
color = seq[i];
if (i < seq.length) {
this.color(color);
i++;
} else {
if (loop) {
i = 0;
} else {
return;
}
}
setTimeout(function(){
self.play(seq, delay, loop, i);
}, delay);
};
/******************************************************************************/
var HID = require('HID');
var vendor = 4756;
var product = 4896;
var hid = new HID.HID(vendor, product);
var notifier = new Notifier(hid);
process.on('SIGINT', function () {
notifier.color('OFF');
process.exit();
});
notifier.play(
['RED','YELLOW','GREEN','CYAN','PURPLE'],
500,
true
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment