Beispiel #1
0
        /// <summary>
        /// Turn on the IR sensor
        /// </summary>
        /// <param name="mode">The data report mode</param>
        private void EnableIR(IRMode mode)
        {
            mWiimoteState.IRState.Mode = mode;

            ClearReport();
            mBuff[0] = (byte)OutputReport.IR;
            mBuff[1] = (byte)(0x04 | GetRumbleBit());
            WriteReport();

            ClearReport();
            mBuff[0] = (byte)OutputReport.IR2;
            mBuff[1] = (byte)(0x04 | GetRumbleBit());
            WriteReport();

            WriteData(REGISTER_IR, 0x08);
            //default
//			WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90, 0x00, 0xc0});
//			WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] {0x40, 0x00});

            //super sensitive according to http://wiibrew.org/index.php?title=Wiimote#Sensitivity_Settings
            WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90, 0x00, 0x41 });
            WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] { 0x40, 0x00 });

            WriteData(REGISTER_IR_MODE, (byte)mode);
        }
Beispiel #2
0
        /// <summary>
        /// Gets the beacon location. This will change the mode to seek
        /// </summary>
        /// <returns>The beacon location.</returns>
        public int ReadBeaconLocation()
        {
            var oldmode = Mode;

            if (Mode != IRMode.Seek)
            {
                Mode = IRMode.Seek;
            }
            //byte[] data = mUartSensor.ReadBytes(4 * 2);
            //return new BeaconLocation((sbyte)data[(int)Channel * 2], (sbyte)data[((int)Channel * 2) + 1]);
            //return brick.BrickPi.Sensor[(int)Port].Value;
            try
            {
                var ret = brick.get_sensor((byte)Port);
                if (Mode != oldmode)
                {
                    Mode = oldmode;
                }
                return(ret[(int)(Channel) * 2] + ret[(int)(Channel) * 2 + 1] >> 8);
            }
            catch (Exception)
            {
                if (Mode != oldmode)
                {
                    Mode = oldmode;
                }
                return(int.MaxValue);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Gets the beacon location. This will change the mode to seek
        /// </summary>
        /// <returns>The beacon location.</returns>
        public int ReadBeaconLocation()
        {
            var oldmode = Mode;

            if (Mode != IRMode.Seek)
            {
                Mode = IRMode.Seek;
            }
            try
            {
                var ret = _brick.GetSensor((byte)Port);
                if (Mode != oldmode)
                {
                    Mode = oldmode;
                }
                return(ret[(int)(Channel) * 2] + ret[(int)(Channel) * 2 + 1] >> 8);
            }
            catch (Exception ex) when(ex is IOException)
            {
                if (Mode != oldmode)
                {
                    Mode = oldmode;
                }
                return(int.MaxValue);
            }
        }
Beispiel #4
0
 /// <summary>
 /// Read the distance of the sensor in CM (0-100). This will change mode to proximity
 /// </summary>
 public int ReadDistance()
 {
     if (_mode != IRMode.Proximity)
     {
         Mode = IRMode.Proximity;
     }
     return(ReadRaw());
 }
Beispiel #5
0
 /// <summary>
 /// Reads commands from the IR-Remote. This will change mode to remote
 /// </summary>
 /// <param name="channel">Channel.</param>
 /// <returns>The remote command.</returns>
 public byte ReadRemoteCommand(IRChannel channel)
 {
     if (Mode != IRMode.Remote)
     {
         Mode = IRMode.Remote;
     }
     return(ReadBytes(4)[(int)channel]);
 }
Beispiel #6
0
 /// <summary>
 /// Read the distance of the sensor in CM (0-100). This will change mode to proximity
 /// </summary>
 public int ReadDistance()
 {
     if (mode != IRMode.Proximity)
     {
         Mode = IRMode.Proximity;
     }
     return(brick.BrickPi.Sensor[(int)Port].Value);
 }
Beispiel #7
0
 /// <summary>
 /// Reads commands from the IR-Remote. This will change mode to remote
 /// </summary>
 /// <returns>The remote command.</returns>
 public byte ReadRemoteCommand()
 {
     if (Mode != IRMode.Remote)
     {
         Mode = IRMode.Remote;
     }
     return((byte)((brick.BrickPi.Sensor[(int)Port].Value >> (int)Channel) & 0x0F));
 }
Beispiel #8
0
 /// <summary>
 /// Read the distance of the sensor in CM (0-100). This will change mode to proximity
 /// </summary>
 public int ReadDistance()
 {
     if (Mode != IRMode.Proximity)
     {
         Mode = IRMode.Proximity;
     }
     return(ReadByte());
 }
Beispiel #9
0
 /// <summary>
 /// Initialize an EV3 IR Sensor
 /// </summary>
 /// <param name="port">Sensor port</param>
 /// <param name="mode">IR mode</param>
 /// <param name="timeout">Period in millisecond to check sensor value changes</param>
 public EV3IRSensor(BrickPortSensor port, IRMode mode, int timeout)
 {
     brick   = new Brick();
     Mode    = mode;
     Channel = IRChannel.One;
     brick.BrickPi.Sensor[(int)Port].Type = (BrickSensorType)mode;
     periodRefresh = timeout;
     timer         = new Timer(UpdateSensor, this, TimeSpan.FromMilliseconds(timeout), TimeSpan.FromMilliseconds(timeout));
 }
Beispiel #10
0
 /// <summary>
 /// Gets the beacon location. This will change the mode to seek
 /// </summary>
 /// <returns>The beacon location.</returns>
 public BeaconLocation ReadBeaconLocation()
 {
     if (Mode != IRMode.Seek)
     {
         Mode = IRMode.Seek;
     }
     byte[] data = ReadBytes(4 * 2);
     return(new BeaconLocation((sbyte)data[(int)Channel * 2], (sbyte)data[((int)Channel * 2) + 1]));
 }
Beispiel #11
0
 /// <summary>
 /// Read the distance of the sensor in CM (0-100). This will change mode to proximity
 /// </summary>
 public int ReadDistance()
 {
     if (mode != IRMode.Proximity)
     {
         Mode = IRMode.Proximity;
     }
     //return brick.BrickPi.Sensor[(int)Port].Value;
     return(ReadRaw());
 }
Beispiel #12
0
 /// <summary>
 /// Initialize an EV3 IR Sensor
 /// </summary>
 /// <param name="brick">Interface to main Brick component</param>
 /// <param name="port">Sensor port</param>
 /// <param name="mode">IR mode</param>
 /// <param name="timeout">Period in millisecond to check sensor value changes</param>
 public EV3InfraredSensor(Brick brick, SensorPort port, IRMode mode, int timeout)
 {
     _brick  = brick;
     Mode    = mode;
     Port    = port;
     Channel = IRChannel.One;
     brick.SetSensorType((byte)Port, (SensorType)mode);
     _periodRefresh = timeout;
     _timer         = new Timer(UpdateSensor, this, TimeSpan.FromMilliseconds(timeout), TimeSpan.FromMilliseconds(timeout));
 }
Beispiel #13
0
 /// <summary>
 /// Gets the beacon location. This will change the mode to seek
 /// </summary>
 /// <returns>The beacon location.</returns>
 public int ReadBeaconLocation()
 {
     if (Mode != IRMode.Seek)
     {
         Mode = IRMode.Seek;
     }
     //byte[] data = mUartSensor.ReadBytes(4 * 2);
     //return new BeaconLocation((sbyte)data[(int)Channel * 2], (sbyte)data[((int)Channel * 2) + 1]);
     return(brick.BrickPi.Sensor[(int)Port].Value);
 }
Beispiel #14
0
 /// <summary>
 /// Initialize an EV3 IR Sensor
 /// </summary>
 /// <param name="port">Sensor port</param>
 /// <param name="mode">IR mode</param>
 /// <param name="timeout">Period in millisecond to check sensor value changes</param>
 public EV3IRSensor(Brick brick, BrickPortSensor port, IRMode mode, int timeout)
 {
     this.brick = brick;
     Mode       = mode;
     Port       = port;
     Channel    = IRChannel.One;
     //brick.BrickPi.Sensor[(int)Port].Type = (BrickSensorType)mode;
     brick.set_sensor_type((byte)Port, (SENSOR_TYPE)mode);
     periodRefresh = timeout;
     timer         = new Timer(UpdateSensor, this, TimeSpan.FromMilliseconds(timeout), TimeSpan.FromMilliseconds(timeout));
 }
        /// <summary>Turn on the IR sensor.</summary>
        /// <param name="mode">The data report mode</param>
        /// <param name="irSensitivity">IR sensitivity</param>
        private void EnableIR(IRMode mode, IRSensitivity irSensitivity)
        {
            wiimoteState.Status.IREnabled    = true;
            wiimoteState.IRState.Mode        = mode;
            wiimoteState.IRState.Sensitivity = irSensitivity;

            byte[] buff = CreateReport(OutputReport.IRPixelClock);
            buff[1] = 0x04;
            WriteReport(buff);

            buff    = CreateReport(OutputReport.IRLogic);
            buff[1] = 0x04;
            WriteReport(buff);

            WriteByte(Registers.IR, 0x08);
            switch (irSensitivity)
            {
            case IRSensitivity.WiiLevel1:
                WriteData(Registers.IRSensitivity1, 9, new byte[] { 0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0x64, 0x00, 0xfe });
                WriteData(Registers.IRSensitivity2, 2, new byte[] { 0xfd, 0x05 });
                break;

            case IRSensitivity.WiiLevel2:
                WriteData(Registers.IRSensitivity1, 9, new byte[] { 0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0x96, 0x00, 0xb4 });
                WriteData(Registers.IRSensitivity2, 2, new byte[] { 0xb3, 0x04 });
                break;

            case IRSensitivity.WiiLevel3:
                WriteData(Registers.IRSensitivity1, 9, new byte[] { 0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0xaa, 0x00, 0x64 });
                WriteData(Registers.IRSensitivity2, 2, new byte[] { 0x63, 0x03 });
                break;

            case IRSensitivity.WiiLevel4:
                WriteData(Registers.IRSensitivity1, 9, new byte[] { 0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0xc8, 0x00, 0x36 });
                WriteData(Registers.IRSensitivity2, 2, new byte[] { 0x35, 0x03 });
                break;

            case IRSensitivity.WiiLevel5:
                WriteData(Registers.IRSensitivity1, 9, new byte[] { 0x07, 0x00, 0x00, 0x71, 0x01, 0x00, 0x72, 0x00, 0x20 });
                WriteData(Registers.IRSensitivity2, 2, new byte[] { 0x1, 0x03 });
                break;

            case IRSensitivity.Maximum:
                WriteData(Registers.IRSensitivity1, 9, new byte[] { 0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0x90, 0x00, 0x41 });
                WriteData(Registers.IRSensitivity2, 2, new byte[] { 0x40, 0x00 });
                break;

            default:
                throw new ArgumentOutOfRangeException("irSensitivity");
            }
            WriteByte(Registers.IRMode, (byte)mode);
            WriteByte(Registers.IR, 0x08);
        }
Beispiel #16
0
        /// <summary>
        /// Turn on the IR sensor
        /// </summary>
        /// <param name="mode">The data report mode</param>
        /// <param name="irSensitivity">IR sensitivity</param>
        private void EnableIR(IRMode mode, IRSensitivity irSensitivity)
        {
            mWiimoteState.IRState.Mode = mode;

            ClearReport();
            mBuff[0] = (byte)OutputReport.IR;
            mBuff[1] = (byte)(0x04 | GetRumbleBit());
            WriteReport();

            ClearReport();
            mBuff[0] = (byte)OutputReport.IR2;
            mBuff[1] = (byte)(0x04 | GetRumbleBit());
            WriteReport();

            WriteData(REGISTER_IR, 0x08);
            switch (irSensitivity)
            {
            case IRSensitivity.WiiLevel1:
                WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] { 0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0x64, 0x00, 0xfe });
                WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] { 0xfd, 0x05 });
                break;

            case IRSensitivity.WiiLevel2:
                WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] { 0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0x96, 0x00, 0xb4 });
                WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] { 0xb3, 0x04 });
                break;

            case IRSensitivity.WiiLevel3:
                WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] { 0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0xaa, 0x00, 0x64 });
                WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] { 0x63, 0x03 });
                break;

            case IRSensitivity.WiiLevel4:
                WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] { 0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0xc8, 0x00, 0x36 });
                WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] { 0x35, 0x03 });
                break;

            case IRSensitivity.WiiLevel5:
                WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] { 0x07, 0x00, 0x00, 0x71, 0x01, 0x00, 0x72, 0x00, 0x20 });
                WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] { 0x1, 0x03 });
                break;

            case IRSensitivity.Maximum:
                WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] { 0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0x90, 0x00, 0x41 });
                WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] { 0x40, 0x00 });
                break;

            default:
                throw new ArgumentOutOfRangeException("irSensitivity");
            }
            WriteData(REGISTER_IR_MODE, (byte)mode);
            WriteData(REGISTER_IR, 0x08);
        }
