Exemplo n.º 1
0
int find_wiimotes(void)
{
	if(wiimotes == NULL)
	{
		wiimotes = wiiuse_init(4);
	}

	int found = wiiuse_find(wiimotes+wiimotes_connected, 4-wiimotes_connected, 5);
	if(!found)
		return 0;

	SDL_mutexP(wii_lock);

	int connected = wiiuse_connect(wiimotes+wiimotes_connected, 4-wiimotes_connected);
	if(connected>0)
	{
		wiimotes_connected+=connected;
		int i = 0;
		for(;i<wiimotes_connected;i++)
		{
			wiiuse_set_leds(wiimotes[i], WIIMOTE_LED_1<<i);
		}
	}

	SDL_mutexV(wii_lock);
}
Exemplo n.º 2
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;
}
Exemplo n.º 3
0
int32_t wiimote_find(CSOUND *csound, WIIMOTE *p)
{
    int32_t n, i;
    wiimote **wiimotes;
    wiirange_t *wiirange;
    int32_t max_wiimotes;

    wiimotes = (wiimote**)csound->QueryGlobalVariable(csound, "wiiMote");
    if (wiimotes == NULL) {
      csound->CreateGlobalVariable(csound, "wiiMote",
                                           MAX_WIIMOTES*sizeof(wiimote*));
      wiimotes = (wiimote**)csound->QueryGlobalVariable(csound, "wiiMote");
    }
    wiirange = (wiirange_t *)csound->QueryGlobalVariable(csound, "wiiRange");
    if (wiirange == NULL) {
      csound->CreateGlobalVariable(csound, "wiiRange",
                                           MAX_WIIMOTES*sizeof(wiirange_t));
      wiirange = (wiirange_t *)csound->QueryGlobalVariable(csound, "wiiRange");
    }
    {                           /* Use our copy not his */
      wiimote** ww = wiiuse_init(MAX_WIIMOTES);
      memcpy(wiimotes, ww, MAX_WIIMOTES*sizeof(wiimote*));
      free(ww);
    }
    i = (int32_t)*p->kControl;
    if (i<=0) i = 10;           /* default timeout */
    max_wiimotes = (int32_t)*p->num;
    if (max_wiimotes<=0 || max_wiimotes>MAX_WIIMOTES) max_wiimotes = MAX_WIIMOTES;
    n  = wiiuse_find(wiimotes, max_wiimotes, i);
    if (LIKELY(n!=0)) n = wiiuse_connect(wiimotes, max_wiimotes);
    if (UNLIKELY(n==0)) {
      return csound->InitError(csound, "%s", Str("unable to open wiimote\n"));
    }
    /* Initialise ranges */
    for (i=0; i<n; i++) {
      wiiuse_set_leds(wiimotes[i], WIIMOTE_LED_1<<i);
      wiirange[i].axis_x_min    = FL(0.0);
      wiirange[i].axis_x_scale  = FL(1.0);
      wiirange[i].axis_y_min    = FL(0.0);
      wiirange[i].axis_y_scale  = FL(1.0);
      wiirange[i].axis_z_min    = FL(0.0);
      wiirange[i].axis_z_scale  = FL(1.0);
      wiirange[i].pitch_min    = -FL(90.0);
      wiirange[i].pitch_scale  = FL(1.0);
      wiirange[i].roll_min    = -FL(90.0);
      wiirange[i].roll_scale  = FL(1.0);
      wiirange[i].nunchuk_pitch_min   = -FL(90.0);
      wiirange[i].nunchuk_pitch_scale = FL(1.0);
      wiirange[i].nunchuk_roll_min   = -FL(90.0);
      wiirange[i].nunchuk_roll_scale = FL(1.0);
      wiiuse_status(wiimotes[i]);
      wiiuse_motion_sensing(wiimotes[i], 1);
    }
    p->wii = wiimotes;
    p->max_wiimotes = n;
    *p->res = FL(1.0);
    return OK;
}
void CryVR_WiimoteManagerPlugin::Init()
{
    wiimotes = wiiuse_init( MAX_WIIMOTES );
    SetBluetooth( bt );

    found = wiiuse_find( wiimotes,  MAX_WIIMOTES, 5 );
    connected = wiiuse_connect( wiimotes, MAX_WIIMOTES );

    CryLogAlways( "Wiimotes found : %i", found );

    init = true;
}
Exemplo n.º 5
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);
    }



}
Exemplo n.º 6
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);
}
Exemplo n.º 7
0
void find_wiimote(GtkWidget *widget, gpointer user_data)
{
	wiimotes = wiiuse_init(2);
	found = wiiuse_find(wiimotes, 2, 5); /* Find remotes (5s timeout) */
	if(found>0)
	{
		wiiuse_connect(wiimotes, found); /* Connect to as many wiimotes that were found */
	}
	else
	{
		printf("No wiimotes found\n");
		return;
	}
	printf("Found %d wiimote%s\n",found, (found!=1)?("s"):(""));
	
	wiiuse_set_leds(wiimotes[0], WIIMOTE_LED_1|WIIMOTE_LED_4);
	wiiuse_motion_sensing(wiimotes[0], 1);
	if(found>1)
	{
		wiiuse_set_leds(wiimotes[1], WIIMOTE_LED_2|WIIMOTE_LED_3);
		wiiuse_motion_sensing(wiimotes[1], 1);
	}
	g_thread_create((GThreadFunc)handle_hit, NULL, FALSE, NULL);
}
Exemplo n.º 8
0
// Device constructor.
// Parameters:
// - name: VRPN name to assign to this server
// - c: VRPN connection this device should be attached to
vrpn_WiiMote::vrpn_WiiMote(const char *name, vrpn_Connection *c, unsigned which):
    vrpn_Analog(name, c),
    vrpn_Button(name, c),
    vrpn_Analog_Output(name, c),
    wiimote(new vrpn_Wiimote_Device)
{
    int i;
    char  msg[1024];

    vrpn_Analog::num_channel = min(64, vrpn_CHANNEL_MAX);
    for (i = 0; i < vrpn_Analog::num_channel; i++) {
        channel[i] = 0;
    }

    // There are bits for up to 16 buttons on the main remote, and for
    // up to 16 more on an expansion pack.
    vrpn_Button::num_buttons = min(64, vrpn_BUTTON_MAX_BUTTONS);
    for (i = 0; i < vrpn_Button::num_buttons; i++) {
        buttons[i] = 0;
    }

    vrpn_Analog_Output::o_num_channel = 1;

    // Register a handler for the request channel change message
    if (register_autodeleted_handler(request_m_id,
                                     handle_request_message, this, d_sender_id)) {
        FAIL("vrpn_WiiMote: can't register change channel request handler");
        return;
    }

    // Register a handler for the request channels change message
    if (register_autodeleted_handler(request_channels_m_id,
                                     handle_request_channels_message, this, d_sender_id)) {
        FAIL("vrpn_WiiMote: can't register change channels request handler");
        return;
    }

    // Register a handler for the no-one's-connected-now message
    if (register_autodeleted_handler(
                d_connection->register_message_type(vrpn_dropped_last_connection),
                handle_last_connection_dropped, this)) {
        FAIL("Can't register self-destruct handler");
        return;
    }

    // Get a list of available devices and select the one we want.
    // Look for up to 4 motes.  Timeout in 5 seconds if one not found.
    wiimote->which = which;
    wiimote_t **available_wiimotes = wiiuse_init(4);
    unsigned num_available = wiiuse_find(available_wiimotes, 4, 5);
    if (num_available < (wiimote->which + 1)) {
        struct timeval now;
        vrpn_gettimeofday(&now, NULL);
        sprintf(msg, "Could not open remote %d (%d found)", wiimote->which, num_available);
        send_text_message(msg, now, vrpn_TEXT_ERROR);
        wiimote->found = false;
    } else {
        wiimote->found = true;

        // Make a list containing just the one we want, and then connect to it.
        wiimote_t *selected_one[1];
        wiimote->device = selected_one[0] = available_wiimotes[wiimote->which];
        wiimote->connected = (wiiuse_connect(selected_one, 1) == 1);
        if (!wiimote->connected) {
            struct timeval now;
            vrpn_gettimeofday(&now, NULL);
            sprintf(msg, "Could not connect to remote %d", wiimote->which);
            send_text_message(msg, now, vrpn_TEXT_ERROR);
        }
    }

    if (wiimote->connected) {
        initialize_wiimote_state();
    }
}
Exemplo n.º 9
0
/**
 * 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;
}
Exemplo n.º 10
0
	virtual void ProcessEvent(EFlowEvent event, SActivationInfo *pActInfo)
	{
		
		
		
		switch (event)
		{
		case eFE_Initialize: 
			//Sleep(5000);
			break;
		case eFE_Activate:
			{	
				//Sleep(3000); // <-- Dernière chose ajoutée
				wiimotes = wiiuse_init(MAX_WIIMOTES); 
				found = wiiuse_find(wiimotes,  MAX_WIIMOTES, 5);
				//if (found!=0) 
					//handle_ctrl_status(wiimotes[0]);

				connected = wiiuse_connect(wiimotes, MAX_WIIMOTES);
				if (connected) {

					CryLogAlways("Connected to %i wiimotes (of %i found).\n", connected, found);
				}
				else CryLogAlways("Failed to connect to any wiimote.\n");
				wiiuse_set_leds(wiimotes[0], WIIMOTE_LED_2);
			
				wiiuse_motion_sensing(wiimotes[0], 1);
				//wiiuse_set_ir(wiimotes[0], 1);
				pActInfo->pGraph->SetRegularlyUpdated(pActInfo->myID,true);
				//break; // <-- Dernière chose commentée !
			}
		
		case eFE_Update:
			{
				//CryLogAlways("Update !");
				if(GetPortBool(pActInfo, 0)){
					wiimotes = wiiuse_init(MAX_WIIMOTES); 
					connected = wiiuse_connect(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) {
						CryLogAlways("attachment:  wiimote :%i  attachement : %i",i,wiimotes[i]->exp.type);

						switch (wiimotes[i]->event) {
							case WIIUSE_EVENT:
								/* a generic event occured */
								CryLogAlways("Generic event");
								handle_event(wiimotes[i]);
								break;
							case WIIUSE_CONNECT:
								CryLogAlways("Connexion");
								break;
							case WIIUSE_STATUS:
								/* a status event occured */
								CryLogAlways("Demande de status");
								handle_ctrl_status(wiimotes[i]);
								break;

							case WIIUSE_DISCONNECT:
							case WIIUSE_UNEXPECTED_DISCONNECT:
								/* the wiimote disconnected */
								CryLogAlways("Deconnexion");
								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);
								handle_ctrl_status(wiimotes[i]);
								printf("Nunchuk inserted.\n");
								break;

							case WIIUSE_CLASSIC_CTRL_INSERTED:
								handle_ctrl_status(wiimotes[i]);
								printf("Classic 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_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;
						}
			
					}
				}
			}
			//catch(EXCEPINFO e){}
		}
		
		//}
	}
