Example #1
0
        public const int BYTES_NEEDED = 3; // Red, Green, Blue

        public static byte[] Serialize(PixelInstruction instruction, byte[] buffer, int startIndex)
        {
            buffer[startIndex]     = instruction.R;
            buffer[startIndex + 1] = instruction.G;
            buffer[startIndex + 2] = instruction.B;
            return(buffer);
        }
Example #2
0
        public void SerializeFrame_FrameTooBigForASinglePackage_CreatesCorrectByteArray()
        {
            int maxPackageSize      = 1016;
            int timeUnitsPerFrame   = 100;
            int frameIndex          = 9;
            FrameWithoutDelta frame = new FrameWithoutDelta(frameIndex, timeUnitsPerFrame, 900);

            for (int i = 0; i < 900; i++)
            {
                frame[i] = new PixelInstruction(1, 2, 3);
            }

            // RUN
            byte[][] packages = FrameProtocol.SerializeFrame(frame, maxPackageSize);

            // Deserialize
            FrameProtocol     frameProtocol     = new FrameProtocol();
            FrameWithoutDelta deserializedFrame = null;

            foreach (byte[] package in packages)
            {
                if (frameProtocol.TryDeserialize(package, out FrameWithoutDelta returnFrame))
                {
                    deserializedFrame = returnFrame;
                }
            }

            // Assert
            Assert.AreEqual(3, packages.Length);
            Assert.IsNotNull(deserializedFrame);
            CollectionAssert.AreEqual(frame.Items, deserializedFrame.Items);
        }
Example #3
0
        public void TryDeserialize_frameWithThreeFrameSetsOutOfOrder_DeserializesCorrectly()
        {
            int maxPackageSize              = 1016;
            int timeUnitsPerFrame           = 100;
            int frameIndex                  = 9;
            FrameWithoutDelta expectedFrame = new FrameWithoutDelta(frameIndex, timeUnitsPerFrame, 900);

            for (int i = 0; i < 900; i++)
            {
                expectedFrame[i] = new PixelInstruction(1, 2, 3);
            }

            // RUN
            byte[][] packages = FrameProtocol.SerializeFrame(expectedFrame, maxPackageSize);

            Assert.AreEqual(3, packages.Length);

            FrameProtocol     protocol = new FrameProtocol();
            FrameWithoutDelta frame;

            Assert.AreEqual(false, protocol.TryDeserialize(packages[0], out frame));
            Assert.AreEqual(false, protocol.TryDeserialize(packages[2], out frame));
            Assert.AreEqual(true, protocol.TryDeserialize(packages[1], out frame));
            Assert.IsNotNull(frame);
            Assert.AreEqual(expectedFrame.Index, frame.Index);
            Assert.AreEqual(expectedFrame.TimeStampRelative, frame.TimeStampRelative);
            Assert.AreEqual(expectedFrame.Count, frame.Count);
            CollectionAssert.AreEquivalent(expectedFrame.Items, frame.Items);
        }
Example #4
0
        public static PixelInstruction Deserialize(byte[] bytes, int startIndex)
        {
            PixelInstruction pixelInstruction = new PixelInstruction();

            pixelInstruction.R = bytes[startIndex];
            pixelInstruction.G = bytes[startIndex + 1];
            pixelInstruction.B = bytes[startIndex + 2];
            return(pixelInstruction);
        }
        public void Deserialize_BytesArray_CorrectlyDeserializesByteArray()
        {
            PixelInstruction expectedPixelInstruction = new PixelInstruction(1, 2, 3);

            byte[] bytes = new byte[1 + 1 + 1]; // Red, green , blue
            bytes[0] = expectedPixelInstruction.R;
            bytes[1] = expectedPixelInstruction.G;
            bytes[2] = expectedPixelInstruction.B;

            PixelInstruction pi = PixelInstructionProtocol.Deserialize(bytes, 0);

            Assert.AreEqual(expectedPixelInstruction, pi);
        }
        public void Serialize_PixelInStructionWithoutDelta_CorrectlyConvertedToByteArray()
        {
            PixelInstruction pi = new PixelInstruction(1, 2, 3);

            byte[] expectedBytes = new byte[1 + 1 + 1]; // Red, green , blue
            expectedBytes[0] = pi.R;
            expectedBytes[1] = pi.G;
            expectedBytes[2] = pi.B;

            byte[] buffer = new byte[PixelInstructionProtocol.BYTES_NEEDED];
            PixelInstructionProtocol.Serialize(pi, buffer, 0);
            Assert.AreEqual(expectedBytes, buffer);
        }
Example #7
0
        public void Render__LedStripRenderGetsCalled()
        {
            FrameWithoutDelta frame = new FrameWithoutDelta(0, 100, 1);

            frame[0] = new PixelInstruction(10, 20, 30);
            var mock = new Mock <ILEDStrip>();

            mock.Setup(x => x.Render());

            LedController controller = new LedController(mock.Object);

            controller.RenderFrame(frame);

            mock.Verify(x => x.Render(), Times.Once);
        }
Example #8
0
        public void GetFrameIndex_FrameAndFrameSection_GetsCorrectIndex()
        {
            int maxPackageSize              = 1016;
            int timeUnitsPerFrame           = 100;
            int frameIndex                  = 9;
            FrameWithoutDelta expectedFrame = new FrameWithoutDelta(frameIndex, timeUnitsPerFrame, 900);

            for (int i = 0; i < 900; i++)
            {
                expectedFrame[i] = new PixelInstruction(1, 2, 3);
            }

            // RUN
            byte[][] packages = FrameProtocol.SerializeFrame(expectedFrame, maxPackageSize);

            Assert.AreEqual(3, packages.Length);
            Assert.AreEqual(frameIndex, FrameProtocol.GetFrameIndex(packages[0]));
            Assert.AreEqual(frameIndex, FrameProtocol.GetFrameIndex(packages[1]));
            Assert.AreEqual(frameIndex, FrameProtocol.GetFrameIndex(packages[2]));
        }
