Ejemplo n.º 1
0
/**
 * Launch wiimote detection and add the corresponding gamepad devices to the
 * device manager.
 * TODO: this should be done in a separate thread, to not block the UI...
 */
void WiimoteManager::launchDetection(int timeout)
{
  // It's only needed on systems with bluez, because wiiuse_find does not find alredy connected wiimotes
#ifdef WIIUSE_BLUEZ
    //Cleans up the config and the disconnected wiimotes
    int number_previous_wiimotes = 0;
    wiimote_t** previous_wiimotes = (wiimote_t**) malloc(sizeof(struct wiimote_t*) * MAX_WIIMOTES);
    memset(previous_wiimotes,0,sizeof(struct wiimote_t*) * MAX_WIIMOTES);
    for (unsigned int i = 0; i < m_wiimotes.size(); i++)
    {
      if (WIIMOTE_IS_CONNECTED(m_all_wiimote_handles[i]))
      {
        previous_wiimotes[i]=m_all_wiimote_handles[i];
        m_all_wiimote_handles[i] = NULL;
        number_previous_wiimotes++;
      }
    }

    //To prevent segmentation fault, have to delete NULLs
    wiimote_t** deletable_wiimotes = (wiimote_t**) malloc(sizeof(struct wiimote_t*) * (m_wiimotes.size()-number_previous_wiimotes));
    memset(deletable_wiimotes,0,sizeof(struct wiimote_t*) * (m_wiimotes.size()-number_previous_wiimotes));
    int number_deletables = 0;
    for (unsigned int i = 0; i < m_wiimotes.size(); i++)
    {
      if (m_all_wiimote_handles[i] != NULL)
      {
        deletable_wiimotes[number_deletables++] = m_all_wiimote_handles[i];
      }
    }
    m_all_wiimote_handles = wiiuse_init(MAX_WIIMOTES);
    wiiuse_cleanup(deletable_wiimotes, number_deletables);

#endif

    // Stop WiiUse, remove wiimotes, gamepads, gamepad configs.
    cleanup();

    m_all_wiimote_handles = wiiuse_init(MAX_WIIMOTES);

    // Detect wiimotes
    int nb_found_wiimotes = wiiuse_find(m_all_wiimote_handles, MAX_WIIMOTES, timeout);

#ifndef WIIUSE_BLUEZ
    // Couldn't find any wiimote?
    if(nb_found_wiimotes == 0)
        return;
#endif

#ifdef WIIUSE_BLUEZ
    // Couldn't find any wiimote?
    if(nb_found_wiimotes + number_previous_wiimotes == 0)
        return;
#endif

    // Try to connect to all found wiimotes
    int nb_wiimotes = wiiuse_connect(m_all_wiimote_handles, nb_found_wiimotes);

#ifndef WIIUSE_BLUEZ
    // Couldn't connect to any wiimote?
    if(nb_wiimotes == 0)
        return;
#endif

#ifdef WIIUSE_BLUEZ
    // Couldn't connect to any wiimote?
    if(nb_wiimotes + number_previous_wiimotes == 0)
        return;

    //Merges previous and new wiimote's list
    int number_merged_wiimotes = 0;
    for (int i = 0; i < number_previous_wiimotes && i + nb_wiimotes < MAX_WIIMOTES; i++)
    {
      m_all_wiimote_handles[i+nb_wiimotes] = previous_wiimotes[i];
      previous_wiimotes[i] = NULL;
      m_all_wiimote_handles[i]->unid = nb_wiimotes+i+1;
      number_merged_wiimotes++;
    }
    nb_wiimotes += number_merged_wiimotes;

    //To prevent segmentation fault, have to delete NULLs
    number_deletables = 0;
    deletable_wiimotes = (wiimote_t**) malloc(sizeof(struct wiimote_t*) * (number_previous_wiimotes-number_merged_wiimotes));
    memset(deletable_wiimotes,0,sizeof(struct wiimote_t*) * (number_previous_wiimotes-number_merged_wiimotes));
    for (int i = 0; i < number_previous_wiimotes; i++)
    {
      if (previous_wiimotes[i] != NULL)
      {
        deletable_wiimotes[number_deletables++] = previous_wiimotes[i];
      }
    }
    // Cleans up wiimotes which ones didn't fit in limit
    wiiuse_cleanup(deletable_wiimotes, number_deletables);

#endif

    // ---------------------------------------------------
    // Create or find a GamepadConfig for all wiimotes
    DeviceManager* device_manager = input_manager->getDeviceManager();
    GamepadConfig* gamepad_config = NULL;

    device_manager->getConfigForGamepad(WIIMOTE_START_IRR_ID, "Wiimote",
                                        &gamepad_config);
    int num_buttons = (int)( log((float)WIIMOTE_BUTTON_ALL) / log((float)2.0f))+1;
    gamepad_config->setNumberOfButtons(num_buttons);
    gamepad_config->setNumberOfAxis(1);

    setWiimoteBindings(gamepad_config);

    // Initialize all Wiimotes, which in turn create their
    // associated GamePadDevices
    for(int i=0 ; i < nb_wiimotes ; i++)
    {
        m_wiimotes.push_back(new Wiimote(m_all_wiimote_handles[i], i,
                                         gamepad_config              ));
    } // end for

    // ---------------------------------------------------
    // Set the LEDs and rumble for 0.2s
    int leds[] = {WIIMOTE_LED_1, WIIMOTE_LED_2, WIIMOTE_LED_3, WIIMOTE_LED_4};
    for(unsigned int i=0 ; i < m_wiimotes.size(); i++)
    {
        wiimote_t*  wiimote_handle = m_wiimotes[i]->getWiimoteHandle();
        wiiuse_set_leds(wiimote_handle, leds[i]);
        wiiuse_rumble(wiimote_handle, 1);
    }

    StkTime::sleep(200);

    for(unsigned int i=0 ; i < m_wiimotes.size(); i++)
    {
        wiimote_t*  wiimote_handle = m_wiimotes[i]->getWiimoteHandle();
        wiiuse_rumble(wiimote_handle, 0);
    }

    // TODO: only enable accelerometer during race
    enableAccelerometer(true);

    // ---------------------------------------------------
    // Launch the update thread
#ifdef WIIMOTE_THREADING
    m_shut = false;
    pthread_create(&m_thread, NULL, &threadFuncWrapper, this);
#endif
}   // launchDetection
Ejemplo n.º 2
0
/**
 * Launch wiimote detection and add the corresponding gamepad devices to the
 * device manager.
 * TODO: this should be done in a separate thread, to not block the UI...
 */
