writeDirect function on device
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Nathan Kellenicki 2019-12-19 10:46:38 -08:00
parent a5a9b2e880
commit 705fbd341a
8 changed files with 16 additions and 41 deletions

View File

@ -22,13 +22,7 @@ export class BasicMotor extends Device {
*/ */
public setPower (power: number) { public setPower (power: number) {
return new Promise((resolve) => { return new Promise((resolve) => {
if (this.isWeDo2SmartHub) { this.writeDirect(0x00, Buffer.from([mapSpeed(power)]));
const data = Buffer.from([this.portId, 0x01, 0x02, mapSpeed(power)]);
this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE);
} else {
const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, mapSpeed(power)]);
this.send(data);
}
return resolve(); return resolve();
}); });
} }

View File

@ -77,6 +77,14 @@ export class Device extends EventEmitter {
return this._isWeDo2SmartHub; return this._isWeDo2SmartHub;
} }
public writeDirect (mode: number, data: Buffer, callback?: () => void) {
if (this.isWeDo2SmartHub) {
this.send(Buffer.concat([Buffer.from([this.portId, 0x01, 0x02]), data]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE);
} else {
this.send(Buffer.concat([Buffer.from([0x81, this.portId, 0x11, 0x51, mode]), data]), Consts.BLECharacteristic.LPF2_ALL, callback);
}
}
public send (data: Buffer, characteristic: string = Consts.BLECharacteristic.LPF2_ALL, callback?: () => void) { public send (data: Buffer, characteristic: string = Consts.BLECharacteristic.LPF2_ALL, callback?: () => void) {
this._ensureConnected(); this._ensureConnected();
this.hub.send(data, characteristic, callback); this.hub.send(data, characteristic, callback);

View File

@ -26,7 +26,7 @@ export class HubLED extends Device {
if (typeof color === "boolean") { if (typeof color === "boolean") {
color = 0; color = 0;
} }
this.send(Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, color])); this.writeDirect(0x00, Buffer.from([color]));
return resolve(); return resolve();
}); });
} }
@ -45,28 +45,7 @@ export class HubLED extends Device {
if (this.mode !== HubLED.Mode.RGB) { if (this.mode !== HubLED.Mode.RGB) {
this.subscribe(HubLED.Mode.RGB); this.subscribe(HubLED.Mode.RGB);
} }
this.send(Buffer.from([0x81, this.portId, 0x11, 0x51, 0x01, red, green, blue])); this.writeDirect(0x00, Buffer.from([red, green, blue]));
return resolve();
});
}
/**
* Set the light brightness.
* @method Light#brightness
* @param {number} brightness Brightness value between 0-100 (0 is off)
* @returns {Promise} Resolved upon successful completion of command.
*/
public setBrightness (brightness: number) {
return new Promise((resolve) => {
if (this.isWeDo2SmartHub) {
const data = Buffer.from([this.portId, 0x01, 0x02, brightness]);
this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE);
} else {
const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, brightness]);
this.send(data);
}
return resolve(); return resolve();
}); });
} }

View File

@ -20,13 +20,7 @@ export class Light extends Device {
*/ */
public setBrightness (brightness: number) { public setBrightness (brightness: number) {
return new Promise((resolve) => { return new Promise((resolve) => {
if (this.isWeDo2SmartHub) { this.writeDirect(0x00, Buffer.from([brightness]));
const data = Buffer.from([this.portId, 0x01, 0x02, brightness]);
this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE);
} else {
const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, brightness]);
this.send(data);
}
return resolve(); return resolve();
}); });
} }

View File

@ -4,8 +4,8 @@ import { PoweredUP } from "./poweredup-browser";
import { BaseHub } from "./hubs/basehub"; import { BaseHub } from "./hubs/basehub";
import { DuploTrainBase } from "./hubs/duplotrainbase"; import { DuploTrainBase } from "./hubs/duplotrainbase";
import { MoveHub } from "./hubs/movehub";
import { Hub } from "./hubs/hub"; import { Hub } from "./hubs/hub";
import { MoveHub } from "./hubs/movehub";
import { RemoteControl } from "./hubs/remotecontrol"; import { RemoteControl } from "./hubs/remotecontrol";
import { TechnicMediumHub } from "./hubs/technicmediumhub"; import { TechnicMediumHub } from "./hubs/technicmediumhub";
import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; import { WeDo2SmartHub } from "./hubs/wedo2smarthub";

View File

@ -4,8 +4,8 @@ import { PoweredUP } from "./poweredup-node";
import { BaseHub } from "./hubs/basehub"; import { BaseHub } from "./hubs/basehub";
import { DuploTrainBase } from "./hubs/duplotrainbase"; import { DuploTrainBase } from "./hubs/duplotrainbase";
import { MoveHub } from "./hubs/movehub";
import { Hub } from "./hubs/hub"; import { Hub } from "./hubs/hub";
import { MoveHub } from "./hubs/movehub";
import { RemoteControl } from "./hubs/remotecontrol"; import { RemoteControl } from "./hubs/remotecontrol";
import { TechnicMediumHub } from "./hubs/technicmediumhub"; import { TechnicMediumHub } from "./hubs/technicmediumhub";
import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; import { WeDo2SmartHub } from "./hubs/wedo2smarthub";

View File

@ -2,8 +2,8 @@ import { WebBLEDevice } from "./webbleabstraction";
import { BaseHub } from "./hubs/basehub"; import { BaseHub } from "./hubs/basehub";
import { DuploTrainBase } from "./hubs/duplotrainbase"; import { DuploTrainBase } from "./hubs/duplotrainbase";
import { MoveHub } from "./hubs/movehub";
import { Hub } from "./hubs/hub"; import { Hub } from "./hubs/hub";
import { MoveHub } from "./hubs/movehub";
import { RemoteControl } from "./hubs/remotecontrol"; import { RemoteControl } from "./hubs/remotecontrol";
import { TechnicMediumHub } from "./hubs/technicmediumhub"; import { TechnicMediumHub } from "./hubs/technicmediumhub";
import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; import { WeDo2SmartHub } from "./hubs/wedo2smarthub";

View File

@ -4,8 +4,8 @@ import { NobleDevice } from "./nobleabstraction";
import { BaseHub } from "./hubs/basehub"; import { BaseHub } from "./hubs/basehub";
import { DuploTrainBase } from "./hubs/duplotrainbase"; import { DuploTrainBase } from "./hubs/duplotrainbase";
import { MoveHub } from "./hubs/movehub";
import { Hub } from "./hubs/hub"; import { Hub } from "./hubs/hub";
import { MoveHub } from "./hubs/movehub";
import { RemoteControl } from "./hubs/remotecontrol"; import { RemoteControl } from "./hubs/remotecontrol";
import { TechnicMediumHub } from "./hubs/technicmediumhub"; import { TechnicMediumHub } from "./hubs/technicmediumhub";
import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; import { WeDo2SmartHub } from "./hubs/wedo2smarthub";