// -------------Interrupt routines-------------------------- start
/* USART, Rx Complete */
SIGNAL(SIG_UART_RECV)		/* USART, Rx Complete */
{
	temp=UDR;
	if(temp==special_char){			
	//An htan special character mhn kaneis tipota, perimene ton epomeno xarakthra
	}
	else{	
		if(previous_char==special_char){
			switch(temp){
				case start_of_frame:
					RX_write=0;				// exoume start_of_frame
					send_rx=1;				// To receive start_of_frame shmatodotei thn enarjh ths omilias
					new_rx=1;
					break;
				case special_char_code:	
					temp=special_char;		// hr8e to special char
					break;
				case mhden_code:				
					temp=0;					// hr8e to 0
					break;

				default:
					break;
			}
		}	//if(previous_char==special_char)
		
		switch (RX_write){
			case 1:
				servo_0=temp;
				break;		
			case 2:
				servo_0=temp;
				break;
			case 3:
				//servo_2=temp;
				break;
			case 4:
				//servo_3=temp;
				break;
			case 5:
				//servo_4=temp;
				break;		
			case 6:
				//servo_5=temp;
				break;
			case 7:
				//servo_6=temp;
				break;
			case 8:
				//servo_7=temp;
				break;

			default:
				break;
		}
		if(new_rx==1)
			RX_write++;						//Increase pointer

		if(RX_write>=RX_buffer_length){	//Something's wrong
			RX_write=0;
			new_rx=0;					// Wait for new packet
		}
		
	}// else
	previous_char=temp;

}
/* USART, Tx Complete */
SIGNAL(SIG_UART_TRANS)		/* USART, Tx Complete */
{
	
	if(previous_tx_char==special_char){
		UDR=previous_tx_code;
		tx_temp='a';
		previous_tx_char=previous_tx_code;
		//TX_read--;						// Na meiw8ei wste na paei pali sthn idia 8esh
	}
	else{
		
		switch (TX_read){
			/*
			case 0:
				tx_temp=special_char;		//Exei stalei
				break;
				*/
			case 1:
				tx_temp=start_of_frame;
				break;
			//front left 2-5
			case 2:						
				tx_temp='a';		
				break;
			case 3:
				tx_temp=fl_1;				// Higher Nibble First
				break;
			case 4:						
				tx_temp=fl_2;				// Higher Nibble First		
				break;		
			case 5:
				tx_temp=fl_3;						
				break;	
			//front right 6-9
			case 6:						// Higher Nibble First
				tx_temp='b';							
				break;	
			case 7:
				tx_temp=fr_1;						
				break;	
			case 8:						// Higher Nibble First
				tx_temp=fr_2;						
				break;		
			case 9:
				tx_temp=fr_3;						
				break;
			//rear left 10-13
			case 10:						// Higher Nibble First
				tx_temp='c';						
				break;
			case 11:
				tx_temp=rl_1;						
				break;
			case 12:						// Higher Nibble First
				tx_temp=rl_2;						
				break;
			case 13:
				tx_temp=rl_3;				
				break;
			//rear right 14-17
			case 14:						// Higher Nibble First
				tx_temp='d';						
				break;
			case 15:
				tx_temp=rr_1;						
				break;
			case 16:						// Higher Nibble First
				tx_temp=rr_2;						
				break;
			case 17:
				tx_temp=rr_3;				
				break;
			//rpm 18-21
			case 18:						// Higher Nibble First
				tx_temp='r';						
				break;
			case 19:
				tx_temp=rpm_1;						
				break;
			case 20:						// Higher Nibble First
				tx_temp=rpm_2;						
				break;
			case 21:
				tx_temp=rpm_3;				
				break;
				//*/

				//*/
			default:			
				tx_temp='*';
				break;						//Default
		}// switch (TX_read)

		if(tx_temp==special_char){
				tx_temp=special_char;		// Panta ayto
				previous_tx_code=special_char_code;
		}
		else{
			if(tx_temp==0){
					tx_temp=special_char;
					previous_tx_code=mhden_code;
			}
		}
		previous_tx_char=tx_temp;
		TX_read++;
		if(TX_read<TX_buffer_length){

			//if(tx_temp==8)				// Backspace	Ayto mas sbhnei tous xarakthres sto terminal
			//	tx_temp='£';
			UDR=tx_temp;
		}
		
	}//else
	
	
		
	if(TX_read>=TX_buffer_length){	
		TX_read=1;						// Start from 1 cause 0 is allready sent whole Data sent
		send_data=1;					// Esteile to paketo
	}

}

