Added speed mapping

This commit is contained in:
Nathan Kunicki 2018-06-26 14:56:39 +01:00
parent b5f298cee3
commit 9c13d2755a
3 changed files with 27 additions and 5 deletions

View File

@ -102,14 +102,14 @@ export class BoostHub extends Hub {
const portObj = this._ports[port]; const portObj = this._ports[port];
if (time) { if (time) {
portObj.busy = true; portObj.busy = true;
const data = Buffer.from([0x0c, 0x00, 0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, speed, 0x64, 0x7f, 0x03]); const data = Buffer.from([0x0c, 0x00, 0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
data.writeUInt16LE(time > 65535 ? 65535 : time, 6); data.writeUInt16LE(time > 65535 ? 65535 : time, 6);
characteristic.write(data, false); characteristic.write(data, false);
portObj.finished = () => { portObj.finished = () => {
return resolve(); return resolve();
}; };
} else { } else {
const data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x01, speed, 0x64, 0x7f, 0x03]); const data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
characteristic.write(data, false); characteristic.write(data, false);
return resolve(); return resolve();
} }
@ -134,7 +134,7 @@ export class BoostHub extends Hub {
portObj.busy = true; portObj.busy = true;
const data = Buffer.from([0x0e, 0x00, 0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x7f, 0x03]); const data = Buffer.from([0x0e, 0x00, 0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x7f, 0x03]);
data.writeUInt32LE(angle, 6); data.writeUInt32LE(angle, 6);
data.writeInt8(speed, 10); data.writeUInt8(this._mapSpeed(speed), 10);
characteristic.write(data, false); characteristic.write(data, false);
portObj.finished = () => { portObj.finished = () => {
return resolve(); return resolve();

24
hub.ts
View File

@ -6,6 +6,7 @@ import { Port } from "./port";
import * as Consts from "./consts"; import * as Consts from "./consts";
import Debug = require("debug"); import Debug = require("debug");
import { METHODS } from "http";
const debug = Debug("hub"); const debug = Debug("hub");
@ -16,7 +17,8 @@ const debug = Debug("hub");
export class Hub extends EventEmitter { export class Hub extends EventEmitter {
public autoSubscribe: boolean; public autoSubscribe: boolean = true;
public useSpeedMap: boolean = true;
public type: Consts.Hubs = Consts.Hubs.UNKNOWN; public type: Consts.Hubs = Consts.Hubs.UNKNOWN;
public uuid: string; public uuid: string;
@ -225,6 +227,26 @@ export class Hub extends EventEmitter {
} }
protected _mapSpeed (speed: number) {
if (!this.useSpeedMap) {
return speed;
}
if (speed >= 1) {
if (speed > 100) {
speed = 100;
}
return Math.round((speed - 1) * (97 - 15) / (100 - 1) + 15);
} else if (speed <= -1) {
if (speed < -100) {
speed = -100;
}
return Math.round((speed - -100) * (245 - 160) / (-1 - -100) + 160);
} else {
return 0;
}
}
private _getModeForDeviceType (type: Consts.Devices) { private _getModeForDeviceType (type: Consts.Devices) {
switch (type) { switch (type) {
case Consts.Devices.BASIC_MOTOR: case Consts.Devices.BASIC_MOTOR:

View File

@ -107,7 +107,7 @@ export class WeDo2Hub extends Hub {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE]; const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE];
if (characteristic) { if (characteristic) {
characteristic.write(Buffer.from([this._ports[port].value, 0x01, 0x02, speed]), false); characteristic.write(Buffer.from([this._ports[port].value, 0x01, 0x02, this._mapSpeed(speed)]), false);
return resolve(); return resolve();
} }
}); });