Compare commits
3 Commits
master
...
feature/te
Author | SHA1 | Date | |
---|---|---|---|
e75899a5a8 | |||
a367cab8c3 | |||
2ecc2e7235 |
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,4 +1,6 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
dist/
|
dist/
|
||||||
|
coverage/
|
||||||
.vscode/
|
.vscode/
|
||||||
*.tgz
|
*.tgz
|
||||||
|
.DS_Store
|
||||||
|
20
.npmignore
20
.npmignore
@ -1,17 +1,5 @@
|
|||||||
boostmovehub.ts
|
src/
|
||||||
consts.ts
|
|
||||||
duplotrainbase.ts
|
|
||||||
hub.ts
|
|
||||||
index.ts
|
|
||||||
lpf2hub.ts
|
|
||||||
port.ts
|
|
||||||
poweredup.ts
|
|
||||||
puphub.ts
|
|
||||||
pupremote.ts
|
|
||||||
package-lock.json
|
|
||||||
tsconfig.json
|
|
||||||
tslint.json
|
|
||||||
utils.ts
|
|
||||||
wedo2smarthub.ts
|
|
||||||
.vscode/
|
.vscode/
|
||||||
examples/
|
examples/
|
||||||
|
coverage/
|
||||||
|
.DS_Store
|
@ -31,12 +31,12 @@ poweredUP.on("discover", async (hub) => { // Wait to discover Vernie and Remote
|
|||||||
switch (state) {
|
switch (state) {
|
||||||
case PoweredUP.Consts.ButtonState.UP: // If up is pressed, move the track forward
|
case PoweredUP.Consts.ButtonState.UP: // If up is pressed, move the track forward
|
||||||
{
|
{
|
||||||
vernie.setMotorSpeed(button === "LEFT" ? "A" : "B", 50);
|
vernie.setMotorSpeed(button === "LEFT" ? "A" : "B", 65);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case PoweredUP.Consts.ButtonState.DOWN: // If down is pressed, move the track backwards
|
case PoweredUP.Consts.ButtonState.DOWN: // If down is pressed, move the track backwards
|
||||||
{
|
{
|
||||||
vernie.setMotorSpeed(button === "LEFT" ? "A" : "B", -50);
|
vernie.setMotorSpeed(button === "LEFT" ? "A" : "B", -65);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case PoweredUP.Consts.ButtonState.RELEASED: // Stop the track when the button is released
|
case PoweredUP.Consts.ButtonState.RELEASED: // Stop the track when the button is released
|
||||||
|
20
jest.config.js
Normal file
20
jest.config.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
module.exports = {
|
||||||
|
preset: "ts-jest",
|
||||||
|
testEnvironment: "node",
|
||||||
|
collectCoverage: true,
|
||||||
|
roots: [
|
||||||
|
"<rootDir>/src"
|
||||||
|
],
|
||||||
|
transform: {
|
||||||
|
"^.+\\.tsx?$": "ts-jest"
|
||||||
|
},
|
||||||
|
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
|
||||||
|
moduleFileExtensions: [
|
||||||
|
"ts",
|
||||||
|
"tsx",
|
||||||
|
"js",
|
||||||
|
"jsx",
|
||||||
|
"json",
|
||||||
|
"node"
|
||||||
|
]
|
||||||
|
};
|
2592
package-lock.json
generated
2592
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
80
package.json
80
package.json
@ -1,40 +1,44 @@
|
|||||||
{
|
{
|
||||||
"name": "node-poweredup",
|
"name": "node-poweredup",
|
||||||
"version": "2.1.0",
|
"version": "2.1.0",
|
||||||
"description": "A Javascript module to interface with LEGO Powered UP components.",
|
"description": "A Javascript module to interface with LEGO Powered UP components.",
|
||||||
"homepage": "https://github.com/nathankellenicki/node-poweredup/",
|
"homepage": "https://github.com/nathankellenicki/node-poweredup/",
|
||||||
"main": "dist/node/index-node.js",
|
"main": "dist/node/index-node.js",
|
||||||
"types": "dist/node/index-node.d.ts",
|
"types": "dist/node/index-node.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build:node": "tslint -c tslint.json \"./src/*.ts\" && tsc",
|
"test": "jest",
|
||||||
"build:browser": "tslint -c tslint.json \"./src/*.ts\" && webpack --mode=production",
|
"build:node": "tslint -c tslint.json \"./src/*.ts\" && tsc",
|
||||||
"build:all": "tslint -c tslint.json \"./src/*.ts\" && tsc && webpack --mode=production",
|
"build:browser": "tslint -c tslint.json \"./src/*.ts\" && webpack --mode=production",
|
||||||
"docs": "jsdoc -d docs -c jsdoc.conf.json -t ./node_modules/ink-docstrap/template -R README.md dist/node/consts.js dist/node/poweredup-node.js dist/node/lpf2hub.js dist/node/wedo2smarthub.js dist/node/boostmovehub.js dist/node/puphub.js dist/node/pupremote.js dist/node/duplotrainbase.js dist/node/hub.js dist/node/consts.js",
|
"build:all": "tslint -c tslint.json \"./src/*.ts\" && tsc && webpack --mode=production",
|
||||||
"all": "npm run build:all && npm run docs",
|
"docs": "jsdoc -d docs -c jsdoc.conf.json -t ./node_modules/ink-docstrap/template -R README.md dist/node/consts.js dist/node/poweredup-node.js dist/node/lpf2hub.js dist/node/wedo2smarthub.js dist/node/boostmovehub.js dist/node/puphub.js dist/node/pupremote.js dist/node/duplotrainbase.js dist/node/hub.js dist/node/consts.js",
|
||||||
"prepublishOnly": "npm run build:node"
|
"all": "npm run test && npm run build:all && npm run docs",
|
||||||
},
|
"prepublishOnly": "npm run build:node"
|
||||||
"author": "Nathan Kellenicki <nathan@kellenicki.com>",
|
},
|
||||||
"license": "MIT",
|
"author": "Nathan Kellenicki <nathan@kellenicki.com>",
|
||||||
"dependencies": {
|
"license": "MIT",
|
||||||
"debug": "^4.1.1",
|
"dependencies": {
|
||||||
"noble": "1.9.1",
|
"debug": "^4.1.1",
|
||||||
"noble-mac": "git+https://github.com/Timeular/noble-mac.git#af4418e"
|
"noble": "1.9.1",
|
||||||
},
|
"noble-mac": "git+https://github.com/Timeular/noble-mac.git#af4418e"
|
||||||
"devDependencies": {
|
},
|
||||||
"@types/debug": "0.0.31",
|
"devDependencies": {
|
||||||
"@types/noble": "0.0.38",
|
"@types/debug": "0.0.31",
|
||||||
"@types/node": "^10.12.21",
|
"@types/jest": "^24.0.11",
|
||||||
"@types/web-bluetooth": "0.0.4",
|
"@types/noble": "0.0.38",
|
||||||
"ink-docstrap": "^1.3.2",
|
"@types/node": "^10.12.21",
|
||||||
"jsdoc": "^3.5.5",
|
"@types/web-bluetooth": "0.0.4",
|
||||||
"jsdoc-to-markdown": "^4.0.1",
|
"ink-docstrap": "^1.3.2",
|
||||||
"ts-loader": "^5.3.3",
|
"jest": "^24.7.1",
|
||||||
"tslint": "^5.12.1",
|
"jsdoc": "^3.5.5",
|
||||||
"typescript": "^3.3.1",
|
"jsdoc-to-markdown": "^4.0.1",
|
||||||
"webpack": "^4.29.2",
|
"ts-jest": "^24.0.1",
|
||||||
"webpack-cli": "^3.2.3"
|
"ts-loader": "^5.3.3",
|
||||||
},
|
"tslint": "^5.12.1",
|
||||||
"resolutions": {
|
"typescript": "^3.3.1",
|
||||||
"xpc-connection": "sandeepmistry/node-xpc-connection#pull/26/head"
|
"webpack": "^4.29.2",
|
||||||
}
|
"webpack-cli": "^3.2.3"
|
||||||
|
},
|
||||||
|
"resolutions": {
|
||||||
|
"xpc-connection": "sandeepmistry/node-xpc-connection#pull/26/head"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
64
src/__tests__/boostmovehub.test.ts
Normal file
64
src/__tests__/boostmovehub.test.ts
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
import * as Consts from "../consts";
|
||||||
|
import { BoostMoveHub } from "../boostmovehub";
|
||||||
|
import { TestDevice } from "../testdevice";
|
||||||
|
|
||||||
|
const device = new TestDevice()
|
||||||
|
const hub = new BoostMoveHub(device);
|
||||||
|
|
||||||
|
|
||||||
|
beforeAll((done) => {
|
||||||
|
device.on("discoverComplete", async () => {
|
||||||
|
await hub.connect();
|
||||||
|
device.clearOutbox(Consts.BLECharacteristic.LPF2_ALL);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
afterAll(async (done) => {
|
||||||
|
await hub.disconnect();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Set motor speed", async (done) => {
|
||||||
|
hub.setMotorSpeed("A", 52);
|
||||||
|
expect(await device.readFromOutbox(Consts.BLECharacteristic.LPF2_ALL)).toEqual(Buffer.from([0x08, 0x00, 0x81, 0x37, 0x11, 0x51, 0x00, 0x34]));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Set motor speed for specific amount of time", async (done) => {
|
||||||
|
hub.setMotorSpeed("A", 52, 3000);
|
||||||
|
expect(await device.readFromOutbox(Consts.BLECharacteristic.LPF2_ALL)).toEqual(Buffer.from([0x08, 0x00, 0x81, 0x37, 0x11, 0x51, 0x00, 0x34]));
|
||||||
|
expect(await device.readFromOutbox(Consts.BLECharacteristic.LPF2_ALL)).toEqual(Buffer.from([0x08, 0x00, 0x81, 0x37, 0x11, 0x51, 0x00, 0x00]));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Brake a motor", async (done) => {
|
||||||
|
hub.brakeMotor("A");
|
||||||
|
expect(await device.readFromOutbox(Consts.BLECharacteristic.LPF2_ALL)).toEqual(Buffer.from([0x08, 0x00, 0x81, 0x37, 0x11, 0x51, 0x00, 0x7f]));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Attempt to set two speeds on a port that only accepts one", async (done) => {
|
||||||
|
expect(() => hub.setMotorSpeed("A", [52, 32])).toThrow("Port A can only accept a single speed");
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Set light brightness", async (done) => {
|
||||||
|
hub.setLightBrightness("B", 74);
|
||||||
|
expect(await device.readFromOutbox(Consts.BLECharacteristic.LPF2_ALL)).toEqual(Buffer.from([0x08, 0x00, 0x81, 0x38, 0x11, 0x51, 0x00, 0x4a]));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Set light brightness for specific amount of time", async (done) => {
|
||||||
|
hub.setLightBrightness("B", 74, 2000);
|
||||||
|
expect(await device.readFromOutbox(Consts.BLECharacteristic.LPF2_ALL)).toEqual(Buffer.from([0x08, 0x00, 0x81, 0x38, 0x11, 0x51, 0x00, 0x4a]));
|
||||||
|
expect(await device.readFromOutbox(Consts.BLECharacteristic.LPF2_ALL)).toEqual(Buffer.from([0x08, 0x00, 0x81, 0x38, 0x11, 0x51, 0x00, 0x00]));
|
||||||
|
done();
|
||||||
|
});
|
64
src/__tests__/puphub.test.ts
Normal file
64
src/__tests__/puphub.test.ts
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
import * as Consts from "../consts";
|
||||||
|
import { PUPHub } from "../puphub";
|
||||||
|
import { TestDevice } from "../testdevice";
|
||||||
|
|
||||||
|
const device = new TestDevice()
|
||||||
|
const hub = new PUPHub(device);
|
||||||
|
|
||||||
|
|
||||||
|
beforeAll((done) => {
|
||||||
|
device.on("discoverComplete", async () => {
|
||||||
|
await hub.connect();
|
||||||
|
device.clearOutbox(Consts.BLECharacteristic.LPF2_ALL);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
afterAll(async (done) => {
|
||||||
|
await hub.disconnect();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Set motor speed", async (done) => {
|
||||||
|
hub.setMotorSpeed("A", 52);
|
||||||
|
expect(await device.readFromOutbox(Consts.BLECharacteristic.LPF2_ALL)).toEqual(Buffer.from([0x0a, 0x00, 0x81, 0x00, 0x11, 0x60, 0x00, 0x34, 0x00, 0x00]));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Set motor speed for specific amount of time", async (done) => {
|
||||||
|
hub.setMotorSpeed("A", 52, 3000);
|
||||||
|
expect(await device.readFromOutbox(Consts.BLECharacteristic.LPF2_ALL)).toEqual(Buffer.from([0x0a, 0x00, 0x81, 0x00, 0x11, 0x60, 0x00, 0x34, 0x00, 0x00]));
|
||||||
|
expect(await device.readFromOutbox(Consts.BLECharacteristic.LPF2_ALL)).toEqual(Buffer.from([0x0a, 0x00, 0x81, 0x00, 0x11, 0x60, 0x00, 0x00, 0x00, 0x00]));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Brake a motor", async (done) => {
|
||||||
|
hub.brakeMotor("A");
|
||||||
|
expect(await device.readFromOutbox(Consts.BLECharacteristic.LPF2_ALL)).toEqual(Buffer.from([0x0a, 0x00, 0x81, 0x00, 0x11, 0x60, 0x00, 0x7f, 0x00, 0x00]));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Attempt to set two speeds on a port that only accepts one", async (done) => {
|
||||||
|
expect(() => hub.setMotorSpeed("A", [52, 32])).toThrow("Port A can only accept a single speed");
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Set light brightness", async (done) => {
|
||||||
|
hub.setLightBrightness("B", 74);
|
||||||
|
expect(await device.readFromOutbox(Consts.BLECharacteristic.LPF2_ALL)).toEqual(Buffer.from([0x08, 0x00, 0x81, 0x01, 0x11, 0x51, 0x00, 0x4a]));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Set light brightness for specific amount of time", async (done) => {
|
||||||
|
hub.setLightBrightness("B", 74, 2000);
|
||||||
|
expect(await device.readFromOutbox(Consts.BLECharacteristic.LPF2_ALL)).toEqual(Buffer.from([0x08, 0x00, 0x81, 0x01, 0x11, 0x51, 0x00, 0x4a]));
|
||||||
|
expect(await device.readFromOutbox(Consts.BLECharacteristic.LPF2_ALL)).toEqual(Buffer.from([0x08, 0x00, 0x81, 0x01, 0x11, 0x51, 0x00, 0x00]));
|
||||||
|
done();
|
||||||
|
});
|
66
src/__tests__/pupremote.test.ts
Normal file
66
src/__tests__/pupremote.test.ts
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
import * as Consts from "../consts";
|
||||||
|
import { PUPRemote } from "../pupremote";
|
||||||
|
import { TestDevice } from "../testdevice";
|
||||||
|
|
||||||
|
const device = new TestDevice()
|
||||||
|
const remote = new PUPRemote(device);
|
||||||
|
|
||||||
|
|
||||||
|
beforeAll((done) => {
|
||||||
|
device.on("discoverComplete", async () => {
|
||||||
|
await remote.connect();
|
||||||
|
device.postToInbox(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x06, 0x00, 0x01, 0x02, 0x06, 0x00]));
|
||||||
|
device.postToInbox(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x0f, 0x00, 0x04, 0x00, 0x01, 0x37, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10]));
|
||||||
|
device.postToInbox(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x0f, 0x00, 0x04, 0x01, 0x01, 0x37, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10]));
|
||||||
|
device.clearOutbox(Consts.BLECharacteristic.LPF2_ALL);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
afterAll(async (done) => {
|
||||||
|
await remote.disconnect();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Set LED color via discrete value", async (done) => {
|
||||||
|
remote.setLEDColor(Consts.Color.BLUE);
|
||||||
|
expect(await device.readFromOutbox(Consts.BLECharacteristic.LPF2_ALL)).toEqual(Buffer.from([0x0a, 0x00, 0x41, 0x34, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]));
|
||||||
|
expect(await device.readFromOutbox(Consts.BLECharacteristic.LPF2_ALL)).toEqual(Buffer.from([0x08, 0x00, 0x81, 0x34, 0x11, 0x51, 0x00, 0x03]));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Turn off LED", async (done) => {
|
||||||
|
remote.setLEDColor(false);
|
||||||
|
expect(await device.readFromOutbox(Consts.BLECharacteristic.LPF2_ALL)).toEqual(Buffer.from([0x0a, 0x00, 0x41, 0x34, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]));
|
||||||
|
expect(await device.readFromOutbox(Consts.BLECharacteristic.LPF2_ALL)).toEqual(Buffer.from([0x08, 0x00, 0x81, 0x34, 0x11, 0x51, 0x00, 0x00]));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Set LED color via RGB values", async (done) => {
|
||||||
|
remote.setLEDRGB(127, 32, 233);
|
||||||
|
expect(await device.readFromOutbox(Consts.BLECharacteristic.LPF2_ALL)).toEqual(Buffer.from([0x0a, 0x00, 0x41, 0x34, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00]));
|
||||||
|
expect(await device.readFromOutbox(Consts.BLECharacteristic.LPF2_ALL)).toEqual(Buffer.from([0x0a, 0x00, 0x81, 0x34, 0x11, 0x51, 0x01, 0x7f, 0x20, 0xe9]));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Ensure buttons are correctly attached", async (done) => {
|
||||||
|
expect(remote.getPortDeviceType("LEFT")).toBe(Consts.DeviceType.POWERED_UP_REMOTE_BUTTON);
|
||||||
|
expect(remote.getPortDeviceType("RIGHT")).toBe(Consts.DeviceType.POWERED_UP_REMOTE_BUTTON);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Ensure UP button press events are emitted", async (done) => {
|
||||||
|
remote.on("button", (button, state) => {
|
||||||
|
expect(button).toBe("LEFT");
|
||||||
|
expect(state).toBe(Consts.ButtonState.UP);
|
||||||
|
remote.removeAllListeners();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
device.postToInbox(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x05, 0x00, 0x45, 0x00, 0x01]));
|
||||||
|
});
|
@ -36,6 +36,9 @@ export class LPF2Hub extends Hub {
|
|||||||
if (this.type === Consts.HubType.DUPLO_TRAIN_HUB) {
|
if (this.type === Consts.HubType.DUPLO_TRAIN_HUB) {
|
||||||
this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01]));
|
this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01]));
|
||||||
}
|
}
|
||||||
|
setTimeout(() => {
|
||||||
|
this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x03, 0x05])); // Request firmware version again
|
||||||
|
}, 200);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,7 +98,7 @@ export class PUPHub extends LPF2Hub {
|
|||||||
data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed)]);
|
data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed)]);
|
||||||
} else {
|
} else {
|
||||||
// @ts-ignore: The type of speed is properly checked at the start
|
// @ts-ignore: The type of speed is properly checked at the start
|
||||||
data = Buffer.from([0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]);
|
data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]);
|
||||||
}
|
}
|
||||||
this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data);
|
this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data);
|
||||||
const timeout = global.setTimeout(() => {
|
const timeout = global.setTimeout(() => {
|
||||||
@ -118,7 +118,7 @@ export class PUPHub extends LPF2Hub {
|
|||||||
data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed)]);
|
data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed)]);
|
||||||
} else {
|
} else {
|
||||||
// @ts-ignore: The type of speed is properly checked at the start
|
// @ts-ignore: The type of speed is properly checked at the start
|
||||||
data = Buffer.from([0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]);
|
data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]);
|
||||||
}
|
}
|
||||||
this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data);
|
this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data);
|
||||||
return resolve();
|
return resolve();
|
||||||
@ -149,6 +149,37 @@ export class PUPHub extends LPF2Hub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public setMotorAngle (port: string, angle: number, speed: number | [number, number] = 100) {
|
||||||
|
const portObj = this._portLookup(port);
|
||||||
|
if (!(portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR)) {
|
||||||
|
throw new Error("Angle rotation is only available when using a Boost Tacho Motor or Boost Move Hub Motor");
|
||||||
|
}
|
||||||
|
if (portObj.id !== "AB" && speed instanceof Array) {
|
||||||
|
throw new Error(`Port ${portObj.id} can only accept a single speed`);
|
||||||
|
}
|
||||||
|
portObj.cancelEventTimer();
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
portObj.busy = true;
|
||||||
|
let data = null;
|
||||||
|
if (portObj.id === "AB") {
|
||||||
|
data = Buffer.from([0x81, portObj.value, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]);
|
||||||
|
} else {
|
||||||
|
// @ts-ignore: The type of speed is properly checked at the start
|
||||||
|
data = Buffer.from([0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
||||||
|
}
|
||||||
|
data.writeUInt32LE(angle, 4);
|
||||||
|
this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data);
|
||||||
|
portObj.finished = () => {
|
||||||
|
return resolve();
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fully (hard) stop the motor on a given port.
|
* Fully (hard) stop the motor on a given port.
|
||||||
* @method PUPHub#brakeMotor
|
* @method PUPHub#brakeMotor
|
||||||
|
150
src/testdevice.ts
Normal file
150
src/testdevice.ts
Normal file
@ -0,0 +1,150 @@
|
|||||||
|
import Debug = require("debug");
|
||||||
|
import { EventEmitter } from "events";
|
||||||
|
import { IBLEDevice } from "./interfaces";
|
||||||
|
const debug = Debug("testdevice");
|
||||||
|
|
||||||
|
|
||||||
|
export class TestDevice extends EventEmitter implements IBLEDevice {
|
||||||
|
|
||||||
|
private _uuid: string;
|
||||||
|
private _name: string = "";
|
||||||
|
|
||||||
|
private _listeners: {[uuid: string]: any} = {};
|
||||||
|
private _characteristics: {[uuid: string]: any} = {};
|
||||||
|
|
||||||
|
private _queue: Promise<any> = Promise.resolve();
|
||||||
|
private _mailbox: Buffer[] = [];
|
||||||
|
|
||||||
|
private _outbox: {[uuid: string]: Buffer[]} = {};
|
||||||
|
private _outboxCallback: {[uuid: string]: () => void} = {};
|
||||||
|
private _inbox: {[uuid: string]: Buffer[]} = {};
|
||||||
|
private _inboxCallback: {[uuid: string]: (data: Buffer) => void} = {};
|
||||||
|
|
||||||
|
private _connected: boolean = false;
|
||||||
|
private _connecting: boolean = false;
|
||||||
|
|
||||||
|
|
||||||
|
constructor () {
|
||||||
|
super();
|
||||||
|
this._uuid = "test-device";
|
||||||
|
this._name = "Test Device";
|
||||||
|
setTimeout(() => {
|
||||||
|
this.emit("discoverComplete");
|
||||||
|
}, 2000);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public get uuid () {
|
||||||
|
return this._uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public get name () {
|
||||||
|
return this._name;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public get connecting () {
|
||||||
|
return this._connecting;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public get connected () {
|
||||||
|
return this._connected;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public connect () {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this._connected = true;
|
||||||
|
return resolve();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public disconnect () {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
return resolve();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public discoverCharacteristicsForService (uuid: string) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
return resolve();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public subscribeToCharacteristic (uuid: string, callback: (data: Buffer) => void) {
|
||||||
|
this._inbox[uuid] = this._inbox[uuid] || [];
|
||||||
|
if (this._inbox[uuid].length >= 1) {
|
||||||
|
// @ts-ignore
|
||||||
|
callback(this._inbox[uuid].shift());
|
||||||
|
}
|
||||||
|
this._inboxCallback[uuid] = (data) => {
|
||||||
|
callback(data);
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public addToCharacteristicMailbox (uuid: string, data: Buffer) {
|
||||||
|
this._mailbox.push(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public readFromCharacteristic (uuid: string, callback: (err: string | null, data: Buffer | null) => void) {
|
||||||
|
callback(null, Buffer.alloc(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public writeToCharacteristic (uuid: string, data: Buffer, callback?: () => void) {
|
||||||
|
this._outbox[uuid] = this._outbox[uuid] || [];
|
||||||
|
this._outbox[uuid].push(data);
|
||||||
|
if (this._outboxCallback[uuid]) {
|
||||||
|
this._outboxCallback[uuid]();
|
||||||
|
delete this._outboxCallback[uuid];
|
||||||
|
}
|
||||||
|
if (callback) {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public readFromOutbox (uuid: string) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this._outbox[uuid] = this._outbox[uuid] || [];
|
||||||
|
if (this._outbox[uuid].length >= 1) {
|
||||||
|
return resolve(this._outbox[uuid].shift());
|
||||||
|
} else {
|
||||||
|
this._outboxCallback[uuid] = () => {
|
||||||
|
return resolve(this._outbox[uuid].shift());
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public postToInbox (uuid: string, data: Buffer) {
|
||||||
|
if (this._inboxCallback[uuid]) {
|
||||||
|
this._inboxCallback[uuid](data);
|
||||||
|
} else {
|
||||||
|
this._inbox[uuid] = this._inbox[uuid] || [];
|
||||||
|
this._inbox[uuid].push(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public clearOutbox (uuid: string) {
|
||||||
|
this._outbox[uuid] = [];
|
||||||
|
delete this._outboxCallback[uuid];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private _sanitizeUUID (uuid: string) {
|
||||||
|
return uuid.replace(/-/g, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
104
tsconfig.json
104
tsconfig.json
@ -1,59 +1,59 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
/* Basic Options */
|
/* Basic Options */
|
||||||
"target": "es2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
|
"target": "es2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
|
||||||
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
|
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
|
||||||
// "lib": [], /* Specify library files to be included in the compilation. */
|
// "lib": [], /* Specify library files to be included in the compilation. */
|
||||||
// "allowJs": true, /* Allow javascript files to be compiled. */
|
// "allowJs": true, /* Allow javascript files to be compiled. */
|
||||||
// "checkJs": true, /* Report errors in .js files. */
|
// "checkJs": true, /* Report errors in .js files. */
|
||||||
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
|
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
|
||||||
"declaration": true, /* Generates corresponding '.d.ts' file. */
|
"declaration": true, /* Generates corresponding '.d.ts' file. */
|
||||||
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
|
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
|
||||||
"sourceMap": true, /* Generates corresponding '.map' file. */
|
"sourceMap": true, /* Generates corresponding '.map' file. */
|
||||||
// "outFile": "./", /* Concatenate and emit output to single file. */
|
// "outFile": "./", /* Concatenate and emit output to single file. */
|
||||||
"outDir": "./dist/node", /* Redirect output structure to the directory. */
|
"outDir": "./dist/node", /* Redirect output structure to the directory. */
|
||||||
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
|
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
|
||||||
// "composite": true, /* Enable project compilation */
|
// "composite": true, /* Enable project compilation */
|
||||||
// "removeComments": true, /* Do not emit comments to output. */
|
// "removeComments": true, /* Do not emit comments to output. */
|
||||||
// "noEmit": true, /* Do not emit outputs. */
|
// "noEmit": true, /* Do not emit outputs. */
|
||||||
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
|
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
|
||||||
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
|
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
|
||||||
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
|
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
|
||||||
|
|
||||||
/* Strict Type-Checking Options */
|
/* Strict Type-Checking Options */
|
||||||
"strict": true, /* Enable all strict type-checking options. */
|
"strict": true, /* Enable all strict type-checking options. */
|
||||||
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
|
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
|
||||||
// "strictNullChecks": true, /* Enable strict null checks. */
|
// "strictNullChecks": true, /* Enable strict null checks. */
|
||||||
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
|
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
|
||||||
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
|
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
|
||||||
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
|
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
|
||||||
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
|
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
|
||||||
|
|
||||||
/* Additional Checks */
|
/* Additional Checks */
|
||||||
// "noUnusedLocals": true, /* Report errors on unused locals. */
|
// "noUnusedLocals": true, /* Report errors on unused locals. */
|
||||||
// "noUnusedParameters": true, /* Report errors on unused parameters. */
|
// "noUnusedParameters": true, /* Report errors on unused parameters. */
|
||||||
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
|
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
|
||||||
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
|
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
|
||||||
|
|
||||||
/* Module Resolution Options */
|
/* Module Resolution Options */
|
||||||
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
|
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
|
||||||
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
|
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
|
||||||
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
|
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
|
||||||
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
|
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
|
||||||
// "typeRoots": [], /* List of folders to include type definitions from. */
|
// "typeRoots": [], /* List of folders to include type definitions from. */
|
||||||
// "types": [], /* Type declaration files to be included in compilation. */
|
// "types": [], /* Type declaration files to be included in compilation. */
|
||||||
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
|
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
|
||||||
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
|
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
|
||||||
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
|
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
|
||||||
|
|
||||||
/* Source Map Options */
|
/* Source Map Options */
|
||||||
// "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
|
// "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
|
||||||
// "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */
|
// "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */
|
||||||
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
|
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
|
||||||
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
|
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
|
||||||
|
|
||||||
/* Experimental Options */
|
/* Experimental Options */
|
||||||
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
|
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
|
||||||
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
|
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user