Example #1
0
struct wmplugin_data *wmplugin_exec(int mesg_count, union cwiid_mesg mesg[])
{
	int i;
	uint8_t button;
	struct cwiid_btn_message *btn_mesg;

	uint8_t led_state = (Led1 ? CWIID_LED1_ON : 0)
	                  | (Led2 ? CWIID_LED2_ON : 0)
	                  | (Led3 ? CWIID_LED3_ON : 0)
	                  | (Led4 ? CWIID_LED4_ON : 0);

	if(Battery != 0) {		
		btn_mesg = NULL;
		for (i=0; i < mesg_count; i++) {
			if (mesg[i].type == CWIID_MESG_BTN) {
				btn_mesg = &mesg[i].btn_mesg;
				button = mesg[i].btn_mesg.buttons;
			}
		}

		if (!btn_mesg) {
			return NULL;
		}

		if(button == Button) {
			show_battery();
		} 
		else {
			cwiid_command(wiimote, CWIID_CMD_LED, led_state);
		}
	}
	
	return &data;
}
Example #2
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);
    }
    
}
Example #3
0
static void wiimote_setRumble(t_wiimote *x, t_floatarg f)
{
   if (x->connected)
   {
     if (cwiid_command(x->wiimote, CWIID_CMD_RUMBLE, f)) {
       pd_error(x, "wiimote: could not set rumble");
     }
   }
}
Example #4
0
static void wiimote_resetReportMode(t_wiimote *x)
{
   if (x->connected)   {
      verbose(1, "changing report mode for Wii%02d to %d", x->wiimoteID, x->reportMode);
      if (cwiid_command(x->wiimote, CWIID_CMD_RPT_MODE, x->reportMode)) {
	pd_error(x, "wiimote: could not set report mode.");
      }
   }
}
Example #5
0
/* Tell cwiid the LED states */
void CWiiRemote::SetLedState()
{ //Sets our leds on the wiiremote
#ifdef CWIID_OLD
  if (cwiid_command(m_wiiremoteHandle, CWIID_CMD_LED, m_ledState))
#else  
  if (cwiid_set_led(m_wiiremoteHandle, m_ledState))
#endif
  {
    CPacketLOG log(LOGERROR, "Error setting WiiRemote LED state");
    log.Send(m_Socket, m_MyAddr);
  }
}
Example #6
0
/* Tell cwiid wich data will be reported */
void CWiiRemote::SetRptMode()
{ //Sets our wiiremote to report something, for example IR, Buttons
#ifdef CWIID_OLD
  if (cwiid_command(m_wiiremoteHandle, CWIID_CMD_RPT_MODE, m_rptMode))
#else  
  if (cwiid_set_rpt_mode(m_wiiremoteHandle, m_rptMode))
#endif
  {
    CPacketLOG log(LOGERROR, "Error setting WiiRemote report mode");
    log.Send(m_Socket, m_MyAddr);
  }
}
Example #7
0
static void wiimote_setLED(t_wiimote *x, t_floatarg f)
{
   // some possible values:
   // CWIID_LED0_ON      0x01
   // CWIID_LED1_ON      0x02
   // CWIID_LED2_ON      0x04
   // CWIID_LED3_ON      0x08
   if (x->connected)
   {
     if (cwiid_command(x->wiimote, CWIID_CMD_LED, f)) {
       pd_error(x, "wiimote: could not set LED.");
     }
   }
}
Example #8
0
int wmplugin_init(int id, cwiid_wiimote_t *arg_wiimote)
{
	wiimote = arg_wiimote;

	uint8_t led_state = (Led1 ? CWIID_LED1_ON : 0)
	                  | (Led2 ? CWIID_LED2_ON : 0)
	                  | (Led3 ? CWIID_LED3_ON : 0)
	                  | (Led4 ? CWIID_LED4_ON : 0);

	cwiid_command(wiimote, CWIID_CMD_LED, led_state);
	
	if (wmplugin_set_rpt_mode(id, CWIID_RPT_BTN)) {
		return -1;
	}	

	return 0;
}
void wiimoteController::setup(){
	
	bdaddr_t bdaddr;
	str2ba("00:1A:E9:44:40:E5", &bdaddr);
	if ((wiimote = cwiid_open(&bdaddr, CWIID_FLAG_MESG_IFC)) == NULL){
		cout << "Unable to connect to the Wiimote!" << endl;
	}
	else{
		cout << "Wiimote connected!" << endl;
		//cwiid_set_rumble(wiimote, true);
		//sleep(1);
		//cwiid_set_rumble(wiimote, false);
		//cwiid_enable(wiimote, CWIID_FLAG_NONBLOCK);
		//cwiid_enable(wiimote, CWIID_FLAG_CONTINUOUS);
		cwiid_command(wiimote, CWIID_CMD_RPT_MODE, CWIID_RPT_IR);
		cwiid_set_mesg_callback(wiimote, messageCallback);
	}
}
Example #10
0
void
WiimoteController::connect()
{
  assert(m_wiimote == 0);

  /* Connect to any wiimote */
  bdaddr_t bdaddr = Bluetooth::addr_any;

  /* Connect to address in string WIIMOTE_BDADDR */
  /* str2ba(WIIMOTE_BDADDR, &bdaddr); */

  /* Connect to the wiimote */
  printf("Put Wiimote in discoverable mode now (press 1+2)...\n");

  // wait forever till a Wiimote is found
  m_wiimote = cwiid_open_timeout(&bdaddr, CWIID_FLAG_MESG_IFC, -1);

  {
    std::cout << "Wiimote connected: " << m_wiimote << std::endl;
    if (cwiid_set_mesg_callback(m_wiimote, &WiimoteController::mesg_callback)) {
      std::cerr << "Unable to set message callback" << std::endl;
    }

    if (cwiid_command(m_wiimote, CWIID_CMD_RPT_MODE, 
                      CWIID_RPT_STATUS  |
                      CWIID_RPT_NUNCHUK |
                      CWIID_RPT_ACC     |
                      CWIID_RPT_IR      |
                      CWIID_RPT_BTN))
    {
      std::cerr << "Wiimote: Error setting report mode" << std::endl;
    }

    read_wiimote_calibration();
    read_nunchuk_calibration();
  }
}
 void WiiTrackExtension::cwiidSetReportMode(cwiid_wiimote_t *wiimote, unsigned char rpt_mode)
 {
   if (cwiid_command(wiimote, CWIID_CMD_RPT_MODE, rpt_mode)) {
     qDebug() << "Error setting report mode";
   }
 }
