Beispiel #1
0
        public void GetBoardAnalogMappingAsync()
        {
            var connection = new MockSerialConnection();
            var session    = CreateFirmataSession(connection);

            connection.EnqueueRequestAndResponse(new byte[] { 0xF0, 0x69, 0xF7 }, 0xF0, 0x6A);
            connection.EnqueueResponse(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x7F, 0x7F);
            connection.EnqueueResponse(0xF7);

            BoardAnalogMapping mapping = session.GetBoardAnalogMappingAsync().Result;

            Assert.AreEqual(6, mapping.PinMappings.Length);
            Assert.AreEqual(0, mapping.PinMappings[0].PinNumber);
            Assert.AreEqual(0, mapping.PinMappings[0].Channel);
            Assert.AreEqual(1, mapping.PinMappings[1].PinNumber);
            Assert.AreEqual(1, mapping.PinMappings[1].Channel);
            Assert.AreEqual(2, mapping.PinMappings[2].PinNumber);
            Assert.AreEqual(2, mapping.PinMappings[2].Channel);
            Assert.AreEqual(3, mapping.PinMappings[3].PinNumber);
            Assert.AreEqual(3, mapping.PinMappings[3].Channel);
            Assert.AreEqual(4, mapping.PinMappings[4].PinNumber);
            Assert.AreEqual(4, mapping.PinMappings[4].Channel);
            Assert.AreEqual(5, mapping.PinMappings[5].PinNumber);
            Assert.AreEqual(5, mapping.PinMappings[5].Channel);
        }
        public void RequestFirmware()
        {
            const int    majorVersion = 5;
            const int    minorVersion = 1;
            const string Name         = "Arduïno Firmata";

            var connection = new MockSerialConnection();
            var session    = CreateFirmataSession(connection);

            connection.EnqueueRequestAndResponse(new byte[] { 0xF0, 0x79, 0xF7 }, new byte[] { 0xF0, 0x79, majorVersion, minorVersion });
            connection.EnqueueResponse(Name.To14BitIso());
            connection.EnqueueResponse(0xF7);

            session.RequestFirmware();
            Assert.AreEqual(1, _messagesReceived.Count, "Message event error");
            IFirmataMessage message = _messagesReceived.Dequeue();

            //Assert.AreEqual(MessageType.FirmwareResponse, message.Type);
            Assert.IsInstanceOfType(message, typeof(FirmataMessage <Firmware>));

            var firmware = (Firmware)message.Value;

            Assert.AreEqual(firmware.MajorVersion, majorVersion);
            Assert.AreEqual(firmware.MinorVersion, minorVersion);
            Assert.AreEqual(firmware.Name, Name);
        }
Beispiel #3
0
        public void GetBoardCapability()
        {
            var connection = new MockSerialConnection();
            var session    = CreateFirmataSession(connection);

            // DIGITAL INPUT/DIGITAL OUTPUT/ANALOG/PWM/SERVO/I2C, 0/1/2/3/4/6
            connection.EnqueueRequestAndResponse(new byte[] { 0xF0, 0x6B, 0xF7 }, 0xF0, 0x6C);
            connection.EnqueueResponse(0, 1, 1, 1, 3, 10, 6, 1, 0x7F);
            connection.EnqueueResponse(0xF7);

            BoardCapability capability = session.GetBoardCapability();

            Assert.AreEqual(1, capability.Pins.Length);

            Assert.AreEqual(0, capability.Pins[0].PinNumber);
            Assert.AreEqual(true, capability.Pins[0].DigitalInput);
            Assert.AreEqual(true, capability.Pins[0].DigitalOutput);
            Assert.AreEqual(true, capability.Pins[0].Pwm);
            Assert.AreEqual(10, capability.Pins[0].PwmResolution);
            Assert.AreEqual(true, capability.Pins[0].I2C);

            Assert.AreEqual(false, capability.Pins[0].Analog);
            Assert.AreEqual(0, capability.Pins[0].AnalogResolution);
            Assert.AreEqual(false, capability.Pins[0].Servo);
            Assert.AreEqual(0, capability.Pins[0].ServoResolution);
        }
Beispiel #4
0
        public void GetPinState_PinNumber_Argument_Is128()
        {
            var connection = new MockSerialConnection();
            var session    = CreateFirmataSession(connection);

            connection.EnqueueRequestAndResponse(new byte[] { 0xF0, 0x6D, 0x00, 0xF7 }, 0xF0, 0x6E);
            // Pin 0 analog mode, value = 1023
            connection.EnqueueResponse(0x00, 0x02, 0x7F, 0x07);
            connection.EnqueueResponse(0xF7);

            PinState state = session.GetPinState(128);
        }
