int main(){ //wiringPi setup-------------------------------------------------------------- wiringPiSetup(); Shield gpio; bool bSpin = true; bool bReconnect = false; //wiimote synack------------------------------------------------------------- std::vector<CWiimote>& wiimotes = wii.FindAndConnect(1); while(!wiimotes.size()) { printf("%sAttempting to connect to wiimote...\n", HEADER.c_str()); wiimotes = wii.FindAndConnect(1); gpio.BlinkOnce(); delay(200); } gpio.Glow(); CWiimote& wiimote = wiimotes[0]; wiimote.SetLEDs(CWiimote::LED_1); printf("\n***********************************************************************\n"); printf("%sBeginning Control sequence:\n\n\n", HEADER.c_str()); int count = 1; do{ if(bReconnect){ // Regenerate the list of wiimotes printf("%sUnexpected disconnect. Attempting to reconnect...\n", HEADER.c_str()); wiimotes = wii.GetWiimotes(); bReconnect = false; } bool sync = false; if(wii.Poll()){ CWiimote & wiimote = *wiimotes.begin(); switch(wiimote.GetEvent()){ case CWiimote::EVENT_EVENT: bSpin = HandleInput(wiimote, gpio, count); count++; break; case CWiimote::EVENT_DISCONNECT: case CWiimote::EVENT_UNEXPECTED_DISCONNECT: bReconnect = true; break; default: break; } } } while(wiimotes.size() && bSpin); printf("\n***********************************************************************\n"); printf("%sExiting Application.\n", HEADER.c_str()); digitalWrite(0, LOW); //Shield destructor automatically kills pins! return 0; }
int main(int argc, char** argv) { CWii wii; // Defaults to 4 remotes std::vector<CWiimote>::iterator i; int reloadWiimotes = 0; int index; // Find and connect to the wiimotes std::vector<CWiimote>& wiimotes = wii.FindAndConnect(); if (!wiimotes.size()) { cout << "No wiimotes found." << endl; return 0; } // Setup the wiimotes for(index = 0, i = wiimotes.begin(); i != wiimotes.end(); ++i, ++index) { // Use a reference to make working with the iterator handy. CWiimote & wiimote = *i; //Set Leds wiimote.SetLEDs(LED_MAP[index]); } cout << "\nPress PLUS (MINUS) to enable (disable) Motion Sensing Report (only accelerometers)" << endl; cout << "Press RIGHT (LEFT) to enable (disable) Motion Plus (requires Motion Sensing enabled)" << endl; cout << "Press UP (DOWN) to enable (disable) IR camera (requires some IR led)" << endl; do { if(reloadWiimotes) { // Regenerate the list of wiimotes wiimotes = wii.GetWiimotes(); reloadWiimotes = 0; } //Poll the wiimotes to get the status like pitch or roll if(wii.Poll()) { for(i = wiimotes.begin(); i != wiimotes.end(); ++i) { // Use a reference to make working with the iterator handy. CWiimote & wiimote = *i; 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_MOTION_PLUS_INSERTED: cout << "Motion Plus inserted." << endl; break; case CWiimote::EVENT_BALANCE_BOARD_INSERTED: cout << "Balance Board connected.\n" << endl; break; case CWiimote::EVENT_BALANCE_BOARD_REMOVED: cout << "Balance Board disconnected.\n" << endl; break; case CWiimote::EVENT_NUNCHUK_REMOVED: case CWiimote::EVENT_CLASSIC_CTRL_REMOVED: case CWiimote::EVENT_GUITAR_HERO_3_CTRL_REMOVED: case CWiimote::EVENT_MOTION_PLUS_REMOVED: cout << "An expansion was removed." << endl; HandleStatus(wiimote); reloadWiimotes = 1; break; default: break; } } } } while(wiimotes.size()); // Go so long as there are wiimotes left to poll return 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; }