wiimote program closes when I press a button

361 Views Asked by At

I am working on a project trying to use the wiimote to control a robotic arm. But first I want to test the wiimote in c#. Basically I want my wiimote to be updated each time I press a button and have a GUI interface that displays which button has been pressed in a check box list. But when I run my code and press a button the window form closes the program. I have tried for several hours to figure out this problem but I'm still stuck.

Wiimote wm = new Wiimote();

public Wiimote1()
{
    InitializeComponent();
}


private void Form1_Load(object sender, EventArgs e)
{
    wm.WiimoteChanged += wm_WiimoteChanged;
    wm.WiimoteExtensionChanged += wm_WiimoteExtensionChanged;
    wm.Connect();
    wm.SetReportType(InputReport.IRAccel, true);
    wm.SetLEDs(true, false, false, false);
}

private void wm_WiimoteExtensionChanged(object sender, WiimoteExtensionChangedEventArgs args)
{

    if (args.Inserted)
        wm.SetReportType(InputReport.IRExtensionAccel, true);
    else
        wm.SetReportType(InputReport.IRAccel, true);
}


private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    wm.Disconnect();
}

private void wm_WiimoteChanged(object sender, WiimoteChangedEventArgs args)
{

    WiimoteState wmi = args.WiimoteState;

    buttonA = wmi.ButtonState.A;
    buttonB = wmi.ButtonState.B;
    buttonHome = wmi.ButtonState.Home;
    buttonOne = wmi.ButtonState.One;
    buttonTwo = wmi.ButtonState.Two;
    buttonPlus = wmi.ButtonState.Plus;
    buttonMinus = wmi.ButtonState.Minus;
    buttonUp = wmi.ButtonState.Up;
    buttonDown = wmi.ButtonState.Down;
    buttonLeft = wmi.ButtonState.Left;
    buttonRight = wmi.ButtonState.Right;
    AccelX = wmi.AccelState.Values.X.ToString();
    AccelY = wmi.AccelState.Values.Y.ToString();
    AccelZ = wmi.AccelState.Values.Z.ToString();

    switch (wmi.ExtensionType)
    {
        case ExtensionType.Nunchuk:
            nunC = wmi.NunchukState.C;
            nunZ = wmi.NunchukState.Z;
            nunAccelX = wmi.NunchukState.AccelState.Values.X.ToString();
            nunAccelY = wmi.NunchukState.AccelState.Values.Y.ToString();
            JoyStickX = wmi.NunchukState.Joystick.X.ToString();
            JoyStickY = wmi.NunchukState.Joystick.Y.ToString();
            break;

        case ExtensionType.ClassicController:
            clsA = wmi.ClassicControllerState.ButtonState.A;
            clsB = wmi.ClassicControllerState.ButtonState.B;
            clsX = wmi.ClassicControllerState.ButtonState.X;
            clsY = wmi.ClassicControllerState.ButtonState.Y;
            clsPlus = wmi.ClassicControllerState.ButtonState.Plus;
            clsMinus = wmi.ClassicControllerState.ButtonState.Minus;
            clsHome = wmi.ClassicControllerState.ButtonState.Home;
            clsUp = wmi.ClassicControllerState.ButtonState.Up;
            clsDown = wmi.ClassicControllerState.ButtonState.Down;
            clsLeft = wmi.ClassicControllerState.ButtonState.Left;
            clsRight = wmi.ClassicControllerState.ButtonState.Right;
            clsTriggerL = wmi.ClassicControllerState.ButtonState.TriggerL;
            clsTriggerR = wmi.ClassicControllerState.ButtonState.TriggerR;
            clsZL = wmi.ClassicControllerState.ButtonState.ZL;
            clsZR = wmi.ClassicControllerState.ButtonState.ZR;
            break;
    }

    checkList();

}


private void checkList()
{
    checkedListBox1.SetItemChecked(0, buttonA);
    checkedListBox1.SetItemChecked(1, buttonB);
    checkedListBox1.SetItemChecked(2, buttonMinus);
    checkedListBox1.SetItemChecked(3, buttonPlus);
    checkedListBox1.SetItemChecked(4, buttonHome);
    checkedListBox1.SetItemChecked(5, buttonOne);
    checkedListBox1.SetItemChecked(6, buttonTwo);
    checkedListBox1.SetItemChecked(7, buttonUp);
    checkedListBox1.SetItemChecked(8, buttonDown);
    checkedListBox1.SetItemChecked(9, buttonRight);
    checkedListBox1.SetItemChecked(10, buttonLeft);
}
2

There are 2 best solutions below

2
On

You find it is much easier to figure out the error when you add exception handling to the code. Look up try...catch

If a caught exception doesn't make sense to you, you can ask questions about that here.

0
On

If you are using a 64bit Windows, exceptions that are thrown in Form_Load event are silently swallowed. Maybe it is a good idea to move your code away from the Form_Load event. That makes future debugging much easier.

public Wiimote1()
{
    InitializeComponent();
    InitWiimote();
}


private void InitWiimote()
{
    wm.WiimoteChanged += wm_WiimoteChanged;
    wm.WiimoteExtensionChanged += wm_WiimoteExtensionChanged;
    wm.Connect();
    wm.SetReportType(InputReport.IRAccel, true);
    wm.SetLEDs(true, false, false, false);
}