/// <summary> /// LEGO Read Sensor Type /// </summary> /// <param name="port"></param> public I2CReadSensorType(NxtSensorPort port) { TXData = new byte[] { NxtCommon.DefaultI2CBusAddress, 0x08 }; ExpectedI2CResponseSize = 16; Port = port; base.RequireResponse = true; }
/// <summary> /// HiTechnic Read Compass Sensor Data /// </summary> /// <param name="port"></param> /// <param name="ultraSonicVariable"></param> public I2CReadSonarSensor(NxtSensorPort port, UltraSonicPacket ultraSonicVariable) : base() { TXData = new byte[] { NxtCommon.DefaultI2CBusAddress, (byte)ultraSonicVariable }; ExpectedI2CResponseSize = 1; Port = port; }
/// <summary> /// LegoLSWrite /// </summary> /// <param name="port"></param> /// <param name="txData"></param> /// <param name="rxDataLength"></param> public LegoLSWrite(NxtSensorPort port, byte[] txData, byte rxDataLength) : base(3, LegoCommand.NxtDirectCommand, (byte)LegoCommandCode.LSWrite, 0x00, 0x00, 0x00) { this.Port = port; this.TXData = txData; this.ExpectedI2CResponseSize = rxDataLength; }
/// <summary> /// MindSensors Read Compass Sensor Data /// </summary> /// <param name="port"></param> public I2CInitializeMindSensorsCompass(NxtSensorPort port) : base() { TXData = new byte[] { NxtCommon.DefaultI2CBusAddress, 0x41, 0x49 }; ExpectedI2CResponseSize = 0; Port = port; }
/// <summary> /// MindSensors Read Accelerometer Sensor Data /// </summary> /// <param name="port"></param> public I2CReadMindSensorsAccelerationSensor(NxtSensorPort port) : base() { TXData = new byte[] { NxtCommon.DefaultI2CBusAddress, 0x42 }; ExpectedI2CResponseSize = 9; Port = port; }
/// <summary> /// <para>GETINPUTVALUES</para> /// </summary> /// <remarks> /// <para>Reference: BDK, Appendix 2, p. 8.</para> /// <para>Used with passive sensors like the Touch, Light and Sound sensors.</para> /// </remarks> /// <param name="sensorPort">Input Port</param> /// <returns>Returns a NxtGetInputValues with the parsed reply</returns> public NxtGetInputValuesReply?GetInputValues(NxtSensorPort sensorPort) { byte[] request = new byte[] { 0x00, (byte)NxtCommand.GetInputValues, (byte)sensorPort }; byte[] reply = Send(request); if (reply == null) { return(null); } byte sensorPortOut = reply[3]; if (sensorPortOut != (byte)sensorPort) { throw new NxtException(string.Format("Output sensor port, {0}, was different from input sensor port, {1}.", sensorPortOut, sensorPort)); } NxtGetInputValuesReply result; result.valid = (reply[4] != 0x00); result.calibrated = (reply[5] != 0x00); result.sensorType = (NxtSensorType)reply[6]; result.sensorMode = (NxtSensorMode)reply[7]; result.rawAdValue = Util.GetUInt16(reply, 8); result.normalizedAdValue = Util.GetUInt16(reply, 10); result.scaledValue = Util.GetInt16(reply, 12); result.calibratedValue = Util.GetInt16(reply, 14); return(result); }
/// <summary> /// <para>LSWRITE</para> /// </summary> /// <remarks> /// <para>Reference: BDK, Appendix 2, p. 10.</para> /// <para>Writes data to the LowSpeed port. Used with digital sensors like the Ultrasonic sensor.</para> /// </remarks> /// <param name="sensorPort">Sensor port</param> /// <param name="txData">Tx Data</param> /// <param name="rxDataLength">Rx Data Length</param> /// <seealso cref="LsRead"/> /// <seealso cref="LsGetStatus"/> public void LsWrite(NxtSensorPort sensorPort, byte[] txData, byte rxDataLength) { byte txDataLength = (byte)txData.Length; if (txDataLength == 0) { throw new ArgumentException("No data to send."); } if (txDataLength > 16) { throw new ArgumentException("Tx data may not exceed 16 bytes."); } if (rxDataLength < 0 || 16 < rxDataLength) { throw new ArgumentException("Rx data length should be in the interval 0-16."); } byte[] request = new byte[5 + txDataLength]; request[0] = (byte)(ReplyRequired ? 0x00 : 0x80); request[1] = (byte)NxtCommand.LsWrite; request[2] = (byte)sensorPort; request[3] = txDataLength; request[4] = rxDataLength; txData.CopyTo(request, 5); Send(request); }
/// <summary> /// Post Configure Sensor Connection with body and return the response port. /// </summary> /// <param name="sensorPort"></param> /// <returns></returns> public virtual PortSet <DefaultUpdateResponseType, Fault> ConnectToBrick(NxtSensorPort sensorPort) { ConnectToBrick op = new ConnectToBrick(sensorPort); this.PostUnknownType(op); return(op.ResponsePort); }
/// <summary> /// HiTechnic Read Compass Sensor Data /// </summary> /// <param name="port"></param> public I2CReadHiTechnicCompassSensor(NxtSensorPort port) : base() { TXData = new byte[] { NxtCommon.DefaultI2CBusAddress, 0x41 }; this.ExpectedI2CResponseSize = 5; Port = port; }
/// <summary> /// Send an I2C command to the specified address /// </summary> /// <param name="port"></param> /// <param name="address"></param> /// <param name="i2CBusAddress"></param> public I2CSendCommand(NxtSensorPort port, int address, int i2CBusAddress) : base() { TXData = new byte[] { (byte)i2CBusAddress, (byte)address }; ExpectedI2CResponseSize = 0; Port = port; RequireResponse = true; }
/// <summary> /// <para>Attaches a sensor to the NXT brick.</para> /// </summary> /// <param name="sensor">The sensor</param> /// <param name="port">The port to attach the sensor to</param> private void AttachSensor(NxtSensor sensor, NxtSensorPort port) { if (sensor != null) { sensor.Brick = this; sensor.Port = port; sensorArr[port] = sensor; } }
/// <summary> /// <para>RESETINPUTSCALEDVALUE</para> /// </summary> /// <remarks> /// <para>Reference: BDK, Appendix 2, p. 8.</para> /// </remarks> /// <param name="sensorPort">Input Port</param> public void ResetInputScaledValue(NxtSensorPort sensorPort) { byte[] request = new byte[] { (byte)(ReplyRequired ? 0x00 : 0x80), (byte)NxtCommand.ResetInputScaledValue, (byte)sensorPort }; Send(request); }
/// <summary> /// <para>SETINPUTMODE</para> /// </summary> /// <remarks> /// <para>Reference: BDK, Appendix 2, p. 7.</para> /// </remarks> /// <param name="sensorPort">Input Port</param> /// <param name="sensorType">Sensor Type</param> /// <param name="sensorMode">Sensor Mode</param> public void SetInputMode(NxtSensorPort sensorPort, NxtSensorType sensorType, NxtSensorMode sensorMode) { byte[] request = new byte[] { (byte)(ReplyRequired ? 0x00 : 0x80), (byte)NxtCommand.SetInputMode, (byte)sensorPort, (byte)sensorType, (byte)sensorMode }; Send(request); }
/// <summary> /// <para>LSGETSTATUS</para> /// </summary> /// <remarks> /// <para>Reference: BDK, Appendix 2, p. 10.</para> /// <para>Returns the number of bytes ready in the LowSpeed port. Used with digital sensors like the Ultrasonic sensor.</para> /// </remarks> /// <param name="sensorPort">Sensor port</param> /// <returns>Bytes Ready (count of available bytes to read)</returns> /// <seealso cref="LsRead"/> /// <seealso cref="LsWrite"/> public byte?LsGetStatus(NxtSensorPort sensorPort) { byte[] request = new byte[] { 0x00, (byte)NxtCommand.LsGetStatus, (byte)sensorPort }; byte[] reply = Send(request); if (reply == null) { return(null); } byte bytesReady = reply[3]; return(bytesReady); }
/// <summary> /// <para>LSREAD</para> /// </summary> /// <remarks> /// <para>Reference: BDK, Appendix 2, p. 10.</para> /// <para>Reads data from the LowSpeed port. Used with digital sensors like the Ultrasonic sensor.</para> /// </remarks> /// <param name="sensorPort">The sensor port</param> /// <returns>The data read from the port</returns> /// <seealso cref="LsGetStatus"/> /// <seealso cref="LsWrite"/> public byte[] LsRead(NxtSensorPort sensorPort) { byte[] request = new byte[] { 0x00, (byte)NxtCommand.LsRead, (byte)sensorPort }; byte[] reply = Send(request); if (reply == null) { return(null); } byte bytesRead = reply[3]; byte[] rxData = new byte[bytesRead]; Array.Copy(reply, 4, rxData, 0, bytesRead); return(rxData); }
/// <summary> /// MindSensors Accelerometer Sensor Configuration. /// </summary> /// <param name="sensorPort"></param> public AccelerometerConfig(NxtSensorPort sensorPort) { this.SensorPort = sensorPort; }
/// <summary> /// Configure Device Connection /// </summary> /// <param name="sensorPort"></param> public ConnectToBrick(NxtSensorPort sensorPort) : base(new AccelerometerConfig(sensorPort)) { }
/// <summary> /// Get the HardwareIdentifier representation of a NxtSensorPort (1-4). /// </summary> /// <param name="port"></param> /// <returns></returns> public static int HardwareIdentifier(NxtSensorPort port) { return ((int)(PortNumber(port) + 1)); }
/// <summary> /// Get the integer representation of a NxtSensorPort (zero based). /// </summary> /// <param name="port"></param> /// <returns></returns> public static byte PortNumber(NxtSensorPort port) { switch (port) { case NxtSensorPort.Sensor1: return 0; case NxtSensorPort.Sensor2: return 1; case NxtSensorPort.Sensor3: return 2; case NxtSensorPort.Sensor4: return 3; default: return 0; } }
/// <summary> /// LEGO NXT Command: Set Input Mode /// </summary> /// <param name="sensorPort"></param> /// <param name="sensorType"></param> /// <param name="sensorMode"></param> public LegoSetInputMode(NxtSensorPort sensorPort, LegoSensorType sensorType, LegoSensorMode sensorMode) : base(3, LegoCommand.NxtDirectCommand, (byte)LegoCommandCode.SetInputMode, 0x00, 0x00, 0x00) { InputPort = sensorPort; SensorType = sensorType; SensorMode = sensorMode; }
/// <summary> /// LEGO NXT Color Sensor Configuration. /// </summary> /// <param name="sensorPort"></param> public ColorSensorConfig(NxtSensorPort sensorPort) { this.SensorPort = sensorPort; }
/// <summary> /// <para>LSGETSTATUS</para> /// </summary> /// <remarks> /// <para>Reference: BDK, Appendix 2, p. 10.</para> /// <para>Returns the number of bytes ready in the LowSpeed port. Used with digital sensors like the Ultrasonic sensor.</para> /// </remarks> /// <param name="sensorPort">Sensor port</param> /// <returns>Bytes Ready (count of available bytes to read)</returns> /// <seealso cref="LsRead"/> /// <seealso cref="LsWrite"/> public byte? LsGetStatus(NxtSensorPort sensorPort) { byte[] request = new byte[] { 0x00, (byte) NxtCommand.LsGetStatus, (byte) sensorPort }; byte[] reply = Send(request); if (reply == null) return null; byte bytesReady = reply[3]; return bytesReady; }
/// <summary> /// <para>SETINPUTMODE</para> /// </summary> /// <remarks> /// <para>Reference: BDK, Appendix 2, p. 7.</para> /// </remarks> /// <param name="sensorPort">Input Port</param> /// <param name="sensorType">Sensor Type</param> /// <param name="sensorMode">Sensor Mode</param> public void SetInputMode(NxtSensorPort sensorPort, NxtSensorType sensorType, NxtSensorMode sensorMode) { byte[] request = new byte[] { (byte) (ReplyRequired ? 0x00 : 0x80), (byte) NxtCommand.SetInputMode, (byte) sensorPort, (byte) sensorType, (byte) sensorMode }; Send(request); }
/// <summary> /// <para>GETINPUTVALUES</para> /// </summary> /// <remarks> /// <para>Reference: BDK, Appendix 2, p. 8.</para> /// <para>Used with passive sensors like the Touch, Light and Sound sensors.</para> /// </remarks> /// <param name="sensorPort">Input Port</param> /// <returns>Returns a NxtGetInputValues with the parsed reply</returns> public NxtGetInputValuesReply? GetInputValues(NxtSensorPort sensorPort) { byte[] request = new byte[] { 0x00, (byte) NxtCommand.GetInputValues, (byte) sensorPort }; byte[] reply = Send(request); if (reply == null) return null; byte sensorPortOut = reply[3]; if (sensorPortOut != (byte)sensorPort) throw new NxtException(string.Format("Output sensor port, {0}, was different from input sensor port, {1}.", sensorPortOut, sensorPort)); NxtGetInputValuesReply result; result.valid = (reply[4] != 0x00); result.calibrated = (reply[5] != 0x00); result.sensorType = (NxtSensorType)reply[6]; result.sensorMode = (NxtSensorMode)reply[7]; result.rawAdValue = Util.GetUInt16(reply, 8); result.normalizedAdValue = Util.GetUInt16(reply, 10); result.scaledValue = Util.GetInt16(reply, 12); result.calibratedValue = Util.GetInt16(reply, 14); return result; }
/// <summary> /// LEGO NXT Touch Sensor Configuration. /// </summary> /// <param name="sensorPort"></param> public TouchSensorConfig(NxtSensorPort sensorPort) { this.SensorPort = sensorPort; }
/// <summary> /// Configure Device Connection /// </summary> /// <param name="sensorPort"></param> public ConnectToBrick(NxtSensorPort sensorPort) : base(new LightSensorConfig(sensorPort)) { }
/// <summary> /// Configure Device Connection /// </summary> /// <param name="sensorPort"></param> public ConnectToBrick(NxtSensorPort sensorPort) : base(new CompassConfig(sensorPort)) { }
/// <summary> /// LEGO NXT Low Speed (I2C) Read /// </summary> /// <param name="port"></param> public LegoLSRead(NxtSensorPort port) : base(20, LegoCommand.NxtDirectCommand, (byte)LegoCommandCode.LSRead, 0x00) { base.RequireResponse = true; Port = port; }
/// <summary> /// <para>LSREAD</para> /// </summary> /// <remarks> /// <para>Reference: BDK, Appendix 2, p. 10.</para> /// <para>Reads data from the LowSpeed port. Used with digital sensors like the Ultrasonic sensor.</para> /// </remarks> /// <param name="sensorPort">The sensor port</param> /// <returns>The data read from the port</returns> /// <seealso cref="LsGetStatus"/> /// <seealso cref="LsWrite"/> public byte[] LsRead(NxtSensorPort sensorPort) { byte[] request = new byte[] { 0x00, (byte) NxtCommand.LsRead, (byte) sensorPort }; byte[] reply = Send(request); if (reply == null) return null; byte bytesRead = reply[3]; byte[] rxData = new byte[bytesRead]; Array.Copy(reply, 4, rxData, 0, bytesRead); return rxData; }
/// <summary> /// LEGO NXT Sonar Sensor Configuration. /// </summary> /// <param name="sensorPort"></param> public SonarSensorConfig(NxtSensorPort sensorPort) { this.SensorPort = sensorPort; }
/// <summary> /// <para>LSWRITE</para> /// </summary> /// <remarks> /// <para>Reference: BDK, Appendix 2, p. 10.</para> /// <para>Writes data to the LowSpeed port. Used with digital sensors like the Ultrasonic sensor.</para> /// </remarks> /// <param name="sensorPort">Sensor port</param> /// <param name="txData">Tx Data</param> /// <param name="rxDataLength">Rx Data Length</param> /// <seealso cref="LsRead"/> /// <seealso cref="LsGetStatus"/> public void LsWrite(NxtSensorPort sensorPort, byte[] txData, byte rxDataLength) { byte txDataLength = (byte)txData.Length; if (txDataLength == 0) throw new ArgumentException("No data to send."); if (txDataLength > 16) throw new ArgumentException("Tx data may not exceed 16 bytes."); if (rxDataLength < 0 || 16 < rxDataLength) throw new ArgumentException("Rx data length should be in the interval 0-16."); byte[] request = new byte[5 + txDataLength]; request[0] = (byte)(ReplyRequired ? 0x00 : 0x80); request[1] = (byte)NxtCommand.LsWrite; request[2] = (byte)sensorPort; request[3] = txDataLength; request[4] = rxDataLength; txData.CopyTo(request, 5); Send(request); }
/// <summary> /// LEGO NXT Command: GetInputValues /// </summary> /// <param name="inputPort"></param> public LegoGetInputValues(NxtSensorPort inputPort) : base(16, LegoCommand.NxtDirectCommand, (byte)LegoCommandCode.GetInputValues, 0x00) { base.RequireResponse = true; InputPort = inputPort; }
/// <summary> /// LEGO NXT Light Sensor Configuration. /// </summary> /// <param name="sensorPort"></param> public LightSensorConfig(NxtSensorPort sensorPort) { this.SensorPort = sensorPort; }
/// <summary> /// HiTechnic Compass Sensor Configuration. /// </summary> /// <param name="sensorPort"></param> public CompassConfig(NxtSensorPort sensorPort) { this.SensorPort = sensorPort; }
/// <summary> /// LEGO NXT Command: Low Speed (I2C) Get Status /// </summary> /// <param name="port"></param> public LegoLSGetStatus(NxtSensorPort port) : base(4, LegoCommand.NxtDirectCommand, (byte)LegoCommandCode.LSGetStatus, 0x00) { base.RequireResponse = true; Port = port; }
/// <summary> /// <para>RESETINPUTSCALEDVALUE</para> /// </summary> /// <remarks> /// <para>Reference: BDK, Appendix 2, p. 8.</para> /// </remarks> /// <param name="sensorPort">Input Port</param> public void ResetInputScaledValue(NxtSensorPort sensorPort) { byte[] request = new byte[] { (byte) (ReplyRequired ? 0x00 : 0x80), (byte) NxtCommand.ResetInputScaledValue, (byte) sensorPort }; Send(request); }
/// <summary> /// LEGO Command: ResetInputScaledValue. /// </summary> /// <param name="inputPort"></param> public LegoResetInputScaledValue(NxtSensorPort inputPort) : base(LegoCommand.NxtDirectCommand, (byte)LegoCommandCode.ResetInputScaledValue, 0x00) { this.InputPort = inputPort; }