Beispiel #5
0
        public void GetPinState_PinNumber_Argument_Is127_Returned_Pin0()
        {
            var connection = new MockSerialConnection();
            var session    = CreateFirmataSession(connection);

            connection.EnqueueRequestAndResponse(new byte[] { 0xF0, 0x6D, 0x7F, 0xF7 }, 0xF0, 0x6E);
            // Pin 0 analog mode, value = 1023
            connection.EnqueueResponse(0x00, 0x02, 0x7F, 0x07);
            connection.EnqueueResponse(0xF7);

            PinState state = session.GetPinState(127);

            Assert.AreEqual(0, state.PinNumber);
            Assert.AreEqual(PinMode.AnalogInput, state.Mode);
            Assert.AreEqual(1023U, state.Value);
        }
Beispiel #6
0
        public void GetPinState_PinNumber_Argument_Is127()
        {
            var connection = new MockSerialConnection();
            var session    = CreateFirmataSession(connection);

            connection.EnqueueRequestAndResponse(new byte[] { 0xF0, 0x6D, 0x7F, 0xF7 }, 0xF0, 0x6E);
            // Pin 127 PWM mode, value = 256
            connection.EnqueueResponse(0x7F, 0x03, 0x00, 0x02);
            connection.EnqueueResponse(0xF7);

            PinState state = session.GetPinState(127);

            Assert.AreEqual(127, state.PinNumber);
            Assert.AreEqual(PinMode.PwmOutput, state.Mode);
            Assert.AreEqual(256U, state.Value);
        }
Beispiel #7
0
        public void GetPinStateAsync()
        {
            var connection = new MockSerialConnection();
            var session    = CreateFirmataSession(connection);

            connection.EnqueueRequestAndResponse(new byte[] { 0xF0, 0x6D, 0x05, 0xF7 }, 0xF0, 0x6E);
            // Pin 5 Digital Input mode, value = 1
            connection.EnqueueResponse(0x05, 0x00, 0x01);
            connection.EnqueueResponse(0xF7);

            PinState state = session.GetPinStateAsync(5).Result;

            Assert.AreEqual(5, state.PinNumber);
            Assert.AreEqual(PinMode.DigitalInput, state.Mode);
            Assert.AreEqual(1U, state.Value);
        }
Beispiel #8
0
        public void GetBoardCapabilityAsync()
        {
            var connection = new MockSerialConnection();
            var session    = CreateFirmataSession(connection);

            // DIGITAL INPUT/DIGITAL OUTPUT/ANALOG/PWM/SERVO, 0/1/2/3/4
            connection.EnqueueRequestAndResponse(new byte[] { 0xF0, 0x6B, 0xF7 }, 0xF0, 0x6C);
            connection.EnqueueResponse(2, 8, 0x7F);
            connection.EnqueueResponse(0, 1, 1, 1, 0x7F);
            connection.EnqueueResponse(1, 1, 3, 7, 4, 7, 0x7F);
            connection.EnqueueResponse(0xF7);

            BoardCapability capability = session.GetBoardCapabilityAsync().Result;

            Assert.AreEqual(3, capability.PinCapabilities.Length);

            Assert.AreEqual(0, capability.PinCapabilities[0].PinNumber);
            Assert.AreEqual(false, capability.PinCapabilities[0].DigitalInput);
            Assert.AreEqual(false, capability.PinCapabilities[0].DigitalOutput);
            Assert.AreEqual(true, capability.PinCapabilities[0].Analog);
            Assert.AreEqual(8, capability.PinCapabilities[0].AnalogResolution);
            Assert.AreEqual(false, capability.PinCapabilities[0].Pwm);
            Assert.AreEqual(0, capability.PinCapabilities[0].PwmResolution);
            Assert.AreEqual(false, capability.PinCapabilities[0].Servo);
            Assert.AreEqual(0, capability.PinCapabilities[0].ServoResolution);

            Assert.AreEqual(1, capability.PinCapabilities[1].PinNumber);
            Assert.AreEqual(true, capability.PinCapabilities[1].DigitalInput);
            Assert.AreEqual(true, capability.PinCapabilities[1].DigitalOutput);
            Assert.AreEqual(false, capability.PinCapabilities[1].Analog);
            Assert.AreEqual(0, capability.PinCapabilities[1].AnalogResolution);
            Assert.AreEqual(false, capability.PinCapabilities[1].Pwm);
            Assert.AreEqual(0, capability.PinCapabilities[1].PwmResolution);
            Assert.AreEqual(false, capability.PinCapabilities[1].Servo);
            Assert.AreEqual(0, capability.PinCapabilities[1].ServoResolution);

            Assert.AreEqual(2, capability.PinCapabilities[2].PinNumber);
            Assert.AreEqual(false, capability.PinCapabilities[2].DigitalInput);
            Assert.AreEqual(true, capability.PinCapabilities[2].DigitalOutput);
            Assert.AreEqual(false, capability.PinCapabilities[2].Analog);
            Assert.AreEqual(0, capability.PinCapabilities[2].AnalogResolution);
            Assert.AreEqual(true, capability.PinCapabilities[2].Pwm);
            Assert.AreEqual(7, capability.PinCapabilities[2].PwmResolution);
            Assert.AreEqual(true, capability.PinCapabilities[2].Servo);
            Assert.AreEqual(7, capability.PinCapabilities[2].ServoResolution);
        }
