diff --git a/src/extensions/scratch3_wedo2/index.js b/src/extensions/scratch3_wedo2/index.js index 75d316cba..a1c904b65 100644 --- a/src/extensions/scratch3_wedo2/index.js +++ b/src/extensions/scratch3_wedo2/index.js @@ -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()); + } } }); } diff --git a/src/io/bleSession.js b/src/io/bleSession.js index f19d81148..572a11283 100644 --- a/src/io/bleSession.js +++ b/src/io/bleSession.js @@ -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); }