Exemplo n.º 1
0
Arquivo: main.c Projeto: HexTank/KADE
int main(void) {
	// Set clock @ 16Mhz
	CPU_PRESCALE(0);		

	//Set initial pin states.
    DDRB=0x00;
    DDRC=0x00;
    DDRD=0x00;
    PORTB=0xFF;
    PORTC=0xFF;
    PORTD=0xFF;
	
	//Generic functions used with this firmware	(there are others)
    uint8_t func_dpad_up       = 1;
    uint8_t func_dpad_down     = 2;
    uint8_t func_dpad_left     = 3;
    uint8_t func_dpad_right    = 4;
    uint8_t func_restrict_4way = 22;
    uint8_t func_invert_y_axis = 23;
    uint8_t func_autofire_1    = 24;
    uint8_t func_autofire_2    = 25;
    uint8_t func_autofire_3    = 26;
    uint8_t func_autofire_4    = 27;
	uint8_t func_ext_shift_led = 28;
    uint8_t func_ext_power_led = 29;	
	uint8_t func_program_mode  = 30;	
	
	//Use default restriction logic (in generic_main_preloop.c)
	uint8_t default_restrict   = 0;	
	
	//Generic main
	#include "../shared/generic_main_init.c"
	
	// Init USB pad emulation
	vs_init(true);
	
	// Init Trackball if needed
	if (setting_enable_tb1==1){
		PORTD |= (1<<6);
		initTrackBall(setting_tb1_counts, setting_tb1_samples);
	}

	// Pins polling and gamepad status updates
	for (;;) {
		vs_reset_watchdog();
				
		//generic stuff at start of main loop
		#include "../shared/generic_main_preloop.c"

		//set default button states - reset direction to centre
		gamepad_state.direction=8;
		gamepad_state.l_y_axis=0x80;
		gamepad_state.l_x_axis=0x80;
		gamepad_state.r_y_axis=0x80;
		gamepad_state.r_x_axis=0x80;
		gamepad_state.square_btn=0x00;
		gamepad_state.cross_btn=0x00;
		gamepad_state.circle_btn=0x00;
		gamepad_state.triangle_btn=0x00;
		gamepad_state.l1_btn=0x00;
		gamepad_state.r1_btn=0x00;					
		gamepad_state.l2_btn=0x00;
		gamepad_state.r2_btn=0x00;
		gamepad_state.l3_btn=0x00;
		gamepad_state.r3_btn=0x00;
		gamepad_state.square_axis=0x00;
		gamepad_state.cross_axis=0x00;
		gamepad_state.circle_axis=0x00;
		gamepad_state.triangle_axis=0x00;
		gamepad_state.l1_axis=0x00;
		gamepad_state.r1_axis=0x00;
		gamepad_state.l2_axis=0x00;
		gamepad_state.r2_axis=0x00;
		gamepad_state.select_btn=0x00;
		gamepad_state.start_btn=0x00;
		gamepad_state.ps_btn=0x00;		
				
		//Handle dpad and restricted movement
		//PS3 restriction is easier to manage - ignore mainloop
		if(restrict4==1){
			if (u==1){gamepad_state.direction=0;}
			if (d==1){gamepad_state.direction=4;}
			if (l==1){gamepad_state.direction=6;}
			if (r==1){gamepad_state.direction=2;}
		} else {				
			//left and right directions
			if (l==1){gamepad_state.direction=6;}  //Left
			if (r==1){gamepad_state.direction=2;}	//Right						
			
			//up, down and diagonal directions
			if (u==1){								//Up
				if (l==1){							//left
					gamepad_state.direction=7;		//up-left
				}
				else if (r==1){    					//right
					gamepad_state.direction=1;		//up-right
				} else {
					gamepad_state.direction=0;		//up
				}		
			}
			if (d==1){								//down
				if (l==1){							//left
					gamepad_state.direction=5;		//down-left
				}
				else if (r==1){   	 				//right
					gamepad_state.direction=3;		//down-right
				} else {
					gamepad_state.direction=4;		//down
				}
			}		
		}

		//Trackballs Analog
		//NOTE: COME BACK AND TIDY THIS LOGIC IF IT WORKS WELL
		if (setting_enable_tb1==1){
			if(setting_tb1_trackball==1){
				if (setting_tb1_stick==1){ 
					//Right Analog (1=Right)
					//
					//Y Axis (Trackball)
					if(abs(ps2_y) < 32){
						gamepad_state.r_y_axis = 255 - (ps2_y + 128);
					} else {
						if (ps2_y > 31){gamepad_state.r_y_axis = 0x00;}
						if (ps2_y < -31){gamepad_state.r_y_axis = 0xFF;}		
					}
					//X Axis (Trackball)
					if(abs(ps2_x) < 32){
						gamepad_state.r_x_axis = ps2_x + 128;
					} else {
						if (ps2_x > 31){gamepad_state.r_x_axis = 0xFF;}
						if (ps2_x < -31){gamepad_state.r_x_axis = 0x00;}		
					}			
				} else {
					//Left Analog (0=Left)
					//
					//Y Axis (Trackball)
					if(abs(ps2_y) < 32){
						gamepad_state.l_y_axis = 255 - (ps2_y + 128);
					} else {
						if (ps2_y > 31){gamepad_state.l_y_axis = 0x00;}
						if (ps2_y < -31){gamepad_state.l_y_axis = 0xFF;}		
					}
					//X Axis (Trackball)
					if(abs(ps2_x) < 32){
						gamepad_state.l_x_axis = ps2_x + 128;
					} else {
						if (ps2_x > 31){gamepad_state.l_x_axis = 0xFF;}
						if (ps2_x < -31){gamepad_state.l_x_axis = 0x00;}		
					}			
				}			
			} else {
				if (setting_tb1_stick==1){ 
					//Right Analog (1=Right)
					gamepad_state.r_y_axis = 255 - (ps2_y + 128);
					gamepad_state.r_x_axis = ps2_x + 128;
				} else {
					//Left Analog (0=Left)
					gamepad_state.l_y_axis = 255 - (ps2_y + 128);
					gamepad_state.l_x_axis = ps2_x + 128;
				}			
			}
		}
		

		//loop through pins checking for inputs from those that are assigned a function
		//for(cnt=0;cnt<20;cnt++) {
		for(cnt=0;cnt<20;cnt++) {
			pos=cnt;					
			if (!(state[cnt])) {
				//there is input on this pin
				if (shift==1){pos=pos+20;}  //+20 if this is shifted input
				
				if (ass[pos]>0) {
					//there is an assignment to a function
					action = ass[pos];
					#include "main_actions.c"
				}
			}
		}
		
		//Trackball Buttons
		if (setting_enable_tb1==1){
			for(pos=0;pos<3;pos++) {
				if (shift==1){
					if (pos==0){action = setting_tb1_but1s;}
					if (pos==1){action = setting_tb1_but2s;}
					if (pos==2){action = setting_tb1_but3s;}
				} else {
					if (pos==0){action = setting_tb1_but1;}
					if (pos==1){action = setting_tb1_but2;}
					if (pos==2){action = setting_tb1_but3;}
				}
			
				if (action > 0) {
					if (((0b00000001 & ps2data[0])&&(pos==0))||((0b00000010 & ps2data[0])&&(pos==1))||((0b00000100 & ps2data[0])&&(pos==2))){
						#include "main_actions.c"
					}
				}
			}
		}
		
		
		_delay_ms(2);  //debounce
		vs_send_pad_state();		
	}
}
Exemplo n.º 2
0
int main(void) {
	// Set clock @ 16Mhz
	CPU_PRESCALE(0);		

	//Set initial pin states.  These are adjusted based on eeprom settings.
    DDRB=0x00;
    DDRC=0x00;
    DDRD=0x00;
    PORTB=0xFF;
    PORTC=0xFF;
    PORTD=0xFF;
	
	//dpad and pad restrictions
	uint8_t	u=0, d=0, l=0, r=0, pu=0, pd=0, pl=0, pr=0;
	
	//pin assignments and button states
	uint8_t	pos=0, cnt=0;
	uint8_t ass[40], state[20];
	
	//handle shift and shift lock
	uint8_t shift=0, shift_last=0, shift_count=0, shift_lock=0;
	
	//handle settings
	uint8_t invert, restrict4;
	uint8_t setting_delay;
	uint8_t autofire, auto_toggle=1;
	uint8_t active=0, delay_power=0;
	
    //Flash LEDs
	#include "../shared/disco.c"

	//read first 40 eeprom into an array (pins + shifted pins)
	for(cnt=0;cnt<40;cnt++){	
		ass[cnt]=read_eeprom_byte(cnt);
		
		//set output pins
		if ((ass[cnt]==28)||(ass[cnt]==29)){
			#include "../shared/outputs.c"		
		}
		if (ass[cnt]==29){delay_power=1;}
		
	}
	//Read other settings (40+ in Eeprom) 
	setting_delay=read_eeprom_byte(41);

	//wait for specified delay time (allow 2 secs for led flash)
	if ((delay_power==1)&&(setting_delay>0)){
		for(cnt=0;cnt<=setting_delay-3;cnt++){
			_delay_ms(1000);
		}
		active=1;
		for(cnt=0;cnt<40;cnt++){	
			if (ass[cnt]==29){
				#include "../shared/showleds.c"
			}
		}
	}

	// Init USB pad emulation
	vs_init(true);

	// Pins polling and gamepad status updates
	for (;;) {
		vs_reset_watchdog();
		
		//read KADE pin states into an array
		#include "../shared/state.c"		
		
		//set shifted status and detect shift lock (double click)
		#include "../shared/shift.c"		

		//set default button states
		//reset direction to centre
		gamepad_state.direction=8;
		gamepad_state.l_y_axis=0x80;
		gamepad_state.l_x_axis=0x80;

		gamepad_state.square_btn=0x00;
		gamepad_state.cross_btn=0x00;
		gamepad_state.circle_btn=0x00;
		gamepad_state.triangle_btn=0x00;
		gamepad_state.l1_btn=0x00;
		gamepad_state.r1_btn=0x00;					
		gamepad_state.l2_btn=0x00;
		gamepad_state.r2_btn=0x00;
		gamepad_state.l3_btn=0x00;
		gamepad_state.r3_btn=0x00;
		gamepad_state.select_btn=0x00;
		gamepad_state.start_btn=0x00;
		gamepad_state.ps_btn=0x00;
		invert=0;
		restrict4=0;
		autofire=0;		
		
		//pre-loop to deal with any switches/toggles		
		pu=u; pd=d; pl=l; pr=r;
		u=0; d=0; l=0; r=0;
		for(cnt=0;cnt<20;cnt++) {
			if (!(state[cnt])) {
				//there is input on this pin, also account for shifted input
				pos = cnt;
				if (shift==1){pos=pos+20;}
				
				if ((ass[cnt]==23)||(ass[pos]==23)){invert=1;}     //invert Y axis
				if ((ass[cnt]==22)||(ass[pos]==22)){restrict4=1;}  //restrict dPad 4-way
				if ((ass[pos]>=24)&&(ass[pos]<=27)){autofire=1;}   //autofire mode
				if ((ass[pos]>=30)&&(ass[pos]<=38)){autofire=1;}   //extra autofire mode
				if (ass[pos]==1){u=1;}
				if (ass[pos]==2){d=1;}				
				if (ass[pos]==3){l=1;}
				if (ass[pos]==4){r=1;}
			}
			
			//Show the shift status LED(s) and power up externals with delay
			if (ass[cnt]==28){
				active = shift;
				#include "../shared/showleds.c"								
			}
		}			

		//invert Y axis on DPAD (if we want to do this)
		//if (invert==1){
		//	if (u==1){d=1; u=0;} 
		//	else if (d==1){d=0; u=1;}
		//}

		//autofire toggle
		if(autofire==1){
			auto_toggle = auto_toggle * -1;
		}

		//Handle dpad and restricted movement
		if(restrict4==1){
			if (u==1){gamepad_state.direction=0;}
			if (d==1){gamepad_state.direction=4;}
			if (l==1){gamepad_state.direction=6;}
			if (r==1){gamepad_state.direction=2;}
		} else {				
			//left and right directions
			if (l==1){gamepad_state.direction=6;}  //Left
			if (r==1){gamepad_state.direction=2;}	//Right						
			
			//up, down and diagonal directions
			if (u==1){								//Up
				if (l==1){							//left
					gamepad_state.direction=7;		//up-left
				}
				else if (r==1){    					//right
					gamepad_state.direction=1;		//up-right
				} else {
					gamepad_state.direction=0;		//up
				}		
			}
			if (d==1){								//down
				if (l==1){							//left
					gamepad_state.direction=5;		//down-left
				}
				else if (r==1){   	 				//right
					gamepad_state.direction=3;		//down-right
				} else {
					gamepad_state.direction=4;		//down
				}
			}		
		}

		//loop through pins checking for inputs from those that are assigned a function
		for(cnt=0;cnt<20;cnt++) {
			pos=cnt;					
			if (!(state[cnt])) {
				//there is input on this pin
				if (shift==1){pos=pos+20;}  //+20 if this is shifted input
				
				if (ass[pos]>0) {
					//there is an assignment to a function

					//A, B, X, Y
					if (ass[pos]==5){gamepad_state.square_btn=0xFF;}
					if (ass[pos]==6){gamepad_state.cross_btn=0xFF;}
					if (ass[pos]==7){gamepad_state.circle_btn=0xFF;}
					if (ass[pos]==8){gamepad_state.triangle_btn=0xFF;}
					
					//Autofire
					if (autofire==1){
						if(auto_toggle==1){
							if (ass[pos]==24){gamepad_state.square_btn=0xFF;}
							if (ass[pos]==25){gamepad_state.cross_btn=0xFF;}
							if (ass[pos]==26){gamepad_state.circle_btn=0xFF;}
							if (ass[pos]==27){gamepad_state.triangle_btn=0xFF;}
						}
						_delay_ms(15);
					}
					
					//PS3 Triggers
					if (ass[pos]==9){gamepad_state.l1_btn=0xFF;}   		    //L1
					if (ass[pos]==10){gamepad_state.r1_btn=0xFF;}  		    //R1					
					if (ass[pos]==11){gamepad_state.l2_btn=0xFF;}  		    //L2
					if (ass[pos]==12){gamepad_state.r2_btn=0xFF;}   			//R2
					if (ass[pos]==15){gamepad_state.l3_btn=0xFF;}  		    //L3
					if (ass[pos]==16){gamepad_state.r3_btn=0xFF;}				//R3
					
					//PS3 Special
					if (ass[pos]==13){gamepad_state.select_btn=0xFF;}			//L3
					if (ass[pos]==14){gamepad_state.start_btn=0xFF;}			//R3
					if (ass[pos]==17){gamepad_state.ps_btn=0xFF;}				//PS Button
					
					//Analog Stick
					if (ass[pos]==18+invert){gamepad_state.l_y_axis = 0x00;} 	//Analog Up
					if (ass[pos]==19-invert){gamepad_state.l_y_axis = 0xff;} 	//Analog Down
					if (ass[pos]==20){gamepad_state.l_x_axis = 0x00;} 			//Analog Left
					if (ass[pos]==21){gamepad_state.l_x_axis = 0xff;} 			//Analog Right
					
					//Enter programming mode
					if (ass[pos]==39){Jump_To_Bootloader();}					//Program mode					
										
				}
			}
		}

		_delay_ms(10);  //debounce
		vs_send_pad_state();
		
	}
}
Exemplo n.º 3
0
int main(void) {

	// Set Teensy clock @ 16Mhz
	CPU_PRESCALE(0);
	
    DDRB = 0x00;
    DDRC = 0x00;
    DDRD = 0x60; // D5,D6 - OUTPUT (LED)
    PORTB = 0xFF;
    PORTC = 0xFF;
    PORTD = 0x9F; // D5,D6 - LOW (LED)
	bit_set(PORTD,0x20);	

	uint8_t b, c, d;
	uint8_t shift = 0, shift_last = 0, shift_count = 0, shift_lock = 0;
	uint8_t up, down, left, right;

	
	// Set callback function for USB connect event
	vs_set_connect_callback(&ledOn);

	// Set callback function for USB disconnect event
	vs_set_disconnect_callback(&ledOff);

	// Init USB pad emulation
	vs_init(true);

    //flash them leds
	#include "..\shared\disco.c"

	// Pins polling and gamepad status updates
	for (;;) {
		vs_reset_watchdog();
		
		b = PINB;
		c = PINC;
		d = PIND;

		//set shifted status and detect shift lock (double click)
		if(!(d & 0x80)) {
			shift = 1;
			if (shift_last == 0) {
				if (shift_count > 1 && shift_count < 50) {  
					if (shift_lock == 0) {shift_lock = 1;} else {shift_lock = 0;}
				}
				shift_count = 0;
			}
		} else {
			shift = 0;
		} 
		shift_last = shift;
		if (shift_count <= 50) {shift_count += 1;}  //no reason to continue counting
		if (shift_lock == 1) {shift = 1;}


		//reset direction to centre
		gamepad_state.direction = 8;          //centre
		
		//DPAD (and shifted analog)
		up    = !(c&0x04);
		down  = !(d&0x01);
		left  = !(d&0x02);
		right = !(d&0x04);
		
		
		if(shift==1) {		     
			bit_clear(PORTD,0x20);
			//Analog
			if ((up)||(down)) {
					if (up) {gamepad_state.l_y_axis = 0x00;}		//A1-Up
					if (down) {gamepad_state.l_y_axis = 0xff;}		//A2-Down
			} else {gamepad_state.l_y_axis = 0x80;}
			
			if ((left)||(right)) {
					if (left) {gamepad_state.l_x_axis = 0x00;}		//A3-Left
					if (right) {gamepad_state.l_x_axis = 0xff;}	//A4-Right
			} else {gamepad_state.l_x_axis = 0x80;}			
			
		} else {
			bit_set(PORTD,0x20);			
			//Digital
			//left and right directions
			if (!(d & 0x02)) { gamepad_state.direction = 6; }	//A3-Left
			if (!(d & 0x04)) {	gamepad_state.direction = 2; }	//(A4-Right)						
			
			//up, down and diagonal directions
			if (!(c & 0x04)) {									//A1-up
				if (!(d & 0x02)) {								//A3-left
					gamepad_state.direction = 7;				//(up-left)
				}
				else if (!(d & 0x04)) {    					//A4-right
					gamepad_state.direction = 1;				//(up-right)
				} else {
					gamepad_state.direction = 0;				//(up)
				}		
			}
			if (!(d & 0x01)) {									//A2-down
				if (!(d & 0x02)) {								//A3-left
					gamepad_state.direction = 5;				//(down-left)
				}
				else if (!(d & 0x04)) {    					//A4-right
					gamepad_state.direction = 3;				//(down-right)
				} else {
					gamepad_state.direction = 4;				//(down)
				}
			}

		}
		

		//PS3 buttons
		gamepad_state.square_btn = (!(d & 0x08) == 1);		//A5-Square
		gamepad_state.cross_btn = (!(d & 0x10) == 1);		//A6-Cross
		gamepad_state.circle_btn = (!(b & 0x01) == 1);		//A9-Circle
		gamepad_state.triangle_btn = (!(b & 0x02) == 1);	//A10-Triangle
		
		//PS3 Triggers
		gamepad_state.l1_btn = (!(b & 0x04) == 1);			//B1-L1
		gamepad_state.r1_btn = (!(b & 0x08) == 1);			//B2-R1
		gamepad_state.l2_btn = (!(b & 0x10) == 1);			//B3-L2
		gamepad_state.r2_btn = (!(b & 0x20) == 1);			//B4-R2
		gamepad_state.l3_btn = (!(c & 0x80) == 1);			//B7-L3
		gamepad_state.r3_btn = (!(c & 0x40) == 1);			//B8-R3

		//PS3 Special
		gamepad_state.select_btn = (!(b & 0x40) == 1);	    //B5-L3
		gamepad_state.start_btn = (!(b & 0x80) == 1);		//B6-R3
		gamepad_state.ps_btn = (!(c & 0x20) == 1);			//B9-PS Button
		

		_delay_ms(10);  //debounce
		vs_send_pad_state();

		
	}
}