Added speed mapping
This commit is contained in:
parent
b5f298cee3
commit
9c13d2755a
@ -102,14 +102,14 @@ export class BoostHub extends Hub {
|
||||
const portObj = this._ports[port];
|
||||
if (time) {
|
||||
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);
|
||||
characteristic.write(data, false);
|
||||
portObj.finished = () => {
|
||||
return resolve();
|
||||
};
|
||||
} 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);
|
||||
return resolve();
|
||||
}
|
||||
@ -134,7 +134,7 @@ export class BoostHub extends Hub {
|
||||
portObj.busy = true;
|
||||
const data = Buffer.from([0x0e, 0x00, 0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x7f, 0x03]);
|
||||
data.writeUInt32LE(angle, 6);
|
||||
data.writeInt8(speed, 10);
|
||||
data.writeUInt8(this._mapSpeed(speed), 10);
|
||||
characteristic.write(data, false);
|
||||
portObj.finished = () => {
|
||||
return resolve();
|
||||
|
24
hub.ts
24
hub.ts
@ -6,6 +6,7 @@ import { Port } from "./port";
|
||||
import * as Consts from "./consts";
|
||||
|
||||
import Debug = require("debug");
|
||||
import { METHODS } from "http";
|
||||
const debug = Debug("hub");
|
||||
|
||||
|
||||
@ -16,7 +17,8 @@ const debug = Debug("hub");
|
||||
export class Hub extends EventEmitter {
|
||||
|
||||
|
||||
public autoSubscribe: boolean;
|
||||
public autoSubscribe: boolean = true;
|
||||
public useSpeedMap: boolean = true;
|
||||
public type: Consts.Hubs = Consts.Hubs.UNKNOWN;
|
||||
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) {
|
||||
switch (type) {
|
||||
case Consts.Devices.BASIC_MOTOR:
|
||||
|
@ -107,7 +107,7 @@ export class WeDo2Hub extends Hub {
|
||||
return new Promise((resolve, reject) => {
|
||||
const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE];
|
||||
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();
|
||||
}
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user