Merge branch 'develop' into bugfix/2125

This commit is contained in:
Eric Rosenbaum 2019-04-29 14:06:13 -04:00 committed by GitHub
commit 85f3a3c3c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -431,9 +431,14 @@ class BoostMotor {
* @param {BoostMotorState} value - set this motor's state. * @param {BoostMotorState} value - set this motor's state.
*/ */
set status (value) { set status (value) {
// Clear any time- or rotation-related state from motor and set new status. if (value !== BoostMotorState.ON_FOR_ROTATION) {
this._clearRotationState(); // clear rotation pending promise only if not already in rotation state
this._clearTimeout(); this._clearRotationState();
}
if (value !== BoostMotorState.ON_FOR_TIME) {
// clear duration pending promise only if not already in duration state
this._clearTimeout();
}
this._status = value; this._status = value;
} }
@ -477,7 +482,6 @@ class BoostMotor {
* @private * @private
*/ */
_turnOn () { _turnOn () {
if (this.power === 0) return;
const cmd = this._parent.generateOutputCommand( const cmd = this._parent.generateOutputCommand(
this._index, this._index,
BoostOutputExecution.EXECUTE_IMMEDIATELY, BoostOutputExecution.EXECUTE_IMMEDIATELY,
@ -493,24 +497,19 @@ class BoostMotor {
/** /**
* Turn this motor on indefinitely * Turn this motor on indefinitely
* @param {boolean} [resetState=true] - whether to reset the state of the motor when running this command.
*/ */
turnOnForever (resetState = true){ turnOnForever () {
if (this.power === 0) return; this.status = BoostMotorState.ON_FOREVER;
if (resetState) this.status = BoostMotorState.ON_FOREVER;
this._turnOn(); this._turnOn();
} }
/** /**
* Turn this motor on for a specific duration. * Turn this motor on for a specific duration.
* @param {number} milliseconds - run the motor for this long. * @param {number} milliseconds - run the motor for this long.
* @param {boolean} [resetState=true] - whether to reset the state of the motor when running this command.
*/ */
turnOnFor (milliseconds, resetState = true) { turnOnFor (milliseconds) {
if (this.power === 0) return;
milliseconds = Math.max(0, milliseconds); milliseconds = Math.max(0, milliseconds);
if (resetState) this.status = BoostMotorState.ON_FOR_TIME; this.status = BoostMotorState.ON_FOR_TIME;
this._turnOn(); this._turnOn();
this._setNewTimeout(this.turnOff, milliseconds); this._setNewTimeout(this.turnOff, milliseconds);
} }
@ -519,14 +518,8 @@ class BoostMotor {
* Turn this motor on for a specific rotation in degrees. * Turn this motor on for a specific rotation in degrees.
* @param {number} degrees - run the motor for this amount of degrees. * @param {number} degrees - run the motor for this amount of degrees.
* @param {number} direction - rotate in this direction * @param {number} direction - rotate in this direction
* @param {boolean} [resetState=true] - whether to reset the state of the motor when running this command.
*/ */
turnOnForDegrees (degrees, direction, resetState = true) { turnOnForDegrees (degrees, direction) {
if (this.power === 0) {
this._clearRotationState();
return;
}
degrees = Math.max(0, degrees); degrees = Math.max(0, degrees);
const cmd = this._parent.generateOutputCommand( const cmd = this._parent.generateOutputCommand(
@ -542,7 +535,7 @@ class BoostMotor {
] ]
); );
if (resetState) this.status = BoostMotorState.ON_FOR_ROTATION; this.status = BoostMotorState.ON_FOR_ROTATION;
this._pendingPositionDestination = this.position + (degrees * this.direction * direction); this._pendingPositionDestination = this.position + (degrees * this.direction * direction);
this._parent.send(BoostBLE.characteristic, cmd); this._parent.send(BoostBLE.characteristic, cmd);
} }
@ -552,8 +545,6 @@ class BoostMotor {
* @param {boolean} [useLimiter=true] - if true, use the rate limiter * @param {boolean} [useLimiter=true] - if true, use the rate limiter
*/ */
turnOff (useLimiter = true) { turnOff (useLimiter = true) {
if (this.power === 0) return;
const cmd = this._parent.generateOutputCommand( const cmd = this._parent.generateOutputCommand(
this._index, this._index,
BoostOutputExecution.EXECUTE_IMMEDIATELY ^ BoostOutputExecution.COMMAND_FEEDBACK, BoostOutputExecution.EXECUTE_IMMEDIATELY ^ BoostOutputExecution.COMMAND_FEEDBACK,
@ -1791,10 +1782,10 @@ class Scratch3BoostBlocks {
motor.power = MathUtil.clamp(Cast.toNumber(args.POWER), 0, 100); motor.power = MathUtil.clamp(Cast.toNumber(args.POWER), 0, 100);
switch (motor.status) { switch (motor.status) {
case BoostMotorState.ON_FOREVER: case BoostMotorState.ON_FOREVER:
motor.turnOnForever(false); motor.turnOnForever();
break; break;
case BoostMotorState.ON_FOR_TIME: case BoostMotorState.ON_FOR_TIME:
motor.turnOnFor(motor.pendingTimeoutStartTime + motor.pendingTimeoutDelay - Date.now(), false); motor.turnOnFor(motor.pendingTimeoutStartTime + motor.pendingTimeoutDelay - Date.now());
break; break;
} }
} }
@ -1837,10 +1828,10 @@ class Scratch3BoostBlocks {
if (motor) { if (motor) {
switch (motor.status) { switch (motor.status) {
case BoostMotorState.ON_FOREVER: case BoostMotorState.ON_FOREVER:
motor.turnOnForever(false); motor.turnOnForever();
break; break;
case BoostMotorState.ON_FOR_TIME: case BoostMotorState.ON_FOR_TIME:
motor.turnOnFor(motor.pendingTimeoutStartTime + motor.pendingTimeoutDelay - Date.now(), false); motor.turnOnFor(motor.pendingTimeoutStartTime + motor.pendingTimeoutDelay - Date.now());
break; break;
} }
} }