void WiimoteManager::launchDetection(int timeout)
{
    // Stop WiiUse, remove wiimotes, gamepads, gamepad configs.
    cleanup();

    m_all_wiimote_handles =  wiiuse_init(MAX_WIIMOTES);

    // Detect wiimotes
    int nb_found_wiimotes = wiiuse_find(m_all_wiimote_handles, MAX_WIIMOTES, timeout);

    // Couldn't find any wiimote?
    if(nb_found_wiimotes == 0)
        return;

    // Try to connect to all found wiimotes
    int nb_wiimotes = wiiuse_connect(m_all_wiimote_handles, nb_found_wiimotes);

    // Couldn't connect to any wiimote?
    if(nb_wiimotes == 0)
        return;

    // ---------------------------------------------------
    // Create or find a GamepadConfig for all wiimotes
    DeviceManager* device_manager = input_manager->getDeviceList();
    GamepadConfig* gamepad_config = NULL;

    device_manager->getConfigForGamepad(WIIMOTE_START_IRR_ID, "Wiimote",
                                        &gamepad_config);
    int num_buttons = (int)( log((float)WIIMOTE_BUTTON_ALL) / log(2.0f))+1;
    gamepad_config->setNumberOfButtons(num_buttons);
    gamepad_config->setNumberOfAxis(1);

    setWiimoteBindings(gamepad_config);

    // Initialize all Wiimotes, which in turn create their
    // associated GamePadDevices
    for(int i=0 ; i < nb_wiimotes ; i++)
    {
        m_wiimotes.push_back(new Wiimote(m_all_wiimote_handles[i], i,
                                         gamepad_config              ));
    } // end for

    // ---------------------------------------------------
    // Set the LEDs and rumble for 0.2s
    int leds[] = {WIIMOTE_LED_1, WIIMOTE_LED_2, WIIMOTE_LED_3, WIIMOTE_LED_4};
    for(unsigned int i=0 ; i < m_wiimotes.size(); i++)
    {
        wiimote_t*  wiimote_handle = m_wiimotes[i]->getWiimoteHandle();
        wiiuse_set_leds(wiimote_handle, leds[i]);
        wiiuse_rumble(wiimote_handle, 1);
    }

    StkTime::sleep(200);

    for(unsigned int i=0 ; i < m_wiimotes.size(); i++)
    {
        wiimote_t*  wiimote_handle = m_wiimotes[i]->getWiimoteHandle();
        wiiuse_rumble(wiimote_handle, 0);
    }

    // TODO: only enable accelerometer during race
    enableAccelerometer(true);

    // ---------------------------------------------------
    // Launch the update thread
#ifdef WIIMOTE_THREADING
    m_shut = false;
    pthread_create(&m_thread, NULL, &threadFuncWrapper, this);
#endif
}   // launchDetection