Skip to content

Commit

Permalink
Merge pull request #11 from iamthefox/v3
Browse files Browse the repository at this point in the history
Re-write to be more up to date with the current js
  • Loading branch information
iamthefox committed Apr 18, 2020
2 parents 3b8821c + f9ca2ab commit a22cd6a
Show file tree
Hide file tree
Showing 15 changed files with 3,449 additions and 304 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["@babel/preset-env"]
}
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# https://editorconfig.org
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
max_line_length = 80
trim_trailing_whitespace = true

[*.md]
max_line_length = 0
trim_trailing_whitespace = false

[COMMIT_EDITMSG]
max_line_length = 0
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/node_modules
/.idea
debug.js
/dist
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
76 changes: 38 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Luxafor

A simple library to control your Luxafor device via node.js
A simple library to control your Luxafor device using Node

## About Luxafor

Expand All @@ -16,24 +16,19 @@ You can go to http://luxafor.com/ to get more information about it.
## Installation
```
npm install luxafor-api --save
// or
yarn add luxafor-api
```

## Usage
```
const Luxafor = require('luxafor-api');
// api call to change the color
let opts = {
defaults: {
wave: {
type: 2,
speed: 90,
repeat: 5
}
}
};
device = new Luxafor(opts);
device.setColor('#fff');
const { device, devices } = require('luxafor-api');
const luxafor = device(); // use devices() to get all devices
device.color('red'); // or use hex color e.g. #fff
```

## Target LEDs
Expand All @@ -44,46 +39,51 @@ top row | 0x41
bottom row | 0x42
led #1-6 | 0x01 - 0x06




## API

### setColor(color, target)
Set target led's color. When target is not provided it assumes we want to change color for all leds.
### color(color, target?)

Set device color. Optionally can set target LED's from the table above. Defaults to all LEDs

### fadeTo(color, target, speed = 20)
### fadeTo(color, speed? = 20, target?)

Similar to setColor, the only difference it will transition smoothly from previous color to the one specified.
Fade device color from current to a given color.

speed is a number 0-255 that represents the speed of the transition, 0 is the quickest 255 is the slowest.

### flash(color, target, speed = 180, repeat = 5)
### flash(color, speed? = 180, repeat? = 5, target?)

Flash color for an amount of times specified in repeat parameter.

speed 0 to 255 number determines delay between each blink

repeat 0 to 255 number amount of times to blink before returning to previous state

### wave(color, type = 2, speed = 90, repeat = 5)
Starts a wave that goes through all the leds with the pattern specified in type variable
### wave(color, type = WAVE_SHORT, speed = 90, repeat = 5)
Starts a wave that goes through all the LEDs with the pattern specified in type variable

There are 4 types available:
* 1 - short wave
* 2 - long wave
* 3 - overlapping short wave
* 4 - overlapping long wave

### off()
Turns off all leds

### getTargets()
returns the object with all available targets
* WAVE_SHORT - short wave
* WAVE_LONG - long wave
* WAVE_SHORT_OVERLAPPING - overlapping short wave
* WAVE_LONG_OVERLAPPING - overlapping long wave

Type constants are available as so

```js
import { device, constants } from 'luxafor-api';

const luxafor = device();
luxafor.wave('blue', constants.WAVE_SHORT);
```

## getWaveTypes()
returns available wave types
### police(repeat? = 5)

*Note: each one of those methods will return (bool) true when command was successfully executed or new Error object when it fails.*
Starts a predefined police pattern. Goes back to previous state once repeat limit is reached.

### rainbow(repeat? = 5)

Starts a predefined rainbow pattern. Goes back to previous state once repeat limit is reached.

### off()
Turns off all leds
4 changes: 0 additions & 4 deletions index.js

This file was deleted.

