示例#1
0
Joystick::Joystick(xml* temp_lll) {

	this->lll = temp_lll;		// the xml class that holds the configuration
	
	strcpy(this->name,""); // reseting the name of the joystick
	
	this->deviceName = new char[256];
	
	strcpy(this->deviceName,this->lll->conf->getDeviceName(0)); // gets the device name usualy it is /dev/js0
	#ifdef DEBUG2
		cout << "Device Name for Joystick : " << this->deviceName << endl;
	#endif
	
	this->numberOfAxes=0;				//reseting the number of axes that the joystick will suport
	this->numberOfButtons=0;			//reseting the number of buttons that the joystick will suport
		
	speed=SPEED;				// initial speed used by the joystick
	
	this->sucess=false;			// sucess to connect with file descriptor
	
	if(!joyInit(this->deviceName)) {
		cout << "Not able to connect with joystick" << endl;
	}
}
示例#2
0
void Joystick::JoyRead() {
	
	struct js_event js;		// joystick structure
	MPI_Status status;
	int received;
	int input_update;
	MPI_Request request;	

	MPI_Irecv(&input_update, 1, MPI_INT, MAIN, INPUT_UPDATE, MPI_COMM_WORLD, &request);
	received=1;
	
	int checkUpdate=0; // to check if joystick was removed
	
	while(true)	{

		if(this->sucess) {
		
    	    if (read(this->fd, &js, sizeof(struct js_event)) != sizeof(struct js_event)) {
				checkUpdate++;
				if(checkUpdate>800) {
					close(this->fd);
					if(!joyInit(this->deviceName)) {
						cout << "Connection lost with joystick" << endl;
						this->sucess=false;
					}
					checkUpdate=0;
				}
    	    }
    	    else {
				checkUpdate=0;
				switch (js.type) {
					case JS_EVENT_BUTTON: // Botoes controlam movimentacao
						button[((int)js.number)] = (short int)js.value;
						break;
					case JS_EVENT_AXIS:   // Axis controla direcao do olhar
						axis[((int)js.number)] = (short int)js.value;
						break;
				}
        	}
		
			MPI_Test(&request, &received, &status);
			if(received) {
				MPI_Wait(&request, &status);
				MPI_Irecv(&input_update, 1, MPI_INT, MAIN, INPUT_UPDATE, MPI_COMM_WORLD, &request);
	
				this->data[0]=0;
				this->data[1]=0;
				this->data[2]=0;
				this->data[3]=0;
				this->data[4]=0;
				this->data[5]=0;


				if(this->lll->conf->getJoystick(0)) {
					cout << "Joystick defined in the configuration file" << endl;
					
					
					
				} else {
					//cout << "Joystick not defined in the configuration file" << endl;
				}
				
				switch(this->mode) {
				case 1: // force feedback
					if (button[2]==1) { this->data[1]= speed;}
					if (button[3]==1) { this->data[1]= -speed; } 
					if ( (!button[2]) && (!button[3])) { this->data[1]= 0; }
					this->data[4]= speed*((float)((int)(-axis[4])))/32767 ; // rotate in y
					this->data[2]= (speed*20*((float)((int)(-axis[1])))/327670) + (speed*((float)((int)(-axis[5])))/327670); // translate in z
					this->data[0]= speed*20*((float)((int)axis[0]))/327670 ; // translate in x
					this->data[3]= speed*((float)((int)axis[7]))/32767 ; // rotate in x
				break;

				case 2: //skate
					if (button[0]==1) { this->data[0]= -speed;} // translate to left
					if (button[2]==1) { this->data[0]= speed;} // translate to right
					if ( (!button[0]) && (!(int)button[2])) { this->data[0]= 0; } // not left and right
					if (button[1]==1) { this->data[1]= speed;} // translate down
					if (button[3]==1) { this->data[1]= -speed;} // translate up
					if ( (!button[1]) && (!(int)button[3])) { this->data[1]= 0; } // not up and down
					if (button[8]==1 || (((int)axis[1]) < 0 )) { this->data[2]= speed;} // left

					//if ((int)button[9]==1 || (((int)axis[1]) > 0 )){ this->data[2]= -0.1; } // fowrward
					if ( ((axis[1]) > 0 )){ this->data[2]= -speed; } // fowrward
					if ( (!button[8]) && (!(int)button[9]) && (((int)axis[1])==0) ) { this->data[2]= 0; } // not left and right
						
					this->data[4]= -((float)((int)axis[0]))/32767;
					break;

				case 3: //game pad
					if ( ((axis[1]) <  -1 )) { this->data[2]= speed;} // 
					if ( ((axis[1]) >  1 )) { this->data[2]= -speed;} // 
					if ( ((axis[0]) <  -1 )) { this->data[4]=+1.25+speed*0.12;} // 
					if ( ((axis[0]) >  1 )) { this->data[4]= -1.25-speed*0.12;} // 

					if ( button[0]==1 ) { this->data[1]= speed;} // 
					if ( button[1]==1 ) { this->data[0]= speed;} // 
					if ( button[2]==1 ) { this->data[0]= -speed;} // 
					if ( button[3]==1 ) { this->data[1]= -speed;} // 

					if (button[4]==1) { speed /= 1.04;} // 
					if (button[5]==1) { speed *= 1.04;} // 
					break;
											
				case 4: //soundblaster
					if(axis[0]<30000) this->data[4] = speed/3; else
					if(axis[0]<10000) this->data[4] = 0; else
					this->data[4] = -speed/3; 

					if(axis[1]<20000) this->data[1] = speed/3; else
					if(axis[1]<10000) this->data[1] = 0; else
					this->data[1] = -speed/3; 
					
					this->data[2]= 2*speed/3;
					break;

				case 5: // 
					if(axis[0]<30000) this->data[4] = speed/3; else
					if(axis[0]<10000) this->data[4] = 0; else
					this->data[4] = -speed/3; 

					if(axis[1]<20000) this->data[1] = speed/3; else
					if(axis[1]<10000) this->data[1] = 0; else
					this->data[1] = -speed/3; 

					this->data[2]= 2*speed/3;
					break;
					
					
				case 6: //game pad
					if ( ((axis[1]) <  -1 )) { this->data[2]= speed;} // 
					if ( ((axis[1]) >  1 )) { this->data[2]= -speed;} // 

					if ( ((axis[0]) <  -1 )) { this->data[4]=+1.25+speed*0.12;} // 
					if ( ((axis[0]) >  1 )) { this->data[4]= -1.25-speed*0.12;} // 

					if ( button[0]==1 ) { this->data[1]= speed;} // 
					if ( button[1]==1 ) { this->data[0]= speed;} // 
					if ( button[3]==1 ) { this->data[0]= -speed;} // 
					if ( button[4]==1 ) { this->data[1]= -speed;} // 

					if (button[6]==1) { speed /= 1.04;} // 
					if (button[7]==1) { speed *= 1.04;} // 
					break;
					
				case 7: //soundblaster
					if(axis[0]<-10000) this->data[4] = 1*speed; else
					//if(axis[0]<-100) this->data[4] = 0; else
					this->data[4] = -1*speed; 

					this->data[1]= 0.3*speed*((float)((int)(-axis[1])))/(32767) ; // rotate in y
					/*
					if(axis[1]<20000) this->data[1] = speed/3; else
					if(axis[1]<10000) this->data[1] = 0; else
					this->data[1] = -speed/3; 
					
					this->data[2]= 2*speed/3;
					
					*/
					break;

					
				default:
					if ( ((axis[1]) <  -1 )) { this->data[2]= speed;} // 
					if ( ((axis[1]) >  1 )) { this->data[2]= -speed;} // 

					if ( ((axis[0]) <  -1 )) { this->data[4]= 2;} // 
					if ( ((axis[0]) >  1 )) { this->data[4]= -2;} // 

					if ( button[0]==1 ) { this->data[1]= speed;} // 
					if ( button[1]==1 ) { this->data[0]= speed;} // 
					if ( button[2]==1 ) { this->data[0]= -speed;} // 
					if ( button[3]==1 ) { this->data[1]= -speed;} // 

					if (button[4]==1) { speed /= 1.1;} // 
					if (button[5]==1) { speed *= 1.1;} // 
					break;
			}

			Input::move(this->data);
					
		}
		
		#ifdef DEBUG
			unsigned short int f;
			for(f=0;f<(this->numberOfAxes);f++) 
				cout << "Axes " << f << " : " << axis[f] << endl;
			for(f=0;f<(this->numberOfButtons);f++) 
				cout << "Button " << f << " : " << button[f] << endl;
			for(f=0;f<6;f++)
				cout << "Data " << f << " : " << data[f] << endl; 
		#endif
			
	} else {
		
		// do nothing
		MPI_Test(&request, &received, &status);
		if(received) {
			MPI_Wait(&request, &status);
			MPI_Irecv(&input_update, 1, MPI_INT, MAIN, INPUT_UPDATE, MPI_COMM_WORLD, &request);
			Input::move(this->data); //just for not locking but have to be fixed
		}
		
		joyInit(this->deviceName); // try to reconect
	}
	
	breath();	// just a sleep	

	}
	
}
int main(void)
{
	initUART(UBBR);
	printf("Simple RAM TEST \r\n");
	initExtMemIface();
	initTimer();
	initInterrupts();
	SRAM_test();
	joyInit();
	initOLED();
	initSPI();
	initCAN(NORMAL);
	
	struct joypos_t p;
	enum joydir_t d;
	struct canMessage m0,m1,m2s;

	 while(1){
		 
		/* slide1Point=getSlidePosition(1);
		 slide2Point=getSlidePosition(2);
		 printf("SLIDE1 -> %d",slide1Point.x);
		 printf("SLIDE2 -> %d",slide2Point.x);SLIDE READINGS*/
		if (readControl) {
			readControl = 0;
			
			d = getJoyDirection();
			p = getJoyPosition();
		    sendCANJoy(2,p,d);
			if (d == TOP || d == RIGHT_TOP || d == LEFT_TOP) menuOption = (menuOption-1)%NUM_MENU_OPTIONS;
			else if (d == BOTTOM || d == RIGHT_BOTTOM|| d == LEFT_BOTTOM) menuOption = (menuOption+1)%NUM_MENU_OPTIONS;
			printMenu(menuOption);
		}
		if (flagJoyButton) {
		
			m2s.id = JOY_BUTTON;
			m2s.size = 0;
			printf("Sending joy bu\r\n");
			fillTxBufferMCP(0,m2s);
			requestToSendMCP(0);
		
			struct canMessage m2s;
			flagJoyButton = 0;
			gotoCharOLED(7,0);
			putsOLED("                        "); //Cleans last selected option from screen
			switch(menuOption){
				case 0: //Play
					gotoCharOLED(7,0);
					putsOLED("PLAY");
					break;
					
			   case 1: //Options
					gotoCharOLED(7,0);
					putsOLED("OPTIONS");
					break;
					
			   case 2: //Help
					printHelp();
					break;
					
			  default:
					gotoCharOLED(7,0);
					putsOLED("Stick to the OPTIONS !!!");
					break;
				
			}
		}
		/*
		if(flagMCP) {
			flagMCP = 0;
			struct canMessage m0,m1;
			m0 = readRxBufferMCP(0);
			m1 = readRxBufferMCP(1);
			printf ("Buffer 0 ID: %d, Size: %d, Data: %s \r\n", m0.id, m0.size, m0.data);
			printf ("Buffer 1 ID: %d, Size: %d, Data: %s \r\n", m1.id, m1.size, m1.data);
		}*/
	}
}