Beispiel #9
0
        public void GetFirmware()
        {
            const int    majorVersion = 3;
            const int    minorVersion = 7;
            const string Name         = "Arduino Firmata";

            var connection = new MockSerialConnection();
            var session    = new ArduinoSession(connection);

            connection.EnqueueRequestAndResponse(new byte[] { 0xF0, 0x79, 0xF7 }, new byte[] { 0xF0, 0x79, majorVersion, minorVersion });
            connection.EnqueueResponse(Name.To14BitIso());
            connection.EnqueueResponse(0xF7);

            Firmware firmware = session.GetFirmware();

            Assert.AreEqual(firmware.MajorVersion, majorVersion);
            Assert.AreEqual(firmware.MinorVersion, minorVersion);
            Assert.AreEqual(firmware.Name, Name);
        }
Beispiel #10
0
        public void RequestBoardAnalogMapping()
        {
            var connection = new MockSerialConnection();
            var session    = CreateFirmataSession(connection);

            connection.EnqueueRequestAndResponse(new byte[] { 0xF0, 0x69, 0xF7 }, 0xF0, 0x6A);
            connection.EnqueueResponse(0x0F);
            connection.EnqueueResponse(0xF7);

            session.RequestBoardAnalogMapping();
            FirmataMessage message = _messagesReceived.Dequeue();

            Assert.AreEqual(MessageType.AnalogMappingResponse, message.Type);

            BoardAnalogMapping mapping = (BoardAnalogMapping)message.Value;

            Assert.AreEqual(1, mapping.PinMappings.Length);
            Assert.AreEqual(0, mapping.PinMappings[0].PinNumber);
            Assert.AreEqual(15, mapping.PinMappings[0].Channel);
        }
Beispiel #11
0
        public void RequestPinState()
        {
            var connection = new MockSerialConnection();
            var session    = CreateFirmataSession(connection);

            connection.EnqueueRequestAndResponse(new byte[] { 0xF0, 0x6D, 0x01, 0xF7 }, 0xF0, 0x6E);
            // Pin 1 Digital Output mode, value = 0
            connection.EnqueueResponse(0x01, 0x01, 0x00);
            connection.EnqueueResponse(0xF7);

            session.RequestPinState(1);
            FirmataMessage message = _messagesReceived.Dequeue();

            Assert.AreEqual(MessageType.PinStateResponse, message.Type);
            PinState state = (PinState)message.Value;

            Assert.AreEqual(1, state.PinNumber);
            Assert.AreEqual(PinMode.DigitalOutput, state.Mode);
            Assert.AreEqual(0, state.Value);
        }
Beispiel #12
0
        public void GetI2CReply_BytesToRead_Parameter_Is0()
        {
            var connection = new MockSerialConnection();
            var session    = CreateFirmataSession(connection);

            connection.EnqueueRequest(0xF0, 0x76, 0x01, 0x08, 0x00, 0x00, 0xF7);
            connection.EnqueueResponse(0xF0, 0x77, 0x01, 0x00, 0x00, 0x00, 0xF7);
            I2CReply reply = session.GetI2CReply(1, 0);

            Assert.AreEqual(1, reply.Address);
            Assert.AreEqual(0, reply.Register);
            Assert.AreEqual(0, reply.Data.Length);
        }
        public void RequestBoardCapability()
        {
            var connection = new MockSerialConnection();
            var session    = CreateFirmataSession(connection);

            // DIGITAL INPUT/DIGITAL OUTPUT/ANALOG/PWM/SERVO, 0/1/2/3/4
            connection.EnqueueRequestAndResponse(new byte[] { 0xF0, 0x6B, 0xF7 }, 0xF0, 0x6C);
            connection.EnqueueResponse(2, 8, 0x7F);
            connection.EnqueueResponse(0, 127, 1, 127, 0x7F);
            connection.EnqueueResponse(0xF7);

            session.RequestBoardCapability();
            IFirmataMessage message = _messagesReceived.Dequeue();

            //Assert.AreEqual(MessageType.CapabilityResponse, message.Type);
            Assert.IsInstanceOfType(message, typeof(FirmataMessage <BoardCapability>));
            BoardCapability capability = (BoardCapability)message.Value;

            Assert.AreEqual(2, capability.Pins.Length);
            Assert.AreEqual(0, capability.Pins[0].PinNumber);
            Assert.AreEqual(false, capability.Pins[0].DigitalInput);
            Assert.AreEqual(false, capability.Pins[0].DigitalOutput);
            Assert.AreEqual(true, capability.Pins[0].Analog);
            Assert.AreEqual(8, capability.Pins[0].AnalogResolution);
            Assert.AreEqual(false, capability.Pins[0].Pwm);
            Assert.AreEqual(0, capability.Pins[0].PwmResolution);
            Assert.AreEqual(false, capability.Pins[0].Servo);
            Assert.AreEqual(0, capability.Pins[0].ServoResolution);

            Assert.AreEqual(1, capability.Pins[1].PinNumber);
            Assert.AreEqual(true, capability.Pins[1].DigitalInput);
            Assert.AreEqual(true, capability.Pins[1].DigitalOutput);
            Assert.AreEqual(false, capability.Pins[1].Analog);
            Assert.AreEqual(0, capability.Pins[1].AnalogResolution);
            Assert.AreEqual(false, capability.Pins[1].Pwm);
            Assert.AreEqual(0, capability.Pins[1].PwmResolution);
            Assert.AreEqual(false, capability.Pins[1].Servo);
            Assert.AreEqual(0, capability.Pins[1].ServoResolution);
        }
