mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 14:32:59 -05:00
Fixing #1460: 'set motor direction' block should work while a motor is running.
This commit is contained in:
parent
3023b7a9c2
commit
d61f6e3d6f
2 changed files with 38 additions and 2 deletions
|
@ -112,6 +112,20 @@ class WeDo2Motor {
|
|||
*/
|
||||
this._pendingTimeoutId = null;
|
||||
|
||||
/**
|
||||
* The starting time for the pending timeout.
|
||||
* @type {Object}
|
||||
* @private
|
||||
*/
|
||||
this._pendingTimeoutStartTime = null;
|
||||
|
||||
/**
|
||||
* The delay/duration of the pending timeout.
|
||||
* @type {Object}
|
||||
* @private
|
||||
*/
|
||||
this._pendingTimeoutDelay = null;
|
||||
|
||||
this.startBraking = this.startBraking.bind(this);
|
||||
this.setMotorOff = this.setMotorOff.bind(this);
|
||||
}
|
||||
|
@ -163,6 +177,20 @@ class WeDo2Motor {
|
|||
return this._isOn;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {boolean} - time, in milliseconds, of when the pending timeout began.
|
||||
*/
|
||||
get pendingTimeoutStartTime () {
|
||||
return this._pendingTimeoutStartTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {boolean} - delay, in milliseconds, of the pending timeout.
|
||||
*/
|
||||
get pendingTimeoutDelay () {
|
||||
return this._pendingTimeoutDelay;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn this motor on indefinitely.
|
||||
*/
|
||||
|
@ -228,6 +256,8 @@ class WeDo2Motor {
|
|||
if (this._pendingTimeoutId !== null) {
|
||||
clearTimeout(this._pendingTimeoutId);
|
||||
this._pendingTimeoutId = null;
|
||||
this._pendingTimeoutStartTime = null;
|
||||
this._pendingTimeoutDelay = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -246,6 +276,8 @@ class WeDo2Motor {
|
|||
callback();
|
||||
}, delay);
|
||||
this._pendingTimeoutId = timeoutID;
|
||||
this._pendingTimeoutStartTime = Date.now();
|
||||
this._pendingTimeoutDelay = delay;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -957,6 +989,10 @@ class Scratch3WeDo2Blocks {
|
|||
log.warn(`Unknown motor direction in setMotorDirection: ${args.DIRECTION}`);
|
||||
break;
|
||||
}
|
||||
// keep the motor on if it's running, and update the pending timeout
|
||||
if (motor.isOn && motor.pendingTimeoutDelay) {
|
||||
motor.setMotorOnFor(motor.pendingTimeoutStartTime + motor.pendingTimeoutDelay - Date.now());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -146,9 +146,9 @@ class BLESession extends JSONRPCWebSocket {
|
|||
});
|
||||
}
|
||||
|
||||
_sendError (e) {
|
||||
_sendError (/* e */) {
|
||||
this._connected = false;
|
||||
log.error(`BLESession error: ${JSON.stringify(e)}`);
|
||||
// log.error(`BLESession error: ${JSON.stringify(e)}`);
|
||||
this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue