/// <summary> /// Write and read an array of bytes to the sensor /// </summary> /// <returns>The bytes that was read</returns> /// <param name="register">Register to write to.</param> /// <param name="data">Byte array to write</param> /// <param name="rxLength">Length of the expected reply</param> protected byte[] WriteAndRead(byte register, byte[] data, int rxLength) { if (rxLength > BufferSize) { throw new ArgumentOutOfRangeException("I2C Receive Buffer only holds " + BufferSize + " bytes"); } if (data.Length > BufferSize) { throw new ArgumentOutOfRangeException("I2C Write Buffer only holds " + BufferSize + " bytes"); } bool dataReady = false; int replyIndex = 0; byte[] writeData = new byte[BufferSize]; //30 Array.Copy(data, 0, writeData, 0, data.Length); ByteArrayCreator command = new ByteArrayCreator(); command.Append((int)-1); command.Append((byte)this.port); command.Append((byte)1); //repeat command.Append((short)0); //time command.Append((byte)(data.Length + 2)); //length of write data command.Append((byte)((byte)I2CAddress >> 1)); command.Append(register); command.Append(writeData); command.Append((byte)-rxLength); replyIndex = command.Data.Length; command.Append(new byte[BufferSize]); //make room for reply byte[] i2cData = command.Data; while (!dataReady) { unchecked { I2CDevice.IoCtl((Int32)I2CIOSetup, i2cData); } int status = BitConverter.ToInt32(i2cData, 0); if (status < 0) { throw new Exception("I2C I/O error"); } if (status == 0) { byte[] reply = new byte[rxLength]; if (rxLength > 0) { Array.Copy(i2cData, replyIndex, reply, 0, rxLength); } return(reply); } } throw new TimeoutException("I2C timeout"); }
static bool Shutdown(Lcd lcd, Buttons btns) { var dialog = new QuestionDialog(Font.MediumFont, lcd, btns, "Are you sure?", "Shutdown EV3"); dialog.Show(); if (dialog.IsPositiveSelected) { lcd.Clear(); lcd.WriteText(font, new Point(0, 0), "Shutting down...", true); lcd.Update(); using (UnixDevice dev = new UnixDevice("/dev/lms_power")) { dev.IoCtl(0, new byte[0]); } btns.LedPattern(2); MenuAction = () => ProcessHelper.RunAndWaitForProcess("/lejos/bin/exit"); return(true); } return(false); }
/// <summary> /// Gets the sensor info based on the mode /// </summary> /// <returns>Raw Sensor info data</returns> protected byte[] GetSensorInfo() { ByteArrayCreator command = new ByteArrayCreator(); command.Append(new Byte[SensorInfoLength]); /*command.Append(new Byte()); * command.Append(new Byte()); * command.Append(new Byte()); * command.Append(new Byte()); * command.Append(new Byte()); * command.Append(new Byte()); * command.Append(new Byte()); * command.Append(new Byte()); * command.Append(new Single()); * command.Append(new Single()); * command.Append(new Single()); * command.Append(new Single()); * command.Append(new Single()); * command.Append(new Single()); * * * command.Append(new Int16()); * command.Append(new Int16()); * command.Append(new Byte()); * command.Append (new Byte[5]); * command.Append(new Int16());*/ command.Append((byte)this.port); command.Append((byte)this.uartMode); byte[] uartData = command.Data; unchecked { UartDevice.IoCtl((Int32)UartIOReadModeInfo, uartData); } return(uartData); }