Beispiel #17
0
 /// <summary>
 /// Reads commands from the IR-Remote. This will change mode to remote
 /// </summary>
 /// <returns>The remote command.</returns>
 public byte ReadRemoteCommand()
 {
     if (Mode != IRMode.Remote)
     {
         Mode = IRMode.Remote;
     }
     try
     {
         var ret = _brick.GetSensor((byte)Port);
         return(ret[(int)Channel]);
     }
     catch (Exception ex) when(ex is IOException)
     {
         return(byte.MaxValue);
     }
 }
Beispiel #18
0
 /// <summary>
 /// Reads commands from the IR-Remote. This will change mode to remote
 /// </summary>
 /// <returns>The remote command.</returns>
 public byte ReadRemoteCommand()
 {
     if (Mode != IRMode.Remote)
     {
         Mode = IRMode.Remote;
     }
     //return (byte)((brick.BrickPi.Sensor[(int)Port].Value >> (int)Channel) & 0x0F);
     try
     {
         var ret = brick.get_sensor((byte)Port);
         return(ret[(int)Channel]);
     }
     catch (Exception)
     {
         return(byte.MaxValue);
     }
 }
Beispiel #19
0
        /// <summary>
        /// Turn on the IR sensor
        /// </summary>
        /// <param name="mode">The data report mode</param>
        private void EnableIR(IRMode mode)
        {
            mWiimoteState.IRState.Mode = mode;

            ClearReport();
            mBuff[0] = (byte)OutputReport.IR;
            mBuff[1] = (byte)(0x04 | GetRumbleBit());
            WriteReport();

            ClearReport();
            mBuff[0] = (byte)OutputReport.IR2;
            mBuff[1] = (byte)(0x04 | GetRumbleBit());
            WriteReport();

            WriteData(REGISTER_IR, 0x08);
            WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90, 0x00, 0xc0 });
            WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] { 0x40, 0x00 });
            WriteData(REGISTER_IR_MODE, (byte)mode);
        }
