/// <summary> /// Finishes the Motor toy and releases used references. /// </summary> public override void Finish() { AlarmHandler.UnregisterAlarm(StartMotor); AlarmHandler.UnregisterAlarm(StopMotor); AlarmHandler = null; base.Finish(); }
/// <summary> /// Finishes the Motor toy and releases used references. /// </summary> public override void Finish() { if (AlarmHandler != null) { AlarmHandler.UnregisterAlarm(KickStartEnd); AlarmHandler.UnregisterAlarm(MaxRunTimeMotorStop); AlarmHandler = null; } base.Finish(); }
private void StopMotor() { KickstartActive = false; SetValue(0); AlarmHandler.UnregisterAlarm(StartMotor); AlarmHandler.UnregisterAlarm(StopMotor); }
private void MaxRunTimeMotorStop() { AlarmHandler.UnregisterAlarm(KickStartEnd); KickstartActive = false; CurrentMotorPower = 0; TargetMotorPower = 0; Output.Value = 0; TurnedOffAfterMaxRunTime = true; }
private void StartMotor() { KickstartActive = false; if (MotorPower == 0) { StopMotor(); } else { SetMotorPower(MotorPower); if (MaxRunTimeMs > 0) { AlarmHandler.UnregisterAlarm(StartMotor); AlarmHandler.RegisterAlarm(MaxRunTimeMs, StopMotor); } } }
/// <summary> /// Updates the output of the toy. /// </summary> public override void UpdateOutputs() { if (Output != null) { int P = FadingCurve.MapValue(GetResultingValue().Limit(0, 255)); if (P != 0) { P = ((int)((double)(MaxPower >= MinPower ? MaxPower - MinPower : MinPower - MaxPower) / 255 * P) + MinPower).Limit(MinPower, MaxPower); } if (P == 0) { TurnedOffAfterMaxRunTime = false; } if (!TurnedOffAfterMaxRunTime) { if (CurrentMotorPower == 0) { //Motor is currently off if (P > 0) { //need to turn the motor on if (KickstartDurationMs > 0 && KickstartPower > 0 && P <= KickstartPower) { //Kickstart is defined, start with kickstart TargetMotorPower = P; if (!KickstartActive) { CurrentMotorPower = KickstartPower; Output.Value = (byte)CurrentMotorPower; KickstartActive = true; AlarmHandler.RegisterAlarm(KickstartDurationMs, KickStartEnd); } } else { //Just turn the motor on CurrentMotorPower = P; TargetMotorPower = P; Output.Value = (byte)P; KickstartActive = false; } if (MaxRunTimeMs > 0) { AlarmHandler.RegisterAlarm(MaxRunTimeMs, MaxRunTimeMotorStop); } } } else if (KickstartActive) { //Motor is in kickstart phase if (P > 0) { //Need to change the motor power TargetMotorPower = P; } else { //Turn off motor AlarmHandler.UnregisterAlarm(KickStartEnd); AlarmHandler.UnregisterAlarm(MaxRunTimeMotorStop); TargetMotorPower = 0; CurrentMotorPower = 0; Output.Value = 0; } } else { //Motor is on if (P == 0) { //Turn of motor AlarmHandler.UnregisterAlarm(KickStartEnd); AlarmHandler.UnregisterAlarm(MaxRunTimeMotorStop); TargetMotorPower = 0; CurrentMotorPower = 0; Output.Value = 0; } else if (P != CurrentMotorPower) { //Power has changed CurrentMotorPower = P; TargetMotorPower = P; Output.Value = (byte)P; } } } } }