Skip to content

Commit 3bc1d7d

Browse files
WolfspiritMNico
authored and
Nico
committed
Initial version (chroma-sdk#5)
* Initial Commit * Changed Readme to include yarn instructions * Add BcaLoader * Reorganized Folders * Add lint and circle-ci * Move VScode task file * Fix linting errors * Update tslint to a stricter ruleset * Update readme [skip ci] * Update readme to mention chroma rest api
1 parent 876e0f1 commit 3bc1d7d

31 files changed

+1366
-1
lines changed

.gitignore

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Project specific
2+
dist/
3+
4+
# Logs
5+
logs
6+
*.log
7+
npm-debug.log*
8+
yarn-debug.log*
9+
yarn-error.log*
10+
11+
# Runtime data
12+
pids
13+
*.pid
14+
*.seed
15+
*.pid.lock
16+
17+
# Directory for instrumented libs generated by jscoverage/JSCover
18+
lib-cov
19+
20+
# Coverage directory used by tools like istanbul
21+
coverage
22+
23+
# nyc test coverage
24+
.nyc_output
25+
26+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
27+
.grunt
28+
29+
# Bower dependency directory (https://bower.io/)
30+
bower_components
31+
32+
# node-waf configuration
33+
.lock-wscript
34+
35+
# Compiled binary addons (http://nodejs.org/api/addons.html)
36+
build/Release
37+
38+
# Dependency directories
39+
node_modules/
40+
jspm_packages/
41+
42+
# Typescript v1 declaration files
43+
typings/
44+
45+
# Optional npm cache directory
46+
.npm
47+
48+
# Optional eslint cache
49+
.eslintcache
50+
51+
# Optional REPL history
52+
.node_repl_history
53+
54+
# Output of 'npm pack'
55+
*.tgz
56+
57+
# Yarn Integrity file
58+
.yarn-integrity
59+
60+
# dotenv environment variables file
61+
.env

.npmignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
src/
2+
sample/
3+
.vscode/
4+
index.ts
5+
tsconfig.json
6+
tslint.json

.vscode/tasks.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "0.1.0",
5+
"command": "tsc",
6+
"isShellCommand": true,
7+
"args": ["-p", "."],
8+
"showOutput": "silent",
9+
"problemMatcher": "$tsc"
10+
}

README.md

+49-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,49 @@
1-
# chroma.js
1+
# Chroma.js
2+
3+
[![CircleCI](https://circleci.com/gh/chroma-sdk/chroma-js.svg?style=svg&circle-token=d066d70fae54ca26b07aad36250e78d478c62790)](https://circleci.com/gh/chroma-sdk/chroma-js)
4+
5+
Chroma.js is a library that provides a simple interface to interact with Razer's Chroma REST API.
6+
7+
## Building from source
8+
9+
After cloning this repository the Typescript Code needs to be transpiled.
10+
11+
This is done either by using your IDE (VS-Code is configured) or by going into the "sdk" directory and running:
12+
13+
```node
14+
npm install
15+
npm run build
16+
```
17+
18+
or
19+
20+
```node
21+
yarn install
22+
yarn run build
23+
```
24+
25+
Now the `dist` folder contains all the needed files.
26+
27+
Afterwards you can start the example by executing the following in the example folder:
28+
29+
```node
30+
npm install
31+
npm run dev
32+
```
33+
34+
or
35+
36+
```node
37+
yarn install
38+
yarn run dev
39+
```
40+
41+
This will start a Javascript development Server running on `http://localhost:8080`
42+
43+
To run the Node.js version just do in the example directory:
44+
45+
```node
46+
yarn install
47+
yarn run build
48+
node dist/Server.bundle.js
49+
```

circle.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
machine:
2+
node:
3+
version: 6.1.0
4+
5+
dependencies:
6+
override:
7+
- npm install
8+
9+
test:
10+
override:
11+
- npm run lint
12+
- npm run build
13+
- npm test

index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
export * from "./src/ChromaApp";
3+
export * from "./src/ChromaInstance";
4+
export * from "./src/Color";
5+
export * from "./src/Key";
6+
export * from "./src/Devices/Base";
7+
export * from "./src/Animation";
8+
export * from "./src/Devices/Keyboard";

package.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "@chromasdk/chromajs",
3+
"version": "0.1.0",
4+
"description": "",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/chroma-sdk/chroma-js.git"
8+
},
9+
"bugs": {
10+
"url": "https://github.com/chroma-sdk/chroma-js/issues"
11+
},
12+
"main": "./dist/index.js",
13+
"types": "./dist/main.d.ts",
14+
"dependencies": {
15+
"isomorphic-fetch": "2.2.1"
16+
},
17+
"devDependencies": {
18+
"nyc": "10.3.2",
19+
"rimraf": "2.6.1",
20+
"tslint": "5.3.2",
21+
"typescript": "2.3.3"
22+
},
23+
"scripts": {
24+
"build": "tsc",
25+
"dev": "tsc -w",
26+
"lint": "tslint src/**/*.ts{,x}"
27+
}
28+
}

