예제 #1
0
        public void TestInitialize()
        {
            _mockSpiDevice = new MockSpiDevice();
            var mockSpiComm = new MockSpiComm(_mockSpiDevice);

            _registerManager = new RegisterManager(mockSpiComm);
        }
        public MockedRegisterManager()
        {
            var spiDevice   = new MockSpiDevice();
            var mockSpiComm = new MockSpiComm(spiDevice);

            RegisterManager = new RegisterManager(mockSpiComm);

            spiDevice.WriteAction = buffer =>
            {
                bool isWriting = (buffer[0] & 0x80) == 0x80;
                byte address   = (byte)(buffer[0] & 0x7F);
                if (isWriting)
                {
                    if (buffer.Length != 2)
                    {
                        Assert.Fail($"Expecting 2 bytes when writing register, got {buffer.Length}.");
                    }

                    _registerValues[address] = buffer[1];
                }
                else
                {
                    if (buffer.Length != 1)
                    {
                        Assert.Fail($"Expecting 1 byte when reading register, got {buffer.Length}.");
                    }

                    byte value;
                    if (!_registerValues.TryGetValue(address, out value))
                    {
                        Assert.Fail($"Failed to read uninitialized register byte at address 0x{address:X2}.");
                    }

                    _spiOutBuffer = value;
                }
            };

            spiDevice.ReadAction = buffer =>
            {
                if (_spiOutBuffer == null)
                {
                    Assert.Fail("SPI reading not initialized.");
                }
                else
                {
                    buffer[0]     = _spiOutBuffer.Value;
                    _spiOutBuffer = null;
                }
            };
        }