Exemplo n.º 11
0
Arquivo: sdl.c Projeto: 0xheart0/xbmc
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();
	}
}
Exemplo n.º 12
0
	virtual void ProcessEvent(EFlowEvent event, SActivationInfo *pActInfo)
	{
		int found =-1;
		int connected;
		
		
		switch (event)
		{
		case eFE_Initialize:{
			//Sleep(3000);
			wiimotes = wiiuse_init(1); 
			found = wiiuse_find(wiimotes, 1, 5);
			if (found!=0) handle_ctrl_status(wiimotes[0]);

			connected = wiiuse_connect(wiimotes, 1);
			if (connected) CryLogAlways("Connected to %i wiimotes (of %i found).\n", connected, found);
			else CryLogAlways("Failed to connect to any wiimote.\n");
			wiiuse_set_leds(wiimotes[0], WIIMOTE_LED_1);
			
			wiiuse_motion_sensing(wiimotes[0], 1);
			wiiuse_set_ir(wiimotes[0], 1);

			//trop rapide !
			
		}
		case eFE_Activate:
			{	

				//Sleep(3000);

				//wiimotes = wiiuse_init(1); 
				//found = wiiuse_find(wiimotes, 1, 5);
				//if (found==0) CryLogAlways("No wiimotes found.\n");
				
				/*
				connected = wiiuse_connect(wiimotes, 1);
				if (connected) CryLogAlways("Connected to %i wiimotes (of %i found).\n", connected, found);
				else CryLogAlways("Failed to connect to any wiimote.\n");
				wiiuse_set_leds(wiimotes[0], WIIMOTE_LED_1);

				wiiuse_motion_sensing(wiimotes[0], 1);
				wiiuse_set_ir(wiimotes[0], 1);
				//WIIUSE_USING_EXP(wiimotes[0]);
				//WIIUSE_U

				//wiimotes[0]->exp.type = EXP_NUNCHUK;
				
				*/
				

				pActInfo->pGraph->SetRegularlyUpdated(pActInfo->myID,true);
			}
			break;
		
		case eFE_Update:
			{
				//int connected = wiiuse_connect(wiimotes, 1); 
				wiiuse_poll(wiimotes, 1);
				wiimote* wm =  wiimotes[0];
				//Ne change rien ! juste qu'on a un retour d'IR
				//handle_ctrl_status(wiimotes[0]);
				CryLogAlways("attachment:      %i",wm->exp.type);

				/*

				Gestion des evts

				*/

				
				
				// A commenter
				switch (wiimotes[0]->event) {
					case WIIUSE_EVENT:
						/* a generic event occured */
						handle_event(wiimotes[0]);
						break;

					case WIIUSE_STATUS:
						/* a status event occured */
						handle_ctrl_status(wiimotes[0]);
						break;

					case WIIUSE_DISCONNECT:
					case WIIUSE_UNEXPECTED_DISCONNECT:
						/* the wiimote disconnected */
						handle_disconnect(wiimotes[0]);
						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.
						 */
						//struct nunchuk_t* nc = (nunchuk_t*)&wiimotes[0]->exp.nunchuk;
						 wiiuse_set_nunchuk_orient_threshold(wiimotes[0], 90.0f);
						 wiiuse_set_nunchuk_accel_threshold(wiimotes[0], 100);
						CryLogAlways("Nunchuk inserted.\n");
						break;

					case WIIUSE_CLASSIC_CTRL_INSERTED:
						CryLogAlways("Classic controller inserted.\n");
						break;

					case WIIUSE_GUITAR_HERO_3_CTRL_INSERTED:
						/* some expansion was inserted */
						handle_ctrl_status(wiimotes[0]);
						//printf("Guitar Hero 3 controller 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[0]);
						//printf("An expansion was removed.\n");
						break;

					default:
						break;
				}














				
					//if (wiiuse_poll(wiimotes, 1)) {
						//int i = 0;
				
						if (IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_A))			ActivateOutput(pActInfo, WIIMOTE_A,true); else ActivateOutput(pActInfo, WIIMOTE_A,false);
						if (IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_B))			ActivateOutput(pActInfo, WIIMOTE_B,true); else ActivateOutput(pActInfo, WIIMOTE_B,false);
						if (IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_UP))			ActivateOutput(pActInfo, WIIMOTE_UP,true); else ActivateOutput(pActInfo, WIIMOTE_UP,false);
						if (IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_DOWN))		ActivateOutput(pActInfo, WIIMOTE_DOWN,true); else ActivateOutput(pActInfo, WIIMOTE_DOWN,false);
						if (IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_LEFT))		ActivateOutput(pActInfo, WIIMOTE_LEFT,true); else ActivateOutput(pActInfo, WIIMOTE_LEFT,false);
						if (IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_RIGHT))		ActivateOutput(pActInfo, WIIMOTE_RIGHT,true); else ActivateOutput(pActInfo, WIIMOTE_RIGHT,false);
						if (IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_MINUS))		ActivateOutput(pActInfo, WIIMOTE_MINUS,true); else ActivateOutput(pActInfo, WIIMOTE_MINUS,false);
						if (IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_PLUS))		ActivateOutput(pActInfo, WIIMOTE_PLUS,true); else ActivateOutput(pActInfo, WIIMOTE_PLUS,false);
						if (IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_ONE))		ActivateOutput(pActInfo, WIIMOTE_ONE,true); else ActivateOutput(pActInfo, WIIMOTE_ONE,false);
						if (IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_TWO))		ActivateOutput(pActInfo, WIIMOTE_TWO,true); else ActivateOutput(pActInfo, WIIMOTE_TWO,false);
						if (IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_HOME))		ActivateOutput(pActInfo, WIIMOTE_HOME,true); else ActivateOutput(pActInfo, WIIMOTE_HOME,false);

						if (WIIUSE_USING_ACC(wiimotes[0])) {
							Vec3 output = Vec3(wiimotes[0]->orient.roll,wiimotes[0]->orient.pitch,wiimotes[0]->orient.yaw);
							ActivateOutput(pActInfo, WIIMOTE_RPY,output);
						}
						if (WIIUSE_USING_IR(wiimotes[0])) {
							Vec3 output = Vec3((float)wiimotes[0]->ir.x,(float)wiimotes[0]->ir.y,(float)wiimotes[0]->ir.z);
							ActivateOutput(pActInfo, WIIMOTE_IR,output);
						}
						/* nunchuk */
						//Toujours faux !
						
						if (wm->exp.type == EXP_NUNCHUK) {
							
							//nunchuk_t* pt =  wm->exp.nunchuk;
						//REssort 0 a chaque fois !!!!
							struct nunchuk_t* nc = (nunchuk_t*)&wm->exp.nunchuk;
							CryLogAlways("nun gravity x EXPANSION : %f",nc->gforce.x);
							if (IS_PRESSED(nc, NUNCHUK_BUTTON_C)) ActivateOutput(pActInfo, NUNCHUK_C,true); else ActivateOutput(pActInfo, NUNCHUK_C,false);
							if (IS_PRESSED(nc, NUNCHUK_BUTTON_Z)) ActivateOutput(pActInfo, NUNCHUK_Z,true); else ActivateOutput(pActInfo, NUNCHUK_Z,false);
							
							Vec3 output = Vec3(nc->orient.a_roll,nc->orient.a_pitch,nc->orient.yaw);
							ActivateOutput(pActInfo, NUNCHUK_RPY,output);
							
							Vec3 output2 = Vec3(nc->js.ang,nc->js.mag,0);
							ActivateOutput(pActInfo, NUNCHUK_JOYSTICK,output2);
						} 


						/*
						switch (wiimotes[0]->event) {
							//case WIIUSE_EVENT:
								//{
								//}
								//break;

							case WIIUSE_STATUS:
								handle_ctrl_status(wiimotes[0]);
								break;

							case WIIUSE_DISCONNECT:
							case WIIUSE_UNEXPECTED_DISCONNECT:
								handle_disconnect(wiimotes[0]);
								break;
								
							default:
								break;
						}
						*/
					//}
				//}
			}
				
		}
	}