Beispiel #20
0
        public MSSensorMUXBase(SensorPort Port, MSSensorMUXPort Address, IRMode SensorMode) : base(Port, (byte)Address, I2CMode.LowSpeed)
        {
            base.Initialise();
            if (SensorMode == IRMode.Proximity)
            {
                Mode = 0;
            }
            else if (SensorMode == IRMode.Seek)
            {
                Mode = 1;
            }
            else if (SensorMode == IRMode.Remote)
            {
                Mode = 2;
            }

            NumberOfM = 2;
            SetSensorMode();
        }
Beispiel #21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MonoBrick.EV3.IRSensor"/> class.
 /// </summary>
 /// <param name="mode">Mode.</param>
 public EV3IRSensor(SensorPort port, IRMode mode)
     : base(port)
 {
     base.Initialise(base.uartMode);
     Mode = mode;
     Channel = IRChannel.One;
 }
		/// <summary>
		/// Initializes a new instance of the <see cref="MonoBrick.EV3.IRSensor"/> class.
		/// </summary>
		/// <param name="mode">Mode.</param>
		public IRSensor (IRMode mode) :  base((SensorMode)mode)
		{
		
		}
Beispiel #23
0
 public void SetIRMode(IRMode irMode)
 {
     SetIRMode(irMode, IRSensitivity.Default);
 }
Beispiel #24
0
        /// <summary>
        /// Turn on the IR sensor
        /// </summary>
        /// <param name="mode">The data report mode</param>
        /// <param name="irSensitivity">IR sensitivity</param>
        private void EnableIR(IRMode mode, IRSensitivity irSensitivity)
        {
            mWiimoteState.IRState.Mode = mode;

            ClearReport();
            mBuff[0] = (byte)OutputReport.IR;
            mBuff[1] = (byte)(0x04 | GetRumbleBit());
            WriteReport();

            ClearReport();
            mBuff[0] = (byte)OutputReport.IR2;
            mBuff[1] = (byte)(0x04 | GetRumbleBit());
            WriteReport();

            WriteData(REGISTER_IR, 0x08);
            switch(irSensitivity)
            {
                case IRSensitivity.WiiLevel1:
                    WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] {0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0x64, 0x00, 0xfe});
                    WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] {0xfd, 0x05});
                    break;
                case IRSensitivity.WiiLevel2:
                    WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] {0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0x96, 0x00, 0xb4});
                    WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] {0xb3, 0x04});
                    break;
                case IRSensitivity.WiiLevel3:
                    WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] {0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0xaa, 0x00, 0x64});
                    WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] {0x63, 0x03});
                    break;
                case IRSensitivity.WiiLevel4:
                    WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] {0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0xc8, 0x00, 0x36});
                    WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] {0x35, 0x03});
                    break;
                case IRSensitivity.WiiLevel5:
                    WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] {0x07, 0x00, 0x00, 0x71, 0x01, 0x00, 0x72, 0x00, 0x20});
                    WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] {0x1, 0x03});
                    break;
                case IRSensitivity.Maximum:
                    WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] {0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0x90, 0x00, 0x41});
                    WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] {0x40, 0x00});
                    break;
                default:
                    throw new ArgumentOutOfRangeException("irSensitivity");
            }
            WriteData(REGISTER_IR_MODE, (byte)mode);
            WriteData(REGISTER_IR, 0x08);
        }
Beispiel #25
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MonoBrick.EV3.IRSensor"/> class.
 /// </summary>
 /// <param name="mode">Mode.</param>
 public EV3IRSensor(SensorPort port, IRMode mode) :  base(port)
 {
     base.Initialise(base.uartMode);
     Mode    = mode;
     Channel = IRChannel.One;
 }
Beispiel #26
0
 /// <summary>
 /// Initializes an EV3 IS Sensor
 /// </summary>
 /// <param name="brick">Interface to main Brick component</param>
 /// <param name="port">Sensor port</param>
 /// <param name="mode">IR mode</param>
 public EV3InfraredSensor(Brick brick, SensorPort port, IRMode mode)
     : this(brick, port, mode, 1000)
 {
 }
