/* static */ int vrpn_WiiMote::handle_request_message(void *userdata, vrpn_HANDLERPARAM p) { const char* bufptr = p.buffer; vrpn_int32 chan_num; vrpn_int32 pad; vrpn_float64 value; vrpn_WiiMote* me = (vrpn_WiiMote*)userdata; // Read the parameters from the buffer vrpn_unbuffer(&bufptr, &chan_num); vrpn_unbuffer(&bufptr, &pad); vrpn_unbuffer(&bufptr, &value); // Set the appropriate value, if the channel number is in the // range of the ones we have. if ( (chan_num < 0) || (chan_num >= me->o_num_channel) ) { fprintf(stderr,"vrpn_WiiMote::handle_request_message(): Index out of bounds\n"); char msg[1024]; sprintf( msg, "Error: (handle_request_message): channel %d is not active. Squelching.", chan_num ); me->send_text_message( msg, p.msg_time, vrpn_TEXT_ERROR ); return 0; } me->o_channel[chan_num] = value; if (value >= 0.5) { wiiuse_rumble(me->wiimote->device, 1); } else { wiiuse_rumble(me->wiimote->device, 0); } return 0; }
int connectWiimote(wiimote** wiimotes) { int found = 0; int connected = 0; wiimote* wm; while(!found) { //Look for Wiimotes. Keep searching until one is found. Wiiuse provides no way to search forever, so search for 5 secs and repeat. found = wiiuse_find(wiimotes, MAX_WIIMOTES, 5); } connected = wiiuse_connect(wiimotes, MAX_WIIMOTES); wm = wiimotes[0]; if (connected) { EventClient.SendHELO("Wii Remote", ICON_PNG, NULL); wiiuse_set_leds(wm, WIIMOTE_LED_1); wiiuse_rumble(wm, 1); wiiuse_set_orient_threshold(wm,1); #ifndef WIN32 usleep(200000); #else Sleep(200); #endif wiiuse_rumble(wm, 0); return 1; } return 0; }
void FWiimoteInputDevice::SetRumbleEnabled(int32 ControllerId, bool IsEnabled) { if (GWiimotes[ControllerId] == nullptr) return; if (IsEnabled) { wiiuse_rumble(GWiimotes[ControllerId], 1); } else { wiiuse_rumble(GWiimotes[ControllerId], 0); } }
void InitWiimotes() { printf("==> Initialisation du tableau des wiimotes\n"); WMTable = wiiuse_init(NB_WIIMOTES); printf("\n==> Recherche des wiimotes...\n"); int nFound = wiiuse_find(WMTable, NB_WIIMOTES, 5); printf("...%d wiimotes trouvees\n", nFound); printf("\n==> Connection des wiimotes...\n"); nConnectedWM = wiiuse_connect(WMTable, nFound); printf("...%d wiimotes connectees\n", nConnectedWM); wiiuse_set_timeout(WMTable, nConnectedWM, 0xFF, 0xFF); for(int n=0 ; n<nConnectedWM; n++) { wiiuse_set_ir(WMTable[n],1); wiiuse_rumble(WMTable[n], true); } //Signal lumineux for(int n=0 ; n<4 ; n++) { int nPow = 1; for(int i=0 ; i<n ; i++) nPow*=2; for(int i=0 ; i<nConnectedWM ; i++) { wiiuse_set_leds(WMTable[i], nPow*16); if(n==0)wiiuse_rumble(WMTable[i], false); } Sleep(100); } for(int n=0 ; n<nConnectedWM ; n++) { wiiuse_set_leds(WMTable[n], 0x90); } }
/** * @brief Toggle the state of the rumble. * * @param wm Pointer to a wiimote_t structure. */ void wiiuse_toggle_rumble(struct wiimote_t* wm) { if (!wm) { return; } wiiuse_rumble(wm, !WIIMOTE_IS_SET(wm, WIIMOTE_STATE_RUMBLE)); }
/* static */ int vrpn_WiiMote::handle_request_channels_message(void* userdata, vrpn_HANDLERPARAM p) { const char* bufptr = p.buffer; vrpn_int32 num; vrpn_int32 pad; vrpn_WiiMote* me = (vrpn_WiiMote*)userdata; // Read the values from the buffer vrpn_unbuffer(&bufptr, &num); vrpn_unbuffer(&bufptr, &pad); if (num > me->o_num_channel) { char msg[1024]; sprintf( msg, "Error: (handle_request_channels_message): channels above %d not active; " "bad request up to channel %d. Squelching.", me->o_num_channel, num ); me->send_text_message( msg, p.msg_time, vrpn_TEXT_ERROR ); num = me->o_num_channel; } if (num < 0) { char msg[1024]; sprintf( msg, "Error: (handle_request_channels_message): invalid channel %d. Squelching.", num ); me->send_text_message( msg, p.msg_time, vrpn_TEXT_ERROR ); return 0; } // Pull only channel 0 from the buffer, no matter how many values we received. vrpn_float64 value; vrpn_unbuffer(&bufptr, &value); if (value >= 0.5) { wiiuse_rumble(me->wiimote->device, 1); } else { wiiuse_rumble(me->wiimote->device, 0); } return 0; }
void wiimote_link(int time) { // time in s to find wiimotes int found, connected; printf("Looking for wiimotes...(%d s)\n", time); wiimotes = wiiuse_init(MAX_WIIMOTES); found = wiiuse_find(wiimotes, MAX_WIIMOTES, time); if (!found) { printf("No wiimotes found.\n"); } connected = wiiuse_connect(wiimotes, MAX_WIIMOTES); if (connected) { printf("Connected to %i wiimotes (of %i found).\n", connected, found); } else { printf("Failed to connect to any wiimote.\n"); } //printf("connnected: %d, found: %d\n", connected, found); wiiuse_set_leds(wiimotes[0], WIIMOTE_LED_1); wiiuse_set_leds(wiimotes[1], WIIMOTE_LED_2); wiiuse_set_leds(wiimotes[2], WIIMOTE_LED_3); wiiuse_set_leds(wiimotes[3], WIIMOTE_LED_4); wiiuse_rumble(wiimotes[0], 1); wiiuse_rumble(wiimotes[1], 1); wiiuse_rumble(wiimotes[2], 1); wiiuse_rumble(wiimotes[3], 1); SDL_Delay(200); wiiuse_rumble(wiimotes[0], 0); wiiuse_rumble(wiimotes[1], 0); wiiuse_rumble(wiimotes[2], 0); wiiuse_rumble(wiimotes[3], 0); }
int32_t wii_send(CSOUND *csound, WIIMOTES *p) { wiimote **wii = p->wii; int32_t num = (int32_t)*p->num; if (UNLIKELY(!(wii[num]->state & WIIMOTE_STATE_CONNECTED))) return csound->PerfError(csound, &(p->h), "%s", Str("Not open")); switch ((int32_t)(*p->kControl+FL(0.5))) { /* case 1: */ /* wii->mode.acc = (int32_t)*p->kValue; */ /* break; */ /* case 2: */ /* wii->mode.ir = (int32_t)*p->kValue; */ /* break; */ case WII_RUMBLE: wiiuse_rumble(wii[num], (int32_t)*p->kValue); break; case WII_SET_LEDS: wiiuse_set_leds(wii[num], ((uint16)*p->kValue)<<4); break; } return OK; }
void vrpn_WiiMote::initialize_wiimote_state(void) { if ( !wiimote->device || !wiimote->found || !wiimote->connected) { return; } // Turn on a light so we know which device we are. switch (wiimote->which) { case 0: wiiuse_set_leds(wiimote->device, WIIMOTE_LED_1); break; case 1: wiiuse_set_leds(wiimote->device, WIIMOTE_LED_2); break; case 2: wiiuse_set_leds(wiimote->device, WIIMOTE_LED_3); break; case 3: wiiuse_set_leds(wiimote->device, WIIMOTE_LED_4); break; default: struct timeval now; vrpn_gettimeofday(&now, NULL); char msg[1024]; sprintf(msg, "Too-large remote %d (0-3 available)", wiimote->which); send_text_message(msg, now, vrpn_TEXT_ERROR); break; } // Ask to look for motion sensing wiiuse_motion_sensing(wiimote->device, 1); // Turn off rumbling wiiuse_rumble(wiimote->device, 0); // Turn on IR sensing wiiuse_set_ir(wiimote->device, 1); }
/** * Try to connect to 2 wiimotes. * Make them rumble to show they are connected. * @param nbConnects number of connections maximum. * @return 0 if there is an error otherwise it returns * the number of wiimotes connected.. */ JNIEXPORT jint JNICALL Java_wiiusej_WiiUseApi_doConnections (JNIEnv *env, jobject obj, jint nbConnects) { /* variables declarations */ int found, connected, i; short leds; nbMaxWiiMotes = nbConnects; /* initialize wiimotes array with the maximum number of wiimotes */ wiimotes = wiiuse_init(nbMaxWiiMotes); /* * Find wiimote devices * Now we need to find some wiimotes. * Give the function the wiimote array we created, and tell it there * are 2 wiimotes we are interested in. * Set the timeout to be 5 seconds. * This will return the number of actual wiimotes that are in discovery mode. */ found = wiiuse_find(wiimotes, nbMaxWiiMotes, 5); if (!found) return 0; /* * Connect to the wiimotes * Now that we found some wiimotes, connect to them. * Give the function the wiimote array and the number of wiimote devices we found. * This will return the number of established connections to the found wiimotes. */ connected = wiiuse_connect(wiimotes, nbMaxWiiMotes); if (!connected) return 0; //no problems during connection show that wiimotes are connected /* * Now set the LEDs and rumble for a second so it's easy * to tell which wiimotes are connected (just like the wii does). */ for (i=0;i<nbMaxWiiMotes;i++) { leds = 0; if (i%4==0) leds |= WIIMOTE_LED_1; else if (i%4==1) leds |= WIIMOTE_LED_2; else if (i%4==2) leds |= WIIMOTE_LED_3; else if (i%4==3) leds |= WIIMOTE_LED_4; wiiuse_set_leds(wiimotes[i], leds); wiiuse_rumble(wiimotes[i], 1); } #ifndef WIN32 usleep(200000); #else Sleep(200); #endif for (i=0;i<nbMaxWiiMotes;i++) { wiiuse_rumble(wiimotes[i], 0); } //no pb connecting leave return connected; }
/** * Deactivate rumble for the wiimote with the given id. * @param id the id of the wiimote.Must be 1 or 2. */ JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_deactivateRumble (JNIEnv *env, jobject obj, jint id) { wiiuse_rumble(wiimotes[id-1], 0); }
int main(int argc, char** argv) { #else int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { #endif int found, connected; int wm; //printf("wiiuse version = %s\n", wiiuse_version()); wiimotes = wiiuse_init(MAX_WIIMOTES); found = wiiuse_find(wiimotes, MAX_WIIMOTES, 5); if (!found) return 0; connected = wiiuse_connect(wiimotes, MAX_WIIMOTES); if (connected) printf("Connected to %i wiimotes (of %i found).\n", connected, found); else { printf("Failed to connect to any wiimote.\n"); return 0; } wiiuse_set_leds(wiimotes[0], WIIMOTE_LED_1 | WIIMOTE_LED_4); wiiuse_set_leds(wiimotes[1], WIIMOTE_LED_2 | WIIMOTE_LED_4); wiiuse_rumble(wiimotes[0], 1); #ifndef WIN32 usleep(200000); #else Sleep(200); #endif wiiuse_rumble(wiimotes[0], 0); /* enable IR and motion sensing for all wiimotes */ for (wm = 0; wm < MAX_WIIMOTES; ++wm) { wiiuse_motion_sensing(wiimotes[wm], 1); wiiuse_set_ir(wiimotes[wm], 1); } if (SDL_Init(SDL_INIT_VIDEO) < 0) { printf("Failed to initialize SDL: %s\n", SDL_GetError()); return 0; } SDL_WM_SetCaption("wiiuse SDL IR Example", "wiiuse SDL IR Example"); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); /* set window size */ width = wiimotes[0]->ir.vres[0]; height = wiimotes[0]->ir.vres[1]; SDL_SetVideoMode(width, height, 16, SDL_RESIZABLE | SDL_OPENGL); for (wm = 0; wm < MAX_WIIMOTES; ++wm) wiiuse_set_ir_vres(wiimotes[wm], width, height); /* set OpenGL stuff */ glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_NORMALIZE); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDepthFunc(GL_LEQUAL); glClearColor(0, 0, 0, 0); /* set the size of the window */ resize_window(width, height); display(); #ifdef WIN32 last_render = GetTickCount(); #endif while (1) { SDL_Event event; if (SDL_PollEvent(&event)) { switch (event.type) { case SDL_VIDEORESIZE: { /* resize the window */ resize_window(event.resize.w, event.resize.h); break; } case SDL_QUIT: { /* shutdown */ SDL_Quit(); wiiuse_cleanup(wiimotes, MAX_WIIMOTES); return 0; } default: { } } } if (wiiuse_poll(wiimotes, MAX_WIIMOTES)) { /* * This happens if something happened on any wiimote. * So go through each one and check if anything happened. */ int i = 0; for (; i < MAX_WIIMOTES; ++i) { switch (wiimotes[i]->event) { case WIIUSE_EVENT: /* a generic event occured */ handle_event(wiimotes[i]); break; default: break; } } } display(); } }
/** * @brief main() * * Connect to up to two wiimotes and print any events * that occur on either device. */ int main(int argc, char** argv) { wiimote** wiimotes; int found, connected; /* * Initialize an array of wiimote objects. * * The parameter is the number of wiimotes I want to create. */ wiimotes = wiiuse_init(MAX_WIIMOTES); /* * Find wiimote devices * * Now we need to find some wiimotes. * Give the function the wiimote array we created, and tell it there * are MAX_WIIMOTES wiimotes we are interested in. * * Set the timeout to be 5 seconds. * * This will return the number of actual wiimotes that are in discovery mode. */ found = wiiuse_find(wiimotes, MAX_WIIMOTES, 5); if (!found) { printf("No wiimotes found.\n"); return 0; } /* * Connect to the wiimotes * * Now that we found some wiimotes, connect to them. * Give the function the wiimote array and the number * of wiimote devices we found. * * This will return the number of established connections to the found wiimotes. */ connected = wiiuse_connect(wiimotes, MAX_WIIMOTES); if (connected) { printf("Connected to %i wiimotes (of %i found).\n", connected, found); } else { printf("Failed to connect to any wiimote.\n"); return 0; } /* * Now set the LEDs and rumble for a second so it's easy * to tell which wiimotes are connected (just like the wii does). */ wiiuse_set_leds(wiimotes[0], WIIMOTE_LED_1); wiiuse_set_leds(wiimotes[1], WIIMOTE_LED_2); wiiuse_set_leds(wiimotes[2], WIIMOTE_LED_3); wiiuse_set_leds(wiimotes[3], WIIMOTE_LED_4); wiiuse_rumble(wiimotes[0], 1); wiiuse_rumble(wiimotes[1], 1); #ifndef WIIUSE_WIN32 usleep(200000); #else Sleep(200); #endif wiiuse_rumble(wiimotes[0], 0); wiiuse_rumble(wiimotes[1], 0); printf("\nControls:\n"); printf("\tB toggles rumble.\n"); printf("\t+ to start Wiimote accelerometer reporting, - to stop\n"); printf("\tUP to start IR camera (sensor bar mode), DOWN to stop.\n"); printf("\t1 to start Motion+ reporting, 2 to stop.\n"); printf("\n\n"); /* * Maybe I'm interested in the battery power of the 0th * wiimote. This should be WIIMOTE_ID_1 but to be sure * you can get the wiimote associated with WIIMOTE_ID_1 * using the wiiuse_get_by_id() function. * * A status request will return other things too, like * if any expansions are plugged into the wiimote or * what LEDs are lit. */ /* wiiuse_status(wiimotes[0]); */ /* * This is the main loop * * wiiuse_poll() needs to be called with the wiimote array * and the number of wiimote structures in that array * (it doesn't matter if some of those wiimotes are not used * or are not connected). * * This function will set the event flag for each wiimote * when the wiimote has things to report. */ while (any_wiimote_connected(wiimotes, MAX_WIIMOTES)) { if (wiiuse_poll(wiimotes, MAX_WIIMOTES)) { /* * This happens if something happened on any wiimote. * So go through each one and check if anything happened. */ int i = 0; for (; i < MAX_WIIMOTES; ++i) { switch (wiimotes[i]->event) { case WIIUSE_EVENT: /* a generic event occurred */ handle_event(wiimotes[i]); break; case WIIUSE_STATUS: /* a status event occurred */ handle_ctrl_status(wiimotes[i]); break; case WIIUSE_DISCONNECT: case WIIUSE_UNEXPECTED_DISCONNECT: /* the wiimote disconnected */ handle_disconnect(wiimotes[i]); break; case WIIUSE_READ_DATA: /* * Data we requested to read was returned. * Take a look at wiimotes[i]->read_req * for the data. */ break; case WIIUSE_NUNCHUK_INSERTED: /* * a nunchuk was inserted * This is a good place to set any nunchuk specific * threshold values. By default they are the same * as the wiimote. */ /* wiiuse_set_nunchuk_orient_threshold((struct nunchuk_t*)&wiimotes[i]->exp.nunchuk, 90.0f); */ /* wiiuse_set_nunchuk_accel_threshold((struct nunchuk_t*)&wiimotes[i]->exp.nunchuk, 100); */ printf("Nunchuk inserted.\n"); break; case WIIUSE_CLASSIC_CTRL_INSERTED: printf("Classic controller inserted.\n"); break; case WIIUSE_WII_BOARD_CTRL_INSERTED: printf("Balance board controller inserted.\n"); break; case WIIUSE_GUITAR_HERO_3_CTRL_INSERTED: /* some expansion was inserted */ handle_ctrl_status(wiimotes[i]); printf("Guitar Hero 3 controller inserted.\n"); break; case WIIUSE_MOTION_PLUS_ACTIVATED: printf("Motion+ was activated\n"); break; case WIIUSE_NUNCHUK_REMOVED: case WIIUSE_CLASSIC_CTRL_REMOVED: case WIIUSE_GUITAR_HERO_3_CTRL_REMOVED: case WIIUSE_WII_BOARD_CTRL_REMOVED: case WIIUSE_MOTION_PLUS_REMOVED: /* some expansion was removed */ handle_ctrl_status(wiimotes[i]); printf("An expansion was removed.\n"); break; default: break; } } } } /* * Disconnect the wiimotes */ wiiuse_cleanup(wiimotes, MAX_WIIMOTES); return 0; }
// id = {1 2 3 4}, state = {0 1} void wiimote_set_rumble(int id, int state) { assert(id >= 1 && id <= MAX_WIIMOTES+1 && (state == 0 || state == 1)); wiiuse_rumble(wiimotes[id-1], state); }
/** * Deactivate rumble for the wiimote with the given id. * @param id the id of the wiimote.Must be 1 or 2. */ JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_deactivateRumble (JNIEnv *env, jobject obj, jint id) { wiiuse_rumble(wiiuse_get_by_id(wiimotes, nbMaxWiimotes, id), 0); }
/** * Try to connect to 2 wiimotes. * Make them rumble to show they are connected. * * @return 0 if there is an error otherwise it returns * the number of wiimotes connected.. */ JNIEXPORT jint JNICALL Java_wiiusej_WiiUseApi_doConnections (JNIEnv *env, jobject obj) { /* variables declarations */ int found, connected; /* * Initialize an array of wiimote objects. * * The first parameter is the number of wiimotes * I want to create. I only have two wiimotes * so I'm limiting the test to just 2. * * Then I get it the array of ids and a couple * callback functions to invoke when something * happens on one of the wiimotes. * * handle_event gets called when a generic event occurs (button press, motion sensing, etc) * handle_ctrl_status gets called when a response to a status request arrives (battery power, etc) * handle_disconnect gets called when the wiimote disconnect (holding power button) */ wiimotes = wiiuse_init(2, ids, handle_event, handle_ctrl_status, handle_disconnect); /* * Find wiimote devices * Now we need to find some wiimotes. * Give the function the wiimote array we created, and tell it there * are 2 wiimotes we are interested in. * Set the timeout to be 5 seconds. * This will return the number of actual wiimotes that are in discovery mode. */ found = wiiuse_find(wiimotes, 2, 5); if (!found) return 0; /* * Connect to the wiimotes * Now that we found some wiimotes, connect to them. * Give the function the wiimote array and the number of wiimote devices we found. * This will return the number of established connections to the found wiimotes. */ connected = wiiuse_connect(wiimotes, 2); if (!connected) return 0; //no problems during connection show that wiimotes are connected /* * Now set the LEDs and rumble for a second so it's easy * to tell which wiimotes are connected (just like the wii does). */ wiiuse_set_leds(wiimotes[0], WIIMOTE_LED_1); wiiuse_set_leds(wiimotes[1], WIIMOTE_LED_2); wiiuse_rumble(wiimotes[0], 1); wiiuse_rumble(wiimotes[1], 1); #ifndef WIN32 usleep(200000); #else Sleep(200); #endif wiiuse_rumble(wiimotes[0], 0); wiiuse_rumble(wiimotes[1], 0); //no pb connecting leave return connected; }
void controllerClass::calibrate() { Sint16 x=0; if(SDL_JoystickOpened(0)) { x = SDL_JoystickGetAxis(joystick, 0); if(SDL_JoystickGetButton(joystick, 0)) { if(!joyBtnALock && var.menuJoyCalStage != 5) { var.menuJoyCalStage++; joyBtnALock=1; } } else { joyBtnALock=0; } } switch(var.menuJoyCalStage) { case 0: calMin=0; calMax=0; calLowJitter=0; calHighJitter=0; break; case 1: if(x < calLowJitter) calLowJitter=x; else if(x > calHighJitter) calHighJitter=x; break; case 2: if(x < calMin) calMin=x; break; case 3: if(x > calMax) calMax=x; break; case 4: setting.JoyCalMin=calMin; setting.JoyCalMax=calMax; setting.JoyCalLowJitter=calLowJitter; setting.JoyCalHighJitter=calHighJitter; cout << "Joystick calibration report:" << endl; cout << "calMin: " << calMin << endl << "calMax: " << calMax << endl; cout << "lowJit: " << calLowJitter << endl << "higJit: " << calHighJitter << endl; var.menuJoyCalStage++; writeSettings(); break; #ifdef WITH_WIIUSE case -1: //We do this to make it draw a frame before freezing (searching) var.menuJoyCalStage--; break; case -2: wiimotes = wiiuse_init(MAX_WIIMOTES); if(wiiuse_find(wiimotes, MAX_WIIMOTES, 4)) { var.wiiConnect=1; } else { var.wiiConnect=0; } if(var.wiiConnect) { var.wiiConnect=0; if(wiiuse_connect(wiimotes, MAX_WIIMOTES)) { var.wiiConnect=1; wiiuse_set_leds(wiimotes[0], WIIMOTE_LED_1); wiiuse_rumble(wiimotes[0], 1); usleep(500000); wiiuse_rumble(wiimotes[0], 0); wiiuse_motion_sensing(wiimotes[0], 1); } var.menuJoyCalStage=-3; } else { var.menuJoyCalStage=-4; } break; #endif } }
void IRThread::initialize() { // wiiuse setup /* * Initialize an array of wiimote objects. * * The parameter is the number of wiimotes I want to create. */ wiimotes = wiiuse_init(MAX_WIIMOTES); /* * Find wiimote devices * * Now we need to find some wiimotes. * Give the function the wiimote array we created, and tell it there * are MAX_WIIMOTES wiimotes we are interested in. * * Set the timeout to be 5 seconds. * * This will return the number of actual wiimotes that are in discovery mode. */ found = wiiuse_find(wiimotes, MAX_WIIMOTES, 5); if (!found) { printf ("No wiimotes found."); exit(-1); } /* * Connect to the wiimotes * * Now that we found some wiimotes, connect to them. * Give the function the wiimote array and the number * of wiimote devices we found. * * This will return the number of established connections to the found wiimotes. */ isConnected = wiiuse_connect(wiimotes, MAX_WIIMOTES); if (isConnected) { printf("isConnected to %i wiimotes (of %i found).\n", isConnected, found); emit connected(); } else { printf("Failed to connect to any wiimote.\n"); return; } /* * Now set the LEDs and rumble for a second so it's easy * to tell which wiimotes are connected (just like the wii does). */ wiiuse_set_leds(wiimotes[0], WIIMOTE_LED_1); wiiuse_rumble(wiimotes[0], 1); #ifndef WIN32 usleep(200000); #else Sleep(200); #endif wiiuse_rumble(wiimotes[0], 0); wiiuse_set_ir(wiimotes[0], 1); // enable motion sensing to get the IR point nonvisible transition events wiiuse_motion_sensing(wiimotes[0], 1); //wiiuse_set_ir_sensitivity(wiimotes[0], 2); for(int i = 0; i < 4; i++) { previousPoints[i].setX(0); previousPoints[i].setY(0); irpoints[i].setX(0); irpoints[i].setY(0); } previous[0] = false; }
/** * 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
void Wiimote::rumble(int status) { wiiuse_rumble(this->wm, status); }
int main(int argc, char** argv) { wiimote** wiimotes; int found, connected; // init printf("[INFO] Looking for wiimotes (5 seconds)...\n"); wiimotes = wiiuse_init(1); // find wii (wait for 5 seconds) found = wiiuse_find(wiimotes, 1, 5); if (!found) { printf ("[INFO] No wiimotes found.\n"); return 0; } // connect connected = wiiuse_connect(wiimotes, 1); if (connected) printf("[INFO] Connected to %i wiimotes (of %i found).\n", connected, found); else { printf("[ERROR] Failed to connect to any wiimote.\n"); return 0; } // rumble and set leds wiiuse_set_leds(wiimotes[0], WIIMOTE_LED_1); wiiuse_rumble(wiimotes[0], 1); usleep(200000); wiiuse_rumble(wiimotes[0], 0); //set up socket to Java code server_type = SERVER_TYPE_SERIAL; int fd = wait_on_socket(); char write_buffer[128]; int button=0; currently_training = 0; // continuously poll wiimote and handle events while (1) { if (wiiuse_poll(wiimotes, 1)) { switch (wiimotes[0]->event) { case WIIUSE_EVENT: button = handle_event(wiimotes[0]); break; case WIIUSE_DISCONNECT: case WIIUSE_UNEXPECTED_DISCONNECT: goto exit; break; default: break; } } usleep(50); // tell Java code info about the wiimote sprintf (write_buffer, "%d:%d\n", currently_training, button); writen(fd, write_buffer, strlen(write_buffer)); } exit: wiiuse_cleanup(wiimotes, 1); close(fd); return 0; }
//int main(int argc, char** argv) { int _tmain(int argc, _TCHAR* argv[]) { wiimote** wiimotes; int found, connected; /* * Initialize an array of wiimote objects. * * The parameter is the number of wiimotes I want to create. */ wiimotes = wiiuse_init(MAX_WIIMOTES); /* * Find wiimote devices * * Now we need to find some wiimotes. * Give the function the wiimote array we created, and tell it there * are MAX_WIIMOTES wiimotes we are interested in. * * Set the timeout to be 5 seconds. * * This will return the number of actual wiimotes that are in discovery mode. */ found = wiiuse_find(wiimotes, MAX_WIIMOTES, 5); if (!found) { printf ("No wiimotes found."); return 0; } /* * Connect to the wiimotes * * Now that we found some wiimotes, connect to them. * Give the function the wiimote array and the number * of wiimote devices we found. * * This will return the number of established connections to the found wiimotes. */ connected = wiiuse_connect(wiimotes, MAX_WIIMOTES); if (connected) printf("Connected to %i wiimotes (of %i found).\n", connected, found); else { printf("Failed to connect to any wiimote.\n"); return 0; } /* * Now set the LEDs and rumble for a second so it's easy * to tell which wiimotes are connected (just like the wii does). */ wiiuse_set_leds(wiimotes[0], WIIMOTE_LED_1); wiiuse_set_leds(wiimotes[1], WIIMOTE_LED_2); wiiuse_set_leds(wiimotes[2], WIIMOTE_LED_3); wiiuse_set_leds(wiimotes[3], WIIMOTE_LED_4); wiiuse_rumble(wiimotes[0], 1); wiiuse_rumble(wiimotes[1], 1); #ifndef WIN32 usleep(200000); #else Sleep(200); #endif wiiuse_rumble(wiimotes[0], 0); wiiuse_rumble(wiimotes[1], 0); /* * Maybe I'm interested in the battery power of the 0th * wiimote. This should be WIIMOTE_ID_1 but to be sure * you can get the wiimote assoicated with WIIMOTE_ID_1 * using the wiiuse_get_by_id() function. * * A status request will return other things too, like * if any expansions are plugged into the wiimote or * what LEDs are lit. */ //wiiuse_status(wiimotes[0]); /* * This is the main loop * * wiiuse_poll() needs to be called with the wiimote array * and the number of wiimote structures in that array * (it doesn't matter if some of those wiimotes are not used * or are not connected). * * This function will invoke the callbacks set in wiiuse_init() * when the wiimote has things to report. */ while (1) { if (wiiuse_poll(wiimotes, MAX_WIIMOTES)) { /* * This happens if something happened on any wiimote. * So go through each one and check if anything happened. */ int i = 0; for (; i < MAX_WIIMOTES; ++i) { switch (wiimotes[i]->event) { case WIIUSE_EVENT: /* a generic event occured */ handle_event(wiimotes[i]); break; case WIIUSE_STATUS: /* a status event occured */ handle_ctrl_status(wiimotes[i]); break; case WIIUSE_DISCONNECT: /* the wiimote disconnected */ handle_disconnect(wiimotes[i]); break; case WIIUSE_NUNCHUK_INSERTED: /* * a nunchuk was inserted * This is a good place to set and nunchuk specific * threshold values. By default they are the same * as the wiimote. */ //wiiuse_set_nunchuk_orient_threshold((struct nunchuk_t*)&wiimotes[i]->exp.nunchuk, 90.0f); //wiiuse_set_nunchuk_accel_threshold((struct nunchuk_t*)&wiimotes[i]->exp.nunchuk, 100); case WIIUSE_CLASSIC_CTRL_INSERTED: case WIIUSE_GUITAR_HERO_3_CTRL_INSERTED: /* some expansion was inserted */ handle_ctrl_status(wiimotes[i]); printf("An expansion was inserted.\n"); break; case WIIUSE_NUNCHUK_REMOVED: case WIIUSE_CLASSIC_CTRL_REMOVED: case WIIUSE_GUITAR_HERO_3_CTRL_REMOVED: /* some expansion was removed */ handle_ctrl_status(wiimotes[i]); printf("An expansion was removed.\n"); break; default: break; } } } } /* * Disconnect the wiimotes */ wiiuse_cleanup(wiimotes, MAX_WIIMOTES); return 0; return 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