Beispiel #1
0
        /// <summary>
        /// Determine how long we should wait before reading a command response on the serial port.
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        private TimeSpan CalcMinWaitTimeSpan(LegoCommandCode code)
        {
            // If this command hasn't been run before, get a baseline.
            if (!_state.RuntimeStatistics.ContainsKey(code))
            {
                return(new TimeSpan((long)1));
            }

            long ticks = (long)(_state.RuntimeStatistics[code].MinimumMicroseconds * 10.0);
            long ms    = ticks / 10000;

            if (ms < 5)  // min -1/2 ms
            {
                return(new TimeSpan(ticks - 5000));
            }

            if (ms < 20) // min -1 ms
            {
                return(new TimeSpan(ticks - 10000));
            }

            // min -5 ms
            return(new TimeSpan(ticks - 50000));
        }
        /// <summary>
        /// Determine how long we should wait before reading a command response on the serial port.
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        private TimeSpan CalcMinWaitTimeSpan(LegoCommandCode code)
        {
            // If this command hasn't been run before, get a baseline.
            if (!_state.RuntimeStatistics.ContainsKey(code))
                return new TimeSpan((long)1);

            long ticks = (long)(_state.RuntimeStatistics[code].MinimumMicroseconds * 10.0);
            long ms = ticks / 10000;

            if (ms < 5)  // min -1/2 ms
                return new TimeSpan(ticks - 5000);

            if (ms < 20) // min -1 ms
                return new TimeSpan(ticks - 10000);

            // min -5 ms
            return new TimeSpan(ticks - 50000);
        }
 public CodeTimer(LegoCommandCode commandCode, double uSec)
 {
     CommandCode = commandCode;
     USec = uSec;
 }
Beispiel #4
0
 public CodeTimer(LegoCommandCode commandCode, double uSec)
 {
     CommandCode = commandCode;
     USec        = uSec;
 }
Beispiel #5
0
 /// <summary>
 /// Communication statistics by LEGO Command.
 /// </summary>
 /// <param name="commandCode"></param>
 public LegoCommandStat(LegoCommandCode commandCode)
 {
     CommandCode = commandCode;
 }
Beispiel #6
0
        /// <summary>
        /// Initializes the header of the command to be sent to the NXT.
        /// Byte 0 will contain the command type and byte 1 the command code.
        /// </summary>
        /// <param name="commandCode">The command to be sent to the NXT.</param>
        /// <param name="type">The type of the command.</param>
        /// <param name="dataLength">The length of the complete message in bytes.</param>
        /// <returns>
        /// A byte array with the length of the complete message,
        /// initialized with the command and command type.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">If the <paramref name="dataLength"/> is less than 2.</exception>
        public static byte[] InitializeData(LegoCommandCode commandCode, CommandType type, int dataLength)
        {
            // Validate dataLength parameter: should be 2 or more.
            if (dataLength < 2)
            {
                throw new ArgumentOutOfRangeException("dataLength", "Data length cannot be less than 2.");
            }

            // Construct a package with the specified length
            var data = new byte[dataLength];

            // Set byte 0 to the command type.
            data[0] = (byte)type;

            // Set byte 1 to the command code.
            data[1] = (byte)commandCode;

            return data;
        }