Example #9
0
        public void RenderFrame(FrameWithoutDelta frame)
        {
            if (Monitor.TryEnter(lockObject))
            {
                try
                {
                    for (int i = 0; i < frame.Count; i++)
                    {
                        PixelInstruction instruction = frame[i];

                        _ledStrip.SetLEDColor(0, i, System.Drawing.Color.FromArgb(instruction.R, instruction.G, instruction.B));
                    }

                    _ledStrip.Render();
                }
                finally
                {
                    Monitor.Exit(lockObject);
                }
            }
        }
Example #10
0
        public void TryDeserialize_frameWithNoFrameSets_DeserializesCorrectly()
        {
            int timeUnitsPerFrame           = 100;
            FrameWithoutDelta expectedFrame = new FrameWithoutDelta(1, timeUnitsPerFrame, 3);

            expectedFrame[0] = new PixelInstruction(1, 2, 3);
            expectedFrame[1] = new PixelInstruction(4, 5, 6);
            expectedFrame[2] = new PixelInstruction(7, 8, 9);

            byte[] bytes      = new byte[sizeof(int) + sizeof(int) + sizeof(int) + sizeof(bool) + PixelInstructionProtocol.BYTES_NEEDED * 3];
            int    startIndex = 0;

            BitConverter.GetBytes(1).CopyTo(bytes, startIndex);                        // Frame index
            BitConverter.GetBytes(timeUnitsPerFrame).CopyTo(bytes, startIndex   += 4); // Timestamp (relative)
            BitConverter.GetBytes(expectedFrame.Count).CopyTo(bytes, startIndex += 4); // Number of PixelInstructions
            BitConverter.GetBytes(false).CopyTo(bytes, startIndex += 4);               // Has FrameSections
            // instruction 1
            bytes[startIndex += 1] = 1;
            bytes[startIndex += 1] = 2;
            bytes[startIndex += 1] = 3;
            // instruction 2
            bytes[startIndex += 1] = 4;
            bytes[startIndex += 1] = 5;
            bytes[startIndex += 1] = 6;
            // instruction 3
            bytes[startIndex += 1] = 7;
            bytes[startIndex += 1] = 8;
            bytes[startIndex += 1] = 9;

            FrameProtocol     protocol = new FrameProtocol();
            FrameWithoutDelta frame;

            Assert.AreEqual(true, protocol.TryDeserialize(bytes, out frame));
            Assert.IsNotNull(frame);
            Assert.AreEqual(expectedFrame.Index, frame.Index);
            Assert.AreEqual(expectedFrame.TimeStampRelative, frame.TimeStampRelative);
            Assert.AreEqual(expectedFrame.Count, frame.Count);
            CollectionAssert.AreEqual(expectedFrame.Items, frame.Items);
        }
Example #11
0
        public void PrepareFrame_Frame_FrameGetsDrawnToStrip()
        {
            FrameWithoutDelta frame = new FrameWithoutDelta(0, 100, 1);

            frame[0] = new PixelInstruction(10, 20, 30);

            var mock = new Mock <ILEDStrip>();

            int   index = -1;
            Color color = new Color();

            mock.Setup(x => x.SetLEDColor(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <Color>())).Callback <int, int, Color>((i, j, c) =>
            {
                index = j;
                color = c;
            });

            LedController controller = new LedController(mock.Object);

            controller.RenderFrame(frame);
            Assert.AreEqual(0, index);
            Assert.AreEqual(Color.FromArgb(10, 20, 30), color);
        }
Example #12
0
        public void SerializeFrame_Frame_CreatesCorrectByteArray()
        {
            int maxPackageSize      = 1016;
            int timeUnitsPerFrame   = 100;
            int frameIndex          = 9;
            FrameWithoutDelta frame = new FrameWithoutDelta(frameIndex, timeUnitsPerFrame, 3);

            frame[0] = new PixelInstruction(1, 2, 3);
            frame[1] = new PixelInstruction(4, 5, 6);
            frame[2] = new PixelInstruction(7, 8, 9);

            byte[] expectedBytes = new byte[sizeof(int) + sizeof(int) + sizeof(int) + sizeof(bool) + 3 + 3 + 3];
            int    startIndex    = 0;

            BitConverter.GetBytes(frameIndex).CopyTo(expectedBytes, startIndex);             // Frame index
            BitConverter.GetBytes(timeUnitsPerFrame).CopyTo(expectedBytes, startIndex += 4);
            BitConverter.GetBytes(frame.Count).CopyTo(expectedBytes, startIndex       += 4); // Number of PixelInstructions
            BitConverter.GetBytes(false).CopyTo(expectedBytes, startIndex             += 4); // Has FrameSections
            // instruction 1
            expectedBytes[startIndex += 1] = 1;
            expectedBytes[startIndex += 1] = 2;
            expectedBytes[startIndex += 1] = 3;
            // instruction 2
            expectedBytes[startIndex += 1] = 4;
            expectedBytes[startIndex += 1] = 5;
            expectedBytes[startIndex += 1] = 6;
            // instruction 3
            expectedBytes[startIndex += 1] = 7;
            expectedBytes[startIndex += 1] = 8;
            expectedBytes[startIndex += 1] = 9;

            byte[][] packages = FrameProtocol.SerializeFrame(frame, maxPackageSize);

            Assert.AreEqual(1, packages.Length);
            Assert.AreEqual(expectedBytes, packages[0]);
        }