public TrainSpeed(SpeedStepSetting speedStepSetting, DrivingDirection drivingDirection, Speed speed) { if (speed > MaxSpeeds[speedStepSetting]) { throw new ArgumentOutOfRangeException(nameof(speed), $"Max speed with setting {speedStepSetting} is {MaxSpeeds[speedStepSetting]}, actual {speed}"); } if (speed < Speed.Stop) { throw new ArgumentOutOfRangeException(nameof(speed), $"Min speed is -1 as {Speed.Stop}, to change direction, use {nameof(drivingDirection)}."); } this.speedStepSetting = speedStepSetting; this.drivingDirection = drivingDirection; this.speed = speed; }
internal TrainSpeed(SpeedStepSetting speedStepSetting, byte speedByte) { this.speedStepSetting = speedStepSetting; this.drivingDirection = (speedByte & DirectionMask) == 0 ? DrivingDirection.Backward : DrivingDirection.Forward; if (speedStepSetting == SpeedStepSetting.Step128) { this.speed = (Speed)((Speed128Mask & speedByte) - 1); } else { this.speed = (Speed)((Speed14Mask & speedByte) - 1); if (speed > 0 && speedStepSetting == SpeedStepSetting.Step28) { this.speed = (Speed)(((sbyte)speed << 1) - ((speedByte & ExtraBitSpeed28Mask) == 0 ? 1 : 0)); } } }
private static byte GetSpeedStepSettingBytes(SpeedStepSetting speedStepSetting) { switch (speedStepSetting) { case SpeedStepSetting.Step14: return(0x10); case SpeedStepSetting.Step28: return(0x12); case SpeedStepSetting.Step128: return(0x13); default: throw new ArgumentOutOfRangeException(nameof(speedStepSetting)); } }