コード例 #1
0
ファイル: wiicursor.cpp プロジェクト: abom/linux-whiteboard
void get_wiis_event_data_collect_all_events(
    std::vector<WiimoteData>& wiimotes,
    point_t const& ir_old,
    std::vector< std::vector<WiiEvent> >& event_batches,
    unsigned int& max_event_counts)
{
    for (unsigned int i = 0; i != wiimotes.size(); ++i) {
	std::vector<WiiEvent>& batch = event_batches[i]; // Readability
	int msg_count = 0;
	cwiid_mesg* msgs = 0;
	// NOTE: The API's been changed, I don't know what to do with the last argument
#ifdef COMPATIBILITY_GUTSY
	cwiid_get_mesg(wiimotes[i].wiimote, &msg_count, &msgs);
#else   
	timespec tspec;
	cwiid_get_mesg(wiimotes[i].wiimote, &msg_count, &msgs, &tspec);
#endif // COMPATIBILITY_GUTSY

	batch.resize(msg_count);
	for (int i = 0; i != msg_count; ++i) {
	    WiiEvent& cur_event = batch[i]; // Readability
	    cur_event.ir = ir_old; // Read wiicontrol.cpp if you're not sure about this
	    cur_event.button = INVALID_BUTTON_MSG_ID;
	    process_messages(msgs[i], &cur_event.ir, &cur_event.button);

	    cur_event.type = (cur_event.button != INVALID_BUTTON_MSG_ID) ? WII_EVENT_TYPE_BUTTON : WII_EVENT_TYPE_IR;
	}

	if ( max_event_counts < static_cast<unsigned int>(msg_count) )
	    max_event_counts = static_cast<unsigned int>(msg_count);
    }
}
コード例 #2
0
ファイル: Wiimote.c プロジェクト: bobbens/cwiid
static PyObject *Wiimote_get_mesg(Wiimote *self)
{
	union cwiid_mesg *mesg;
	int mesg_count;
	struct timespec t;
	PyObject *PyMesg;

	if (!self->wiimote) {
		SET_CLOSED_ERROR;
		return NULL;
	}

	if (cwiid_get_mesg(self->wiimote, &mesg_count, &mesg, &t)) {
		if (errno == EAGAIN) {
			Py_RETURN_NONE;
		}
		else {
			PyErr_SetString(PyExc_RuntimeError,
			                "Error getting wiimote message list");
			return NULL;
		}
	}

	PyMesg = ConvertMesgArray(mesg_count, mesg);

	free(mesg);

	return PyMesg;
}
コード例 #3
0
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;
}