possible fix for #118, always return resolve

* changed _finished pointer to array to not overwrite callbacks
* all completed and discarded commands get resolved
* 0x10 bitset exists in documentation only? -> not implemented
This commit is contained in:
debenben 2020-12-30 20:54:24 +01:00
parent a3f66d5df0
commit a51aac1066
4 changed files with 15 additions and 19 deletions

View File

@ -51,7 +51,6 @@ export class AbsoluteMotor extends TachoMotor {
} }
this.cancelEventTimer(); this.cancelEventTimer();
return new Promise<void>((resolve) => { return new Promise<void>((resolve) => {
this._busy = true;
if (speed === undefined || speed === null) { if (speed === undefined || speed === null) {
speed = 100; speed = 100;
} }
@ -65,9 +64,9 @@ export class AbsoluteMotor extends TachoMotor {
message.writeInt32LE(normalizeAngle(angle), 4); message.writeInt32LE(normalizeAngle(angle), 4);
} }
this.send(message); this.send(message);
this._finished = () => { this._finishedCallbacks.push(() => {
return resolve(); return resolve();
}; });
}); });
} }

View File

@ -15,7 +15,7 @@ export class Device extends EventEmitter {
protected _mode: number | undefined; protected _mode: number | undefined;
protected _busy: boolean = false; protected _busy: boolean = false;
protected _finished: (() => void) | undefined; protected _finishedCallbacks: (() => void)[] = [];
private _hub: IDeviceInterface; private _hub: IDeviceInterface;
private _portId: number; private _portId: number;
@ -167,11 +167,13 @@ export class Device extends EventEmitter {
this.send(Buffer.from([0x21, this.portId, 0x00])); this.send(Buffer.from([0x21, this.portId, 0x00]));
} }
public finish () { public finish (message: number) {
this._busy = false; this._busy = (message & 0x01) === 0x01;
if (this._finished) { while(this._finishedCallbacks.length > Number(this._busy)) {
this._finished(); const callback = this._finishedCallbacks.shift();
this._finished = undefined; if(callback) {
callback();
}
} }
} }

View File

@ -104,7 +104,6 @@ export class TachoMotor extends BasicMotor {
} }
this.cancelEventTimer(); this.cancelEventTimer();
return new Promise<void>((resolve) => { return new Promise<void>((resolve) => {
this._busy = true;
if (speed === undefined || speed === null) { if (speed === undefined || speed === null) {
speed = 100; speed = 100;
} }
@ -124,9 +123,9 @@ export class TachoMotor extends BasicMotor {
} }
} }
this.send(message); this.send(message);
this._finished = () => { this._finishedCallbacks.push(() => {
return resolve(); return resolve();
}; });
}); });
} }
@ -146,7 +145,6 @@ export class TachoMotor extends BasicMotor {
} }
this.cancelEventTimer(); this.cancelEventTimer();
return new Promise<void>((resolve) => { return new Promise<void>((resolve) => {
this._busy = true;
if (speed === undefined || speed === null) { if (speed === undefined || speed === null) {
speed = 100; speed = 100;
} }
@ -158,9 +156,9 @@ export class TachoMotor extends BasicMotor {
} }
message.writeUInt32LE(degrees, 4); message.writeUInt32LE(degrees, 4);
this.send(message); this.send(message);
this._finished = () => { this._finishedCallbacks.push(() => {
return resolve(); return resolve();
}; });
}); });
} }

View File

@ -353,10 +353,7 @@ export class LPF2Hub extends BaseHub {
const device = this._getDeviceByPortId(portId); const device = this._getDeviceByPortId(portId);
if (device) { if (device) {
const finished = (message[4] === 0x0a); device.finish(message[4]);
if (finished) {
device.finish();
}
} }
} }