public override bool Handle(byte messageByte) { if (!CanHandle(messageByte)) { Reset(); throw new MessageHandlerException("Error with the incoming byte. This is not a valid DigitalMessage"); } switch (currentHandlerState) { case HandlerState.StartEnd: message.Port = messageByte & MessageConstants.MESSAGEPINMASK; currentHandlerState = HandlerState.LSB; return(true); case HandlerState.LSB: LSBCache = messageByte; currentHandlerState = HandlerState.MSB; return(true); case HandlerState.MSB: message.PinStates = BitHelper.PortVal2PinVals((byte)BitHelper.BytesToInt(LSBCache, messageByte)); messageBroker.CreateEvent(message); Reset(); return(false); default: throw new ArgumentOutOfRangeException(); } }
public void Successfull_Analog_Message() { var bytes = new byte[] { // Analog Message Pin (byte)(handler.START_MESSAGE | 0x01), 0x01, //LSB bits 0-6 0x71, //MSB bits 0-6 }; for (int index = 0; index < bytes.Length - 1; index++) { var b = bytes[index]; Assert.IsTrue(handler.CanHandle(b)); Assert.IsTrue(handler.Handle(b)); } Assert.IsTrue(handler.CanHandle(bytes.Last())); Assert.IsFalse(handler.Handle(bytes.Last())); // Check to see if the handler has reset and can handle a new analog message Assert.IsTrue(handler.CanHandle(bytes[0])); mockBroker.Verify( p => p.CreateEvent( It.Is <AnalogMessage>( mes => mes.Pin == (bytes[0] & MessageConstants.MESSAGEPINMASK) && mes.Value == BitHelper.BytesToInt(bytes[1], bytes[2]))), Times.Once()); }
public void Throws_Error_On_Wrong_Pin() { var creator = new ExtendedAnalogMessageCreator(); var message = new ExtendedAnalogMessage() { Pin = 188, Value = BitHelper.BytesToInt(53, 42) }; Assert.Throws <MessageCreatorException>(() => creator.CreateMessage(message)); }
protected void HandleChar(byte charByte) { if (cacheChar != 255) { stringBuilder.Append((char)BitHelper.BytesToInt(cacheChar, charByte)); cacheChar = 255; } else { cacheChar = charByte; } }
public void Sevens2Fourteen_Functions_Correctly() { for (byte lsb = 0; lsb < byte.MaxValue; lsb++) { for (byte msb = 0; msb < byte.MaxValue; msb++) { var tempMSB = msb & 0x7F; var tempLSB = lsb & 0x7F; var value = tempMSB << 7 | tempLSB; Assert.AreEqual(BitHelper.BytesToInt(lsb, msb), value); } } }
public override void Creates_Appropriate_Message() { var bytes = new byte[] { MessageConstants.SYSEX_START, SysexCommands.SAMPLING_INTERVAL, 34, 54, MessageConstants.SYSEX_END }; var message = new SamplingIntervalMessage() { Interval = BitHelper.BytesToInt(34, 54) }; var creator = new SamplingIntervalMessageCreator(); var newBytes = creator.CreateMessage(message); Assert.AreEqual(bytes, newBytes); }
public void Creates_Other_Appropriate_Message() { var bytes = new byte[] { MessageConstants.SYSEX_START, SysexCommands.I2C_REQUEST, 0x34, 0x10, // 7bit, read continuously MessageConstants.SYSEX_END }; var message = new I2CRequestMessage { IsAddress10BitMode = false, ReadWriteOptions = I2CReadWriteOptions.ReadContinuously, SlaveAddress = BitHelper.BytesToInt(0x34, 0x00) }; TestMessage(bytes, message); }
public override void Creates_Appropriate_Message() { var bytes = new byte[] { MessageConstants.SYSEX_START, SysexCommands.I2C_REQUEST, 0x34, 0x18, // 7bit, stop reading MessageConstants.SYSEX_END }; var message = new I2CRequestMessage { IsAddress10BitMode = false, ReadWriteOptions = I2CReadWriteOptions.StopReading, SlaveAddress = BitHelper.BytesToInt(0x34, 0x0) }; TestMessage(bytes, message); }
public override void Creates_Appropriate_Message() { var bytes = new byte[] { MessageConstants.SYSEX_START, SysexCommands.I2C_CONFIG, 120, 34, MessageConstants.SYSEX_END }; var message = new I2CConfigMessage(); message.Delay = BitHelper.BytesToInt(120, 34); //message.IsPowerPinOn = true; var creator = new I2CConfigMessageCreator(); var newBytes = creator.CreateMessage(message); Assert.AreEqual(bytes, newBytes); }
public override void Creates_Appropriate_Message() { var bytes = new byte[] { MessageConstants.SYSEX_START, SysexCommands.SERVO_CONFIG, 53, //Pin 53, 43, //Min 46, 16, //Max 52, 1, //Angle MessageConstants.SYSEX_END }; var message = new ServoConfigMessage { Pin = 53, Angle = 180, MinPulse = BitHelper.BytesToInt(53, 43), MaxPulse = BitHelper.BytesToInt(46, 16) }; var creator = new ServoConfigMessageCreator(); var newBytes = creator.CreateMessage(message); Assert.AreEqual(bytes, newBytes); }
public override void Creates_Appropriate_Message() { var creator = new ExtendedAnalogMessageCreator(); var bytes = new byte[] { MessageConstants.SYSEX_START, SysexCommands.EXTENDED_ANALOG, 88, 53, 42, MessageConstants.SYSEX_END }; var message = new ExtendedAnalogMessage() { Pin = 88, Value = BitHelper.BytesToInt(53, 42) }; var newBytes = creator.CreateMessage(message); Assert.AreEqual(bytes, newBytes); }
public void Creates_Appropriate_Message_With_10bit_Address() { var bytes = new byte[] { MessageConstants.SYSEX_START, SysexCommands.I2C_REQUEST, 0x34, 0x23, // 10bit, write 0x71, 0x77, MessageConstants.SYSEX_END }; var message = new I2CRequestMessage { IsAddress10BitMode = true, ReadWriteOptions = I2CReadWriteOptions.Write, SlaveAddress = BitHelper.BytesToInt(0x34, 0x3) }; message.Data.Add(BitHelper.BytesToInt(0x71, 0x77)); TestMessage(bytes, message); }
protected override bool HandleByte(byte messageByte) { switch (currentState) { case HandlerState.StartEnd: currentState = HandlerState.SysexCommand; return(true); case HandlerState.SysexCommand: currentState = HandlerState.Address; return(true); case HandlerState.Address: if (messageByte > 127) { Reset(); throw new MessageHandlerException(BaseExceptionMessage + "This is not a valid address"); } if (firstByte) { byteCache = messageByte; firstByte = false; } else { message.SlaveAddress = BitHelper.BytesToInt(byteCache, messageByte); currentState = HandlerState.Register; firstByte = true; } return(true); case HandlerState.Register: if (messageByte > 127) { Reset(); throw new MessageHandlerException(BaseExceptionMessage + "This is not a valid register"); } if (firstByte) { byteCache = messageByte; firstByte = false; } else { message.Register = BitHelper.BytesToInt(byteCache, messageByte); currentState = HandlerState.Data; firstByte = true; } return(true); case HandlerState.Data: if (messageByte > 127 && messageByte != MessageConstants.SYSEX_END) { Reset(); throw new MessageHandlerException(BaseExceptionMessage + "These are not valid data"); } if (firstByte) { if (message.Data.Count == 0 && messageByte == MessageConstants.SYSEX_END) { Reset(); throw new MessageHandlerException(BaseExceptionMessage + "There was no data incoming"); } if (messageByte == MessageConstants.SYSEX_END) { messageBroker.CreateEvent(message); Reset(); return(false); } byteCache = messageByte; firstByte = false; return(true); } else { if (messageByte == MessageConstants.SYSEX_END) { Reset(); throw new MessageHandlerException(BaseExceptionMessage + "The message should not end here"); } message.Data.Add(BitHelper.BytesToInt(byteCache, messageByte)); firstByte = true; return(true); } default: throw new ArgumentOutOfRangeException(); } }
public void Succesful_Digital_Message() { var bytes = new byte[] { // Start Digital Message Pin (byte)(handler.START_MESSAGE | 0x05), 0x7F, // 0-6 bitmask 0x01, // 7-13 bitmask }; mockBroker.Setup(p => p.CreateEvent(It.IsAny <DigitalMessage>())); for (int i = 0; i < bytes.Length - 1; i++) { Assert.IsTrue(handler.CanHandle(bytes[i])); Assert.IsTrue(handler.Handle(bytes[i])); } Assert.IsTrue(handler.CanHandle(bytes.Last())); Assert.IsFalse(handler.Handle(bytes.Last())); mockBroker.Verify(p => p.CreateEvent(It.Is <DigitalMessage>(mes => mes.Port == (bytes[0] & MessageConstants.MESSAGEPINMASK) && mes.PinStates.SequenceEqual(BitHelper.PortVal2PinVals((byte)BitHelper.BytesToInt(bytes[1], bytes[2]))))), Times.Once()); // Check if the handler has reset Assert.IsTrue(handler.CanHandle(bytes[0])); }