/* Timer/Counter2 Overflow */
SIGNAL(TIMER2_OVF_vect){			// Exoume Overflow Ka8e 128us

	wheel_big_counter++;
	if(wheel_big_counter>781){							// An mpei edw exoune perasei 781*128us=99968usec=0.1sec
		
		//front left 2-5
		//front right 6-9
		//rear left 10-13
		//rear right 14		
		//rpm 18-21

		if(TX_read<=2||TX_read>5)		
			front_left_speed=front_left_ticks;				
		if(TX_read<=6||TX_read>9)		
			front_right_speed=front_right_ticks;	
		if(TX_read<=10||TX_read>13)		
			rear_left_speed=rear_left_ticks;				
		if(TX_read<=14||TX_read>17)		
			rear_right_speed=rear_right_ticks;

		wheel_big_counter=0;							// Start Counting Again
		front_left_ticks=front_right_ticks=rear_left_ticks=rear_right_ticks=0;


		// Front left
		fl_1=front_left_speed/100;
		fl_2=(front_left_speed-fl_1*100)/10;
		fl_3=(front_left_speed-(fl_1*100)-(fl_2*10));
	
		itoa(fl_1, buffer, 10);
		fl_1=buffer[0];
	
		itoa(fl_2, buffer, 10);
		fl_2=buffer[0];

		itoa(fl_3, buffer, 10);
		fl_3=buffer[0];

		//-------------------------

		//Front Right
		fr_1=front_right_speed/100;
		fr_2=(front_right_speed-fr_1*100)/10;
		fr_3=(front_right_speed-fr_1*100-fr_2*10);

		itoa(fr_1, buffer, 10);
		fr_1=buffer[0];


		itoa(fr_2, buffer, 10);
		fr_2=buffer[0];

		itoa(fr_3, buffer, 10);
		fr_3=buffer[0];
		//-------------------------

		// Rear left
		rl_1=rear_left_speed/100;
		rl_2=(rear_left_speed-rl_1*100)/10;
		rl_3=(rear_left_speed-rl_1*100-rl_2*10);
		
		itoa(rl_1, buffer, 10);
		rl_1=buffer[0];
	
		itoa(rl_2, buffer, 10);
		rl_2=buffer[0];

		itoa(rl_3, buffer, 10);
		rl_3=buffer[0];
		//-------------------------

		//Front Right
		rr_1=rear_right_speed/100;
		rr_2=(rear_right_speed-rr_1*100)/10;
		rr_3=(rear_right_speed-rr_1*100-rr_2*10);
		
		itoa(rr_1, buffer, 10);
		rr_1=buffer[0];
	
		itoa(rr_2, buffer, 10);
		rr_2=buffer[0];

		itoa(rr_3, buffer, 10);
		rr_3=buffer[0];
		//-------------------------
		
		send_rx=1;				// To receive start_of_frame shmatodotei thn enarjh ths omilias

	}



	rpm_big_counter++;
	if(rpm_big_counter>1950){							// An mpei edw exoune perasei 1950*128us=249600usec=0.25sec

		//rpm 18-21
		if(TX_read<=18||TX_read>21)		
			rpm_speed=rpm_ticks;

		rpm_big_counter=0;							// Start Counting Again
		rpm_ticks=0;

		//Front Right
		rpm_1=rpm_speed/100;
		rpm_2=(rpm_speed-rpm_1*100)/10;
		rpm_3=(rpm_speed-rpm_1*100-rpm_2*10);
		
		itoa(rpm_1, buffer, 10);
		rpm_1=buffer[0];
	
		itoa(rpm_2, buffer, 10);
		rpm_2=buffer[0];

		itoa(rpm_3, buffer, 10);
		rpm_3=buffer[0];
		//-------------------------
		

	}

}






















































// ADC Conversion Complete
SIGNAL(ADC_vect){
}


// Default interrupt vector to avoid reset 
//http://hubbard.engr.scu.edu/embedded/avr/doc/avr-libc/avr-libc-user-manual/index.html
SIGNAL(__vector_default){
    // user code here
}

// -------------Interrupt routines---------------------------- end



/*
// External Interrupt Request 0 Wheel speeds

SIGNAL(INT0_vect)
{

	temp=PINB;
	if (temp&0b00010000){							// An einai high koita to previous state
		if ((previous_state&0b00010000)==0)			// An isxyei exei allaxei to state tou PB4
			front_right_ticks++;
	}

	if (temp&0b00100000){							// An einai high koita to previous state
		if ((previous_state&0b00100000)==0)			// An isxyei exei allaxei to state tou PB5
			front_left_ticks++;
			
	}
	if (temp&0b01000000){							// An einai high koita to previous state
		if ((previous_state&0b01000000)==0)			// An isxyei exei allaxei to state tou PB6
			back_right_ticks++;
	}

	if (temp&0b10000000){
		if((previous_state&0b10000000)==0)			// An einai high koita to previous state
			back_left_ticks++;						// An isxyei exei allaxei to state tou PB7
			
	}

	previous_state=temp;			
}
*/


/*
// Timer/Counter0 Overflow 
SIGNAL(TIMER0_OVF_vect){

	if(servo_clock==0){														// Ola sto high
		PORTD|=(1<<PD6)|(1<<PD7);											// PD6:7 High
		PORTB|=(1<<PB0)|(1<<PB1)|(1<<PB2)|(1<<PB3)|(1<<PB4)|(1<<PB5);		// PB0:5 high
		servo_clock++;
	}
	else{
		if(servo_clock==servo_0)
			PORTD&=~(1<<PD6);						// PD6 low
		if(servo_clock==servo_1)
			PORTD&=~(1<<PD7);						// PD7 low
		if(servo_clock==servo_2)
			PORTB&=~(1<<PB0);						// PB0 low
		if(servo_clock==servo_3)
			PORTB&=~(1<<PB1);						// PB1 low
		if(servo_clock==servo_4)
			PORTB&=~(1<<PB2);						// PB2 low
		if(servo_clock==servo_5)
			PORTB&=~(1<<PB3);						// PB3 low
		if(servo_clock==servo_6)				
			PORTB&=~(1<<PB4);						// PB4 low
		if(servo_clock==servo_7)
			PORTB&=~(1<<PB5);						// PB5 low
	

		servo_clock++;
		if (servo_clock>1250) 					// An perasei to 1250, tote  oloklhrwse ton Palmo
			servo_clock=0;	
	}
}

*/
