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); }
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)); }
public void DataArrayPositionAfterAddWithOffSetTest() { ProtocolArray array = new ProtocolArray(); array.SetBits(1, 4, 123456); Assert.AreEqual(4, array.Position); }
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); }
public void DataArrayAddBitsRangeOverflow() { uint value = 16; ProtocolArray array = new ProtocolArray(); array.SetBits(0, 4, value); Assert.AreNotEqual(value, BitConverter.ToInt32(array.Data, 0)); }
public void DataArrayAddBitsCorrectRange() { uint value = 15; ProtocolArray array = new ProtocolArray(); array.SetBits(0, 4, value); Assert.AreEqual(value, BitConverter.ToUInt32(array.Data, 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); }
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)); }
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); } }
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]); } } } } }
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)); } } }