Skip to content

Commit

Permalink
Add TS (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
jreina authored Jul 22, 2019
1 parent 4e50f7d commit d05bf4c
Show file tree
Hide file tree
Showing 29 changed files with 559 additions and 158 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,5 @@ typings/

# DynamoDB Local files
.dynamodb/

dist/
51 changes: 51 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc",
"prestart": "npm run build",
"start": "node ./dist/main.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"node-hid": "^0.7.9"
"bluebird": "^3.5.5",
"chance": "^1.0.18",
"node-hid": "^0.7.9",
"ramda": "^0.26.1"
},
"devDependencies": {
"@types/bluebird": "^3.5.27",
"@types/chance": "^1.0.5",
"@types/node": "^12.6.8",
"@types/node-hid": "^0.7.2",
"@types/ramda": "^0.26.16",
"typescript": "^3.5.3"
}
}
33 changes: 33 additions & 0 deletions src/engine/FixedDelayPatternProcessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { IPattern } from "../patterns/IPattern";
import { IBlyncDevice } from "../lib/IBlyncDevice";
import Bluebird from "bluebird";
import { Color } from "../patterns/Color";
import { IPatternEngine } from "./IPatternEngine";

export class FixedDelayPatternProcessor implements IPatternEngine {
private _createCommand([red, green, blue]: Color) {
return {
red,
green,
blue,
blink: 0 as 0 | 1 | 2 | 3,
dim: false
};
}
async process({ colors, delay, loop }: IPattern, device: IBlyncDevice) {
if (typeof colors === "function")
throw new Error("`colors` must be an array of colors");

let index = 0;
while (true) {
if (index === colors.length) {
if (loop) index = 0;
else break;
}
await device.sendCommand(this._createCommand(colors[index]));
await Bluebird.delay(delay);
index++;
}
return 0;
}
}
22 changes: 22 additions & 0 deletions src/engine/FunctionPatternProcessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Color } from "../patterns/Color";
import { IPattern } from "../patterns/IPattern";
import { IBlyncDevice } from "../lib/IBlyncDevice";
import Bluebird from "bluebird";
import { CommandFactory } from "../util/commandFactory";
import { IPatternEngine } from "./IPatternEngine";

export class FunctionPatternProcessor implements IPatternEngine {
private _current: Color = [0, 0, 0];

async process(pattern: IPattern, device: IBlyncDevice): Promise<number> {
if (Array.isArray(pattern.colors))
throw new Error("`colors` must be a function!");
while (this._current !== null) {
this._current = pattern.colors(this._current);
const command = CommandFactory.fromColor(this._current);
await Bluebird.delay(pattern.delay);
await device.sendCommand(command);
}
return 0;
}
}
6 changes: 6 additions & 0 deletions src/engine/IPatternEngine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { IBlyncDevice } from "../lib/IBlyncDevice";
import { IPattern } from "../patterns/IPattern";

export interface IPatternEngine {
process(pattern: IPattern, device: IBlyncDevice): Promise<number>;
}
33 changes: 33 additions & 0 deletions src/lib/BlyncStatic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { HID, devices } from "node-hid";
import { BlyncWireless } from "./BlyncWireless";

class BlyncStatic {
static getDevices() {
const hidDevices = devices();

const blyncs = hidDevices
.filter(
({ vendorId, productId, path }) =>
vendorId === 11277 && productId === 11 && path
)
.map(x => ({ ...x, path: x.path ? x.path : "" }))
.map(({ path }) => new BlyncWireless(new HID(path)));

return blyncs;
}
static getDevice(index: number) {
index = +index || 0;

const devices = this.getDevices();
if (index < 0) {
throw new Error("Invalid device index");
}
if (index >= devices.length) {
throw new Error("Device index #" + index + " not found");
}

return devices[index];
}
}

export { BlyncStatic };
59 changes: 59 additions & 0 deletions src/lib/BlyncWireless.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { HID } from "node-hid";
import { IBlyncCommand } from "./IBlyncCommand";
import { IBlyncDevice } from "./IBlyncDevice";

class BlyncWireless implements IBlyncDevice {
constructor(private hidDevice: HID) {}

private _createBuffer({
red,
green,
blue,
dim,
blink
}: IBlyncCommand): Array<number> {
let lightControl = 0b000000;
// Bit0: 0 - Light On, 1 - Light Off
// Bit1: 0 - No Dim (Full Brightness), 1 - Dim by 50%
if (dim) {
lightControl += 0b000010;
}
// Bit2: 0 - No Flash, 1- Start Flash (Blink)
// Bit5-Bit3 - Flash speed - 001 - slow, 010 - medium, 100- fast
switch (blink) {
case 0:
// no blink
break;
case 1: // slow
lightControl += 0b001100;
break;
case 2: // medium
lightControl += 0b010100;
break;
case 3: // slow
lightControl += 0b100100;
break;
}

const buffer = [
0x00,
red,
blue,
green,
lightControl,
0x00,
0x80,
0xff,
0x22
];

return buffer;
}

sendCommand = async (command: IBlyncCommand) => {
const buffer = this._createBuffer(command);
return this.hidDevice.write(buffer);
}
}

export { BlyncWireless };
7 changes: 7 additions & 0 deletions src/lib/IBlyncCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface IBlyncCommand {
red: number;
green: number;
blue: number;
dim: boolean;
blink: 0 | 1 | 2 | 3;
}
5 changes: 5 additions & 0 deletions src/lib/IBlyncDevice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { IBlyncCommand } from "./IBlyncCommand";

export interface IBlyncDevice {
sendCommand(command: IBlyncCommand): Promise<number>;
}
32 changes: 0 additions & 32 deletions src/lib/blync.js

This file was deleted.

71 changes: 0 additions & 71 deletions src/lib/device.js

This file was deleted.

Loading

0 comments on commit d05bf4c

Please sign in to comment.