Example #1
0
        /// <summary>Set Wiimote reporting mode.</summary>
        /// <param name="reportType">Report type</param>
        /// <param name="irSensitivity">IR sensitivity</param>
        /// <param name="continuous">Continuous data</param>
        public void SetReportType(ReportType reportType, IRSensitivity irSensitivity, bool continuous)
        {
            InputReport         type       = (InputReport)reportType;
            DataReportAttribute dataReport =
                EnumInfo <InputReport> .TryGetAttribute <DataReportAttribute>(type);

            if (dataReport == null)
            {
                throw new WiimoteException(this, $"{type} is not a valid report type!");
            }

            int irSize = dataReport.IRSize;

            if (dataReport.IsInterleaved)
            {
                irSize *= 2;
            }

            switch (dataReport.IRSize)
            {
            case 10:
                EnableIR(IRMode.Basic, irSensitivity);
                break;

            case 12:
                EnableIR(IRMode.Extended, irSensitivity);
                break;

            case 36:
                EnableIR(IRMode.Full, irSensitivity);
                break;

            default:
                DisableIR();
                break;
            }

            byte[] buff = CreateReport(OutputReport.InputReportType);

            buff[1] = (byte)(continuous ? 0x04 : 0x00);
            buff[2] = (byte)type;

            WriteReport(buff);
            wiimoteState.ReportType       = reportType;
            wiimoteState.ContinuousReport = continuous;
        }
        /// <summary>
        /// Set Wiimote reporting mode
        /// </summary>
        /// <param name="type">Report type</param>
        /// <param name="irSensitivity">IR sensitivity</param>
        /// <param name="continuous">Continuous data</param>
        public void SetReportType(InputReport type, IRSensitivity irSensitivity, bool continuous)
        {
            // only 1 report type allowed for the BB
            //if (mWiimoteState.ExtensionType == ExtensionType.BalanceBoard)
            //	type = InputReport.ButtonsExt19;


            DataReportAttribute dataReport =
                EnumInfo <InputReport> .TryGetAttribute <DataReportAttribute>(type);

            if (dataReport == null)
            {
                throw new WiimoteException($"{type} is not a valid report type!");
            }

            int irSize = dataReport.IRSize;

            if (dataReport.IsInterleaved)
            {
                irSize *= 2;
            }

            switch (dataReport.IRSize)
            {
            case 10:
                EnableIR(IRMode.Basic, irSensitivity);
                break;

            case 12:
                EnableIR(IRMode.Extended, irSensitivity);
                break;

            case 36:
                EnableIR(IRMode.Full, irSensitivity);
                break;

            default:
                DisableIR();
                break;
            }

            /*switch (type) {
             * case InputReport.ButtonsAccelIR12:
             *      EnableIR(IRMode.Extended, irSensitivity);
             *      break;
             * case InputReport.ButtonsAccelIR10Ext6:
             *      EnableIR(IRMode.Basic, irSensitivity);
             *      break;
             * default:
             *      DisableIR();
             *      break;
             * }*/

            /*byte[] buff = CreateReport();
             * buff[0] = (byte) OutputReport.DataReportType;
             * buff[1] = (byte) ((continuous ? 0x04 : 0x00) | GetRumbleBit());
             * buff[2] = (byte) type;*/

            byte[] buff = CreateReport();
            buff[0] = (byte)((continuous ? 0x04 : 0x00) | GetRumbleBit());
            buff[1] = (byte)type;

            WriteReport2(OutputReport.DataReportType, buff);
        }
        /// <summary>
        /// Parse a report sent by the Wiimote
        /// </summary>
        /// <param name="buff">Data buffer to parse</param>
        /// <returns>Returns a boolean noting whether an event needs to be posted</returns>
        private bool ParseInputReport(byte[] buff)
        {
            //try {
            InputReport         type       = (InputReport)buff[0];
            DataReportAttribute dataReport =
                EnumInfo <InputReport> .TryGetAttribute <DataReportAttribute>(type);

            if (dataReport != null)
            {
                // Buttons are ALWAYS parsed
                if (dataReport.HasButtons)
                {
                    ParseButtons2(buff, dataReport.ButtonsOffset + 1);
                }

                switch (dataReport.Interleave)
                {
                case Interleave.None:
                    if (dataReport.HasAccel)
                    {
                        ParseAccel2(buff, dataReport.AccelOffset + 1);
                    }

                    if (dataReport.HasIR)
                    {
                        ParseIR2(buff, dataReport.IROffset + 1, dataReport.IRSize);
                    }

                    if (dataReport.HasExt)
                    {
                        ParseExtension2(buff, dataReport.ExtOffset + 1, dataReport.ExtSize);
                    }
                    break;

                case Interleave.A:
                    interleavedBufferA = buff;
                    interleavedReportA = dataReport;
                    break;

                case Interleave.B:
                    byte[] buffA = interleavedBufferA;
                    byte[] buffB = buff;
                    DataReportAttribute reportA = interleavedReportA;
                    DataReportAttribute reportB = dataReport;
                    ParseAccelInterleaved2(buffA, buffB, reportA.AccelOffset + 1, reportB.AccelOffset + 1);
                    ParseIRInterleaved2(buffA, buffB, reportA.IROffset + 1, reportB.IROffset + 1);
                    break;
                }

                return(true);
            }
            else
            {
                switch (type)
                {
                case InputReport.Status:
                    Debug.WriteLine("******** STATUS ********");

                    ExtensionType extensionTypeLast = wiimoteState.ExtensionType;
                    bool          extensionLast     = wiimoteState.Status.Extension;
                    ParseButtons2(buff, 1);
                    ParseStatus2(buff, 3);
                    bool extensionNew = WiimoteState.Status.Extension;

                    using (AsyncReadState state = BeginAsyncRead()) {
                        byte extensionType = 0;
                        if (extensionNew)
                        {
                            ReadByte(Registers.ExtensionType2);
                        }

                        Debug.WriteLine($"Extension byte={extensionType:X2}");

                        // extension connected?
                        Debug.WriteLine($"Extension, Old: {extensionLast}, New: {extensionNew}");

                        if (extensionNew != extensionLast || extensionType == 0x04 || extensionType == 0x5)
                        {
                            if (wiimoteState.Extension)
                            {
                                InitializeExtension(extensionType);
                                SetReportType(wiimoteState.ReportType,
                                              wiimoteState.IRState.Sensitivity,
                                              wiimoteState.ContinuousReport);
                            }
                            else
                            {
                                wiimoteState.ExtensionType     = ExtensionType.None;
                                wiimoteState.Nunchuk           = new NunchukState();
                                wiimoteState.ClassicController = new ClassicControllerState();
                                RaiseExtensionChanged(extensionTypeLast, false);
                                SetReportType(wiimoteState.ReportType,
                                              wiimoteState.IRState.Sensitivity,
                                              wiimoteState.ContinuousReport);
                            }
                        }
                    }
                    statusDone.Set();
                    //Respond(OutputReport.Status, true);
                    break;

                case InputReport.ReadData:
                    Debug.WriteLine("******** READ DATA ********");
                    ParseButtons2(buff, 1);
                    ParseReadData(buff);
                    break;

                case InputReport.AcknowledgeOutputReport:
                    Debug.WriteLine("******** ACKNOWLEDGE ********");
                    ParseButtons2(buff, 1);
                    OutputReport outputType = (OutputReport)buff[3];
                    WriteResult  result     = (WriteResult)buff[4];
                    if (outputType == OutputReport.WriteMemory)
                    {
                        writeDone.Set();
                        Debug.WriteLine("Write done");
                    }
                    //Acknowledge(outputType, result);
                    break;

                default:
                    Debug.WriteLine($"Unknown input report: {type}");
                    break;
                }
            }
            //}
            //catch (TimeoutException) { }
            return(true);
        }