예제 #1
0
        public void DataArrayAddGetBits()
        {
            ProtocolArray array = new ProtocolArray();

            array.SetBits(0, 10, 421);
            array.SetBits(0, 10, 751);
            array.SetBits(0, 10, 637);
            array.SetBits(0, 2, 3);
            Assert.AreEqual(32, array.Position);

            array.Position = 0;
            Assert.AreEqual(array.GetBits(0, 4), (uint)5);
            Assert.AreEqual(array.GetBits(0, 4), (uint)10);
            Assert.AreEqual(array.GetBits(0, 4), (uint)13);
            Assert.AreEqual(array.GetBits(0, 4), (uint)11);
            Assert.AreEqual(array.GetBits(0, 4), (uint)11);
            Assert.AreEqual(array.GetBits(0, 4), (uint)13);
            Assert.AreEqual(array.GetBits(0, 4), (uint)7);
            Assert.AreEqual(array.GetBits(0, 4), (uint)14);

            array.Position = 0;
            Assert.AreEqual(array.GetBits(0, 10), (uint)421);
            Assert.AreEqual(array.GetBits(0, 10), (uint)751);
            Assert.AreEqual(array.GetBits(0, 10), (uint)637);
            Assert.AreEqual(array.GetBits(0, 2), (uint)3);

            Assert.AreEqual(array[0], 165);
            Assert.AreEqual(array[1], 189);
            Assert.AreEqual(array[2], 219);
            Assert.AreEqual(array[3], 231);
        }
예제 #2
0
        public void DataArrayAddMultipleBitsCorrectRange2()
        {
            ProtocolArray array = new ProtocolArray();

            array.SetBits(0, 1, 1);
            array.SetBits(0, 5, 20);
            Assert.AreEqual(41, BitConverter.ToInt32(array.Data, 0));
        }
예제 #3
0
        public void DataArrayPositionAfterAddWithOffSetTest()
        {
            ProtocolArray array = new ProtocolArray();

            array.SetBits(1, 4, 123456);
            Assert.AreEqual(4, array.Position);
        }
예제 #4
0
        public virtual void UpdateVelocityRequest(ProtocolArray requestData)
        {
            int speed = Velocity;

            speed = ((Math.Abs(speed) & 0xFF) << 1 | -(Math.Sign(speed) >> 1)) << 1 | (Enabled.ToInt() & 0x01);
            requestData.SetBits(1, 10, (uint)speed);
        }
예제 #5
0
        public void DataArrayAddBitsRangeOverflow()
        {
            uint          value = 16;
            ProtocolArray array = new ProtocolArray();

            array.SetBits(0, 4, value);
            Assert.AreNotEqual(value, BitConverter.ToInt32(array.Data, 0));
        }
예제 #6
0
        public void DataArrayAddBitsCorrectRange()
        {
            uint          value = 15;
            ProtocolArray array = new ProtocolArray();

            array.SetBits(0, 4, value);
            Assert.AreEqual(value, BitConverter.ToUInt32(array.Data, 0));
        }
예제 #7
0
        public override TimeSpan?InitializeSensorRequest(ProtocolArray requestData)
        {
            TimeSpan?result = base.InitializeSensorRequest(requestData);

            //I2C setup
            if (SensorType == SensorType.I2C || SensorType == SensorType.I2C_9V || SensorType == SensorType.ULTRASONIC_CONT)
            {
                requestData.SetBits(3, 8, Speed);

                requestData.SetBits(3, 3, (uint)(I2CData.Count > 8 ? 8 : (I2CData.Count < 1 ? 1 : I2CData.Count)) - 1);

                foreach (I2CData device in I2CData)
                {
                    requestData.SetBits(3, 7, (uint)(device.Address >> 1));
                    requestData.SetBits(3, 2, (uint)device.Setting);
                    if ((device.Setting & Const.BIT_I2C_SAME) == Const.BIT_I2C_SAME)
                    {
                        requestData.SetBits(3, 4, (uint)device.WriteBytes);
                        requestData.SetBits(3, 4, (uint)device.ReadBytes);

                        for (int out_byte = 0; out_byte < device.WriteBytes; out_byte++)
                        {
                            requestData.SetBits(3, 8, (uint)device.WriteData[out_byte]);
                        }
                    }
                }
            }
            return(result);
        }
예제 #8
0
        public void DataArraySetGetSingleBitTest()
        {
            ProtocolArray array = new ProtocolArray(8);

            array.SetBits(0, 1, 1);
            Assert.AreEqual(1, BitConverter.ToInt32(array.Data, 0));
            array.Position = 0;
            Assert.AreEqual(1, (int)array.GetBits(0, 1));
        }
예제 #9
0
 public virtual void UpdateEncoderRequest(ProtocolArray requestData)
 {
     if (EncoderOffset != 0)
     {
         if (0 == encoderOffsetCache)
         {
             encoderOffsetBits = ProtocolArray.SignificantBits((uint)Math.Abs(EncoderOffset));
             encoderOffsetMask = (byte)((encoderOffsetBits << 1) + 1);
             encoderOffsetBits++;
             encoderOffsetCache = (Math.Abs(EncoderOffset) << 1) | -(Math.Sign(EncoderOffset) >> 1); //odd numbers are negative speed/backward direction
         }
         requestData.SetBits(1, 6, encoderOffsetMask);
         requestData.SetBits(1, encoderOffsetBits, (uint)encoderOffsetCache);
     }
     else
     {
         requestData.SetBits(1, 1, 0);
     }
 }
예제 #10
0
        public override void UpdateSensorRequest(ProtocolArray requestData)
        {
            base.UpdateSensorRequest(requestData);

            int arduinoPort = (int)SensorPort.ArduinoPort();

            if ((SensorType == SensorType.I2C) ||
                (SensorType == SensorType.I2C_9V) ||
                (SensorType == SensorType.ULTRASONIC_CONT))
            {
                foreach (I2CData device in I2CData)
                {
                    if ((device.Setting & Const.BIT_I2C_SAME) != Const.BIT_I2C_SAME)
                    {
                        requestData.SetBits(1, 4, (uint)device.WriteBytes);
                        requestData.SetBits(1, 4, (uint)device.ReadBytes);
                        for (int out_byte = 0; out_byte < device.WriteBytes; out_byte++)
                        {
                            requestData.SetBits(1, 8, (uint)device.WriteData[out_byte]);
                        }
                    }
                }
            }
        }
예제 #11
0
 public void DataArrayAddMultipleBitsCorrectRange()
 {
     //byte offset = 3;
     for (byte offset = 1; offset < 16; offset++)
     {
         UInt64        result = 0;
         ProtocolArray array  = new ProtocolArray();
         for (int i = 0; i < 8; i++)
         {
             array.SetBits(0, offset, 1);
             //array.AddBits(0, 0, offset, 1);
             result += ((ulong)1 << (offset * i));
             Assert.AreEqual(result, BitConverter.ToUInt64(array.Data, 0), string.Format("Failed at offset {0} in step {1}", offset, i));
         }
     }
 }