// Invoked when extensions are attached private static void wm_WiimoteExtensionChanged(object sender, WiimoteExtensionChangedEventArgs e) { // find the control for this Wiimote WiimoteInfo wi = CollectWiimotes.wmMap[((Wiimote)sender).ID]; wi.UpdateExtension(e); if (e.Inserted) { ((Wiimote)sender).SetReportType(InputReport.IRExtensionAccel, true); } else { ((Wiimote)sender).SetReportType(InputReport.IRAccel, true); } }
private void ReceiveCallback(System.IAsyncResult ar) { ReceiveStateObject state = (ReceiveStateObject)ar.AsyncState; int bytesreceived = state.sock.EndReceive(ar); if (bytesreceived != state.BufferSize) { throw new System.Exception("Failed to get an entire response!"); } byte[] buff = state.buffer; WiimoteInfo info = new WiimoteInfo(); info.FromBytes(buff); StateMutex.WaitOne(); Wiimotes[info.Name] = info; StateMutex.ReleaseMutex(); wiimoteSocket.BeginReceive(state.buffer, 0, state.BufferSize, 0, new System.AsyncCallback(ReceiveCallback), state); }
static void Main(string[] args) { int wiimoteIndex = 0; //Used to identify the wiimotes. // Try and find all wiimotes. Throw an error if we can't find any. try { CollectWiimotes.WiiCollect.FindAllWiimotes(); } catch (WiimoteNotFoundException ex) { Console.WriteLine(ex.Message); Console.WriteLine("Wiimote not found"); } catch (WiimoteException ex) { Console.WriteLine(ex.Message); Console.WriteLine("Wiimote Error"); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine("Unknown Error In Acquiring Wiimote"); } // Run through each wiimote we found in the HID list and: // Instantiate a new Class // Put its ID into the dictionary // Connects Wiimote // Associate the LEDE number with the instance // Changes LED to indicate unit number foreach (Wiimote wm in CollectWiimotes.WiiCollect) { Console.Clear(); // Create a new class instance for each wiimote we find in HID list WiimoteInfo wi = new WiimoteInfo(wm); // Place our wiimote's ID into the dictionary. CollectWiimotes.wmMap[wm.ID] = wi; wiimoteIndex++; // Set the LED count wi.WiimoteLED = wiimoteIndex; // Connect it and tell it which functions to call when the wiimote updates state wm.WiimoteChanged += wm_WiimoteChanged; // add functor wm.WiimoteExtensionChanged += wm_WiimoteExtensionChanged; wm.Connect(); // Set the acquisition rate for the wiimote if (wm.WiimoteState.ExtensionType != ExtensionType.BalanceBoard) wm.SetReportType(InputReport.IRExtensionAccel, WiimoteLib.IRSensitivity.Maximum, true); // Set LEDs so we know it is connected wm.SetLEDs(wi.WiimoteLED); Console.SetCursorPosition(0, wi.WiimoteLED); Console.WriteLine("Wiimote " + wi.WiimoteLED.ToString() + " Connected!"); Console.SetCursorPosition(0, wi.WiimoteLED + 1); Console.WriteLine("Press escape to exit"); } // Hang around and display the battery value (in UpdateState) ConsoleKeyInfo info = Console.ReadKey(); while (info.Key != ConsoleKey.Escape) { info = Console.ReadKey(); } Console.Clear(); }
// mathod that gets called any time the wiimote changes state private static void wm_WiimoteChanged(object sender, WiimoteChangedEventArgs e) { WiimoteInfo wi = CollectWiimotes.wmMap[((Wiimote)sender).ID]; wi.UpdateState(e); }
static void Main(string[] args) { int wiimoteIndex = 0; //Used to identify the wiimotes. // Try and find all wiimotes. Throw an error if we can't find any. try { CollectWiimotes.WiiCollect.FindAllWiimotes(); } catch (WiimoteNotFoundException ex) { Console.WriteLine(ex.Message); Console.WriteLine("Wiimote not found"); } catch (WiimoteException ex) { Console.WriteLine(ex.Message); Console.WriteLine("Wiimote Error"); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine("Unknown Error In Acquiring Wiimote"); } // Run through each wiimote we found in the HID list and: // Instantiate a new Class // Put its ID into the dictionary // Connects Wiimote // Associate the LEDE number with the instance // Changes LED to indicate unit number foreach (Wiimote wm in CollectWiimotes.WiiCollect) { Console.Clear(); // Create a new class instance for each wiimote we find in HID list WiimoteInfo wi = new WiimoteInfo(wm); // Place our wiimote's ID into the dictionary. CollectWiimotes.wmMap[wm.ID] = wi; wiimoteIndex++; // Set the LED count wi.WiimoteLED = wiimoteIndex; // Connect it and tell it which functions to call when the wiimote updates state wm.WiimoteChanged += wm_WiimoteChanged; // add functor wm.WiimoteExtensionChanged += wm_WiimoteExtensionChanged; wm.Connect(); // Set the acquisition rate for the wiimote if (wm.WiimoteState.ExtensionType != ExtensionType.BalanceBoard) { wm.SetReportType(InputReport.IRExtensionAccel, WiimoteLib.IRSensitivity.Maximum, true); } // Set LEDs so we know it is connected wm.SetLEDs(wi.WiimoteLED); Console.SetCursorPosition(0, wi.WiimoteLED); Console.WriteLine("Wiimote " + wi.WiimoteLED.ToString() + " Connected!"); Console.SetCursorPosition(0, wi.WiimoteLED + 1); Console.WriteLine("Press escape to exit"); } // Hang around and display the battery value (in UpdateState) ConsoleKeyInfo info = Console.ReadKey(); while (info.Key != ConsoleKey.Escape) { info = Console.ReadKey(); } Console.Clear(); }