33 changes: 27 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
{
"name": "luxafor-api",
"version": "2.0.0",
"description": "Control your Luxafor device via node.js",
"main": "index.js",
"version": "3.0.0",
"description": "Control your Luxafor device via Node",
"main": "dist/cjs/luxafor.js",
"module": "dist/es/luxafor.js",
"scripts": {
"test": "test"
"build": "rollup -c --environment BUILD:production"
},
"husky": {
"hooks": {
"pre-commit": "pretty-quick --staged"
}
},
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "https://github.com/iamthefox/luxafor"
Expand All @@ -14,7 +23,19 @@
"license": "MIT",
"keywords": "usb,luxafor,productivity",
"dependencies": {
"hex-rgb": "^1.0.0",
"node-hid": "^0.5.1"
"color-name": "^1.1.4",
"hex-rgb": "^4.1.0",
"node-hid": "^1.2.0"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.0",
"@babel/node": "^7.8.7",
"@babel/preset-env": "^7.9.5",
"@rollup/plugin-commonjs": "^11.1.0",
"@rollup/plugin-node-resolve": "^7.1.3",
"prettier": "^2.0.4",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-terser": "^5.3.0"
}
}
21 changes: 21 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import babel from "rollup-plugin-babel";
import { terser } from "rollup-plugin-terser";
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import pkg from "./package.json";

export default {
input: "src/Luxafor.js",
output: [
{
file: pkg.main,
format: "cjs",
},
{
file: pkg.module,
format: "es",
},
],
plugins: [resolve(), commonjs(), babel(), terser()],
external: ["node-hid"],
};
111 changes: 111 additions & 0 deletions src/Device.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import os from "os";
import hex2rgb from "hex-rgb";
import colors from "color-name";
import { WAVE_SHORT } from "./util/constants";

class Device {
constructor(hidDevice) {
this._hidDevice = hidDevice;
this._hidDevice.pause();
}

/**
* Set device color
* @param color string
* @param target Number
*/
color(color, target = 0xff) {
this._write(0x01, target, color, [0x00, 0x00, 0x00]);
}

/**
* Fade to color
* @param color string
* @param target Number
* @param speed Number
*/
fadeTo(color, speed = 20, target = 0xff) {
this._write(0x02, target, color, [speed, 0x00, 0x00]);
}

/**
* Flash color given number of times
* @param color string
* @param target Number
* @param speed Number
* @param repeat Number
*/
flash(color, speed = 20, repeat = 5, target = 0xff) {
this._write(0x03, target, color, [speed, 0x00, repeat]);
}

/**
* Start a wave with a given parameters
* @param color
* @param type
* @param speed
* @param repeat
*/
wave(color, type = WAVE_SHORT, speed = 5, repeat = 5) {
this._write(0x04, type, color, [0x00, repeat, speed]);
}

/**
* Start a police pattern
* @param repeat
*/
police(repeat = 5) {
this._writeRaw([0x06, 0x05, repeat]);
}

/**
* Start a rainbow pattern
* @param repeat
*/
rainbow(repeat = 5) {
this._writeRaw([0x06, 0x08, repeat]);
}

/**
* Turn off all the LEDs
*/
off() {
this.color("#000");
}

_resolveColor(color) {
if (color && color.startsWith("#")) {
return hex2rgb(color);
}

if (color && colors[color]) {
return {
red: colors[color][0],
green: colors[color][1],
blue: colors[color][2],
};
}

return { red: 0x00, green: 0x00, blue: 0x00 };
}

_write(command, target, color, options = []) {
const { red, green, blue } = this._resolveColor(color);

const data = [command, target, red, green, blue, ...options];

this._writeRaw(data);
}

_writeRaw(data) {
if (os.platform() === "win32") {
data.unshift(0);
}

this._hidDevice.resume();
this._hidDevice.write(data);
this._hidDevice.pause();
}
}

export default Device;
8 changes: 8 additions & 0 deletions src/Luxafor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import resolveDevices from "./util/resolveDevices";
import * as constants from "./util/constants";

export default {
devices: resolveDevices,
device: () => resolveDevices().shift(),
constants,
};
47 changes: 0 additions & 47 deletions src/constants.js

This file was deleted.

Loading

0 comments on commit a22cd6a

Please sign in to comment.