Exemplo n.º 1
0
void WiiCursor::process(std::vector<WiimoteData>& wiimotes, bool const& running) {
    // Sets up the Wiimotes
    for (std::vector<WiimoteData>::iterator iter = wiimotes.begin(); iter != wiimotes.end(); ++iter) {
	DEBUG_MSG(1, "Enabling reporting of events and non-block signal checking in Wiimote #%d...\n", iter-wiimotes.begin());
	cwiid_enable(iter->wiimote, CWIID_FLAG_MESG_IFC);
	cwiid_disable(iter->wiimote, CWIID_FLAG_NONBLOCK);
    }

    // Sets up itself
    m_thread_data.running = &running; // No need to check anything here since running is a reference

    while (running) {
	std::vector<WiiEvent> events;
	get_wiis_event_data(wiimotes, m_thread_data.ir, events);
	for (std::vector<WiiEvent>::const_iterator iter = events.begin(); iter != events.end(); ++iter) {
	    switch (iter->type) {
		case WII_EVENT_TYPE_BUTTON:
		    //running = false; // NOTE: Not handling any key events for now
		    break;
		case WII_EVENT_TYPE_IR:
		    process_ir_events(iter->ir, iter->transform);
		    break;
		case WII_EVENT_TYPE_NON_EVENT:
		default:
		    break;
	    }
	}
    }
}
Exemplo n.º 2
0
int main(int argc, char *argv[]) 
{
  cwiid_wiimote_t *wiimote;	/* wiimote handle */
  //struct cwiid_state state;	/* wiimote state */
  bdaddr_t bdaddr;	/* bluetooth device address */
  //unsigned char mesg = 0;
  //unsigned char led_state = 0;
  unsigned char rpt_mode = 0;
  //unsigned char rumble = 0;

  /* Make stdout unbuffered, which is useful for piping the output of
   * this program into a timestamping utility, such as tai64n(1) */
  setvbuf(stdout, NULL, _IOLBF, 0);

  cwiid_set_err(err);

  /* Connect to address given on command-line, if present */
  if (argc > 1) {
    str2ba(argv[1], &bdaddr);
  }
  else {
    bdaddr = *BDADDR_ANY;
  }

  printf("Put Wiimote in discoverable mode now (press 1+2)...\n");
  if (!(wiimote = cwiid_open(&bdaddr, 0))) {
    fprintf(stderr, "Unable to connect to wiimote\n");
    exit(1);
  }
  if (cwiid_set_mesg_callback(wiimote, cwiid_callback)) {
    fprintf(stderr, "Unable to set message callback\n");
    exit(1);
  }

  toggle_bit(rpt_mode, CWIID_RPT_ACC);
  toggle_bit(rpt_mode, CWIID_RPT_BTN);
  set_rpt_mode(wiimote, rpt_mode);


  if (cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC)) {
    fprintf(stderr, "Unable to set message flag\n");
    exit(1);
  }


  while(1) {
    sleep(1);
  }
}
Exemplo n.º 3
0
static void wiimote_report(t_wiimote*x, t_symbol*s, int onoff)
{
  int flag=-1;
  if(gensym("status")==s) flag=CWIID_RPT_STATUS;
  else if(gensym("button")==s) flag=CWIID_RPT_BTN;
  else if(gensym("acceleration")==s) flag=CWIID_RPT_ACC;
  else if(gensym("ir")==s) flag=CWIID_RPT_IR;
#ifdef CWIID_RPT_NUNCHUK
  else if(gensym("nunchuk")==s) flag=CWIID_RPT_NUNCHUK;
#endif
#ifdef CWIID_RPT_CLASSIC
  else if(gensym("classic")==s) flag=CWIID_RPT_CLASSIC;
#endif
#ifdef CWIID_RPT_BALANCE
  else if(gensym("balance")==s) flag=CWIID_RPT_BALANCE;
#endif
#ifdef CWIID_RPT_MOTIONPLUS
  else if(gensym("motionplus")==s) flag=CWIID_RPT_MOTIONPLUS;
#endif
  else if(gensym("ext")==s) flag=CWIID_RPT_EXT;
  else {
    pd_error(x, "unknown report mode '%s'", s->s_name);
  }

#ifdef CWIID_RPT_MOTIONPLUS
  if(CWIID_RPT_MOTIONPLUS==flag) {
      if(x->connected) {
	int err=0;
	if(onoff) {
	  err=cwiid_enable(x->wiimote, CWIID_FLAG_MOTIONPLUS);
	} else {
	  err=cwiid_disable(x->wiimote, CWIID_FLAG_MOTIONPLUS);
	}
	if(err) {
	  pd_error(x, "turning %s motionplus returned %d", (flag?"on":"off"), err);
	}
      }
  }
#endif

  if(flag!=-1) {
    if(onoff) {
      x->reportMode |= flag;
    } else {
      x->reportMode &= ~flag;
    }
  }
   wiimote_resetReportMode(x);
}
Exemplo n.º 4
0
int wii_open()
{
  bdaddr = *BDADDR_ANY;

  accmode = WII_ACCMODE_BTN;

  as = 0;
  speedacc = 0;

  gettimeofday(&t1,NULL);

  printf ( "N: wii: trying to find wiimote...\n" );
  if (!(wiimote = cwiid_open(&bdaddr, 0))) {
    fprintf(stderr, "E: wii: Unable to connect to wiimote\n");
    return -1;
  }
  printf ("N: wii: open\n" );
  if (cwiid_set_mesg_callback(wiimote, cwiid_callback)) {
    fprintf(stderr, "E: wii: Unable to set message callback\n");
    cwiid_close(wiimote);
    return -1;
  }
  printf ("N: wii: callback\n" );
  if (cwiid_set_led(wiimote, 0x02)) {
    fprintf(stderr, "Error setting LEDs \n");
    cwiid_close(wiimote);
    return -1;
  }
  printf ("N: wii: led\n" );
  if (cwiid_set_rpt_mode(wiimote, CWIID_RPT_BTN | CWIID_RPT_ACC)) {
    fprintf(stderr, "E: wii: could not set report mode\n");
    cwiid_close(wiimote);
    return -1;
  }
  printf ("N: wii: report mode set\n" );

  if (cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC | CWIID_FLAG_CONTINUOUS)) {
    fprintf(stderr, "E: wii: could not enable reporting\n");
    cwiid_close(wiimote);
    return -1;
  }
  printf ("N: wii: reporting enabled\n" );

  wii_connected = 1;


  return 0;
}
Exemplo n.º 5
0
Wiimote::Wiimote(bdaddr_t addr,QObject *parent) : QObject(parent), buttons_old(0), addr(addr) {
    Q_ASSERT(instance == NULL);
    
    qDebug("setting up cwiid");
    {
        cwiid_set_err(cwiid_err_callback);
        instance = cwiid_open(&addr,0);
        qinstance = this;
        Q_ASSERT(instance);
        cwiid_set_mesg_callback(instance,cwiid_msg_callback);
        cwiid_enable(instance,CWIID_FLAG_MESG_IFC);
        cwiid_command(instance,CWIID_CMD_LED,CWIID_LED2_ON | CWIID_LED3_ON);
        //cwiid_command(instance,CWIID_CMD_RPT_MODE,CWIID_RPT_EXT | CWIID_RPT_BTN | CWIID_RPT_ACC | CWIID_RPT_STATUS | CWIID_RPT_IR);
        cwiid_command(instance,CWIID_CMD_RPT_MODE,CWIID_RPT_EXT | CWIID_RPT_BTN | CWIID_RPT_STATUS | CWIID_RPT_ACC);
    }
    
}
Exemplo n.º 6
0
static PyObject *Wiimote_enable(Wiimote *self, PyObject *args, PyObject *kwds)
{
	static char *kwlist[] = {"flags", NULL};
	int flags;

	if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:cwiid.Wiimote.enable",
	                                 kwlist, &flags)) {
		return NULL;
	}

	if (cwiid_enable(self->wiimote, flags)) {
		PyErr_SetString(PyExc_RuntimeError, "Error enabling wiimote flags");
		return NULL;
	}

	Py_RETURN_NONE;
}
// Creadora del device
TPFC_device_wiimote::TPFC_device_wiimote(int ident, string bta):TPFC_device(ident){
  // creamos el bufer de datos
  data = new TrackingPFC_data(TrackingPFC_data::TPFCDATA2D);
  // sobreescribimos el mensaje de error por defecto con el nuestro, para evitar spam
  cwiid_set_err(err);

  // comprobamos si tenemos una direccion asignada
  if (bta.compare("")==0){
    // conectarse al primer wiimote que se encuentre
    bdaddr = *BDADDR_ANY;// bluetooth device address
  }else{
    // conectarse al wiimote con direccion == bta
    str2ba(bta.c_str(), &bdaddr);
  }

  // Conectar los wiimotes
  printf("Pon el wiimote en modo discoverable (pulsa 1+2)...\n");
  wiimote=NULL;
  while (!wiimote){
    if ( !(wiimote = cwiid_open(&bdaddr, 0)) ){
	    printf("Esperando al wiimote (pulsa 1+2)...\n");
    }
  }

  // registramos el wiimote para poder identificar despues los callbacks 
  registerwiimote(wiimote, this);
  
  // registramos nuestro callback con el wiimote
  if (cwiid_set_mesg_callback(wiimote, callback)) {
	  fprintf(stderr, "No se ha podido registrar el callback\n");
  }
  // activamos el paso de mensajes desde el wiimote
  if (cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC)) {
	  fprintf(stderr, "Error activando los mensajes\n");
  }
  // configuramos el report mode
  unsigned char rpt_mode = 0;
  toggle_bit(rpt_mode, CWIID_RPT_IR);
  set_rpt_mode(wiimote, rpt_mode);

  printf("Wiimote conectado!\n");

}
Exemplo n.º 8
0
Arquivo: main.c Projeto: bobbens/cwiid
void menuConnect_activate(void)
{
	char reset_bdaddr = 0;

	if (bacmp(&bdaddr, BDADDR_ANY) == 0) {
		reset_bdaddr = 1;
	}
	message(GTK_MESSAGE_INFO,
	        "Put Wiimote in discoverable mode (press 1+2) and press OK",
	         GTK_WINDOW(winMain));
	if ((wiimote = cwiid_open(&bdaddr, CWIID_FLAG_MESG_IFC)) == NULL) {
		message(GTK_MESSAGE_ERROR, "Unable to connect", GTK_WINDOW(winMain));
		status("No connection");
	}
	else if (cwiid_set_mesg_callback(wiimote, &cwiid_callback)) {
		message(GTK_MESSAGE_ERROR, "Error setting callback",
		        GTK_WINDOW(winMain));
		if (cwiid_close(wiimote)) {
			message(GTK_MESSAGE_ERROR, "Error on disconnect",
			        GTK_WINDOW(winMain));
		}
		wiimote = NULL;
		status("No connection");
	}
	else {
		status("Connected");
		if (cwiid_get_acc_cal(wiimote, CWIID_EXT_NONE, &wm_cal)) {
			message(GTK_MESSAGE_ERROR, "Unable to retrieve accelerometer "
			        "calibration", GTK_WINDOW(winMain));
		}
		set_gui_state();
		set_report_mode();
		cwiid_enable(wiimote, CWIID_FLAG_MOTIONPLUS);
		cwiid_request_status(wiimote);
	}

	if (reset_bdaddr) {
		bdaddr = *BDADDR_ANY;
	}
}
Exemplo n.º 9
0
 void WiiTrackExtension::cwiidConnect()
 {
   bdaddr_t bdaddr;        /* bluetooth device address */
   bdaddr = *BDADDR_ANY;
   
   // connect
   if (!(m_wiimote = cwiid_connect(&bdaddr, 0))) {
     qDebug() << "Unable to connect to wiimote";
     return;
   } else {
     qDebug() << "Connected to wiimote";
     m_actions.at(StartIndex)->setEnabled(false);
     m_actions.at(StopIndex)->setEnabled(true);
   }
   
   // Set the callback function
   if (cwiid_set_mesg_callback(m_wiimote, cwiid_callback)) {
     qDebug() << "Unable to set message callback";
     cwiidDisconnect(); 
   }
   
   // set the report mode
   unsigned char rpt_mode = 0;
   toggle_bit(rpt_mode, CWIID_RPT_IR);
   cwiidSetReportMode(m_wiimote, rpt_mode);
   
   // enable messaging
   if (cwiid_enable(m_wiimote, CWIID_FLAG_MESG_IFC)) {
     qDebug() << "Error enabling messages";
   }
     
   m_lastDistance = 0.0;
   m_lastDot1x = 0.0;
   m_lastDot1y = 0.0;
   m_lastDot2x = 0.0;
   m_lastDot2y = 0.0;
 }
