Comparing version 0.0.0 to 0.0.1
var blink = require('../'); | ||
// get your blink(1) device | ||
blink(function(err, b) { | ||
// b.blink(); | ||
// blink from #ff0000 to #000000 every 1000ms | ||
// or 1s | ||
b.blink([255, 0, 0], 1000, [0, 0, 0]); | ||
}); |
var blink = require('../'); | ||
// Get your blink(1) device | ||
blink(function(err, b) { | ||
// start with pure white | ||
var color = [255, 255, 255]; | ||
// randomize colors, bounding them by 0->255 | ||
function wander(x) { | ||
return Math.min(255, | ||
return Math.round( | ||
// bound by 255 as the max | ||
Math.min(255, | ||
// bound by 0 as the min | ||
Math.max(0, | ||
x + (Math.random() - 0.5) * 50)) | 0; | ||
// change the number by adding it to a random number from -128 to 128 | ||
x + (Math.random() - 0.5) * 256))); | ||
} | ||
// Every second, wander to a new random color, having it fade | ||
// for 1000ms - 1s | ||
setInterval(function() { | ||
// randomize colors | ||
color = color.map(wander); | ||
b.setRGB(color, 1000); | ||
}, 100); | ||
}, 1000); | ||
}); |
92
index.js
@@ -1,21 +0,43 @@ | ||
var HID = require('HID'); | ||
var HID = require('HID'), | ||
parseColor = require('./lib/colorparser').parseCSSColor; | ||
var VENDOR_ID = 10168; | ||
function blink(cb) { | ||
var b = {}, | ||
VENDOR_ID = 10168, | ||
white = [255, 255, 255], | ||
black = [0, 0, 0], | ||
interval; | ||
b.blink1_degamma_log2lin = function(n) { | ||
// this mostly just from blink1-lib - converts colors into usable | ||
// values for wRGB's message format | ||
function degamma(n) { | ||
return (((1<<(n/32))-1) + ((1<<(n/32))*((n%32)+1)+15)/32); | ||
}; | ||
} | ||
// parse a color value if it needs parsing into a rgb 3-element array, | ||
// and set the color to black if it cannot be parsed | ||
function parseRgb(rgb) { | ||
if (typeof rgb === 'object') return rgb; | ||
if (typeof rgb === 'undefined') { | ||
console.log('Color value undefined'); | ||
return black; | ||
} | ||
var c = parseColor(rgb); | ||
if (!c) { | ||
console.log('Could not parse color', rgb); | ||
return black; | ||
} | ||
return c; | ||
} | ||
// create a message that writes a certain rgb value to the blink | ||
function wRGB(rgb, ms) { | ||
var dms = ms/10; | ||
var msg = new Buffer(9); | ||
rgb = parseRgb(rgb); | ||
msg[0] = 1; | ||
msg[1] = 'c'.charCodeAt(0); | ||
msg[2] = b.blink1_degamma_log2lin(rgb[0]) | 0; | ||
msg[3] = b.blink1_degamma_log2lin(rgb[1]) | 0; | ||
msg[4] = b.blink1_degamma_log2lin(rgb[2]) | 0; | ||
msg[1] = (ms === 0 ? 'n' : 'c').charCodeAt(0); | ||
msg[2] = degamma(rgb[0]) | 0; | ||
msg[3] = degamma(rgb[1]) | 0; | ||
msg[4] = degamma(rgb[2]) | 0; | ||
msg[5] = dms >> 8; | ||
@@ -26,15 +48,4 @@ msg[6] = dms % 0xff; | ||
function rRGB(rgb, ms) { | ||
var msg = new Buffer(9); | ||
msg[0] = 1; | ||
msg[1] = 'R'.charCodeAt(0); | ||
msg[2] = 0; | ||
msg[3] = 0; | ||
msg[4] = 0; | ||
msg[5] = 0; | ||
msg[6] = 0; | ||
msg[7] = 0; | ||
return msg; | ||
} | ||
// try to find and connect to a blink(1) device, returning an error | ||
// if it is not accessible | ||
function setup(b) { | ||
@@ -49,18 +60,28 @@ b.device = HID.devices().filter(function(d) { | ||
// set an rgb value. if ms is not given or is 0, it is set immediately - | ||
// otherwise it is faded two with the blink(1)'s internal functionality | ||
b.setRGB = function(rgb, ms) { | ||
b.connection.write(wRGB(rgb, ms || 100)); | ||
b.connection.write(wRGB(rgb, ms || 0)); | ||
return b; | ||
}; | ||
b.getRGB = function(rgb, ms) { | ||
b.connection.read(function(err, res) { | ||
console.log(arguments); | ||
}); | ||
b.connection.write(rRGB()); | ||
// simple on & off shortcuts | ||
b.on = function() { b.setRGB(white); }; | ||
b.off = function() { b.setRGB(black); }; | ||
// get the blink's version number. mine is 100. this bit of code | ||
// is very directly inspired by node-blink1 | ||
b.version = function() { | ||
b.connection.sendFeatureReport([1, | ||
'v'.charCodeAt(0), 0, 0, 0, 0, 0, 0, 0]); | ||
var response = b.connection.getFeatureReport(1, 9); | ||
return ((response[3] - 0x30) * 100 + (response[4] - 0x30)); | ||
}; | ||
// a manual blink from one color to another | ||
b.blink = function(on, ms, off) { | ||
on = on || [255, 255, 255]; | ||
b.stop(); | ||
on = on || white; | ||
ms = ms || 500; | ||
off = off || [0, 0, 0]; | ||
off = off || black; | ||
var phase = true; | ||
@@ -74,4 +95,8 @@ interval = setInterval(function() { | ||
// stop any currently running inverval | ||
b.stop = function() { | ||
if (interval) clearInterval(interval); | ||
if (interval) { | ||
clearInterval(interval); | ||
interval = null; | ||
} | ||
}; | ||
@@ -81,6 +106,5 @@ | ||
if (err) return cb(err); | ||
else return cb(null, b); | ||
return cb(err, b); | ||
} | ||
module.exports = blink; |
{ | ||
"name": "node-blink", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "blink(1) support for node", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,2 +5,4 @@ # node-blink | ||
npm install node-blink | ||
```javascript | ||
@@ -11,11 +13,49 @@ var blink = require('../'); | ||
blink(function(err, b) { | ||
// blink on & off every second from #fff to #000 | ||
b.blink(); | ||
// or with specifics | ||
b.blink([255, 0, 0], 1000, [0, 0, 0]); | ||
// fade to an rgb color | ||
b.setRGB(color, 1000); | ||
b.setRGB([0, 10, 50], 1000); | ||
// set an rgb color instantly | ||
b.setRGB([255, 0, 100]); | ||
// even use hex colors | ||
b.setRGB('#ace'); | ||
// named css colors | ||
b.setRGB('cyan'); | ||
// and hsl! | ||
b.setRGB('hsl(35, 100%, 50%)'); | ||
// just turn it on (white) | ||
b.on(); | ||
// and off (black) | ||
b.off(); | ||
// get your blink(1)'s version | ||
console.log(b.version()); | ||
// programatically change colors with setInterval | ||
var angle = 0; | ||
setInterval(function() { | ||
if (++angle > 360) angle = 0; | ||
b.setRGB('hsl(' + angle + ', 100%, 50%)'); | ||
}, 100); | ||
}); | ||
``` | ||
Uses [node-hid](https://github.com/hanshuebner/node-hid) for most magic. | ||
* Uses [node-hid](https://github.com/hanshuebner/node-hid) for most magic. | ||
* Includes deanm's wonderful [css-color-parser-js](https://github.com/deanm/css-color-parser-js) for CSS color support | ||
## See Also | ||
* [node-blink1](https://github.com/sandeepmistry/node-blink1) beat me by 23 hours | ||
* [the official software for the blink1](https://github.com/todbot/blink1) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26524
15
533
60
1