Beispiel #27
0
        /// <summary>
        /// Turn on the IR sensor
        /// </summary>
        /// <param name="mode">The data report mode</param>
        private void EnableIR(IRMode mode)
        {
            mWiimoteState.IRState.Mode = mode;

            ClearReport();
            mBuff[0] = (byte)OutputReport.IR;
            mBuff[1] = (byte)(0x04 | GetRumbleBit());
            WriteReport();

            ClearReport();
            mBuff[0] = (byte)OutputReport.IR2;
            mBuff[1] = (byte)(0x04 | GetRumbleBit());
            WriteReport();

            WriteData(REGISTER_IR, 0x08);
            WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90, 0x00, 0xc0});
            WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] {0x40, 0x00});
            WriteData(REGISTER_IR_MODE, (byte)mode);
        }
        protected override bool ParseReport(byte[] report)
        {
            if (report[0] < 0x20)
                throw new InvalidOperationException("Received an output report!");
            InputReport type = (InputReport)report[0];
            if (type >= InputReport.Buttons && type <= InputReport.ButtonsAccelerometer36IrB)
            {
                if (type == InputReport.ButtonsAccelerometer36IrB)
                    ReportingMode = ReportingMode.ButtonsAccelerometer36Ir;
                else
                    ReportingMode = (ReportingMode)type;

                IRMode irMode = GetIrMode(type);
                if (this._IrMode != irMode)
                {
                    for (int i = 0; i < _CachedIRBeacons.Length; i++)
                    {
                        BasicIRBeacon newBeacon = null;
                        switch (irMode)
                        {
                            case IRMode.Basic:
                                newBeacon = new BasicIRBeacon();
                                break;
                            case IRMode.Extended:
                                newBeacon = new ExtendedIRBeacon();
                                break;
                            case IRMode.Full:
                                newBeacon = new FullIRBeacon();
                                break;
                        }
                        _CachedIRBeacons[i] = newBeacon;
                        _IRBeacons[i] = null;
                    }
                    this._IrMode = irMode;
                }
            }
            switch (type)
            {
                case InputReport.Buttons:
                    ParseButtons(report, 1);
                    break;
                case InputReport.ButtonsAccelerometer:
                    ParseButtons(report, 1);
                    ParseAccelerometer(report, 3);
                    break;
                case InputReport.Buttons8Extension:
                    ParseButtons(report, 1);
                    ParseExtension(report, 3, 8);
                    break;
                case InputReport.ButtonsAccelerometer12Ir:
                    {
                        ParseButtons(report, 1);
                        ParseAccelerometer(report, 3);

                        ExtendedIRBeacon irBeacon;

                        // Parse beacon 0.
                        irBeacon = (ExtendedIRBeacon)_CachedIRBeacons[0];
                        _IRBeacons[0] = ParseExtendedBeacon(report, 6, irBeacon) ? irBeacon : null;

                        // Parse beacon 1.
                        irBeacon = (ExtendedIRBeacon)_CachedIRBeacons[1];
                        _IRBeacons[1] = ParseExtendedBeacon(report, 9, irBeacon) ? irBeacon : null;

                        // Parse beacon 2.
                        irBeacon = (ExtendedIRBeacon)_CachedIRBeacons[2];
                        _IRBeacons[2] = ParseExtendedBeacon(report, 12, irBeacon) ? irBeacon : null;

                        // Parse beacon 3.
                        irBeacon = (ExtendedIRBeacon)_CachedIRBeacons[3];
                        _IRBeacons[3] = ParseExtendedBeacon(report, 15, irBeacon) ? irBeacon : null;
                    }
                    break;
                case InputReport.Buttons19Extension:
                    ParseButtons(report, 1);
                    ParseExtension(report, 3, 19);
                    break;
                case InputReport.ButtonsAccelerometer16Extension:
                    ParseButtons(report, 1);
                    ParseAccelerometer(report, 3);
                    ParseExtension(report, 6, 16);
                    break;
                case InputReport.Buttons10Ir9Extension:
                case InputReport.ButtonsAccelerometer10Ir6Extension:
                    {
                        int offset = 1;
                        offset += ParseButtons(report, offset);
                        if (type == InputReport.ButtonsAccelerometer10Ir6Extension)
                            offset += ParseAccelerometer(report, offset);

                        BasicIRBeacon irBeacon;

                        // Parse beacon 0.
                        irBeacon = _CachedIRBeacons[0];
                        _IRBeacons[0] = ParseBasicBeacon(report, offset, 0, irBeacon) ? irBeacon : null;

                        // Parse beacon 1.
                        irBeacon = _CachedIRBeacons[1];
                        _IRBeacons[1] = ParseBasicBeacon(report, offset, 1, irBeacon) ? irBeacon : null;

                        // Parse beacon 2.
                        irBeacon = _CachedIRBeacons[2];
                        _IRBeacons[2] = ParseBasicBeacon(report, offset + 5, 0, irBeacon) ? irBeacon : null;

                        // Parse beacon 3.
                        irBeacon = _CachedIRBeacons[3];
                        _IRBeacons[3] = ParseBasicBeacon(report, offset + 5, 1, irBeacon) ? irBeacon : null;

                        offset += 10; // Each pair of IR-beacons is 5 bytes.

                        if (type == InputReport.ButtonsAccelerometer10Ir6Extension)
                            ParseExtension(report, offset, 6);
                        else
                            ParseExtension(report, offset, 9);
                    }
                    break;
                case InputReport.Extension:
                    ParseExtension(report, 1, 21);
                    break;
                case InputReport.ButtonsAccelerometer36IrA:
                    {
                        ParseButtons(report, 1); // since button information is complete on both the interlaced reports it needs no temporary storage

                        _PartialAccelerometerX = report[3];
                        _PartialAccelerometerZ = (ushort)(((report[2] & 0x60) << 1) | ((report[1] & 0x60) >> 1));

                        _PartialIRBeaconOneResult = ParseFullBeacon(report, 4, (FullIRBeacon)_CachedIRBeacons[0]);
                        _PartialIRBeaconTwoResult = ParseFullBeacon(report, 13, (FullIRBeacon)_CachedIRBeacons[1]);
                    }
                    break;
                case InputReport.ButtonsAccelerometer36IrB:
                    {
                        ParseButtons(report, 1);

                        // Complete the _PartialAccelerometerZ field
                        _PartialAccelerometerZ = (ushort)(_PartialAccelerometerZ | ((report[2] & 60) >> 3) | ((report[1] & 60) >> 5));

                        // Initialize the accelerometer if it was not yet initialized (should never happen).
                        if (Accelerometer == null)
                            ReadCalibrationData();

                        // Construct the new accelerometer-values from now completed data
                        Accelerometer.Raw.X = _PartialAccelerometerX;
                        Accelerometer.Raw.Y = report[3];
                        Accelerometer.Raw.Z = _PartialAccelerometerZ;

                        UpdateCalibratedAccelerometer();

                        FullIRBeacon irBeacon;

                        // Commit partial irbeacon from 'a' report
                        IRBeacons[0] = _PartialIRBeaconOneResult ? _CachedIRBeacons[0] : null;

                        // Commit partial irbeacon from 'a' report
                        IRBeacons[1] = _PartialIRBeaconTwoResult ? _CachedIRBeacons[1] : null;

                        // Parse beacon 2.
                        irBeacon = (FullIRBeacon)_CachedIRBeacons[2];
                        IRBeacons[2] = ParseFullBeacon(report, 4, irBeacon) ? irBeacon : null;

                        // Parse beacon 3.
                        irBeacon = (FullIRBeacon)_CachedIRBeacons[3];
                        IRBeacons[3] = ParseFullBeacon(report, 13, irBeacon) ? irBeacon : null;
                    }
                    break;

                case InputReport.GetStatusResult:
                    ParseButtons(report, 1);
                    ParseStatus(report);
                    break;
                case InputReport.ReadDataResult:
                    ParseButtons(report, 1);
                    // The rest is handled by the ReadData method (waiting for the report).
                    break;
                case InputReport.WriteDataResult:
                    // The rest is handled by the WriteData method (waiting for the report).
                    break;
                default:
                    Debug.WriteLine("Unknown report type: " + type.ToString("x"));
                    return false;
            }

            if ((type >= InputReport.Buttons || type == InputReport.GetStatusResult) && (type != InputReport.ButtonsAccelerometer36IrA))
                OnUpdated();
            return true;
        }