Exemplo n.º 10
0
int wmplugin_init(int id, cwiid_wiimote_t *wiimote)
{
	plugin_id = id;

	ccd_center.valid=1;
	ccd_center.size=1;
	ccd_center.pos[0]=512;
	ccd_center.pos[1]=384;
	motionplus_cal.zero[0] = 8050;
	motionplus_cal.zero[1] = 8000;
	motionplus_cal.zero[2] = 8580;
	data.buttons = 0;
	data.axes[0].valid = 1;
	data.axes[1].valid = 1;
	data.axes[2].valid = 1;
	data.axes[3].valid = 1;
	data.axes[4].valid = 1;
	data.axes[4].value = 0;
	data.axes[5].valid = 1;

	if (wmplugin_set_rpt_mode(id, CWIID_RPT_ACC | CWIID_RPT_MOTIONPLUS | CWIID_RPT_IR | CWIID_RPT_BTN)) {
		return -1;
	}

	if(cwiid_enable(wiimote, CWIID_FLAG_MOTIONPLUS)) {
		wmplugin_err(id, "You need wiimotion plus to run this plugin");
		return -1;
	}

	if (cwiid_get_acc_cal(wiimote, CWIID_EXT_NONE, &acc_cal)) {
		wmplugin_err(id, "accelerometers calibration error");
		return -1;
	}

	return 0;
}
Exemplo n.º 11
0
int main()
{
	cwiid_wiimote_t *wiimote = NULL;

	struct cwiid_state state;

	double wlt, wrt, wlb, wrb;
	double bal_x, bal_y;

	if ((wiimote = cwiid_open(BDADDR_ANY, 0)) == NULL) {
		fputs("Unable to connect\n", stderr);
		return EXIT_FAILURE;
	}
	fputs("connected\n", stdout);

	sleep(2);

	if (cwiid_set_led(wiimote, 1))
		fputs("Unable to set LED state\n", stderr);

	if (cwiid_get_balance_cal(wiimote, &balance_cal))
		fputs("unable to retrieve balance calibration\n", stderr);

	printf("bcal %d/%d/%d %d/%d/%d\n     %d/%d/%d %d/%d/%d\n",
		balance_cal.left_top[0],
		balance_cal.left_top[1],
		balance_cal.left_top[2],
		balance_cal.right_top[0],
		balance_cal.right_top[1],
		balance_cal.right_top[2],
		balance_cal.left_bottom[0],
		balance_cal.left_bottom[1],
		balance_cal.left_bottom[2],
		balance_cal.right_bottom[0],
		balance_cal.right_bottom[1],
		balance_cal.right_bottom[2]
	);

	if (!cwiid_get_state(wiimote, &state))
		printf("battery at %d%%\n",
			(int)(100.0 * state.battery / CWIID_BATTERY_MAX));

	if (cwiid_set_mesg_callback(wiimote, cwiid_callback))
		fputs("cannot set callback. buttons won't work.\n", stderr);
	
	if (cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC))
		fputs("cannot enable callback. buttons won't work.\n", stderr);

	if (cwiid_set_rpt_mode(wiimote,
			CWIID_RPT_ACC | CWIID_RPT_STATUS | CWIID_RPT_EXT))
		fputs("cannot set report mode. buttons won't work.\n", stderr);

	while (1) {
		cwiid_get_state(wiimote, &state);

		wlt = weight(state.ext.balance.left_top, balance_cal.left_top);
		wrt = weight(state.ext.balance.right_top, balance_cal.right_top);
		wlb = weight(state.ext.balance.left_bottom, balance_cal.left_bottom);
		wrb = weight(state.ext.balance.right_bottom, balance_cal.right_bottom);

		bal_x = (wrt + wrb) / (wlt + wlb);
		if (bal_x > 1)
			bal_x = ((wlt + wlb) / (wrt + wrb) * (-1.0)) + 1.0;
		else
			bal_x -= 1;

		bal_y = (wlt + wrt) / (wlb + wrb);
		if (bal_y > 1)
			bal_y = ((wlb + wrb) / (wlt + wrt) * (-1.0)) + 1.0;
		else
			bal_y -= 1;

		printf("%6.1f kg  %6.1f kg    %04x %04x     (%5.1f kg)\n%6.1f kg  %6.1f kg    %04x %04x\n\n",
			wlt, wrt,
			state.ext.balance.left_top, state.ext.balance.right_top,
			wlt + wrt + wlb + wrb,
			wlb, wrb,
			state.ext.balance.left_bottom, state.ext.balance.right_bottom
		);
		printf("balance %6f %6f\n\n", bal_x, bal_y);
		sleep(1);
	}

	return EXIT_SUCCESS;
}
Exemplo n.º 12
0
/*!
 @brief Driver for Wiimote operation menu

 Handles parsing wiimote input to determine correct operating mode.

 This function will never exit once entered.
 */