Beispiel #14
0
        public void GetI2CReply_WithSlaveRegister()
        {
            var connection = new MockSerialConnection();
            var session    = CreateFirmataSession(connection);

            connection.EnqueueRequest(0xF0, 0x76, 0x01, 0x08, 0x02, 0x00, 0x01, 0x00, 0xF7);
            connection.EnqueueResponse(0xF0, 0x77, 0x01, 0x00, 0x02, 0x00, 0x04, 0x00, 0xF7);
            I2CReply reply = session.GetI2CReply(1, 2, 1);

            Assert.AreEqual(1, reply.Address);
            Assert.AreEqual(2, reply.Register);
            Assert.AreEqual(1, reply.Data.Length);
            Assert.AreEqual(4, reply.Data[0]);
        }
Beispiel #15
0
        public void GetI2CReplyAsync_WithSlaveRegister()
        {
            var connection = new MockSerialConnection();
            var session    = CreateFirmataSession(connection);

            connection.EnqueueRequest(0xF0, 0x76, 0x7F, 0x08, 0x04, 0x02, 0x02, 0x00, 0xF7);
            connection.EnqueueResponse(0xF0, 0x77, 0x7F, 0x00, 0x04, 0x02, 0x7F, 0x01, 0x7E, 0x01, 0xF7);
            I2CReply reply = session.GetI2CReplyAsync(127, 260, 2).Result;

            Assert.AreEqual(127, reply.Address);
            Assert.AreEqual(260, reply.Register);
            Assert.AreEqual(2, reply.Data.Length);
            Assert.AreEqual(255, reply.Data[0]);
            Assert.AreEqual(254, reply.Data[1]);
        }
Beispiel #16
0
        public void GetI2CReply()
        {
            var connection = new MockSerialConnection();
            var session    = CreateFirmataSession(connection);

            connection.EnqueueRequest(0xF0, 0x76, 0x01, 0x08, 0x03, 0x00, 0xF7);
            connection.EnqueueResponse(0xF0, 0x77, 0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0xF7);
            I2CReply reply = session.GetI2CReply(1, 3);

            Assert.AreEqual(1, reply.Address);
            Assert.AreEqual(0, reply.Register);
            Assert.AreEqual(3, reply.Data.Length);
            Assert.AreEqual(5, reply.Data[0]);
            Assert.AreEqual(6, reply.Data[1]);
            Assert.AreEqual(7, reply.Data[2]);
        }
Beispiel #17
0
        public void Receive_StringDataMessage()
        {
            var connection = new MockSerialConnection();
            var session    = CreateFirmataSession(connection);
            int eventHits  = 0;

            session.MessageReceived += (o, e) =>
            {
                Assert.AreEqual(MessageType.StringData, e.Value.Type);
                Assert.AreEqual("Hello!", ((StringData)e.Value.Value).Text);
                eventHits++;
            };

            connection.EnqueueRequest(0xF0, 0x71, 0xF7);
            connection.EnqueueResponse(0xF0, 0x71, 0x48, 0x00, 0x65, 0x00, 0x6C, 0x00, 0x6C, 0x00, 0x6F, 0x00, 0x21, 0x00, 0xF7);
            session.SendStringData(null);
            Assert.AreEqual(1, eventHits, "MessageReceived event not hit once.");
        }