Beispiel #29
0
 public override void SelectNextMode()
 {
     Mode = Mode.Next();
     return;
 }
Beispiel #30
0
 public override void SelectPreviousMode()
 {
     Mode = Mode.Previous();
     return;
 }
Beispiel #31
0
 /// <summary>
 /// Reads commands from the IR-Remote. This will change mode to remote
 /// </summary>
 /// <returns>The remote command.</returns>
 public byte ReadRemoteCommand()
 {
     if (Mode != IRMode.Remote) {
         Mode = IRMode.Remote;
     }
     return ReadBytes (4) [(int)Channel];
 }
        /// <summary>
        /// Turn on the IR sensor
        /// </summary>
        /// <param name="mode">The data report mode</param>
        /// <param name="irSensitivity">IR sensitivity</param>
        void EnableIR(WiimoteDevice device, IRMode mode, IRSensitivity irSensitivity)
        {
            device.irMode = mode;

            UnityEngine.Debug.Log("Sync1");

            byte[] mBuff = new byte[REPORT_LENGTH];
            mBuff[0] = (byte)OutputReport.IR;
            mBuff[1] = (byte)(0x04 | device.RumbleBit);
            _hidInterface.Write(mBuff, device.ID);

            UnityEngine.Debug.Log("Sync2");
            Array.Clear(mBuff, 0, mBuff.Length);
            mBuff = new byte[REPORT_LENGTH];
            mBuff[0] = (byte)OutputReport.IR2;
            mBuff[1] = (byte)(0x04 | device.RumbleBit);
            _hidInterface.Write(mBuff, device.ID);


            UnityEngine.Debug.Log("Sync3");
            WriteMemory(device, REGISTER_IR, 0x08);

            UnityEngine.Debug.Log("Sync2x");
            setSensitivity(device,irSensitivity);

            UnityEngine.Debug.Log("Sync4");
            WriteMemory(device, REGISTER_IR_MODE, (byte)mode);

            UnityEngine.Debug.Log("Sync5");
            WriteMemory(device, REGISTER_IR, 0x08);

       
       
        }
Beispiel #33
0
 /// <summary>
 /// Gets the beacon location. This will change the mode to seek
 /// </summary>
 /// <returns>The beacon location.</returns>
 public BeaconLocation ReadBeaconLocation()
 {
     if (Mode != IRMode.Seek) {
         Mode = IRMode.Seek;
     }
     byte[] data = ReadBytes(4*2);
     return new BeaconLocation((sbyte)data[(int)Channel*2], (sbyte)data[((int)Channel*2)+1]);
 }
Beispiel #34
0
        private void EnableIR(IRMode mode, IRSensitivity Sensitivity)
        {
            _WiiMoteState.IRState.Mode = mode;

            ClearReport();
            _Buff[0] = (byte)OutputReport.IR;
            _Buff[1] = (byte)(0x04 | RumbleBit);
            CHIDAPI.Write(_Handle, _Buff);
            Thread.Sleep(50);

            ClearReport();
            _Buff[0] = (byte)OutputReport.IR2;
            _Buff[1] = (byte)(0x04 | RumbleBit);
            CHIDAPI.Write(_Handle, _Buff);
            Thread.Sleep(50);

            WriteData(REGISTER_IR, 0x08);
            Thread.Sleep(50);

            switch (Sensitivity)
            {
            case IRSensitivity.Level1:
                WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] { 0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0x64, 0x00, 0xfe });
                Thread.Sleep(50);
                WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] { 0xfd, 0x05 });
                break;

            case IRSensitivity.Level2:
                WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] { 0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0x96, 0x00, 0xb4 });
                Thread.Sleep(50);
                WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] { 0xb3, 0x04 });
                break;

            case IRSensitivity.Level3:
                WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] { 0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0xaa, 0x00, 0x64 });
                Thread.Sleep(50);
                WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] { 0x63, 0x03 });
                break;

            case IRSensitivity.Level4:
                WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] { 0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0xc8, 0x00, 0x36 });
                Thread.Sleep(50);
                WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] { 0x35, 0x03 });
                break;

            case IRSensitivity.Level5:
                WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] { 0x07, 0x00, 0x00, 0x71, 0x01, 0x00, 0x72, 0x00, 0x20 });
                Thread.Sleep(50);
                WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] { 0x1, 0x03 });
                break;

            case IRSensitivity.Max:
                WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90, 0x00, 0x41 });
                Thread.Sleep(50);
                WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] { 0x40, 0x00 });
                break;
            }
            Thread.Sleep(50);
            WriteData(REGISTER_IR_MODE, (byte)mode);
            Thread.Sleep(50);
            WriteData(REGISTER_IR, 0x08);
            Thread.Sleep(50);
        }
Beispiel #35
0
		public MSSensorMUXBase (SensorPort Port, MSSensorMUXPort Address, IRMode SensorMode) : base (Port, (byte)Address, I2CMode.LowSpeed)
		{
			base.Initialise ();
			if (SensorMode == IRMode.Proximity)
				Mode = 0;
			else if (SensorMode == IRMode.Seek)
				Mode = 1;
			else if (SensorMode == IRMode.Remote)
				Mode = 2;

			NumberOfM = 2;
			SetSensorMode ();
		}
