Skip to content

Instantly share code, notes, and snippets.

@karlosgliberal
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save karlosgliberal/9ef27f01c30cebbd6274 to your computer and use it in GitHub Desktop.

Select an option

Save karlosgliberal/9ef27f01c30cebbd6274 to your computer and use it in GitHub Desktop.
Envio al puerto serie desde firebase
var Firebase = require('firebase');
var pixelDataRef = new Firebase('https://tufirebase.firebaseio.com/default');
var drawPixel = function(snapshot){
var coords = snapshot.name().split(":");
if (myPort.options.open) {
myPort.write("my string\n"+coords[0]);
myPort.write("my string\t"+coords[1]);
myPort.write("my string\f"+hexToRgb(snapshot.val()).r);
myPort.write("my string\\"+hexToRgb(snapshot.val()).g);
myPort.write("my string\b"+hexToRgb(snapshot.val()).b);
}
};
pixelDataRef.on('child_added', drawPixel);
pixelDataRef.on('child_changed', drawPixel);
pixelDataRef.on('child_removed', drawPixel);
function hexToRgb(hex) {
var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, function(m, r, g, b) {
return r + r + g + g + b + b;
});
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment