-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultipleWiimoteForm.cs
91 lines (77 loc) · 2.35 KB
/
MultipleWiimoteForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using WiimoteLib;
namespace WiimoteTest
{
public partial class MultipleWiimoteForm : Form
{
// map a wiimote to a specific state user control dealie
Dictionary<Guid,WiimoteInfo> mWiimoteMap = new Dictionary<Guid,WiimoteInfo>();
WiimoteCollection mWC;
public MultipleWiimoteForm()
{
InitializeComponent();
}
private void MultipleWiimoteForm_Load(object sender, EventArgs e)
{
// find all wiimotes connected to the system
mWC = new WiimoteCollection();
int index = 1;
try
{
mWC.FindAllWiimotes();
}
catch(WiimoteNotFoundException ex)
{
MessageBox.Show(ex.Message, "Wiimote not found error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch(WiimoteException ex)
{
MessageBox.Show(ex.Message, "Wiimote error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Unknown error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
foreach(Wiimote wm in mWC)
{
// create a new tab
TabPage tp = new TabPage("Wiimote " + index);
tabWiimotes.TabPages.Add(tp);
// create a new user control
WiimoteInfo wi = new WiimoteInfo(wm);
tp.Controls.Add(wi);
// setup the map from this wiimote's ID to that control
mWiimoteMap[wm.ID] = wi;
// connect it and set it up as always
wm.WiimoteChanged += wm_WiimoteChanged;
wm.WiimoteExtensionChanged += wm_WiimoteExtensionChanged;
wm.Connect();
if(wm.WiimoteState.ExtensionType != ExtensionType.BalanceBoard)
wm.SetReportType(InputReport.IRExtensionAccel, IRSensitivity.Maximum, true);
wm.SetLEDs(index++);
}
}
void wm_WiimoteChanged(object sender, WiimoteChangedEventArgs e)
{
WiimoteInfo wi = mWiimoteMap[((Wiimote)sender).ID];
wi.UpdateState(e);
}
void wm_WiimoteExtensionChanged(object sender, WiimoteExtensionChangedEventArgs e)
{
// find the control for this Wiimote
WiimoteInfo wi = mWiimoteMap[((Wiimote)sender).ID];
wi.UpdateExtension(e);
if(e.Inserted)
((Wiimote)sender).SetReportType(InputReport.IRExtensionAccel, true);
else
((Wiimote)sender).SetReportType(InputReport.IRAccel, true);
}
private void MultipleWiimoteForm_FormClosing(object sender, FormClosingEventArgs e)
{
foreach(Wiimote wm in mWC)
wm.Disconnect();
}
}
}