Beispiel #36
0
 private void EnableIR(IRMode mode, IRSensitivity irSensitivity)
 {
     this.mWiimoteState.IRState.Mode = mode;
     this.ClearReport();
     this.mBuff[0] = 19;
     this.mBuff[1] = (byte)(4 | this.GetRumbleBit());
     this.WriteReport();
     this.ClearReport();
     this.mBuff[0] = 26;
     this.mBuff[1] = (byte)(4 | this.GetRumbleBit());
     this.WriteReport();
     this.WriteData(78643248, 8);
     switch (irSensitivity)
     {
         case IRSensitivity.WiiLevel1:
             this.WriteData(78643200, 9, new byte[]
         {
             2,
             0,
             0,
             113,
             1,
             0,
             100,
             0,
             254
         });
             this.WriteData(78643226, 2, new byte[]
         {
             253,
             5
         });
             break;
         case IRSensitivity.WiiLevel2:
             this.WriteData(78643200, 9, new byte[]
         {
             2,
             0,
             0,
             113,
             1,
             0,
             150,
             0,
             180
         });
             this.WriteData(78643226, 2, new byte[]
         {
             179,
             4
         });
             break;
         case IRSensitivity.WiiLevel3:
             this.WriteData(78643200, 9, new byte[]
         {
             2,
             0,
             0,
             113,
             1,
             0,
             170,
             0,
             100
         });
             this.WriteData(78643226, 2, new byte[]
         {
             99,
             3
         });
             break;
         case IRSensitivity.WiiLevel4:
             this.WriteData(78643200, 9, new byte[]
         {
             2,
             0,
             0,
             113,
             1,
             0,
             200,
             0,
             54
         });
             this.WriteData(78643226, 2, new byte[]
         {
             53,
             3
         });
             break;
         case IRSensitivity.WiiLevel5:
             this.WriteData(78643200, 9, new byte[]
         {
             7,
             0,
             0,
             113,
             1,
             0,
             114,
             0,
             32
         });
             this.WriteData(78643226, 2, new byte[]
         {
             1,
             3
         });
             break;
         case IRSensitivity.Maximum:
             {
                 this.WriteData(78643200, 9, new byte[]
         {
             2,
             0,
             0,
             113,
             1,
             0,
             144,
             0,
             65
         });
                 int arg_215_1 = 78643226;
                 byte arg_215_2 = 2;
                 byte[] array = new byte[2];
                 array[0] = 64;
                 this.WriteData(arg_215_1, arg_215_2, array);
                 break;
             }
         default:
             throw new ArgumentOutOfRangeException("irSensitivity");
     }
     this.WriteData(78643251, (byte)mode);
     this.WriteData(78643248, 8);
 }
Beispiel #37
0
 /// <summary>
 /// Initializes an EV3 IS Sensor
 /// </summary>
 /// <param name="mode">IR mode</param>
 public EV3IRSensor(BrickPortSensor port, IRMode mode)
     : this(port, mode, 1000)
 {
 }
        public void SetIRMode(IRMode irMode, IRSensitivity sensitivity)
        {
            bool enabled = (irMode != IRMode.Off);

            // Enable Pixel Clock.
            CreateReport(OutputReport.SetPixelClock);
            OutputBuffer[1] = (byte)(enabled ? 0x04 : 0x00);
            SendReport();

            // Enable IR Logic.
            CreateReport(OutputReport.SetCamera);
            OutputBuffer[1] = (byte)(enabled ? 0x04 : 0x00);
            SendReport();

            // No need to configure the camera when it is disabled.
            if (enabled)
            {
                // Configure the camera.
                WriteMemory(0x04b00030, 0x08);

                if (sensitivity != null)
                {
                    // Set sensitivity configuration.
                    WriteMemory(0x4b00000, sensitivity.ConfigurationBlock1);
                    WriteMemory(0x4b0001a, sensitivity.ConfigurationBlock2);
                }

                // Set the ir-mode
                WriteMemory(0x04b00033, (byte)irMode);
            }
            _IrSensorEnabled = enabled;
        }
Beispiel #39
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MonoBrick.EV3.IRSensor"/> class.
 /// </summary>
 /// <param name="mode">Mode.</param>
 public IRSensor(IRMode mode) :  base((SensorMode)mode)
 {
 }
        /// <summary>
        /// Turn on the IR sensor
        /// </summary>
        /// <param name="mode">The data report mode</param>
        private void EnableIR(IRMode mode)
        {
            mWiimoteState.IRState.Mode = mode;

            ClearReport();
            mBuff[0] = (byte)OutputReport.IR;
            mBuff[1] = (byte)(0x04 | GetRumbleBit());
            WriteReport();

            ClearReport();
            mBuff[0] = (byte)OutputReport.IR2;
            mBuff[1] = (byte)(0x04 | GetRumbleBit());
            WriteReport();

            WriteData(REGISTER_IR, 0x08);
            //default
            //			WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90, 0x00, 0xc0});
            //			WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] {0x40, 0x00});

            //super sensitive according to http://wiibrew.org/index.php?title=Wiimote#Sensitivity_Settings
            WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90, 0x00, 0x41 });
            WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] { 0x40, 0x00 });

            WriteData(REGISTER_IR_MODE, (byte)mode);
        }
Beispiel #41
0
		private void EnableIR(IRMode mode, IRSensitivity Sensitivity)
		{
			_WiiMoteState.IRState.Mode = mode;

			ClearReport();
			_Buff[0] = (byte)OutputReport.IR;
			_Buff[1] = (byte)(0x04 | RumbleBit);
            CHIDAPI.Write(_Handle, _Buff);
            Thread.Sleep(50);

			ClearReport();
			_Buff[0] = (byte)OutputReport.IR2;
			_Buff[1] = (byte)(0x04 | RumbleBit);
            CHIDAPI.Write(_Handle, _Buff);
            Thread.Sleep(50);

			WriteData(REGISTER_IR, 0x08);
            Thread.Sleep(50);

			switch(Sensitivity)
			{
				case IRSensitivity.Level1:
					WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] {0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0x64, 0x00, 0xfe});
                    Thread.Sleep(50);
                    WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] {0xfd, 0x05});
					break;
				case IRSensitivity.Level2:
					WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] {0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0x96, 0x00, 0xb4});
                    Thread.Sleep(50);
                    WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] {0xb3, 0x04});
					break;
				case IRSensitivity.Level3:
					WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] {0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0xaa, 0x00, 0x64});
                    Thread.Sleep(50);
                    WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] {0x63, 0x03});
					break;
				case IRSensitivity.Level4:
					WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] {0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0xc8, 0x00, 0x36});
                    Thread.Sleep(50);
                    WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] {0x35, 0x03});
					break;
				case IRSensitivity.Level5:
					WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] {0x07, 0x00, 0x00, 0x71, 0x01, 0x00, 0x72, 0x00, 0x20});
                    Thread.Sleep(50);
                    WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] {0x1, 0x03});
					break;
				case IRSensitivity.Max:
					WriteData(REGISTER_IR_SENSITIVITY_1, 9, new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90, 0x00, 0x41});
                    Thread.Sleep(50);
                    WriteData(REGISTER_IR_SENSITIVITY_2, 2, new byte[] {0x40, 0x00});
					break;
			}
            Thread.Sleep(50);
			WriteData(REGISTER_IR_MODE, (byte)mode);
            Thread.Sleep(50);
			WriteData(REGISTER_IR, 0x08);
            Thread.Sleep(50);
		}
 public void SetIRMode(IRMode irMode)
 {
     SetIRMode(irMode, IRSensitivity.Default);
 }
