Comparing version
#!/usr/bin/env node | ||
const blinkstick = require('blinkstick') | ||
// https://github.com/arvydas/blinkstick-node/wiki | ||
/* eslint-disable no-await-in-loop */ | ||
/* eslint-disable no-console */ | ||
const stick = blinkstick.findFirst() | ||
if (!stick) { | ||
console.error('Cannot find blinkstick') | ||
process.exit(2) | ||
} | ||
const { allLeds, setColorAsync } = require('./helpers') | ||
const channel = 0 | ||
const SINGLE_COMMAND = 3 | ||
const LED_COMMAND = 4 | ||
const setColorAsync = (color, opts) => ( | ||
new Promise( (resolve, reject) => { | ||
stick.setColor(color, opts, (err, result) => { | ||
if (!err) { | ||
resolve(result) | ||
} else { | ||
console.error(color, opts, err, result) | ||
reject(err, result) | ||
} | ||
}) | ||
}) | ||
) | ||
const processLedCommand = (color, led) => { | ||
setColorAsync(color, led) | ||
} | ||
const allLeds = async (color) => { | ||
const result = [] | ||
for (let index = 0; index < 8; index += 1) { | ||
const r = await setColorAsync(`${color}`, { channel, index }) | ||
result.push(r) | ||
} | ||
return result | ||
const processSingleCommand = (color) => { | ||
allLeds(color) | ||
} | ||
@@ -42,11 +22,17 @@ | ||
const cmd = process.argv[2] | ||
let color = process.argv[2] | ||
if (color === 'off') color = 'black' | ||
if (color === 'on') color = 'white' | ||
switch (cmd) { | ||
case 'off' : | ||
allLeds("#000000") | ||
switch (process.argv.length) { | ||
case SINGLE_COMMAND: | ||
processSingleCommand(color) | ||
break | ||
default : | ||
allLeds(cmd) | ||
case LED_COMMAND: | ||
processLedCommand(color, process.argv[3]) | ||
break | ||
default: | ||
console.error('Too many arguments') | ||
process.exit(9) | ||
} | ||
console.log('done') | ||
{ | ||
"name": "blinkie", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "App for controlling blinkstick", | ||
@@ -5,0 +5,0 @@ "main": "blinkie.js", |
@@ -5,7 +5,15 @@ # blinkie | ||
The following should install and link the executable onto your path: | ||
``` | ||
npm install blinkie | ||
npm link blinkie | ||
``` | ||
# Controling all 8 LEDs | ||
You can turn them all off with: | ||
You can turn them all on then off with: | ||
``` | ||
blinkie on | ||
blinkie off | ||
@@ -20,1 +28,9 @@ ``` | ||
``` | ||
Finally you can control a single LED with its index, by adding the index to the command line, | ||
for example, any of the following will work: | ||
``` | ||
blinkie red 0 | ||
blinkie "#ff0000" 2 | ||
``` |
3956
28.48%5
25%64
45.45%35
84.21%