internal void WriteCommand(CommandByte command, params byte[] data) { List <byte> completeData = new List <byte>(); completeData.Add(Convert.ToByte(command)); completeData.InsertRange(completeData.Count, data); _i2cInterface.Write(_i2cConnectionSettingIo, completeData.ToArray()); }
private static Boolean ValidateResponse(CommandByte expectedCommand, Byte[] input) { if (input.Length < 3 || input.Length != input[1] + 3) { return(false); } byte[] crcBytes = input.Skip(2).Take(input[1]).ToArray(); return(BlockityHelpers.GetCrc(crcBytes) == input.Last()); }
public static Boolean SimpleResponse(CommandByte expectedCommand, Byte[] input) { if (input == null || input.Length != 3) { return(false); } if (input[0] != (Byte)expectedCommand || input[1] != 0) { return(false); } return(true); }
/// <summary> /// Initializes a new instance of the <see cref="MonoBrick.NXT.Command"/> class. /// </summary> /// <param name='type'> /// The command type. Can be a system command, direct command or reply command /// </param> /// <param name='commandByte'> /// The command byte /// </param> /// <param name='reply'> /// If set to <c>true</c> the NXT will send a reply /// </param> public Command(CommandType type, CommandByte commandByte, bool reply) { commandType = type; this.commandByte = commandByte; if (reply){ replyRequired = true; dataArr.Add((byte)type); } else{ replyRequired = false; dataArr.Add((byte)((byte) type | 0x80)); } dataArr.Add((byte)commandByte); }
/// <summary> /// Initializes a new instance of the <see cref="MonoBrick.NXT.Command"/> class. /// </summary> /// <param name='type'> /// The command type. Can be a system command, direct command or reply command /// </param> /// <param name='commandByte'> /// The command byte /// </param> /// <param name='reply'> /// If set to <c>true</c> the NXT will send a reply /// </param> public Command(CommandType type, CommandByte commandByte, bool reply) { commandType = type; this.commandByte = commandByte; if (reply) { replyRequired = true; dataArr.Add((byte)type); } else { replyRequired = false; dataArr.Add((byte)((byte)type | 0x80)); } dataArr.Add((byte)commandByte); }
/// <summary> /// Initializes a new instance of the <see cref="MonoBrick.NXT.Command"/> class using an array of bytes /// </summary> /// <param name='data'> /// The data to be used for the command /// </param> public Command(byte [] data) { if (data.Length < 2) { throw new System.ArgumentException("Invalid NXT Command"); } for (int i = 0; i < data.Length; i++) { dataArr.Add(data[i]); } try{ commandType = (CommandType)(data[0] & 0x7f); commandByte = (CommandByte)data[1]; } catch (Exception) { throw new System.ArgumentException("Invalid NXT Command"); } replyRequired = !Convert.ToBoolean(data[0] & 0x80); }
/// <summary> /// Initializes a new instance of the <see cref="MonoBrick.NXT.Reply"/> class. /// </summary> /// <param name='type'> /// The command type. Can be a system command, direct command or reply command /// </param> /// <param name='command'> /// The command byte /// </param> /// <param name='data'> /// Byte array to be used for reply payload /// </param> /// <param name='errorCode'> /// Error code /// </param> public Reply(CommandType type, CommandByte command, byte[] data, byte errorCode) { if (data != null) { dataArray = new byte[data.Length + 3]; } else { dataArray = new byte[3]; } dataArray[0] = (byte)type; dataArray[1] = (byte)command; dataArray[2] = errorCode; if (data != null) { for (int i = 0; i < data.Length; i++) { dataArray[i + 3] = data[i]; } } }
/// <summary> /// Initializes a new instance of the <see cref="MonoBrick.NXT.Reply"/> class without payload with an error code /// </summary> /// <param name='type'> /// The command type. Can be a system command, direct command or reply command /// </param> /// <param name='command'> /// The command byte /// </param> /// <param name='errorCode'> /// Error code /// </param> public Reply(CommandType type, CommandByte command, byte errorCode):this(type,command,null,errorCode){ }
/// <summary> /// Initializes a new instance of the <see cref="MonoBrick.NXT.Reply"/> class without payload and errors /// </summary> /// <param name='type'> /// The command type. Can be a system command, direct command or reply command /// </param> /// <param name='command'> /// The command byte /// </param> public Reply(CommandType type, CommandByte command):this(type,command,null,0){ }
/// <summary> /// Initializes a new instance of the <see cref="MonoBrick.NXT.Reply"/> class without errors /// </summary> /// <param name='type'> /// The command type. Can be a system command, direct command or reply command /// </param> /// <param name='command'> /// The command byte /// </param> /// <param name='data'> /// Byte array to be used for reply payload /// </param> public Reply(CommandType type, CommandByte command, byte[] data):this(type,command,data,0){ }
/// <summary> /// Initializes a new instance of the <see cref="MonoBrick.NXT.Reply"/> class. /// </summary> /// <param name='type'> /// The command type. Can be a system command, direct command or reply command /// </param> /// <param name='command'> /// The command byte /// </param> /// <param name='data'> /// Byte array to be used for reply payload /// </param> /// <param name='errorCode'> /// Error code /// </param> public Reply(CommandType type, CommandByte command, byte[] data, byte errorCode){ if(data!= null) dataArray = new byte[data.Length+3]; else dataArray = new byte[3]; dataArray[0] = (byte) type; dataArray[1] = (byte) command; dataArray[2] = errorCode; if(data != null){ for(int i = 0; i < data.Length; i++){ dataArray[i+3] = data[i]; } } }
/// <summary> /// Initializes a new instance of the <see cref="MonoBrick.NXT.Reply"/> class without payload and errors /// </summary> /// <param name='type'> /// The command type. Can be a system command, direct command or reply command /// </param> /// <param name='command'> /// The command byte /// </param> public Reply(CommandType type, CommandByte command) : this(type, command, null, 0) { }
/// <summary> /// Initializes a new instance of the <see cref="MonoBrick.NXT.Reply"/> class without errors /// </summary> /// <param name='type'> /// The command type. Can be a system command, direct command or reply command /// </param> /// <param name='command'> /// The command byte /// </param> /// <param name='data'> /// Byte array to be used for reply payload /// </param> public Reply(CommandType type, CommandByte command, byte[] data) : this(type, command, data, 0) { }
private static HeightMapVertex DecodeVertex(byte[] data, ref int index, short defaultX, short defaultZ) { short x = defaultX; short y = 0; short z = defaultZ; CommandByte command = new CommandByte(data[index]); index++; if (command.IsQuitCommand) { return(null); } //Obtain the X if (!command.UseDefaultX) { switch (command.XFlags) { case CommandByte.ByteReadingType.Read2Bytes: x = (short)((short)(data[index] << 8) + data[index + 1]); index += 2; break; case CommandByte.ByteReadingType.Read1ByteNoShift: x = (short)data[index]; index++; break; case CommandByte.ByteReadingType.Read1ByteShift1: x = (short)(data[index] << 5); index++; break; case CommandByte.ByteReadingType.Read1ByteShift2: x = (short)(data[index] << 6); index++; break; } } //Then obtain the Y switch (command.YFlags) { case CommandByte.ByteReadingType.Read2Bytes: y = (short)((short)(data[index] << 8) + data[index + 1]); index += 2; break; case CommandByte.ByteReadingType.Read1ByteNoShift: y = (short)data[index]; index++; break; case CommandByte.ByteReadingType.Read1ByteShift1: y = (short)(data[index] << 1); index++; break; case CommandByte.ByteReadingType.Read1ByteShift2: y = (short)(data[index] << 2); index++; break; } //And finally Z if (!command.UseDefaultZ) { switch (command.ZFlags) { case CommandByte.ByteReadingType.Read2Bytes: z = (short)((short)(data[index] << 8) + data[index + 1]); index += 2; break; case CommandByte.ByteReadingType.Read1ByteNoShift: z = (short)data[index]; index++; break; case CommandByte.ByteReadingType.Read1ByteShift1: z = (short)(data[index] << 5); index++; break; case CommandByte.ByteReadingType.Read1ByteShift2: z = (short)(data[index] << 6); index++; break; } } return(new HeightMapVertex(x, y, z)); }
private static void EncodeVertex(HeightMapVertex vertex, List <byte> output, short defaultX, short defaultZ) { CommandByte command = new CommandByte(); command.UseDefaultX = (defaultX == vertex.X); command.UseDefaultZ = (defaultZ == vertex.Z); List <byte> bytes = new List <byte>(); if (!command.UseDefaultX) { //Determine x flags if (((ushort)(vertex.X << 8) >> 8) == vertex.X) { command.XFlags = CommandByte.ByteReadingType.Read1ByteNoShift; bytes.Add((byte)(vertex.X & 0xff)); } else if ((vertex.X & 0x3fc0) == vertex.X && ((ushort)(vertex.X << 2) >> 2) == vertex.X) { command.XFlags = CommandByte.ByteReadingType.Read1ByteShift2; bytes.Add((byte)((vertex.X >> 6) & 0xff)); } else if ((vertex.X & 0x1fe0) == vertex.X && ((ushort)(vertex.X << 3) >> 3) == vertex.X) { command.XFlags = CommandByte.ByteReadingType.Read1ByteShift1; bytes.Add((byte)((vertex.X >> 5) & 0xff)); } else { command.XFlags = CommandByte.ByteReadingType.Read2Bytes; bytes.Add((byte)((vertex.X >> 8) & 0xff)); bytes.Add((byte)(vertex.X & 0xff)); } } //Determine y flags if (((ushort)(vertex.Y << 8) >> 8) == vertex.Y) { command.YFlags = CommandByte.ByteReadingType.Read1ByteNoShift; bytes.Add((byte)(vertex.Y & 0xff)); } else if ((vertex.Y & 0x03fc) == vertex.Y && ((ushort)(vertex.Y << 6) >> 6) == vertex.Y) { command.YFlags = CommandByte.ByteReadingType.Read1ByteShift2; bytes.Add((byte)((vertex.Y >> 2) & 0xff)); } else if ((vertex.Y & 0x01fe) == vertex.Y && ((ushort)(vertex.Y << 7) >> 7) == vertex.Y) { command.YFlags = CommandByte.ByteReadingType.Read1ByteShift1; bytes.Add((byte)((vertex.Y >> 1) & 0xff)); } else { command.YFlags = CommandByte.ByteReadingType.Read2Bytes; bytes.Add((byte)((vertex.Y >> 8) & 0xff)); bytes.Add((byte)(vertex.Y & 0xff)); } if (!command.UseDefaultZ) { //Determine z flags if (((ushort)(vertex.Z << 8) >> 8) == vertex.Z) { command.ZFlags = CommandByte.ByteReadingType.Read1ByteNoShift; bytes.Add((byte)(vertex.Z & 0xff)); } else if ((vertex.Z & 0x3fc0) == vertex.Z && ((ushort)(vertex.Z << 2) >> 2) == vertex.Z) { command.ZFlags = CommandByte.ByteReadingType.Read1ByteShift2; bytes.Add((byte)((vertex.Z >> 6) & 0xff)); } else if ((vertex.Z & 0x1fe0) == vertex.Z && ((ushort)(vertex.Z << 3) >> 3) == vertex.Z) { command.ZFlags = CommandByte.ByteReadingType.Read1ByteShift1; bytes.Add((byte)((vertex.Z >> 5) & 0xff)); } else { command.ZFlags = CommandByte.ByteReadingType.Read2Bytes; bytes.Add((byte)((vertex.Z >> 8) & 0xff)); bytes.Add((byte)(vertex.Z & 0xff)); } } bytes.Insert(0, command.GetAsByte()); output.AddRange(bytes); return; }
private byte[] ReadIoCommand(CommandByte commandRead, int numbOfDataToRead) { return(_i2cInterface.WriteRead(_i2cConnectionSettingIo, Convert.ToUInt32(numbOfDataToRead), new byte[] { Convert.ToByte(commandRead) })); }
/// <summary> /// Initializes a new instance of the <see cref="MonoBrick.NXT.Command"/> class using an array of bytes /// </summary> /// <param name='data'> /// The data to be used for the command /// </param> public Command(byte [] data){ if(data.Length < 2){ throw new System.ArgumentException("Invalid NXT Command"); } for(int i = 0; i < data.Length; i++) dataArr.Add(data[i]); try{ commandType = (CommandType) (data[0] & 0x7f); commandByte = (CommandByte) data[1]; } catch(Exception){ throw new System.ArgumentException("Invalid NXT Command"); } replyRequired = !Convert.ToBoolean(data[0]&0x80); }
/// <summary> /// Initializes a new instance of the <see cref="MonoBrick.NXT.Reply"/> class without payload with an error code /// </summary> /// <param name='type'> /// The command type. Can be a system command, direct command or reply command /// </param> /// <param name='command'> /// The command byte /// </param> /// <param name='errorCode'> /// Error code /// </param> public Reply(CommandType type, CommandByte command, byte errorCode) : this(type, command, null, errorCode) { }