Beispiel #43
0
 /// <summary>
 /// Initializes an EV3 IS Sensor
 /// </summary>
 /// <param name="mode">IR mode</param>
 public EV3IRSensor(BrickPortSensor port, IRMode mode) : this(port, mode, 1000)
 {
 }
Beispiel #44
0
        protected override bool ParseReport(byte[] report)
        {
            if (report[0] < 0x20)
            {
                throw new InvalidOperationException("Received an output report!");
            }
            InputReport type = (InputReport)report[0];

            if (type >= InputReport.Buttons && type <= InputReport.ButtonsAccelerometer36IrB)
            {
                if (type == InputReport.ButtonsAccelerometer36IrB)
                {
                    ReportingMode = ReportingMode.ButtonsAccelerometer36Ir;
                }
                else
                {
                    ReportingMode = (ReportingMode)type;
                }

                IRMode irMode = GetIrMode(type);
                if (this._IrMode != irMode)
                {
                    for (int i = 0; i < _CachedIRBeacons.Length; i++)
                    {
                        BasicIRBeacon newBeacon = null;
                        switch (irMode)
                        {
                        case IRMode.Basic:
                            newBeacon = new BasicIRBeacon();
                            break;

                        case IRMode.Extended:
                            newBeacon = new ExtendedIRBeacon();
                            break;

                        case IRMode.Full:
                            newBeacon = new FullIRBeacon();
                            break;
                        }
                        _CachedIRBeacons[i] = newBeacon;
                        _IRBeacons[i]       = null;
                    }
                    this._IrMode = irMode;
                }
            }
            switch (type)
            {
            case InputReport.Buttons:
                ParseButtons(report, 1);
                break;

            case InputReport.ButtonsAccelerometer:
                ParseButtons(report, 1);
                ParseAccelerometer(report, 3);
                break;

            case InputReport.Buttons8Extension:
                ParseButtons(report, 1);
                ParseExtension(report, 3, 8);
                break;

            case InputReport.ButtonsAccelerometer12Ir:
            {
                ParseButtons(report, 1);
                ParseAccelerometer(report, 3);

                ExtendedIRBeacon irBeacon;

                // Parse beacon 0.
                irBeacon      = (ExtendedIRBeacon)_CachedIRBeacons[0];
                _IRBeacons[0] = ParseExtendedBeacon(report, 6, irBeacon) ? irBeacon : null;

                // Parse beacon 1.
                irBeacon      = (ExtendedIRBeacon)_CachedIRBeacons[1];
                _IRBeacons[1] = ParseExtendedBeacon(report, 9, irBeacon) ? irBeacon : null;

                // Parse beacon 2.
                irBeacon      = (ExtendedIRBeacon)_CachedIRBeacons[2];
                _IRBeacons[2] = ParseExtendedBeacon(report, 12, irBeacon) ? irBeacon : null;

                // Parse beacon 3.
                irBeacon      = (ExtendedIRBeacon)_CachedIRBeacons[3];
                _IRBeacons[3] = ParseExtendedBeacon(report, 15, irBeacon) ? irBeacon : null;
            }
            break;

            case InputReport.Buttons19Extension:
                ParseButtons(report, 1);
                ParseExtension(report, 3, 19);
                break;

            case InputReport.ButtonsAccelerometer16Extension:
                ParseButtons(report, 1);
                ParseAccelerometer(report, 3);
                ParseExtension(report, 6, 16);
                break;

            case InputReport.Buttons10Ir9Extension:
            case InputReport.ButtonsAccelerometer10Ir6Extension:
            {
                int offset = 1;
                offset += ParseButtons(report, offset);
                if (type == InputReport.ButtonsAccelerometer10Ir6Extension)
                {
                    offset += ParseAccelerometer(report, offset);
                }

                BasicIRBeacon irBeacon;

                // Parse beacon 0.
                irBeacon      = _CachedIRBeacons[0];
                _IRBeacons[0] = ParseBasicBeacon(report, offset, 0, irBeacon) ? irBeacon : null;

                // Parse beacon 1.
                irBeacon      = _CachedIRBeacons[1];
                _IRBeacons[1] = ParseBasicBeacon(report, offset, 1, irBeacon) ? irBeacon : null;

                // Parse beacon 2.
                irBeacon      = _CachedIRBeacons[2];
                _IRBeacons[2] = ParseBasicBeacon(report, offset + 5, 0, irBeacon) ? irBeacon : null;

                // Parse beacon 3.
                irBeacon      = _CachedIRBeacons[3];
                _IRBeacons[3] = ParseBasicBeacon(report, offset + 5, 1, irBeacon) ? irBeacon : null;

                offset += 10;         // Each pair of IR-beacons is 5 bytes.

                if (type == InputReport.ButtonsAccelerometer10Ir6Extension)
                {
                    ParseExtension(report, offset, 6);
                }
                else
                {
                    ParseExtension(report, offset, 9);
                }
            }
            break;

            case InputReport.Extension:
                ParseExtension(report, 1, 21);
                break;

            case InputReport.ButtonsAccelerometer36IrA:
            {
                ParseButtons(report, 1);         // since button information is complete on both the interlaced reports it needs no temporary storage

                _PartialAccelerometerX = report[3];
                _PartialAccelerometerZ = (ushort)(((report[2] & 0x60) << 1) | ((report[1] & 0x60) >> 1));

                _PartialIRBeaconOneResult = ParseFullBeacon(report, 4, (FullIRBeacon)_CachedIRBeacons[0]);
                _PartialIRBeaconTwoResult = ParseFullBeacon(report, 13, (FullIRBeacon)_CachedIRBeacons[1]);
            }
            break;

            case InputReport.ButtonsAccelerometer36IrB:
            {
                ParseButtons(report, 1);

                // Complete the _PartialAccelerometerZ field
                _PartialAccelerometerZ = (ushort)(_PartialAccelerometerZ | ((report[2] & 60) >> 3) | ((report[1] & 60) >> 5));

                // Initialize the accelerometer if it was not yet initialized (should never happen).
                if (Accelerometer == null)
                {
                    ReadCalibrationData();
                }

                // Construct the new accelerometer-values from now completed data
                Accelerometer.Raw.X = _PartialAccelerometerX;
                Accelerometer.Raw.Y = report[3];
                Accelerometer.Raw.Z = _PartialAccelerometerZ;

                UpdateCalibratedAccelerometer();

                FullIRBeacon irBeacon;

                // Commit partial irbeacon from 'a' report
                IRBeacons[0] = _PartialIRBeaconOneResult ? _CachedIRBeacons[0] : null;

                // Commit partial irbeacon from 'a' report
                IRBeacons[1] = _PartialIRBeaconTwoResult ? _CachedIRBeacons[1] : null;

                // Parse beacon 2.
                irBeacon     = (FullIRBeacon)_CachedIRBeacons[2];
                IRBeacons[2] = ParseFullBeacon(report, 4, irBeacon) ? irBeacon : null;

                // Parse beacon 3.
                irBeacon     = (FullIRBeacon)_CachedIRBeacons[3];
                IRBeacons[3] = ParseFullBeacon(report, 13, irBeacon) ? irBeacon : null;
            }
            break;

            case InputReport.GetStatusResult:
                ParseButtons(report, 1);
                ParseStatus(report);
                break;

            case InputReport.ReadDataResult:
                ParseButtons(report, 1);
                // The rest is handled by the ReadData method (waiting for the report).
                break;

            case InputReport.WriteDataResult:
                // The rest is handled by the WriteData method (waiting for the report).
                break;

            default:
                Debug.WriteLine("Unknown report type: " + type.ToString("x"));
                return(false);
            }

            if ((type >= InputReport.Buttons || type == InputReport.GetStatusResult) && (type != InputReport.ButtonsAccelerometer36IrA))
            {
                OnUpdated();
            }
            return(true);
        }
        /// <summary>
        /// Turn on the IR sensor
        /// </summary>
        /// <param name="mode">The data report mode</param>
        /// <param name="irSensitivity">IR sensitivity</param>
        private void EnableIR(IRMode mode, IRSensitivity irSensitivity)
        {
            mWiimoteState.Status.IREnabled = true;
            mWiimoteState.IRState.Mode     = mode;

            /*byte[] buff = CreateReport();
             * buff[0] = (byte) OutputReport.IR1;
             * buff[1] = (byte) (0x04 | GetRumbleBit());
             * WriteReport(buff);
             *
             * Array.Clear(buff, 0, buff.Length);
             * buff[0] = (byte) OutputReport.IR2;
             * buff[1] = (byte) (0x04 | GetRumbleBit());
             * WriteReport(buff);*/

            byte[] buff = CreateReport2();
            buff[0] = (byte)(0x04 | GetRumbleBit());
            WriteReport2(OutputReport.IR1, buff);

            Array.Clear(buff, 0, buff.Length);
            buff[0] = (byte)(0x04 | GetRumbleBit());
            WriteReport2(OutputReport.IR2, buff);

            WriteData(Registers.IR, 0x08);
            switch (irSensitivity)
            {
            case IRSensitivity.WiiLevel1:
                WriteData(Registers.IRSensitivity1, 9, new byte[] { 0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0x64, 0x00, 0xfe });
                WriteData(Registers.IRSensitivity2, 2, new byte[] { 0xfd, 0x05 });
                break;

            case IRSensitivity.WiiLevel2:
                WriteData(Registers.IRSensitivity1, 9, new byte[] { 0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0x96, 0x00, 0xb4 });
                WriteData(Registers.IRSensitivity2, 2, new byte[] { 0xb3, 0x04 });
                break;

            case IRSensitivity.WiiLevel3:
                WriteData(Registers.IRSensitivity1, 9, new byte[] { 0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0xaa, 0x00, 0x64 });
                WriteData(Registers.IRSensitivity2, 2, new byte[] { 0x63, 0x03 });
                break;

            case IRSensitivity.WiiLevel4:
                WriteData(Registers.IRSensitivity1, 9, new byte[] { 0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0xc8, 0x00, 0x36 });
                WriteData(Registers.IRSensitivity2, 2, new byte[] { 0x35, 0x03 });
                break;

            case IRSensitivity.WiiLevel5:
                WriteData(Registers.IRSensitivity1, 9, new byte[] { 0x07, 0x00, 0x00, 0x71, 0x01, 0x00, 0x72, 0x00, 0x20 });
                WriteData(Registers.IRSensitivity2, 2, new byte[] { 0x1, 0x03 });
                break;

            case IRSensitivity.Maximum:
                WriteData(Registers.IRSensitivity1, 9, new byte[] { 0x02, 0x00, 0x00, 0x71, 0x01, 0x00, 0x90, 0x00, 0x41 });
                WriteData(Registers.IRSensitivity2, 2, new byte[] { 0x40, 0x00 });
                break;

            default:
                throw new ArgumentOutOfRangeException("irSensitivity");
            }
            WriteData(Registers.IRMode, (byte)mode);
            WriteData(Registers.IR, 0x08);
        }
Beispiel #46
0
 /// <summary>
 /// Read the distance of the sensor in CM (0-100). This will change mode to proximity
 /// </summary>
 public int ReadDistance()
 {
     if (Mode != IRMode.Proximity) {
         Mode = IRMode.Proximity;
     }
     return ReadByte();
 }
Beispiel #47
0
 /// <summary>
 /// Initialize an EV3 IR Sensor
 /// </summary>
 /// <param name="port">Sensor port</param>
 /// <param name="mode">IR mode</param>
 /// <param name="timeout">Period in millisecond to check sensor value changes</param>
 public EV3IRSensor(BrickPortSensor port, IRMode mode, int timeout)
 {
     brick = new Brick();
     Mode = mode;
     Channel = IRChannel.One;
     brick.BrickPi.Sensor[(int)Port].Type = (BrickSensorType)mode;
     periodRefresh = timeout;
     timer = new Timer(UpdateSensor, this, TimeSpan.FromMilliseconds(timeout), TimeSpan.FromMilliseconds(timeout));
 }