src/Animation.ts

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import {AnimationFrame} from "./AnimationFrame";
2+
import {ChromaInstance} from "./ChromaInstance";
3+
import Color from "./Color";
4+
import {DeviceRequestData} from "./DeviceRequestData";
5+
import DeviceContainer from "./Devices";
6+
import {IDevice, IDeviceData} from "./Devices/Base";
7+
import ChromaLink from "./Devices/ChromaLink";
8+
import Headset from "./Devices/Headset";
9+
import Keyboard from "./Devices/Keyboard";
10+
import Keypad from "./Devices/Keypad";
11+
import Mouse from "./Devices/Mouse";
12+
import Mousepad from "./Devices/Mousepad";
13+
import Effect from "./Effect";
14+
15+
function sleep(time: number) {
16+
return new Promise((resolve) => setTimeout(resolve, time));
17+
}
18+
19+
export interface IPlayInstance {
20+
send(container: DeviceContainer): void;
21+
deleteEffect(effects: string[]): Promise<any>;
22+
sendDeviceUpdate(devices: IDeviceData[], store: boolean): Promise<any>;
23+
}
24+
25+
export class Animation {
26+
public Frames: AnimationFrame[] = [];
27+
public isPlaying: boolean = false;
28+
public Instance: IPlayInstance = null;
29+
public currentFrame: number = 0;
30+
private isInit: boolean= false;
31+
32+
public async play(instance: IPlayInstance) {
33+
if (!this.isInit) {
34+
this.isInit = true;
35+
await this.createFrames();
36+
}
37+
this.Instance = instance;
38+
this.isPlaying = true;
39+
this.currentFrame = 0;
40+
await this.createEffects(instance);
41+
42+
this.playLoop(instance);
43+
}
44+
45+
public async playLoop(instance: IPlayInstance) {
46+
for (const i of this.Frames){
47+
await instance.send(i);
48+
await sleep(i.delay);
49+
if (!this.isPlaying) {
50+
break;
51+
}
52+
}
53+
if (this.isPlaying) {
54+
this.playLoop(instance);
55+
}
56+
}
57+
58+
public async stop() {
59+
this.isPlaying = false;
60+
const effectIds: string[] = [];
61+
for (const frame of this.Frames){
62+
if (frame.Keyboard.effectId !== "") {
63+
effectIds.push(frame.Keyboard.effectId);
64+
}
65+
frame.Keyboard.effectId = "";
66+
}
67+
68+
await this.Instance.deleteEffect(effectIds);
69+
}
70+
71+
public async createEffects(instance: IPlayInstance) {
72+
this.Instance = instance;
73+
const keyboardEffectData: any = [];
74+
const device = new DeviceRequestData();
75+
device.device = "keyboard";
76+
77+
for (const frame of this.Frames){
78+
keyboardEffectData.push(frame.Keyboard.effectData);
79+
}
80+
81+
device.effectData = {
82+
effects: keyboardEffectData,
83+
};
84+
85+
const response = await instance.sendDeviceUpdate([device], true);
86+
const keyboardids = response[0];
87+
88+
for (let i = 0; i < keyboardids.length; i++) {
89+
this.Frames[i].Keyboard.effectId = keyboardids[i] !== null ? keyboardids[i].id : "";
90+
}
91+
return;
92+
}
93+
94+
public async createFrames() {
95+
for (let i = 0; i < 10; i++) {
96+
const frame = new AnimationFrame();
97+
frame.Keyboard.setAll(new Color("ff0000"));
98+
this.Frames.push(frame);
99+
}
100+
}
101+
}

src/AnimationFrame.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import DeviceContainer from "./Devices";
2+
3+
export class AnimationFrame extends DeviceContainer {
4+
public delay: number = 1000 / 15;
5+
}

0 commit comments

Comments
 (0)