ErrorID_t main_menu(cwiid_wiimote_t *wiimote,
		volatile WiimoteStatusDataType *wiimote_status)
{
	ErrorID_t error;
	uint8_t menu_count;
	uint16_t last_button_state = 0;
	WII_OPERATE_STATE wii_operate_state = WII_OPERATE_INIT_MENU;

	write_status_led(STATUS_LED_OFF, 0);

	for (;;)
	{
		switch (wii_operate_state)
		{
		case WII_OPERATE_INIT_MENU:
			menu_count = 0;
			last_button_state = 0;
			wii_operate_state = WII_OPERATE_WAIT_FOR_BUTTON_PRESS;
			write_status_led(STATUS_LED_FLASH, 500);

			if (0 > cwiid_set_led(wiimote, CWIID_LED1_ON))
				return WII_ERROR_TRANSMIT;
			if (0 > cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC))
				return WII_ERROR_TRANSMIT;
			if (0 > cwiid_set_rpt_mode(wiimote, CWIID_RPT_BTN))
				return WII_ERROR_TRANSMIT;

			/* Menu */
			debug_print("%s", MENU);
			set_lcd(0, "Connected.");
			set_lcd(1, "Select Mode.");
			break;

		case WII_OPERATE_WAIT_FOR_BUTTON_PRESS:
			error = wait_for_wiimotedata(wiimote_status, 2000);
			if (error == ERR_WII_DATA_TIMEOUT)
			{
				set_lcd(0, "Select Mode");
				set_lcd(1, (char *) lcd_menu_strings[menu_count]);
				if (++menu_count >= MENU_ENTRIES)
					menu_count = 0;
			}
			else if (0 < error)
			{
				shutdown_application(error);
			}
			else if (wiimote_status->button_data != last_button_state)
			{
				last_button_state = wiimote_status->button_data;
				if (last_button_state & CWIID_BTN_A) // collect acceleration data
				{
					wii_operate_state = WII_OPERATE_DISPLAY_ACCELEROMETER_INFO;
				}
				else if (last_button_state & CWIID_BTN_B) // collect IR data
				{
					wii_operate_state = WII_OPERATE_DISPLAY_IR_STATUS;
				}
				else if (last_button_state & CWIID_BTN_HOME) // reset
				{
					shutdown_application(0);
				}
				else if (last_button_state & CWIID_BTN_PLUS)
				{
					sensor_cutoff_fwd += 5;
					if (sensor_cutoff_fwd > 100)
						sensor_cutoff_fwd = 100;
#if REVERSE_SENSOR_PRESENT
					sensor_cutoff_rev += 5;
					if (sensor_cutoff_rev > 100)
					sensor_cutoff_rev = 100;
#endif

				}
				else if (last_button_state & CWIID_BTN_MINUS)
				{
					sensor_cutoff_fwd -= 5;
					if (sensor_cutoff_fwd < 0)
						sensor_cutoff_fwd = 0;
#if REVERSE_SENSOR_PRESENT
					sensor_cutoff_rev -= 5;
					if (sensor_cutoff_rev < 0)
					sensor_cutoff_rev = 0;
#endif
				}
				else if (last_button_state & (CWIID_BTN_2 | CWIID_BTN_1))
				{
					wii_operate_state = WII_OPERATE_DISPLAY_BUTTON_STATUS;
				}
			}
			break;

		case WII_OPERATE_DISPLAY_ACCELEROMETER_INFO:
			if (ERR_NONE != acceleration_mode(wiimote, wiimote_status))
				wii_operate_state = WII_OPERATE_ERROR_STATE;
			else
				wii_operate_state = WII_OPERATE_INIT_MENU;
			break;

		case WII_OPERATE_DISPLAY_IR_STATUS:
			if (ERR_NONE != infrared_mode(wiimote, wiimote_status))
				wii_operate_state = WII_OPERATE_ERROR_STATE;
			else
				wii_operate_state = WII_OPERATE_INIT_MENU;
			break;

		case WII_OPERATE_DISPLAY_BUTTON_STATUS:
			if (ERR_NONE != button_mode(wiimote, wiimote_status))
				wii_operate_state = WII_OPERATE_ERROR_STATE;
			else
				wii_operate_state = WII_OPERATE_INIT_MENU;
			break;

		case WII_OPERATE_ERROR_STATE:
			if (ERR_NONE == error_mode(wiimote, wiimote_status))
				wii_operate_state = WII_OPERATE_INIT_MENU;
			break;
		}
	}
	return ERR_NONE;
}
Exemplo n.º 13
0
int main(int argc, char** argv) {
    cwiid_wiimote_t *wiimote;
    struct cwiid_state state;
    bdaddr_t bdaddr;
    int exit = 0;
    point3Df dimensions3PtsCap[3];

    bdaddr = *BDADDR_ANY;

    printf("connecting!\n");
    
    if (!(wiimote = cwiid_open(&bdaddr, 0))) {
        fprintf(stderr, "Unable to connect to wiimote\n");
        return -1;
    }

    printf("connected\n");

    cwiid_set_led(wiimote, CWIID_LED1_ON | CWIID_LED4_ON);
    cwiid_set_rpt_mode(wiimote, CWIID_RPT_STATUS | CWIID_RPT_IR);

    //*

    if (cwiid_set_mesg_callback(wiimote, cwiid_callback)) {
        fprintf(stderr, "Unable to set message callback\n");
    }
    if (cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC)) {
        fprintf(stderr, "Error enabling messages\n");
    }
    // */


    dimensions3PtsCap[0].x = 70;
    dimensions3PtsCap[0].y = 80;
    dimensions3PtsCap[0].z = 100;

    Initialize3PCapModel(dimensions3PtsCap);

    while (!exit) {
        switch(getchar()) {
            case 'q':
                exit = 1;
                break;
            default:
                if (cwiid_get_state(wiimote, &state)) {
                    printf("Error getting state\n");
                } else {
                    print_state(&state);
                }
        }
    }

    /*
    if (getchar()) {
        printf("Exiting\n");
    }
     **/

    if (cwiid_close(wiimote)) {
        printf("Error on wiimote disconnect\n");
        return -1;
    }
    
    return (EXIT_SUCCESS);
}
Exemplo n.º 14
0
int main(int argc, char *argv[])
{
	cwiid_wiimote_t *wiimote;	/* wiimote handle */
	struct cwiid_state state;	/* wiimote state */
	bdaddr_t bdaddr;	/* bluetooth device address */
	unsigned char mesg = 0;
	unsigned char led_state = 0;
	unsigned char rpt_mode = 0;
	unsigned char rumble = 0;
	int exit = 0;

	deviceState.numButtons = NUM_BUTTONS;
	deviceState.numAxis = NUM_AXIS;
	deviceState.numSensors = NUM_SENSORS;
	deviceState.axisValues = NULL;
	deviceState.buttonValues = NULL;
	deviceState.sensorValues = new SensorData[NUM_SENSORS];

	cwiid_set_err(err);

	if (argc < 2) {
		fprintf(stderr, "Usage: %s HOSTNAME\n\n", argv[0]);
		return 1;
	} // if

// TODO: parse data from config file!!!
	/* Connect to address given on command-line, if present */
	if (argc > 2) {
		str2ba(argv[2], &bdaddr);
	}
	else {
		bdaddr = *BDADDR_ANY;
	}

	/* Connect to the wiimote */
	printf("Put Wiimote in discoverable mode now (press 1+2)...\n");
	if (!(wiimote = cwiid_open(&bdaddr, 0))) {
		fprintf(stderr, "Unable to connect to wiimote\n");
		return -1;
	}
	if (cwiid_set_mesg_callback(wiimote, cwiid_callback)) {
		fprintf(stderr, "Unable to set message callback\n");
	}

	toggle_bit(led_state, CWIID_LED1_ON);
	set_led_state(wiimote, led_state);
	if (cwiid_get_state(wiimote, &state)) {
		fprintf(stderr, "Error getting state\n");
	}
	print_state(&state);
	toggle_bit(rpt_mode, CWIID_RPT_IR);
	toggle_bit(rpt_mode, CWIID_RPT_BTN);
//	toggle_bit(rpt_mode, CWIID_RPT_ACC);
	set_rpt_mode(wiimote, rpt_mode);
	if (cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC)) {
	    fprintf(stderr, "Error enabling messages\n");
	} else {
	    mesg = 1;
	}

	if (initNetwork(argv[1], CLIENTPORT)) {
		fprintf(stderr, "Error at network initialization\n");
		return -1;
	} // if

	bool running = true;

	while (running) {sleep(1);}

	if (cwiid_close(wiimote)) {
		fprintf(stderr, "Error on wiimote disconnect\n");
		return -1;
	}

	return 0;
}
Exemplo n.º 15
0
int main(int argc, char *argv[]) 
{
  if (argc != 2) {
    printf("Usage: %s 3, where 3 is the number of gestures to recognize\n", argv[0]);
    exit(1);
  }
  n_gestures = atoi(argv[1]);
  hmms = malloc(n_gestures * sizeof(HmmStateRef));

  int n_states = n_gestures+3;
  int n_obs = 17;

  for(int i = 0; i < n_gestures; i++) {
    hmms[i] = hmm_new(n_states, n_obs);
  }

  cwiid_wiimote_t *wiimote;	/* wiimote handle */
  //struct cwiid_state state;	/* wiimote state */
  bdaddr_t bdaddr;	/* bluetooth device address */
  //unsigned char mesg = 0;
  //unsigned char led_state = 0;
  unsigned char rpt_mode = 0;
  //unsigned char rumble = 0;

  /* Make stdout unbuffered, which is useful for piping the output of
   * this program into a timestamping utility, such as tai64n(1) */
  setvbuf(stdout, NULL, _IOLBF, 0);

  cwiid_set_err(err);

#if WE_ACTUALLY_CARE_ABOUT_BLUETOOTH
  /* Connect to address given on command-line, if present */
  if (argc > 1) {
    str2ba(argv[1], &bdaddr);
  }
  else {
#endif
    bdaddr = *BDADDR_ANY;
#if WE_ACTUALLY_CARE_ABOUT_BLUETOOTH
  }
#endif

  printf("Put Wiimote in discoverable mode now (press 1+2)...\n");
  if (!(wiimote = cwiid_open(&bdaddr, 0))) {
    fprintf(stderr, "Unable to connect to wiimote\n");
    exit(1);
  }
  if (cwiid_set_mesg_callback(wiimote, cwiid_callback)) {
    fprintf(stderr, "Unable to set message callback\n");
    exit(1);
  }

  toggle_bit(rpt_mode, CWIID_RPT_ACC);
  toggle_bit(rpt_mode, CWIID_RPT_BTN);
  set_rpt_mode(wiimote, rpt_mode);


  if (cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC)) {
    fprintf(stderr, "Unable to set message flag\n");
    exit(1);
  }


  while(1) {
    sleep(1);
  }
}
Exemplo n.º 16
0
int main(int argc, char *argv[])
{
	cwiid_wiimote_t *wiimote;	/* wiimote handle */
	struct cwiid_state state;	/* wiimote state */
	bdaddr_t bdaddr;	/* bluetooth device address */
	unsigned char mesg = 0;
	unsigned char led_state = 0;
	unsigned char rpt_mode = 0;
	unsigned char rumble = 0;
	int exit = 0;

	cwiid_set_err(err);

	/* Connect to address given on command-line, if present */
	if (argc > 1) {
		str2ba(argv[1], &bdaddr);
	}
	else {
		bdaddr = *BDADDR_ANY;
	}

	/* Connect to the wiimote */
	printf("Put Wiimote in discoverable mode now (press 1+2)...\n");
	if (!(wiimote = cwiid_open(&bdaddr, 0))) {
		fprintf(stderr, "Unable to connect to wiimote\n");
		return -1;
	}
	if (cwiid_set_mesg_callback(wiimote, cwiid_callback)) {
		fprintf(stderr, "Unable to set message callback\n");
	}

	printf("Note: To demonstrate the new API interfaces, wmdemo no longer "
	       "enables messages by default.\n"
		   "Output can be gathered through the new state-based interface (s), "
	       "or by enabling the messages interface (c).\n");

	/* Menu */
	printf("%s", MENU);

	while (!exit) {
		switch (getchar()) {
		case '1':
			toggle_bit(led_state, CWIID_LED1_ON);
			set_led_state(wiimote, led_state);
			break;
		case '2':
			toggle_bit(led_state, CWIID_LED2_ON);
			set_led_state(wiimote, led_state);
			break;
		case '3':
			toggle_bit(led_state, CWIID_LED3_ON);
			set_led_state(wiimote, led_state);
			break;
		case '4':
			toggle_bit(led_state, CWIID_LED4_ON);
			set_led_state(wiimote, led_state);
			break;
		case '5':
			toggle_bit(rumble, 1);
			if (cwiid_set_rumble(wiimote, rumble)) {
				fprintf(stderr, "Error setting rumble\n");
			}
			break;
		case 'a':
			toggle_bit(rpt_mode, CWIID_RPT_ACC);
			set_rpt_mode(wiimote, rpt_mode);
			break;
		case 'b':
			toggle_bit(rpt_mode, CWIID_RPT_BTN);
			set_rpt_mode(wiimote, rpt_mode);
			break;
		case 'e':
			/* CWIID_RPT_EXT is actually
			 * CWIID_RPT_NUNCHUK | CWIID_RPT_CLASSIC */
			toggle_bit(rpt_mode, CWIID_RPT_EXT);
			set_rpt_mode(wiimote, rpt_mode);
			break;
		case 'i':
			/* libwiimote picks the highest quality IR mode available with the
			 * other options selected (not including as-yet-undeciphered
			 * interleaved mode */
			toggle_bit(rpt_mode, CWIID_RPT_IR);
			set_rpt_mode(wiimote, rpt_mode);
			break;
		case 'm':
			if (!mesg) {
				if (cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC)) {
					fprintf(stderr, "Error enabling messages\n");
				}
				else {
					mesg = 1;
				}
			}
			else {
				if (cwiid_disable(wiimote, CWIID_FLAG_MESG_IFC)) {
					fprintf(stderr, "Error disabling message\n");
				}
				else {
					mesg = 0;
				}
			}
			break;
		case 'p':
			printf("%s", MENU);
			break;
		case 'r':
			if (cwiid_request_status(wiimote)) {
				fprintf(stderr, "Error requesting status message\n");
			}
			break;
		case 's':
			if (cwiid_get_state(wiimote, &state)) {
				fprintf(stderr, "Error getting state\n");
			}
			print_state(&state);
			break;
		case 't':
			toggle_bit(rpt_mode, CWIID_RPT_STATUS);
			set_rpt_mode(wiimote, rpt_mode);
			break;
		case 'x':
			exit = -1;
			break;
		case '\n':
			break;
		default:
			fprintf(stderr, "invalid option\n");
		}
	}

	if (cwiid_close(wiimote)) {
		fprintf(stderr, "Error on wiimote disconnect\n");
		return -1;
	}

	return 0;
}
Exemplo n.º 17
0
int main()
{
	cwiid_wiimote_t *wiimote = NULL;

	struct cwiid_state state;

	uint8_t ledstate = 0x0f;

	uint8_t cnt = 0;
	uint8_t led[4] = {0, 0, 0, 0};

	uint8_t step = 0;
	uint8_t x = 0;

	uint8_t i;

	uint8_t next_mode = 0;

	puts("Press 1+2 to connect wiimote.");
	if ((wiimote = cwiid_open(BDADDR_ANY, 0)) == NULL) {
		fputs("Unable to connect\n", stderr);
		return EXIT_FAILURE;
	}
	fputs("connected\n", stdout);

	sleep(2);

	set_led_fun(0);

	if (cwiid_get_acc_cal(wiimote, CWIID_EXT_NONE, &wm_cal))
		fputs("unable to retrieve accelerometer calibration\n", stderr);

	if (!cwiid_get_state(wiimote, &state))
		printf("battery at %d%%\n",
			(int)(100.0 * state.battery / CWIID_BATTERY_MAX));

	if (cwiid_set_mesg_callback(wiimote, cwiid_callback))
		fputs("cannot set callback. buttons won't work.\n", stderr);
	
/*	cwiid_enable(wiimote, CWIID_FLAG_MOTIONPLUS);
*/
	if (cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC))
		fputs("cannot enable callback. buttons won't work.\n", stderr);

	if (cwiid_set_rpt_mode(wiimote,
			CWIID_RPT_BTN | CWIID_RPT_ACC | CWIID_RPT_STATUS | CWIID_RPT_EXT))
		fputs("cannot set report mode. buttons won't work.\n", stderr);

	while (1) {

		if (++cnt >= cnt_max) {
			cnt = 0;
			
			if (++x == x_max) {
				x = 0;

				if (!auto_mode && (++next_mode == 42)) {
					set_led_fun(cur_mode + 1);
					next_mode = 0;
				}
			}

			for (i = 0; i < 4; i++)
				led[i] = f_led[i][x];
		}

		step = cnt % MAX_BRIGHTNESS;

		if (step == 0)
			ledstate = 0x0f;

		for (i = 0; i < 4; i++)
			if (step == led[i])
				ledstate &= ~(1 << i);

		if (cwiid_set_led(wiimote, ledstate))
			fputs("Error setting LED state\n", stderr);
	}

	return EXIT_SUCCESS;
}
Exemplo n.º 18
0
void *passthrough_polling_thread(struct wiimote *wiimote)
{
	unsigned char buf, data;	

	while(1)
	{
		if ((wiimote->passthrough_activate_flag && wiimote->ext == NUNCHUK_MOTIONPLUS_PRESENT) || 
				(wiimote->passthrough_activate_flag && wiimote->ext == CLASSIC_MOTIONPLUS_PRESENT) || 
				(!wiimote->passthrough_activate_flag && wiimote->ext == MOTIONPLUS_PRESENT) || 
				(!wiimote->passthrough_activate_flag && wiimote->ext == EXT_PRESENT_NOT_MOTIONPLUS))
		{
			pthread_mutex_lock(&wiimote->poll_mutex);
			pthread_cond_wait(&wiimote->condition_var,&wiimote->poll_mutex);
			pthread_mutex_unlock(&wiimote->poll_mutex);
		}

		//fprintf(stderr,"passthrough_thread: resume\n");

		if (wiimote->passthrough_activate_flag) {

			//fprintf(stderr,"passthrough_thread: try activating passthrough\n");

			if (wiimote->ext != NUNCHUK_MOTIONPLUS_PRESENT && wiimote->ext != CLASSIC_MOTIONPLUS_PRESENT && wiimote->ext != MOTIONPLUS_PRESENT) {
				/* re-detecting motionplus */
				buf=0xAB;																	//any junk value initialisation
				cwiid_try_read(wiimote, CWIID_RW_REG, 0xA600FE, 1, &buf);					//Checking for the MotionPlus jackin
				if(buf==0x00 || buf==0x04 || buf==0x05 )	
				{														
					cwiid_enable(wiimote, CWIID_FLAG_MOTIONPLUS);
				}
			}

			if (wiimote->ext == MOTIONPLUS_PRESENT) {
				/* motionplus_present trying passthrough */
				data = 0x55;
				cwiid_write(wiimote, CWIID_RW_REG, 0xA400F0, 1, &data);
				data = 0x00;
				cwiid_write(wiimote, CWIID_RW_REG, 0xA400FB, 1, &data);
			}

			usleep(POLL_THREAD_DELAY);
			cwiid_request_status(wiimote);

		} else {

			/* disabling passthrough and/or re-detecting motionplus if necessary */

			/* only do this call if some other expansion is not already connected
			   this works because in this implementation motionplus takes precedence
			   over other kinds of extensions, like nunchuk and classic */

			//fprintf(stderr,"passthrough_thread: reverting to motionplus\n");

			if (wiimote->ext != EXT_PRESENT_NOT_MOTIONPLUS && wiimote->ext != EXT_UNINITIALIZED) {

				//fprintf(stderr,"passthrough_thread: !EXT_PRESENT_NOT_MOTIONPLUS\n");
				data = 0x55;
				cwiid_write(wiimote, CWIID_RW_REG, 0xA400F0, 1, &data);
				data = 0x00;
				cwiid_write(wiimote, CWIID_RW_REG, 0xA400FB, 1, &data);

				buf=0xAB;																	//any junk value initialisation
				cwiid_try_read(wiimote, CWIID_RW_REG, 0xA600FE, 1, &buf);					//Checking for the MotionPlus jackin
				if(buf==0x00 || buf==0x04 || buf==0x05 || buf==0x07)	
				{												
					cwiid_enable(wiimote, CWIID_FLAG_MOTIONPLUS);

					usleep(POLL_THREAD_DELAY);
					cwiid_request_status(wiimote);
				}
			}
		}				
	}

	return NULL;
}