DEFINE_THREAD_ROUTINE(wiimote_logic, data){
    
    //wiimote connection variables
    static bdaddr_t bdaddr = {{0}};
    static cwiid_wiimote_t *wiimote = NULL;
    union cwiid_mesg *msg = NULL;
    struct timespec timestamp;
    
    int wiimote_connected = 0;
    
    int drone_in_sight = 0;
    int trigger_button = 0;
    int number_of_led = 0;
    int recharger_in_sight = 0;
    int recharging_button = 0;
    
    int bullets = magazine_capacity;
    
    int i = 0;
    int j = 0;
    int msg_count = 0;
    
    struct timespec shot_rumble_time;
    shot_rumble_time.tv_sec = 1;
    shot_rumble_time.tv_nsec = 0000000;

    struct timespec recharging_time;
    recharging_time.tv_sec = 10;
    recharging_time.tv_nsec = 0000000;
    
    while(game_active){
        
        //CONNECT TO THE WIIMOTE
        if(wiimote_connected == 0) {
            if( ! (wiimote = cwiid_open(&bdaddr, CWIID_FLAG_MESG_IFC)) ) {
                printf("Unable to connect to wiimote\n");
            } else {
                wiimote_connected = 1;
                printf("Wiimote found\n");
                cwiid_command(wiimote, CWIID_CMD_LED, CWIID_LED1_ON|CWIID_LED2_ON|CWIID_LED3_ON|CWIID_LED4_ON);
                cwiid_command(wiimote, CWIID_CMD_RPT_MODE, CWIID_RPT_IR|CWIID_RPT_BTN);
            }
            
        //ALREADY CONNECTED
        } else {
            if(match_active){
                
                //--- RESET VARIABLES ---//
                number_of_led = 0;
                drone_in_sight = 0;
                recharger_in_sight = 0;
                trigger_button = 0;
                msg_count = 0;
                
                //--- GET INPUTS FROM THE WIIMOTE ---//
                
                //get messages (blocking)
                cwiid_get_mesg(wiimote, &msg_count, &msg, &timestamp);
                
                //scan the messages for the event "pression of trigger_button" or "pression of recharging_button"
                //and to count the number of IR leds found
                //NOTE: the wiimote find false positive (sometimes 1led == 4leds :O)
                //NOTE: the wiimote is REALLY sensitive to sun light
                for(i = 0; i < msg_count; i++){
                    
                    if(msg[i].type == CWIID_MESG_BTN){
                        
                        //is button B pressed?
                        if(msg[i].btn_mesg.buttons == CWIID_BTN_B){
                            trigger_button = 1;
                            printf("SHOOT\n");
                        } else {
                            trigger_button = 0;
                        }
                        
                        //is button A pressed?
                        if(msg[i].btn_mesg.buttons == CWIID_BTN_A){
                            recharging_button = 1;
                            printf("BUTTON A\n");
                        } else {
                            recharging_button = 0;
                        }
                        
                        //TODO: this is here in case the ar.drone stop sending video update
                        if(msg[i].btn_mesg.buttons == CWIID_BTN_HOME){
                            printf("The program will shutdown...\n");
                            
                            match_active = 0; //This tell the drone_logic thread to land the drone
                            game_active = 0; //This make all the threads exit the while loop
                            
                            exit_program = 0;  // Force ardrone_tool to close
                            // Sometimes, ardrone_tool might not finish properly. 
                            //This happens mainly because a thread is blocked on a syscall, in this case, wait 5 seconds then kill the app
                            sleep(5);
                            exit(0);
                        }
                        
                    }
                    
                    //are there leds?
                    if(msg[i].type == CWIID_MESG_IR){ 
                        for(j = 0; j < CWIID_IR_SRC_COUNT; j++){
                            if(msg[i].ir_mesg.src[j].valid != 0){
                                number_of_led++;
                            }
                        }
                        
                        if(number_of_led > 0){
                            printf("LEDS\n");
                            drone_in_sight = 1;
                        } else {
                            drone_in_sight = 0;
                        }
                    }
                }
                
                //--- WIIMOTE LOGIC ---//
                //SHOOTING
                if((bullets > 0) && trigger_button){
                    
                    bullets--;
                    printf("lost one bullet\n");
                    
                    //haptic feedback
                    cwiid_command(wiimote, CWIID_CMD_RUMBLE, 1);
                    nanosleep(&shot_rumble_time, NULL);
                    cwiid_command(wiimote, CWIID_CMD_RUMBLE, 0);
                    
                    if(drone_in_sight){
                        
                        vp_os_mutex_lock(&drone_score_mutex);
                            drone_lose_score = 1;
                        vp_os_mutex_unlock(&drone_score_mutex);
                        
                        printf("DRONE HIT\n");
                        
                    } else {
                        printf("DRONE MISSED!!\n");
                    }
                    
                    //This is to limit the frequency of shooting (i.e. the gun need to load)
                    nanosleep(&shot_rumble_time, NULL);
                    
                //you can recharge only if you don't have bullets any more
                } else if(bullets < 1){
                    if(recharging_button){
                        //TODO: How should I let the enemy know that the wiimote is full again?
                        nanosleep(&recharging_time, NULL);
                        bullets = magazine_capacity;
                    }
                }
            }
        }
    }
    
    return C_OK;
}