Exemplo n.º 13
0
/**
 *	@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;
}
Exemplo n.º 14
0
/**
 *	@brief main()
 *
 *	Connect to up to two wiimotes and print any events
 *	that occur on either device.
 */
ControlResult TickRemote()
{
   // If not connected then attempt to connect
   if (sControlMode == ControlMode::Off)
   {
      sResult.steering_demand.x = 0;
      sResult.steering_demand.y = 0;
      sResult.steering_demand.z = 0;
      sResult.fire = false;
      sResult.exit = false;

      int found = wiiuse_find(sWiimotes, 1, 5);
      
      if (found > 0)
      {
         int connected = wiiuse_connect(sWiimotes, 1);
         if (connected > 0)
         {
            change_control_mode(ControlMode::Slaved);
            wiiuse_set_leds(sWiimotes[0], WIIMOTE_LED_1);
            wiiuse_motion_sensing(sWiimotes[0], 0);
            usleep(100000);
            wiiuse_set_ir(sWiimotes[0], 1);
            wiiuse_set_ir_sensitivity(sWiimotes[0], 3);
         }
      }
   }

   bool first = true;
   while (sControlMode != ControlMode::Off &&
          wiiuse_poll(sWiimotes, 1)) // Returns number of events to be processed
   {
      if (first)
      {
         first = false;
         sResult.steering_demand.x = 0;
         sResult.steering_demand.y = 0;
         sResult.steering_demand.z = 0;
         sResult.fire = false;
         sResult.exit = false;
      }

      switch (sWiimotes[0]->event) {
         case WIIUSE_EVENT:
            /* a generic event occured */
            handle_event(sWiimotes[0]);
            break;

         case WIIUSE_DISCONNECT:
         case WIIUSE_UNEXPECTED_DISCONNECT:
            /* the wiimote disconnected */
            wiiuse_disconnected(sWiimotes[0]);
            change_control_mode(ControlMode::Off);
            break;

         default:
            break;
      }
   }

   clock_t current_time = clock();
   if (sControlMode == ControlMode::Seeking)
   {
      if (current_time > sTrackAge + 0.25 * CLOCKS_PER_SEC && sTrackValid)
      {
         sResult.steering_demand.x = 0;
         sResult.steering_demand.y = 0;
         sTrackValid = false;
         Log("Lost track\n"); 
      }

      if (current_time > sTrackAge + 1 * CLOCKS_PER_SEC)
      {
         change_control_mode(ControlMode::Searching);
         sSearchDirectionChangeTime = current_time + 2 * CLOCKS_PER_SEC;
         sSearchDirection = 1;
      }
   }

   if (sControlMode == ControlMode::Searching)
   {
      if (current_time > sSearchDirectionChangeTime)
      {
         sSearchDirectionChangeTime = current_time + 2 * CLOCKS_PER_SEC;
         Log("Turning\n"); 
         sSearchDirection *= -1;
      }
      sResult.steering_demand.x = sSearchDirection;
      sResult.steering_demand.y = 0;
      sResult.steering_demand.z = -1;

   } 
   
   return sResult;
}
int main(void)
{
    wiimote *wm;
    wiimote** wiimotes;
    int found, connected;
    HANDLE canHandle;
    TPCANMsg Message;
    int wiimote_led_state;
    int exit = 0;

    canHandle = initCAN();
    if(canHandle == NULL)
    {
        printf("Error opening CAN device!\n");
        return -1;
    }

    wiimotes =  wiiuse_init(MAX_WIIMOTES);

    found = wiiuse_find(wiimotes, MAX_WIIMOTES, 5);
    if (!found)
    {
        printf ("No wiimotes found.\n");
        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;
    }

    wm = wiimotes[0];
    wiiuse_status(wm);
    while(wm->event != WIIUSE_STATUS)
    {
        wiiuse_poll(wiimotes, MAX_WIIMOTES);
    }
    printf("Battery level: %f%%\n", wm->battery_level*100);

    while (1)
    {
        if(exit)
            break;
        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]);
                        Message.ID = CAN_INPUT_MSG_ID;
                        Message.MSGTYPE = MSGTYPE_STANDARD;
                        Message.LEN = 7;
                        Message.DATA[0] = carInputs.accel;
                        Message.DATA[1] = carInputs.brake;
                        Message.DATA[2] = carInputs.steer;
                        Message.DATA[3] = carInputs.gear;
                        Message.DATA[4] = carInputs.clutch;
                        Message.DATA[5] = carInputs.controls;
			Message.DATA[6] = carInputs.cruisedist;
                        CAN_Write(canHandle,&Message);

                        // Show the status of ABS/TC/Cruise on the LEDs
                        wiimote_led_state = 0;
                        if(carInputs.controls & ABS)
                            wiimote_led_state |= WIIMOTE_LED_1;
                        if(carInputs.controls & TC)
                            wiimote_led_state |= WIIMOTE_LED_2;
                        if(carInputs.controls & STABILITY)
                            wiimote_led_state |= WIIMOTE_LED_3;
                        if(carInputs.controls & CRUISE)
                            wiimote_led_state |= WIIMOTE_LED_4;
                        wiiuse_set_leds(wm, wiimote_led_state);

                        break;
                    case WIIUSE_DISCONNECT:
                    case WIIUSE_UNEXPECTED_DISCONNECT:
                        /* the wiimote disconnected */
                        handle_disconnect(wiimotes[i]);
                        exit = 1;
                        break;
                    default:
                        break;
                }
            }
        }
    }
    wiiuse_cleanup(wiimotes, MAX_WIIMOTES);
    return 0;

}
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
  }
}
Exemplo n.º 17
0
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;
}
Exemplo n.º 18
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
Exemplo n.º 19
0
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;
}
Exemplo n.º 20
0
void triple_buffer(BITMAP **page) {
	int i = 0, w, repeat, active_page = 0;
	int time_diff, bar_number, *used_voices = NULL, hw_voices;
	int text_bar_height, half_bar_size;
	unsigned int radius;
	
	coord_t dot[2], dot_old[2], cal[2], cal_tmp[2];
	
	wiimote *wiimote;
	
	char *fonts[NUM_MAX_FONTS] = FONTS;
	FONT *font_msg, *font_notes;
	BITMAP *background;
	bars *bar;
	
	/* dot_old data initialization. dot_old's X coord is not used */
	dot_old[0].y = SCREEN_H;
	dot_old[1].y = SCREEN_H;
	
	/* Wiimote initialization*/
	wiimote = *wiiuse_init(1);
	
	/* Load fonts, size of font is related to SCREEN_W */
	do {
		font_msg = load_font(fonts[i], NULL, NULL);
		__ASSERT(font_msg, ERROR_CANT_LOAD_FONT(fonts[i]));
		w = text_length(font_msg, WIIMOTE_CONNECTION_MSG);
	} while (w >= SCREEN_W && ++i < (NUM_MAX_FONTS-1));
	
	/* Notes' font is smaller than messages' font */
	font_notes = load_font(fonts[i], NULL, NULL);
	__ASSERT(font_notes, ERROR_CANT_LOAD_FONT(fonts[i]));
	
	/* Initialize and calculate bars size, point radius, text bar height, and calculate half bar size for text output under bars */
	bar = bar_create(NUM_BARS);
	radius = RADIUS(bar);
	text_bar_height = TEXT_BAR_HEIGHT;
	half_bar_size = HALF_BAR_SIZE(bar);
	
	/* Control the max number of available hardware voices. Though ALSA driver voices are up to 64, max hardware voices generally are 8-16. Checking the allocated voices after the driver allocation seems to be the only way */
	hw_voices = get_mixer_voices();
	if (hw_voices < NUM_BARS)
		used_voices = reallocate_voices(bar, hw_voices);
	
	/* Install timer, 10 ticks a second */
	LOCK_VARIABLE(timer);
	LOCK_FUNCTION(inc_timer);
	install_int_ex(inc_timer, BPS_TO_TIMER(10));
	
	/* Load background from file */
	background = load_tga("images/back.tga", NULL);
	__ASSERT(background, ERROR_CANT_LOAD_IMAGE("back.tga"));
	
	/* Enables vertical syncronization*/
	vsync();
	
	i = 0;
	/* First frame, this lasts until wiimote is connected, the user is prompted to activate the wiimote by pressing 1 & 2 keys on the wiimote */
	while(i == 0 && !keypressed()) {
		active_page = start_credits3buf(page, active_page);
		
		clear_keybuf();
		
		title3buf(page, active_page, font_msg);
		
		clear_keybuf();
		
		repeat = 0;
		while(repeat++ < 8 && i == 0 && !keypressed()) {
			stretch_blit(background, page[active_page], 0, 0, background->w, background->h, 0, 0, SCREEN_W, SCREEN_H); // background
			if(repeat%2 == 1)
				textout_centre_ex(page[active_page], font_msg, WIIMOTE_CONNECTION_MSG, SCREEN_W/2, SCREEN_H/2, makecol(0, 0, 0), -1); // text prompt
			
			release_bitmap(page[active_page]);
			/* make sure the last flip request has actually happened */
			do {
			} while (poll_scroll());
			
			/* post a request to display the page we just drew */
			request_video_bitmap(page[active_page]);

			/* update counters to point to the next page */
			switch (active_page) {
				case 0: active_page = 1; break;
				case 1: active_page = 2; break;
				case 2: active_page = 0; break;
			}

			/* Search for a wiimote */
			i = wiiuse_find(&wiimote, 1, 1);
		}
	}
	
	/* Try to connect to the wiimote */
	__ASSERT(wiiuse_connect(&wiimote, 1) > 0, ERROR_CANT_OPEN_WIIMOTE);
	
	/* Activate the first led on the wiimote */
	wiiuse_set_leds(wiimote, WIIMOTE_LED_1);
	
	/* Activate the ir module on the wiimote */
	wiiuse_set_ir(wiimote, TRUE);
	
	wiiuse_motion_sensing(wiimote, FALSE);
	
	wiiuse_set_ir_sensitivity(wiimote, 1);
	wiiuse_set_flags(wiimote, WIIUSE_CONTINUOUS, 0);
	
	cal[0].x = 0;
	cal[0].y = 0;
	cal[1].x = 1023;
	cal[1].y = 767;
	
	/* CALIBRATION LOOP */
	for(repeat=0;repeat<2;repeat++) {
		clear_keybuf();
		
		while(!keypressed()) {
			if (wiiuse_poll(&wiimote, 1)) // if there are datas pending from/to wiimote or ESC is pressed
				if (key[KEY_ESC] || wiimote->event == WIIUSE_DISCONNECT || wiimote->event == WIIUSE_UNEXPECTED_DISCONNECT || IS_PRESSED(wiimote, WIIMOTE_BUTTON_HOME)) { // if ESC is pressed, if wiimote update fails, or if HOME key on wiimote is pressed
				wiiuse_disconnect(wiimote);
				destroy_bitmap(background);
				destroy_font(font_msg);
				destroy_font(font_notes);
				for (i=0;i<NUM_BARS;i++) {
					deallocate_voice(bar[i].voice);
					destroy_sample(bar[i].sound);
				}
				free(bar); // YO!! :-)
				free(used_voices);
				
				return;
				}
			/* background */
			stretch_blit(background, page[active_page], 0, 0, background->w, background->h, 0, 0, SCREEN_W, SCREEN_H);
			
			if (wiimote->ir.dot[0].visible) { // if ir source is visible
				/* Read coords from the wiimote's ir*/
				dot[0] = transpose(wiimote->ir.dot[0], cal, 0);
			}
			
			switch(repeat) {
				case 0:
					textout_centre_ex(page[active_page], font_msg, WIIMOTE_CAL_ASX, SCREEN_W/2, SCREEN_H/2, makecol(0, 0, 0), -1); // text prompt
					rect(page[active_page], dot[0].x, dot[0].y, SCREEN_W+1, SCREEN_H+1, makecol(0, 0, 0));
					break;
				case 1:
					textout_centre_ex(page[active_page], font_msg, WIIMOTE_CAL_BDX, SCREEN_W/2, SCREEN_H/2, makecol(0, 0, 0), -1); // text prompt
					
					rect(page[active_page], dot[1].x, dot[1].y, SCREEN_W+1, SCREEN_H+1, makecol(0, 0, 0));
					rect(page[active_page], -1, -1, dot[0].x, dot[0].y, makecol(0, 0, 0));
					break;
			}
			circlefill(page[active_page], dot[0].x, dot[0].y, radius, makecol(0, 0, 0));

			release_bitmap(page[active_page]);
			
			/* make sure the last flip request has actually happened */
			do {
			} while (poll_scroll());

			/* post a request to display the page we just drew */
			request_video_bitmap(page[active_page]);

			/* update counters to point to the next page */
			switch (active_page) {
				case 0: active_page = 1; break;
				case 1: active_page = 2; break;
				case 2: active_page = 0; break;
			}
		}
		
		cal_tmp[repeat].x = wiimote->ir.dot[0].x;
		cal_tmp[repeat].y = wiimote->ir.dot[0].y;
		dot[1] = dot[0];
	}
	
	__ASSERT((cal_tmp[0].x < cal_tmp[1].x && cal_tmp[0].y > cal_tmp[1].y), ERROR_WHILE_CALIBRATING);
	
	cal[0].x = cal_tmp[0].x;
	cal[0].y = 767-cal_tmp[0].y;
	cal[1].x = cal_tmp[1].x;
	cal[1].y = 767-cal_tmp[1].y;
	
	/* MAIN LOOP */
	while (TRUE) {
		/* Draw a frame */
		if (wiiuse_poll(&wiimote, 1) || key[KEY_ESC]) // if there are datas pending from/to wiimote or ESC is pressed
			if (key[KEY_ESC] || wiimote->event == WIIUSE_DISCONNECT || wiimote->event == WIIUSE_UNEXPECTED_DISCONNECT || IS_PRESSED(wiimote, WIIMOTE_BUTTON_HOME)) { // if ESC is pressed, if wiimote update fails, or if HOME key on wiimote is pressed
				wiiuse_disconnect(wiimote);
				destroy_bitmap(background);
				destroy_font(font_msg);
				destroy_font(font_notes);
				for (i=0;i<NUM_BARS;i++) {
					deallocate_voice(bar[i].voice);
					destroy_sample(bar[i].sound);
				}
				free(bar); // YO!! :-)
				free(used_voices);
				
				return;
			}
		
		/* background */
		stretch_blit(background, page[active_page], 0, 0, background->w, background->h, 0, 0, SCREEN_W, SCREEN_H);
		
		/* Xylophone's bars and notes names */
		for(i=0;i<NUM_BARS;i++) {
			if (bar[i].t_start != -1 && (time_diff = timer-bar[i].t_start) > TICKS_TO_BLACK) // if color animation ends
				bar[i].t_start = -1;
			if (bar[i].t_start == -1) // if no color animation
				bar[i].color = 0;
			else	bar[i].color = COLORVAL(time_diff); // if color animation is running
			
			/* Draw bar */
			rectfill(page[active_page], bar[i].min.x, bar[i].min.y, bar[i].max.x, bar[i].max.y, makecol(bar[i].color, bar[i].color, bar[i].color));
			/* Print bar's associated note */
			textout_centre_ex(page[active_page], font_notes, bar[i].note, (bar[i].min.x + half_bar_size), text_bar_height, makecol(0, 0, 0), -1);
		}
		
		// da normalizzare e da contenere nello schermo, ir
		for(i=0;i<MAX_IR_DOTS;i++) {
			if (wiimote->ir.dot[i].visible) { // if ir source is visible
				/* Read coords from the wiimote's ir*/
				dot[i] = transpose(wiimote->ir.dot[i], cal, radius);
				
				/* If the ir source is under the bars and in previous frame it was above the bars, then play the sound and start the animation */
				if (dot[i].y > bar[0].min.y-radius) {
					if(dot_old[i].y <= bar[0].min.y-radius) {
						/* This calculates on which bar the ir source actually is */
						bar_number = is_onbar(bar, dot[i].x, NUM_BARS);
						
						/* play bar_number's sound with specified volume */
						play_bar_voice(bar, bar_number, volume(dot[i].y-dot_old[i].y), used_voices, hw_voices);
					}
					
					/* The dot have not to go under the bars or out of the screen */
					circlefill(page[active_page], dot[i].x, bar[0].min.y-radius, radius, makecol(0, 0, 0));
				}
				else	circlefill(page[active_page], dot[i].x, dot[i].y, radius, makecol(0, 0, 0));
				
				dot_old[i].y = dot[i].y;
			}
			else dot_old[i].y = SCREEN_H;
		}

		release_bitmap(page[active_page]);

		/* make sure the last flip request has actually happened */
		do {
		} while (poll_scroll());

		/* post a request to display the page we just drew */
		request_video_bitmap(page[active_page]);

		/* update counters to point to the next page */
		switch (active_page) {
			case 0: active_page = 1; break;
			case 1: active_page = 2; break;
			case 2: active_page = 0; break;
		}
	}
}
Exemplo n.º 21
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;
}
Exemplo n.º 22
0
static u32 WII_Run(void *par)
{
	GF_BitStream *bs;
	char *buf;
	u32 i, buf_size, count, scan_delay;
	const char *opt;

	GF_InputSensorDevice *ifce = (GF_InputSensorDevice *)par;
	GF_WiiMote *wii = (GF_WiiMote *)ifce->udta;


	scan_delay = 5;
	opt = gf_modules_get_option((GF_BaseInterface*)ifce, "WII", "ScanDelay");
	if (opt) scan_delay = atoi(opt);

	/*locate the wiimotes*/
	count = wiiuse_find(wii->wiimotes, wii->nb_wiimotes, scan_delay); 
	GF_LOG(GF_LOG_INFO, GF_LOG_MMIO, ("[Wii] Found %d wiimotes\n", count));
	count = wiiuse_connect(wii->wiimotes, wii->nb_wiimotes);
	if (count) {
		GF_LOG(GF_LOG_INFO, GF_LOG_MMIO, ("[Wii] Connected to %d connected wiimotes\n", count));
	} else {
		GF_LOG(GF_LOG_INFO, GF_LOG_MMIO, ("[Wii] Failed to connect to any wiimote\n"));
	}

	opt = gf_modules_get_option((GF_BaseInterface*)ifce, "WII", "MotionSensing");
	/*enable motion sensing*/
	if (!opt || !strcmp(opt, "yes")) {
		Float smooth_alpha = 0.5;
		Float ori_threshold = 10.0;
		opt = gf_modules_get_option((GF_BaseInterface*)ifce, "WII", "OrientationThreshold");
		if (opt) ori_threshold = (Float) atof(opt);
		opt = gf_modules_get_option((GF_BaseInterface*)ifce, "WII", "SmoothAlpha");
		if (opt) {
			smooth_alpha = (Float) atof(opt);
			if (smooth_alpha<0) smooth_alpha = 0.5;
			else if (smooth_alpha>1.0) smooth_alpha=0.5;
		}
		GF_LOG(GF_LOG_INFO, GF_LOG_MMIO, ("[Wii] Enabling motion sensing - Alpha smoothing %f - Orientation Threshold %f\n", smooth_alpha, ori_threshold));

		for (i=0; i<count; i++) {
			wiiuse_motion_sensing(wii->wiimotes[i], 1);
			wiiuse_set_smooth_alpha(wii->wiimotes[i],smooth_alpha);
			wiiuse_set_orient_threshold(wii->wiimotes[i], ori_threshold);
		}
	}

	while (wii->running) { 
		count = wiiuse_poll(wii->wiimotes, wii->nb_wiimotes);
		if (!count) {
			continue;
		}
		for (i=0; i<count; i++) {
			struct wiimote_t* wm = wii->wiimotes[i];
			switch (wm->event) { 
			case WIIUSE_EVENT:/* A generic event occured on the wiimote. */
				/*create the data frame*/
				bs = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE);
				/*if not the same wiimote write the UID*/
				if (wii->prev_id != wm->unid) {
					gf_bs_write_int(bs, 1, 1); 
					gf_bs_write_int(bs,  wm->unid, 32); 
					wii->prev_id = wm->unid;
				} else {
					gf_bs_write_int(bs, 0, 1); 
				}

				/*write buttons state*/
				WRITE_BUTTON(WIIMOTE_BUTTON_ONE);
				WRITE_BUTTON(WIIMOTE_BUTTON_TWO);
				WRITE_BUTTON(WIIMOTE_BUTTON_A);
				WRITE_BUTTON(WIIMOTE_BUTTON_B);
				WRITE_BUTTON(WIIMOTE_BUTTON_MINUS);
				WRITE_BUTTON(WIIMOTE_BUTTON_HOME);
				WRITE_BUTTON(WIIMOTE_BUTTON_PLUS);
				WRITE_BUTTON(WIIMOTE_BUTTON_LEFT);
				WRITE_BUTTON(WIIMOTE_BUTTON_RIGHT);
				WRITE_BUTTON(WIIMOTE_BUTTON_DOWN);
				WRITE_BUTTON(WIIMOTE_BUTTON_UP);

				/*write yaw-pitch-roll - FIXME: wiiuse seems to output NaN in these values upon init*/
				gf_bs_write_int(bs, 1, 1); 
				gf_bs_write_float(bs, WII_PI * wm->orient.yaw / 24 );
				gf_bs_write_float(bs, WII_PI * wm->orient.pitch / 180);
				gf_bs_write_float(bs, WII_PI * wm->orient.roll / 180);

				/*write gravity - FIXME: wiiuse seems to output NaN in these values upon init*/
				gf_bs_write_int(bs, 1, 1); 
				gf_bs_write_float(bs, wm->gforce.x);
				gf_bs_write_float(bs, wm->gforce.y);
				gf_bs_write_float(bs, wm->gforce.z);

				gf_bs_align(bs);
				gf_bs_get_content(bs, &buf, &buf_size);
				gf_bs_del(bs);

				ifce->DispatchFrame(ifce, buf, buf_size);
				gf_free(buf);
				break;
			case WIIUSE_STATUS: /*A status report was obtained from the wiimote. */
				break;
			case WIIUSE_DISCONNECT:/*The wiimote disconnected. */
				break;
			case WIIUSE_READ_DATA:/* Data was returned that was previously requested from the wiimote ROM/registers. */
				break;
			case WIIUSE_NUNCHUK_INSERTED: 
			case WIIUSE_NUNCHUK_REMOVED:
			case  WIIUSE_CLASSIC_CTRL_INSERTED: 
			case WIIUSE_CLASSIC_CTRL_REMOVED:
			case WIIUSE_GUITAR_HERO_3_CTRL_INSERTED:
			case WIIUSE_GUITAR_HERO_3_CTRL_REMOVED:
				break;
			}
		}
	} 
	return 0;
}
Exemplo n.º 23
0
/**
 * Find a wiimote or wiimotes.
 * @param nbMaxWiimotes The number of wiimotes.
 * @param timeout The number of seconds before the search times out.
 * @return The number of wiimotes found.
 */
