public void TestTotalPinCountSuccess() { // Arrange RemoteDevice deviceUnderTest = null; RemoteDeviceHelper deviceHelper = new RemoteDeviceHelper(); int totalExpectedPins = 10; var pins = new List <MockPin>(); for (uint i = 0; i < totalExpectedPins; i++) { var pin = new MockPin(i); pins.Add(pin); } var board = new MockBoard(pins); // Act deviceUnderTest = deviceHelper.CreateDeviceUnderTestAndConnect(board); // Wait until the mock board is ready SpinWait.SpinUntil(() => { return(deviceHelper.DeviceState == DeviceState.Ready); }, 100000); // Assert Assert.AreEqual(totalExpectedPins, deviceUnderTest.DeviceHardwareProfile.TotalPinCount, "Total pin count on device was not communicated properly"); }
public void TestAnalogOffsetSuccess() { // Arrange RemoteDevice deviceUnderTest = null; RemoteDeviceHelper deviceHelper = new RemoteDeviceHelper(); int expectedOffset = 6; var pins = new List<MockPin>(); // We setup the board to have 10 total pins // on the 6th pin we start inserting analog pins for (uint i = 0; i < 10; i++) { var pin = new MockPin(i); if (i >= expectedOffset) pin.SupportedModes.Add(new KeyValuePair<PinMode, ushort>(PinMode.ANALOG, 1)); pins.Add(pin); } var board = new MockBoard(pins); // Act deviceUnderTest = deviceHelper.CreateDeviceUnderTestAndConnect(board); // Wait until the mock board is ready SpinWait.SpinUntil(() => { return deviceHelper.DeviceState == DeviceState.Ready; }, 100000); // Assert Assert.AreEqual(expectedOffset, deviceUnderTest.DeviceHardwareProfile.AnalogOffset, "Total analog pin count on device was not communicated properly"); }
public void TestDigitalPinSetModeSuccess() { // Arrange RemoteDevice deviceUnderTest = null; RemoteDeviceHelper deviceHelper = new RemoteDeviceHelper(); var expectedPinMode = PinMode.INPUT; byte pinUnderTest = 0; var pin = new MockPin(0); pin.SupportedModes.Add(new KeyValuePair <PinMode, ushort>(PinMode.INPUT, 1)); pin.SupportedModes.Add(new KeyValuePair <PinMode, ushort>(PinMode.PULLUP, 1)); pin.SupportedModes.Add(new KeyValuePair <PinMode, ushort>(PinMode.OUTPUT, 1)); var board = new MockBoard(new List <MockPin>() { pin }); // Act deviceUnderTest = deviceHelper.CreateDeviceUnderTestAndConnect(board); // Wait until the mock board is ready SpinWait.SpinUntil(() => { return(deviceHelper.DeviceState == DeviceState.Ready); }, 100000); deviceUnderTest.pinMode(pinUnderTest, expectedPinMode); var actualPinMode = deviceUnderTest.getPinMode(pinUnderTest); // Assert Assert.AreEqual(expectedPinMode, actualPinMode, "Pin mode was not set properly in cache"); Assert.AreEqual(expectedPinMode, board.Pins[pinUnderTest].CurrentMode, "Pin mode was communicated to board properly"); }
public void TestDigitalPinSetModeSuccess() { // Arrange RemoteDevice deviceUnderTest = null; RemoteDeviceHelper deviceHelper = new RemoteDeviceHelper(); var expectedPinMode = PinMode.INPUT; byte pinUnderTest = 0; var pin = new MockPin(0); pin.SupportedModes.Add(new KeyValuePair<PinMode, ushort>(PinMode.INPUT, 1)); pin.SupportedModes.Add(new KeyValuePair<PinMode, ushort>(PinMode.PULLUP, 1)); pin.SupportedModes.Add(new KeyValuePair<PinMode, ushort>(PinMode.OUTPUT, 1)); var board = new MockBoard(new List<MockPin>() { pin }); // Act deviceUnderTest = deviceHelper.CreateDeviceUnderTestAndConnect(board); // Wait until the mock board is ready SpinWait.SpinUntil(() => { return deviceHelper.DeviceState == DeviceState.Ready; }, 100000); deviceUnderTest.pinMode(pinUnderTest, expectedPinMode); var actualPinMode = deviceUnderTest.getPinMode(pinUnderTest); // Assert Assert.AreEqual(expectedPinMode, actualPinMode, "Pin mode was not set properly in cache"); Assert.AreEqual(expectedPinMode, board.Pins[pinUnderTest].CurrentMode, "Pin mode was communicated to board properly"); }
public RemoteDevice CreateDeviceUnderTestAndConnect(MockBoard board) { // setup and start connection events RemoteDevice device = null; mockFirmataStream = new MockStream(board); device = new RemoteDevice(mockFirmataStream); device.DeviceReady += OnDeviceReady; device.DeviceConnectionFailed += OnConnectionFailed; mockFirmataStream.begin(0, SerialConfig.SERIAL_8N1); // Wait for connection DateTime timeout = DateTime.UtcNow.AddMilliseconds(10000); System.Threading.SpinWait.SpinUntil(() => ( timeout <= DateTime.UtcNow || mockFirmataStream.connectionReady() || this.DeviceState == DeviceState.Ready )); foreach(var pin in board.Pins) { pin.CurrentValueChanged += Pin_CurrentValueChanged; } return device; }
public async Task TestDigitalPinReadValueSuccess() { // Arrange RemoteDevice deviceUnderTest = null; RemoteDeviceHelper deviceHelper = new RemoteDeviceHelper(); var pinMode = PinMode.INPUT; byte pinUnderTest = 0; PinState expectedPinState = PinState.HIGH; var pin = new MockPin(pinUnderTest); pin.SupportedModes.Add(new KeyValuePair<PinMode, ushort>(PinMode.INPUT, 1)); pin.SupportedModes.Add(new KeyValuePair<PinMode, ushort>(PinMode.PULLUP, 1)); pin.SupportedModes.Add(new KeyValuePair<PinMode, ushort>(PinMode.OUTPUT, 1)); var board = new MockBoard(new List<MockPin>() { pin }); // Act deviceUnderTest = deviceHelper.CreateDeviceUnderTestAndConnect(board); // Wait until the mock board is ready SpinWait.SpinUntil(() => { return deviceHelper.DeviceState == DeviceState.Ready; }, 100000); deviceUnderTest.pinMode(pinUnderTest, pinMode); board.Pins[pinUnderTest].CurrentValue = (ushort)expectedPinState; // Wait for the mock board to report the state change await Task.Delay(100); var actualPinState = deviceUnderTest.digitalRead(pinUnderTest); // Assert Assert.AreEqual(expectedPinState, actualPinState, "Pin state was incorrect"); }
public void TestAnalogPinCountSuccess() { // Arrange RemoteDevice deviceUnderTest = null; RemoteDeviceHelper deviceHelper = new RemoteDeviceHelper(); int totalExpectedPins = 10; var pins = new List<MockPin>(); for (uint i = 0; i < totalExpectedPins; i++) { var pin = new MockPin(i); pin.SupportedModes.Add(new KeyValuePair<PinMode, ushort>(PinMode.ANALOG, 1)); pins.Add(pin); } var board = new MockBoard(pins); // Act deviceUnderTest = deviceHelper.CreateDeviceUnderTestAndConnect(board); // Wait until the mock board is ready SpinWait.SpinUntil(() => { return deviceHelper.DeviceState == DeviceState.Ready; }, 100000); // Assert Assert.AreEqual(totalExpectedPins, deviceUnderTest.DeviceHardwareProfile.AnalogPinCount, "Total analog pin count on device was not communicated properly"); }
public void TestAnalogPinSetModeSuccess() { // Arrange RemoteDevice deviceUnderTest = null; RemoteDeviceHelper deviceHelper = new RemoteDeviceHelper(); var expectedPinMode = PinMode.ANALOG; var pin = new MockPin(0); pin.SupportedModes.Add(new KeyValuePair <PinMode, ushort>(PinMode.ANALOG, 1)); var board = new MockBoard(new List <MockPin>() { pin }); // Act deviceUnderTest = deviceHelper.CreateDeviceUnderTestAndConnect(board); // Wait until the mock board is ready SpinWait.SpinUntil(() => { return(deviceHelper.DeviceState == DeviceState.Ready); }, 100000); deviceUnderTest.pinMode("A0", PinMode.ANALOG); // Assert Assert.AreEqual(expectedPinMode, deviceUnderTest.getPinMode("A0"), "Pin mode was not set properly"); }
public void TestAnalogOffsetSuccess() { // Arrange RemoteDevice deviceUnderTest = null; RemoteDeviceHelper deviceHelper = new RemoteDeviceHelper(); int expectedOffset = 6; var pins = new List <MockPin>(); // We setup the board to have 10 total pins // on the 6th pin we start inserting analog pins for (uint i = 0; i < 10; i++) { var pin = new MockPin(i); if (i >= expectedOffset) { pin.SupportedModes.Add(new KeyValuePair <PinMode, ushort>(PinMode.ANALOG, 1)); } pins.Add(pin); } var board = new MockBoard(pins); // Act deviceUnderTest = deviceHelper.CreateDeviceUnderTestAndConnect(board); // Wait until the mock board is ready SpinWait.SpinUntil(() => { return(deviceHelper.DeviceState == DeviceState.Ready); }, 100000); // Assert Assert.AreEqual(expectedOffset, deviceUnderTest.DeviceHardwareProfile.AnalogOffset, "Total analog pin count on device was not communicated properly"); }
public void TestPinCapabilityNoneSupportedSuccess() { // Arrange RemoteDevice deviceUnderTest = null; RemoteDeviceHelper deviceHelper = new RemoteDeviceHelper(); int totalExpectedPins = 1; var pins = new List <MockPin>(); for (uint i = 0; i < totalExpectedPins; i++) { var pin = new MockPin(i); pins.Add(pin); } var board = new MockBoard(pins); // Act deviceUnderTest = deviceHelper.CreateDeviceUnderTestAndConnect(board); // Wait until the mock board is ready SpinWait.SpinUntil(() => { return(deviceHelper.DeviceState == DeviceState.Ready); }, 100000); // Assert Assert.AreEqual(totalExpectedPins, deviceUnderTest.DeviceHardwareProfile.TotalPinCount, "Total pin count on device was not communicated properly"); Assert.IsFalse(deviceUnderTest.DeviceHardwareProfile.isAnalogSupported(0), "isAnalogSupported did not get set properly"); Assert.IsFalse(deviceUnderTest.DeviceHardwareProfile.isDigitalInputSupported(0), "isDigitalInputSupported did not get set properly"); Assert.IsFalse(deviceUnderTest.DeviceHardwareProfile.isDigitalInputPullupSupported(0), "isDigitalInputPullupSupported did not get set properly"); Assert.IsFalse(deviceUnderTest.DeviceHardwareProfile.isDigitalOutputSupported(0), "isDigitalOutputSupported did not get set properly"); Assert.IsFalse(deviceUnderTest.DeviceHardwareProfile.isI2cSupported(0), "isI2cSupported did not get set properly"); Assert.IsFalse(deviceUnderTest.DeviceHardwareProfile.isPwmSupported(0), "isPwmSupported did not get set properly"); Assert.IsFalse(deviceUnderTest.DeviceHardwareProfile.isServoSupported(0), "isServoSupported did not get set properly"); }
public RemoteDevice CreateDeviceUnderTestAndConnect(MockBoard board) { // setup and start connection events RemoteDevice device = null; mockFirmataStream = new MockStream(board); device = new RemoteDevice(mockFirmataStream); device.DeviceReady += OnDeviceReady; device.DeviceConnectionFailed += OnConnectionFailed; mockFirmataStream.begin(0, SerialConfig.SERIAL_8N1); // Wait for connection DateTime timeout = DateTime.UtcNow.AddMilliseconds(10000); System.Threading.SpinWait.SpinUntil(() => ( timeout <= DateTime.UtcNow || mockFirmataStream.connectionReady() || this.DeviceState == DeviceState.Ready )); foreach (var pin in board.Pins) { pin.CurrentValueChanged += Pin_CurrentValueChanged; } return(device); }
public MockStream(MockBoard board) { this.Board = board; this.ConnectionReady = true; this.BufferSize = 100; this.ResponseBuffer = new List<UInt16>(); this.ActiveReadBuffer = new List<UInt16>(); this.LastFlushedReadBuffer = new List<UInt16>(); }
public MockStream(MockBoard board) { this.Board = board; this.ConnectionReady = true; this.BufferSize = 100; this.ResponseBuffer = new List <UInt16>(); this.ActiveReadBuffer = new List <UInt16>(); this.LastFlushedReadBuffer = new List <UInt16>(); }
private static List <UInt16> prepareCapabilityResponseMessage(MockBoard board) { var message = new List <UInt16>(); message.Add((ushort)Command.START_SYSEX); message.Add((ushort)SysexCommand.CAPABILITY_RESPONSE); foreach (var pin in board.Pins) { foreach (var mode in pin.SupportedModes) { message.Add((ushort)mode.Key); message.Add(mode.Value); } message.Add(127); } message.Add((ushort)Command.END_SYSEX); return(message); }
public async Task TestDigitalPinReadValueSuccess() { // Arrange RemoteDevice deviceUnderTest = null; RemoteDeviceHelper deviceHelper = new RemoteDeviceHelper(); var pinMode = PinMode.INPUT; byte pinUnderTest = 0; PinState expectedPinState = PinState.HIGH; var pin = new MockPin(pinUnderTest); pin.SupportedModes.Add(new KeyValuePair <PinMode, ushort>(PinMode.INPUT, 1)); pin.SupportedModes.Add(new KeyValuePair <PinMode, ushort>(PinMode.PULLUP, 1)); pin.SupportedModes.Add(new KeyValuePair <PinMode, ushort>(PinMode.OUTPUT, 1)); var board = new MockBoard(new List <MockPin>() { pin }); // Act deviceUnderTest = deviceHelper.CreateDeviceUnderTestAndConnect(board); // Wait until the mock board is ready SpinWait.SpinUntil(() => { return(deviceHelper.DeviceState == DeviceState.Ready); }, 100000); deviceUnderTest.pinMode(pinUnderTest, pinMode); board.Pins[pinUnderTest].CurrentValue = (ushort)expectedPinState; // Wait for the mock board to report the state change await Task.Delay(100); var actualPinState = deviceUnderTest.digitalRead(pinUnderTest); // Assert Assert.AreEqual(expectedPinState, actualPinState, "Pin state was incorrect"); }
public void TestAnalogPinSetModeSuccess() { // Arrange RemoteDevice deviceUnderTest = null; RemoteDeviceHelper deviceHelper = new RemoteDeviceHelper(); var expectedPinMode = PinMode.ANALOG; var pin = new MockPin(0); pin.SupportedModes.Add(new KeyValuePair<PinMode, ushort>(PinMode.ANALOG, 1)); var board = new MockBoard(new List<MockPin>() { pin }); // Act deviceUnderTest = deviceHelper.CreateDeviceUnderTestAndConnect(board); // Wait until the mock board is ready SpinWait.SpinUntil(() => { return deviceHelper.DeviceState == DeviceState.Ready; }, 100000); deviceUnderTest.pinMode("A0", PinMode.ANALOG); // Assert Assert.AreEqual(expectedPinMode, deviceUnderTest.getPinMode("A0"), "Pin mode was not set properly"); }
public void TestPinCapabilityNoneSupportedSuccess() { // Arrange RemoteDevice deviceUnderTest = null; RemoteDeviceHelper deviceHelper = new RemoteDeviceHelper(); int totalExpectedPins = 1; var pins = new List<MockPin>(); for (uint i = 0; i < totalExpectedPins; i++) { var pin = new MockPin(i); pins.Add(pin); } var board = new MockBoard(pins); // Act deviceUnderTest = deviceHelper.CreateDeviceUnderTestAndConnect(board); // Wait until the mock board is ready SpinWait.SpinUntil(() => { return deviceHelper.DeviceState == DeviceState.Ready; }, 100000); // Assert Assert.AreEqual(totalExpectedPins, deviceUnderTest.DeviceHardwareProfile.TotalPinCount, "Total pin count on device was not communicated properly"); Assert.IsFalse(deviceUnderTest.DeviceHardwareProfile.isAnalogSupported(0), "isAnalogSupported did not get set properly"); Assert.IsFalse(deviceUnderTest.DeviceHardwareProfile.isDigitalInputSupported(0), "isDigitalInputSupported did not get set properly"); Assert.IsFalse(deviceUnderTest.DeviceHardwareProfile.isDigitalInputPullupSupported(0), "isDigitalInputPullupSupported did not get set properly"); Assert.IsFalse(deviceUnderTest.DeviceHardwareProfile.isDigitalOutputSupported(0), "isDigitalOutputSupported did not get set properly"); Assert.IsFalse(deviceUnderTest.DeviceHardwareProfile.isI2cSupported(0), "isI2cSupported did not get set properly"); Assert.IsFalse(deviceUnderTest.DeviceHardwareProfile.isPwmSupported(0), "isPwmSupported did not get set properly"); Assert.IsFalse(deviceUnderTest.DeviceHardwareProfile.isServoSupported(0), "isServoSupported did not get set properly"); }
public void TestPinArraysProperlyPopulatedSuccess() { // Arrange RemoteDevice deviceUnderTest = null; RemoteDeviceHelper deviceHelper = new RemoteDeviceHelper(); int totalExpectedPins = 10; int totalExpectedAnalogPins = 1; int totalExpectedDigitalPins = 2; int totalExpectedI2cPins = 3; int totalExpectedPwmPins = 4; int totalExpectedServoPins = 5; // Since we only have 5 pins with capabilities the left over will be disabled int totalExpectedDisabledPins = totalExpectedPins - totalExpectedServoPins; var pins = new List<MockPin>(); for (uint i = 0; i < totalExpectedPins; i++) { var pin = new MockPin(i); if (i < totalExpectedAnalogPins) pin.SupportedModes.Add(new KeyValuePair<PinMode, ushort>(PinMode.ANALOG, 1)); if (i < totalExpectedDigitalPins) pin.SupportedModes.Add(new KeyValuePair<PinMode, ushort>(PinMode.OUTPUT, 1)); if (i < totalExpectedI2cPins) pin.SupportedModes.Add(new KeyValuePair<PinMode, ushort>(PinMode.I2C, 1)); if (i < totalExpectedPwmPins) pin.SupportedModes.Add(new KeyValuePair<PinMode, ushort>(PinMode.PWM, 1)); if (i < totalExpectedServoPins) pin.SupportedModes.Add(new KeyValuePair<PinMode, ushort>(PinMode.SERVO, 1)); pins.Add(pin); } var board = new MockBoard(pins); // Act deviceUnderTest = deviceHelper.CreateDeviceUnderTestAndConnect(board); // Wait until the mock board is ready SpinWait.SpinUntil(() => { return deviceHelper.DeviceState == DeviceState.Ready; }, 100000); // Assert Assert.AreEqual(totalExpectedPins, deviceUnderTest.DeviceHardwareProfile.TotalPinCount, "Total pin count on device was not communicated properly"); Assert.AreEqual(totalExpectedAnalogPins, deviceUnderTest.DeviceHardwareProfile.AnalogPins.Count(), "Analog pins were not correctly enumerated"); Assert.AreEqual(totalExpectedDigitalPins, deviceUnderTest.DeviceHardwareProfile.DigitalPins.Count(), "Digital pins were not correctly enumerated"); Assert.AreEqual(totalExpectedDisabledPins, deviceUnderTest.DeviceHardwareProfile.DisabledPins.Count(), "Disabled pins were not correctly enumerated"); Assert.AreEqual(totalExpectedI2cPins, deviceUnderTest.DeviceHardwareProfile.I2cPins.Count(), "I2c pins were not correctly enumerated"); Assert.AreEqual(totalExpectedPwmPins, deviceUnderTest.DeviceHardwareProfile.PwmPins.Count(), "Pwm pins were not correctly enumerated"); Assert.AreEqual(totalExpectedServoPins, deviceUnderTest.DeviceHardwareProfile.ServoPins.Count(), "Servo pins were not correctly enumerated"); }
private static List<UInt16> prepareCapabilityResponseMessage(MockBoard board) { var message = new List<UInt16>(); message.Add((ushort)Command.START_SYSEX); message.Add((ushort)SysexCommand.CAPABILITY_RESPONSE); foreach (var pin in board.Pins) { foreach (var mode in pin.SupportedModes) { message.Add((ushort)mode.Key); message.Add(mode.Value); } message.Add(127); } message.Add((ushort)Command.END_SYSEX); return message; }
public void TestPinArraysProperlyPopulatedSuccess() { // Arrange RemoteDevice deviceUnderTest = null; RemoteDeviceHelper deviceHelper = new RemoteDeviceHelper(); int totalExpectedPins = 10; int totalExpectedAnalogPins = 1; int totalExpectedDigitalPins = 2; int totalExpectedI2cPins = 3; int totalExpectedPwmPins = 4; int totalExpectedServoPins = 5; // Since we only have 5 pins with capabilities the left over will be disabled int totalExpectedDisabledPins = totalExpectedPins - totalExpectedServoPins; var pins = new List <MockPin>(); for (uint i = 0; i < totalExpectedPins; i++) { var pin = new MockPin(i); if (i < totalExpectedAnalogPins) { pin.SupportedModes.Add(new KeyValuePair <PinMode, ushort>(PinMode.ANALOG, 1)); } if (i < totalExpectedDigitalPins) { pin.SupportedModes.Add(new KeyValuePair <PinMode, ushort>(PinMode.OUTPUT, 1)); } if (i < totalExpectedI2cPins) { pin.SupportedModes.Add(new KeyValuePair <PinMode, ushort>(PinMode.I2C, 1)); } if (i < totalExpectedPwmPins) { pin.SupportedModes.Add(new KeyValuePair <PinMode, ushort>(PinMode.PWM, 1)); } if (i < totalExpectedServoPins) { pin.SupportedModes.Add(new KeyValuePair <PinMode, ushort>(PinMode.SERVO, 1)); } pins.Add(pin); } var board = new MockBoard(pins); // Act deviceUnderTest = deviceHelper.CreateDeviceUnderTestAndConnect(board); // Wait until the mock board is ready SpinWait.SpinUntil(() => { return(deviceHelper.DeviceState == DeviceState.Ready); }, 100000); // Assert Assert.AreEqual(totalExpectedPins, deviceUnderTest.DeviceHardwareProfile.TotalPinCount, "Total pin count on device was not communicated properly"); Assert.AreEqual(totalExpectedAnalogPins, deviceUnderTest.DeviceHardwareProfile.AnalogPins.Count(), "Analog pins were not correctly enumerated"); Assert.AreEqual(totalExpectedDigitalPins, deviceUnderTest.DeviceHardwareProfile.DigitalPins.Count(), "Digital pins were not correctly enumerated"); Assert.AreEqual(totalExpectedDisabledPins, deviceUnderTest.DeviceHardwareProfile.DisabledPins.Count(), "Disabled pins were not correctly enumerated"); Assert.AreEqual(totalExpectedI2cPins, deviceUnderTest.DeviceHardwareProfile.I2cPins.Count(), "I2c pins were not correctly enumerated"); Assert.AreEqual(totalExpectedPwmPins, deviceUnderTest.DeviceHardwareProfile.PwmPins.Count(), "Pwm pins were not correctly enumerated"); Assert.AreEqual(totalExpectedServoPins, deviceUnderTest.DeviceHardwareProfile.ServoPins.Count(), "Servo pins were not correctly enumerated"); }