示例#1
0
  /**
    * Reduce the number of data to output. Inform if the data is interesting or not.
    * @return TRUE if data is newed ; FALSE if data is useless.
    */
  bool WmDevice::reduceSentData( CWiimote &wm )
  {
    bool hasChanged=false ;
    int timeOut=0 ;

    // If( current state != previous state )
      // Save & send.
    // else
      // Wait 10 ms before to send data.

    m_t2 = m_time.elapsed() ;
    timeOut = m_t2 - m_t1 ;

    // TimeOut ?
    if( timeOut > PLUGIN_WM_TIMEOUT_BEFORE_SENDDATA )
      hasChanged = true ;

    if( !hasChanged )
    { // Action Wiimote ?
      
      if( wm.GetEvent()!=CWiimote::EVENT_NONE 
          && (wm.GetEvent()!=m_previousEvent || wm.Buttons.isJustChanged()) )
        hasChanged = true ;
    }

    if( !hasChanged )
    { // Action Nunchuk ?

      int exType = wm.ExpansionDevice.GetType();
      if( exType == wm.ExpansionDevice.TYPE_NUNCHUK ) 
      {
        if( wm.ExpansionDevice.Nunchuk.Buttons.isJustChanged() )
          hasChanged = true ;
      }
    }

    // Has changed ?
    if( hasChanged )
    { // YES, update data.
    
      m_t1 = m_t2 ;
      m_previousEvent = wm.GetEvent() ;
      return true ;
    }
    else // NO, no update.
      return false ;
  }
示例#2
0
int main(int argc, char** argv)
{

  //std::vector<CIRDot*> a ;

  //a.clear() ;

    CWii wii; // Defaults to 4 remotes
    
    int reloadWiimotes=0 ;
    int numFound=0 ;
    
    traveledDistance = 0 ;

    //Find the wiimote & search for up to five seconds.
    printf("Searching for wiimotes... Turn them on!\n");
    numFound = wii.Find(5);
    printf("Found %d wiimotes\n", numFound);

    // Under Windows, even if a Wiimote is not connected but it can appear
    // in "Devices and printers", so it connects it ...
    if( numFound <= 0 )
      return 0 ;


    // Connect to the wiimote
    printf( "Connecting to wiimotes...\n" ) ;
    std::vector<CWiimote*>& wiimotes = wii.Connect() ;
    printf( "Connected to %d wiimotes\n", (int)wiimotes.size() ) ;

    if( (int) wiimotes.size() <= 0 )
      return 0 ;


    // Setup the wiimotes
    int index=0 ;
    for( size_t i = 0; i < wiimotes.size() ; ++i )
    {
        // Use a reference to make working with the iterator handy.
        CWiimote *wiimote = wiimotes[i];

        //Set Leds
        wiimote->SetLEDs(LED_MAP[++index]);

        // Continuous information.
        //wiimote->SetFlags( CWiimote::FLAG_CONTINUOUS, 0x0 ) ;

        //Rumble for 0.2 seconds as a connection ack
        wiimote->SetRumbleMode(CWiimote::ON);

        #ifndef WIN32
        usleep(200000);
        #else
        Sleep(200);
        #endif

        wiimote->SetRumbleMode(CWiimote::OFF);
    }

    while( wiimotes.size() ) // Go so long as there are wiimotes left to poll
    {
        if( reloadWiimotes )
        {
            // Regenerate the list of wiimotes
            wiimotes = wii.GetWiimotes();
            reloadWiimotes = 0;
        }

        
        // (My) Bad idea.
        // The data are received in continuous when the accelerometer values are on.
        // So the received data is late ... Worse, latency appears.
        #ifndef WIN32
        //usleep(200000);
        #else
        //Sleep(20);
        #endif
        
        //Poll the wiimotes to get the status like pitch or roll
        if( wii.Poll() )
        {
            for( size_t i = 0; i < wiimotes.size() ; ++i )
            {
                // Use a reference to make working with the iterator handy.
				        CWiimote *wiimote = wiimotes[i];
                CWiimoteData *wm=wiimote->copyData() ;
                switch(wiimote->GetEvent())
                {
                    case CWiimote::EVENT_EVENT:
                        HandleEvent(*wiimote);
                        break;

                    case CWiimote::EVENT_STATUS:
                        HandleStatus(*wiimote);
                        break;
                    case CWiimote::EVENT_DISCONNECT:
                    case CWiimote::EVENT_UNEXPECTED_DISCONNECT:
                        HandleDisconnect(*wiimote);
                        reloadWiimotes = 1;
                        break;

                    case CWiimote::EVENT_READ_DATA:
                        HandleReadData(*wiimote);
                        break;
                    case CWiimote::EVENT_NUNCHUK_INSERTED:
                        HandleNunchukInserted(*wiimote);
                        reloadWiimotes = 1;
                        break;
                    case CWiimote::EVENT_CLASSIC_CTRL_INSERTED:
                        HandleClassicInserted(*wiimote);
                        reloadWiimotes = 1;
                        break;
                    case CWiimote::EVENT_GUITAR_HERO_3_CTRL_INSERTED:
                        HandleGH3Inserted(*wiimote);
                        reloadWiimotes = 1;
                        break;
                    case CWiimote::EVENT_NUNCHUK_REMOVED:
                    case CWiimote::EVENT_CLASSIC_CTRL_REMOVED:
                    case CWiimote::EVENT_GUITAR_HERO_3_CTRL_REMOVED:
                        printf("An expansion was removed.\n");
                        HandleStatus(*wiimote);
                        reloadWiimotes = 1;
                        break;
                    default:
                        break;
                }
            }
        }
    }

    return 0;
}