JNIEXPORT jint JNICALL Java_wiiusej_WiiUseApi_find
(JNIEnv *env, jobject obj, jint nbMaxWiimotes, jint timeout) {
	return wiiuse_find(wiimotes, nbMaxWiimotes, timeout);
}
FWiimoteInputDevice::FWiimoteInputDevice(const TSharedRef<FGenericApplicationMessageHandler>& InMessageHandler)
    : MessageHandler(InMessageHandler)
{
    InitialButtonRepeatDelay = 0.2f;
    ButtonRepeatDelay = 0.1f;

    Buttons[0] = FGamepadKeyNames::FaceButtonBottom;            //WIIMOTE_BUTTON_A
    Buttons[1] = FGamepadKeyNames::FaceButtonRight;         //WIIMOTE_BUTTON_B
    Buttons[2] = FGamepadKeyNames::FaceButtonLeft;          //WIIMOTE_BUTTON_ONE
    Buttons[3] = FGamepadKeyNames::FaceButtonTop;               //WIIMOTE_BUTTON_TWO
    Buttons[4] = FGamepadKeyNames::SpecialRight;                //WIIMOTE_BUTTON_PLUS
    Buttons[5] = FGamepadKeyNames::SpecialLeft;             //WIIMOTE_BUTTON_MINUS
    Buttons[6] = FGamepadKeyNames::DPadUp;                  //WIIMOTE_BUTTON_UP
    Buttons[7] = FGamepadKeyNames::DPadDown;                    //WIIMOTE_BUTTON_DOWN
    Buttons[8] = FGamepadKeyNames::DPadLeft;                    //WIIMOTE_BUTTON_LEFT
    Buttons[9] = FGamepadKeyNames::DPadRight;                   //WIIMOTE_BUTTON_RIGHT
    Buttons[10] = FGamepadKeyNames::LeftShoulder;               //NUNCHUK_BUTTON_C
    Buttons[11] = FGamepadKeyNames::LeftTriggerThreshold;       //NUNCHUK_BUTTON_Z
    Buttons[12] = FGamepadKeyNames::LeftStickUp;                //NUNCHUK_JOYSTICK
    Buttons[13] = FGamepadKeyNames::LeftStickDown;          //NUNCHUK_JOYSTICK
    Buttons[14] = FGamepadKeyNames::LeftStickLeft;          //NUNCHUK_JOYSTICK
    Buttons[15] = FGamepadKeyNames::LeftStickRight;         //NUNCHUK_JOYSTICK

    int found, connected;
    /*
    *   Initialize an array of wiimote objects.
    *
    *   The parameter is the number of wiimotes I want to create.
    */
    GWiimotes = 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(GWiimotes, MAX_WIIMOTES, 5);
    if (!found)
    {
        UE_LOG(LogWiimote, Log, TEXT("No wiimotes found."));
        return;
    }

    /*
    *   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(GWiimotes, MAX_WIIMOTES);
    if (connected)
    {
        UE_LOG(LogWiimote, Log, TEXT("Connected to %i wiimotes (of %i found)."), connected, found);
    }
    else
    {
        UE_LOG(LogWiimote, Log, TEXT("Failed to connect to any wiimote."));
        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(GWiimotes[0], WIIMOTE_LED_1);
    wiiuse_set_leds(GWiimotes[1], WIIMOTE_LED_2);
    wiiuse_set_leds(GWiimotes[2], WIIMOTE_LED_3);
    wiiuse_set_leds(GWiimotes[3], WIIMOTE_LED_4);

    UE_LOG(LogWiimote, Log, TEXT("\nControls:"));
    UE_LOG(LogWiimote, Log, TEXT("\tB toggles rumble."));
    UE_LOG(LogWiimote, Log, TEXT("\t+ to start Wiimote accelerometer reporting, - to stop"));
    UE_LOG(LogWiimote, Log, TEXT("\tUP to start IR camera (sensor bar mode), DOWN to stop."));
    UE_LOG(LogWiimote, Log, TEXT("\t1 to start Motion+ reporting, 2 to stop."));
    UE_LOG(LogWiimote, Log, TEXT("\n"));

    FMemory::Memzero(ControllerStates);
    for (int i = 0; i < MAX_WIIMOTES; ++i)
    {
        ControllerStates[i].ControllerId = i;
        ControllerStates[i].Wiimote = GWiimotes[i];
    }
}
//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;
}
Exemplo n.º 26
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