Beispiel #18
0
        public void I2CReplyReceived()
        {
            var connection = new MockSerialConnection();
            var session    = CreateFirmataSession(connection);
            int eventHits  = 0;

            session.I2CReplyReceived += (o, e) =>
            {
                Assert.AreEqual(1, e.Value.Address);
                Assert.AreEqual(0, e.Value.Register);
                Assert.AreEqual(3, e.Value.Data.Length);
                Assert.AreEqual(5, e.Value.Data[0]);
                Assert.AreEqual(6, e.Value.Data[1]);
                Assert.AreEqual(7, e.Value.Data[2]);
                eventHits++;
            };

            connection.EnqueueRequest(0xF0, 0x76, 0x01, 0x08, 0x03, 0x00, 0xF7);
            connection.EnqueueResponse(0xF0, 0x77, 0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0xF7);
            I2CReply reply = session.GetI2CReply(1, 3);

            Assert.AreEqual(1, eventHits);
        }
        public void GetI2CReplyAsync_WithSlaveRegister()
        {
            var connection = new MockSerialConnection();
            var session = CreateFirmataSession(connection);

            connection.EnqueueRequest(0xF0, 0x76, 0x7F, 0x08, 0x04, 0x02, 0x02, 0x00, 0xF7);
            connection.EnqueueResponse(0xF0, 0x77, 0x7F, 0x00, 0x04, 0x02, 0x7F, 0x01, 0x7E, 0x01, 0xF7);
            I2CReply reply = session.GetI2CReplyAsync(127, 260, 2).Result;
            Assert.AreEqual(127, reply.Address);
            Assert.AreEqual(260, reply.Register);
            Assert.AreEqual(2, reply.Data.Length);
            Assert.AreEqual(255, reply.Data[0]);
            Assert.AreEqual(254, reply.Data[1]);
        }
        public void RequestBoardCapability()
        {
            var connection = new MockSerialConnection();
            var session = CreateFirmataSession(connection);

            // DIGITAL INPUT/DIGITAL OUTPUT/ANALOG/PWM/SERVO, 0/1/2/3/4
            connection.EnqueueRequestAndResponse(new byte[] { 0xF0, 0x6B, 0xF7 }, 0xF0, 0x6C);
            connection.EnqueueResponse(2, 8, 0x7F);
            connection.EnqueueResponse(0, 127, 1, 127, 0x7F);
            connection.EnqueueResponse(0xF7);

            session.RequestBoardCapability();
            FirmataMessage message = _messagesReceived.Dequeue();

            Assert.AreEqual(MessageType.CapabilityResponse, message.Type);
            BoardCapability capability = (BoardCapability)message.Value;

            Assert.AreEqual(2, capability.PinCapabilities.Length);
            Assert.AreEqual(0, capability.PinCapabilities[0].PinNumber);
            Assert.AreEqual(false, capability.PinCapabilities[0].DigitalInput);
            Assert.AreEqual(false, capability.PinCapabilities[0].DigitalOutput);
            Assert.AreEqual(true, capability.PinCapabilities[0].Analog);
            Assert.AreEqual(8, capability.PinCapabilities[0].AnalogResolution);
            Assert.AreEqual(false, capability.PinCapabilities[0].Pwm);
            Assert.AreEqual(0, capability.PinCapabilities[0].PwmResolution);
            Assert.AreEqual(false, capability.PinCapabilities[0].Servo);
            Assert.AreEqual(0, capability.PinCapabilities[0].ServoResolution);

            Assert.AreEqual(1, capability.PinCapabilities[1].PinNumber);
            Assert.AreEqual(true, capability.PinCapabilities[1].DigitalInput);
            Assert.AreEqual(true, capability.PinCapabilities[1].DigitalOutput);
            Assert.AreEqual(false, capability.PinCapabilities[1].Analog);
            Assert.AreEqual(0, capability.PinCapabilities[1].AnalogResolution);
            Assert.AreEqual(false, capability.PinCapabilities[1].Pwm);
            Assert.AreEqual(0, capability.PinCapabilities[1].PwmResolution);
            Assert.AreEqual(false, capability.PinCapabilities[1].Servo);
            Assert.AreEqual(0, capability.PinCapabilities[1].ServoResolution);
        }
        public void GetBoardAnalogMappingAsync()
        {
            var connection = new MockSerialConnection();
            var session = CreateFirmataSession(connection);

            connection.EnqueueRequestAndResponse(new byte[] { 0xF0, 0x69, 0xF7 }, 0xF0, 0x6A);
            connection.EnqueueResponse(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x7F, 0x7F);
            connection.EnqueueResponse(0xF7);

            BoardAnalogMapping mapping = session.GetBoardAnalogMappingAsync().Result;

            Assert.AreEqual(6, mapping.PinMappings.Length);
            Assert.AreEqual(0, mapping.PinMappings[0].PinNumber);
            Assert.AreEqual(0, mapping.PinMappings[0].Channel);
            Assert.AreEqual(1, mapping.PinMappings[1].PinNumber);
            Assert.AreEqual(1, mapping.PinMappings[1].Channel);
            Assert.AreEqual(2, mapping.PinMappings[2].PinNumber);
            Assert.AreEqual(2, mapping.PinMappings[2].Channel);
            Assert.AreEqual(3, mapping.PinMappings[3].PinNumber);
            Assert.AreEqual(3, mapping.PinMappings[3].Channel);
            Assert.AreEqual(4, mapping.PinMappings[4].PinNumber);
            Assert.AreEqual(4, mapping.PinMappings[4].Channel);
            Assert.AreEqual(5, mapping.PinMappings[5].PinNumber);
            Assert.AreEqual(5, mapping.PinMappings[5].Channel);
        }
        public void RequestBoardAnalogMapping()
        {
            var connection = new MockSerialConnection();
            var session = CreateFirmataSession(connection);

            connection.EnqueueRequestAndResponse(new byte[] { 0xF0, 0x69, 0xF7 }, 0xF0, 0x6A);
            connection.EnqueueResponse(0x0F);
            connection.EnqueueResponse(0xF7);

            session.RequestBoardAnalogMapping();
            FirmataMessage message = _messagesReceived.Dequeue();
            Assert.AreEqual(MessageType.AnalogMappingResponse, message.Type);

            BoardAnalogMapping mapping = (BoardAnalogMapping)message.Value;

            Assert.AreEqual(1, mapping.PinMappings.Length);
            Assert.AreEqual(0, mapping.PinMappings[0].PinNumber);
            Assert.AreEqual(15, mapping.PinMappings[0].Channel);
        }
        public void GetPinState_PinNumber_Argument_Is128()
        {
            var connection = new MockSerialConnection();
            var session = CreateFirmataSession(connection);

            connection.EnqueueRequestAndResponse(new byte[] { 0xF0, 0x6D, 0x00, 0xF7 }, 0xF0, 0x6E);
            // Pin 0 analog mode, value = 1023
            connection.EnqueueResponse(0x00, 0x02, 0x7F, 0x07);
            connection.EnqueueResponse(0xF7);

            PinState state = session.GetPinState(128);
        }
        public void GetPinState_PinNumber_Argument_Is127_Returned_Pin0()
        {
            var connection = new MockSerialConnection();
            var session = CreateFirmataSession(connection);

            connection.EnqueueRequestAndResponse(new byte[] { 0xF0, 0x6D, 0x7F, 0xF7 }, 0xF0, 0x6E);
            // Pin 0 analog mode, value = 1023
            connection.EnqueueResponse(0x00, 0x02, 0x7F, 0x07);
            connection.EnqueueResponse(0xF7);

            PinState state = session.GetPinState(127);

            Assert.AreEqual(0, state.PinNumber);
            Assert.AreEqual(PinMode.AnalogInput, state.Mode);
            Assert.AreEqual(1023U, state.Value);
        }
        public void GetPinState_PinNumber_Argument_Is127()
        {
            var connection = new MockSerialConnection();
            var session = CreateFirmataSession(connection);

            connection.EnqueueRequestAndResponse(new byte[] { 0xF0, 0x6D, 0x7F, 0xF7 }, 0xF0, 0x6E);
            // Pin 127 PWM mode, value = 256
            connection.EnqueueResponse(0x7F, 0x03, 0x00, 0x02);
            connection.EnqueueResponse(0xF7);

            PinState state = session.GetPinState(127);

            Assert.AreEqual(127, state.PinNumber);
            Assert.AreEqual(PinMode.PwmOutput, state.Mode);
            Assert.AreEqual(256U, state.Value);
        }
        public void GetPinStateAsync()
        {
            var connection = new MockSerialConnection();
            var session = CreateFirmataSession(connection);

            connection.EnqueueRequestAndResponse(new byte[] { 0xF0, 0x6D, 0x05, 0xF7 }, 0xF0, 0x6E);
            // Pin 5 Digital Input mode, value = 1
            connection.EnqueueResponse(0x05, 0x00, 0x01);
            connection.EnqueueResponse(0xF7);

            PinState state = session.GetPinStateAsync(5).Result;

            Assert.AreEqual(5, state.PinNumber);
            Assert.AreEqual(PinMode.DigitalInput, state.Mode);
            Assert.AreEqual(1U, state.Value);
        }
        public void RequestPinState()
        {
            var connection = new MockSerialConnection();
            var session = CreateFirmataSession(connection);

            connection.EnqueueRequestAndResponse(new byte[] { 0xF0, 0x6D, 0x01, 0xF7 }, 0xF0, 0x6E);
            // Pin 1 Digital Output mode, value = 0
            connection.EnqueueResponse(0x01, 0x01, 0x00);
            connection.EnqueueResponse(0xF7);

            session.RequestPinState(1);
            FirmataMessage message = _messagesReceived.Dequeue();

            Assert.AreEqual(MessageType.PinStateResponse, message.Type);
            PinState state = (PinState)message.Value;

            Assert.AreEqual(1, state.PinNumber);
            Assert.AreEqual(PinMode.DigitalOutput, state.Mode);
            Assert.AreEqual(0, state.Value);
        }
        public void GetFirmware()
        {
            const int majorVersion = 3;
            const int minorVersion = 7;
            const string Name = "Arduino Firmata";

            var connection = new MockSerialConnection();
            var session = new ArduinoSession(connection);

            connection.EnqueueRequestAndResponse(new byte[] { 0xF0, 0x79, 0xF7 }, new byte[] { 0xF0, 0x79, majorVersion, minorVersion });
            connection.EnqueueResponse(Name.To14BitIso());
            connection.EnqueueResponse(0xF7);

            Firmware firmware = session.GetFirmware();
            Assert.AreEqual(firmware.MajorVersion, majorVersion);
            Assert.AreEqual(firmware.MinorVersion, minorVersion);
            Assert.AreEqual(firmware.Name, Name);
        }
        public void Receive_StringDataMessage()
        {
            var connection = new MockSerialConnection();
            var session = CreateFirmataSession(connection);
            int eventHits = 0;

            session.MessageReceived += (o, e) =>
            {
                Assert.AreEqual(MessageType.StringData, e.Value.Type);
                Assert.AreEqual("Hello!", ((StringData)e.Value.Value).Text);
                eventHits++;
            };

            connection.EnqueueRequest(0xF0, 0x71, 0xF7);
            connection.EnqueueResponse(0xF0, 0x71, 0x48, 0x00, 0x65, 0x00, 0x6C, 0x00, 0x6C, 0x00, 0x6F, 0x00, 0x21, 0x00, 0xF7);
            session.SendStringData(null);
            Assert.AreEqual(1, eventHits, "MessageReceived event not hit once.");
        }
        public void GetI2CReply()
        {
            var connection = new MockSerialConnection();
            var session = CreateFirmataSession(connection);

            connection.EnqueueRequest(0xF0, 0x76, 0x01, 0x08, 0x03, 0x00, 0xF7);
            connection.EnqueueResponse(0xF0, 0x77, 0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0xF7);
            I2CReply reply = session.GetI2CReply(1, 3);
            Assert.AreEqual(1, reply.Address);
            Assert.AreEqual(0, reply.Register);
            Assert.AreEqual(3, reply.Data.Length);
            Assert.AreEqual(5, reply.Data[0]);
            Assert.AreEqual(6, reply.Data[1]);
            Assert.AreEqual(7, reply.Data[2]);
        }
        public void GetI2CReply_BytesToRead_Parameter_Is0()
        {
            var connection = new MockSerialConnection();
            var session = CreateFirmataSession(connection);

            connection.EnqueueRequest(0xF0, 0x76, 0x01, 0x08, 0x00, 0x00, 0xF7);
            connection.EnqueueResponse(0xF0, 0x77, 0x01, 0x00, 0x00, 0x00, 0xF7);
            I2CReply reply = session.GetI2CReply(1, 0);
            Assert.AreEqual(1, reply.Address);
            Assert.AreEqual(0, reply.Register);
            Assert.AreEqual(0, reply.Data.Length);
        }
        public void GetI2CReply_WithSlaveRegister()
        {
            var connection = new MockSerialConnection();
            var session = CreateFirmataSession(connection);

            connection.EnqueueRequest(0xF0, 0x76, 0x01, 0x08, 0x02, 0x00, 0x01, 0x00, 0xF7);
            connection.EnqueueResponse(0xF0, 0x77, 0x01, 0x00, 0x02, 0x00, 0x04, 0x00, 0xF7);
            I2CReply reply = session.GetI2CReply(1, 2, 1);
            Assert.AreEqual(1, reply.Address);
            Assert.AreEqual(2, reply.Register);
            Assert.AreEqual(1, reply.Data.Length);
            Assert.AreEqual(4, reply.Data[0]);
        }
        public void GetBoardCapabilityAsync()
        {
            var connection = new MockSerialConnection();
            var session = CreateFirmataSession(connection);

            // DIGITAL INPUT/DIGITAL OUTPUT/ANALOG/PWM/SERVO, 0/1/2/3/4
            connection.EnqueueRequestAndResponse(new byte[] { 0xF0, 0x6B, 0xF7 }, 0xF0, 0x6C);
            connection.EnqueueResponse(2, 8, 0x7F);
            connection.EnqueueResponse(0, 1, 1, 1, 0x7F);
            connection.EnqueueResponse(1, 1, 3, 7, 4, 7, 0x7F);
            connection.EnqueueResponse(0xF7);

            BoardCapability capability = session.GetBoardCapabilityAsync().Result;
            Assert.AreEqual(3, capability.PinCapabilities.Length);

            Assert.AreEqual(0, capability.PinCapabilities[0].PinNumber);
            Assert.AreEqual(false, capability.PinCapabilities[0].DigitalInput);
            Assert.AreEqual(false, capability.PinCapabilities[0].DigitalOutput);
            Assert.AreEqual(true, capability.PinCapabilities[0].Analog);
            Assert.AreEqual(8, capability.PinCapabilities[0].AnalogResolution);
            Assert.AreEqual(false, capability.PinCapabilities[0].Pwm);
            Assert.AreEqual(0, capability.PinCapabilities[0].PwmResolution);
            Assert.AreEqual(false, capability.PinCapabilities[0].Servo);
            Assert.AreEqual(0, capability.PinCapabilities[0].ServoResolution);

            Assert.AreEqual(1, capability.PinCapabilities[1].PinNumber);
            Assert.AreEqual(true, capability.PinCapabilities[1].DigitalInput);
            Assert.AreEqual(true, capability.PinCapabilities[1].DigitalOutput);
            Assert.AreEqual(false, capability.PinCapabilities[1].Analog);
            Assert.AreEqual(0, capability.PinCapabilities[1].AnalogResolution);
            Assert.AreEqual(false, capability.PinCapabilities[1].Pwm);
            Assert.AreEqual(0, capability.PinCapabilities[1].PwmResolution);
            Assert.AreEqual(false, capability.PinCapabilities[1].Servo);
            Assert.AreEqual(0, capability.PinCapabilities[1].ServoResolution);

            Assert.AreEqual(2, capability.PinCapabilities[2].PinNumber);
            Assert.AreEqual(false, capability.PinCapabilities[2].DigitalInput);
            Assert.AreEqual(true, capability.PinCapabilities[2].DigitalOutput);
            Assert.AreEqual(false, capability.PinCapabilities[2].Analog);
            Assert.AreEqual(0, capability.PinCapabilities[2].AnalogResolution);
            Assert.AreEqual(true, capability.PinCapabilities[2].Pwm);
            Assert.AreEqual(7, capability.PinCapabilities[2].PwmResolution);
            Assert.AreEqual(true, capability.PinCapabilities[2].Servo);
            Assert.AreEqual(7, capability.PinCapabilities[2].ServoResolution);
        }
        public void I2CReplyReceived()
        {
            var connection = new MockSerialConnection();
            var session = CreateFirmataSession(connection);
            int eventHits = 0;

            session.I2CReplyReceived += (o, e) =>
            {
                Assert.AreEqual(1, e.Value.Address);
                Assert.AreEqual(0, e.Value.Register);
                Assert.AreEqual(3, e.Value.Data.Length);
                Assert.AreEqual(5, e.Value.Data[0]);
                Assert.AreEqual(6, e.Value.Data[1]);
                Assert.AreEqual(7, e.Value.Data[2]);
                eventHits++;
            };

            connection.EnqueueRequest(0xF0, 0x76, 0x01, 0x08, 0x03, 0x00, 0xF7);
            connection.EnqueueResponse(0xF0, 0x77, 0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0xF7);
            I2CReply reply = session.GetI2CReply(1, 3);
            Assert.AreEqual(1, eventHits);
        }
        public void RequestFirmware()
        {
            const int majorVersion = 5;
            const int minorVersion = 1;
            const string Name = "Arduïno Firmata";

            var connection = new MockSerialConnection();
            var session = CreateFirmataSession(connection);

            connection.EnqueueRequestAndResponse(new byte[] { 0xF0, 0x79, 0xF7 }, new byte[] { 0xF0, 0x79, majorVersion, minorVersion });
            connection.EnqueueResponse(Name.To14BitIso());
            connection.EnqueueResponse(0xF7);

            session.RequestFirmware();
            Assert.AreEqual(1, _messagesReceived.Count, "Message event error");
            FirmataMessage message = _messagesReceived.Dequeue();
            Assert.AreEqual(MessageType.FirmwareResponse, message.Type);
            var firmware = (Firmware)message.Value;

            Assert.AreEqual(firmware.MajorVersion, majorVersion);
            Assert.AreEqual(firmware.MinorVersion, minorVersion);
            Assert.AreEqual(firmware.Name, Name);
        }