/*
 * Lance_ATmega88.c
 *
 * Created: 16.11.2013 13:20:29
 *  Author: Sashal
 */ 

#define F_CPU 16000000UL

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#include "7Segment.h"

uint8_t Attempts = 0;
uint8_t Percents = 0;
uint8_t Meters = 0;
uint8_t Cantimeters = 0;

uint8_t MaxMeters = 0;
uint8_t MaxCantimeters = 0;

uint8_t SetMaxResult = 0;//1 - set maximal result
uint8_t ChoiseDigitForSetMaxResult = 0; 
/*------------------
1 - tens meters
2 - unity meters
3 - tens centimeters
4 - unity centimeters
------------------*/
uint8_t OnOffDigit = 0; // 0 - off, 1 - on

uint8_t Timer0Counter = 0;
uint16_t Timer1calibrate = 0;

void Button_delay(void);

/*ISR(PCINT1_vect)
{
	if ((PINC & (1<<PC0)) == 0) //set maximal result
	{
		while ((PINC & (1<<PC0)) == 0) ;
		if (SetMaxResult == 0) 
		{
			SetMaxResult = 1;
			ChoiseDigitForSetMaxResult = 1;
			TIMSK0 |= (1<<TOIE0);
		}
		else 
		{
			ChoiseDigitForSetMaxResult++;
			if (ChoiseDigitForSetMaxResult == 5)
			{
				ChoiseDigitForSetMaxResult = 0;
				SetMaxResult = 0;
				TIMSK0 &=~ (1<<TOIE0);
				StartDisplay(Attempts, Percents, Meters, Cantimeters);
			}
		}	
		Button_delay();		
	}
	if ((PINC & (1<<PC1)) == 0)//reset, check digits for set maximal results
	{
		while ((PINC & (1<<PC1)) == 0) ;
		if (SetMaxResult == 1)
		{
			switch(ChoiseDigitForSetMaxResult)
			{
				case 1:
				{
					MaxMeters = MaxMeters + 10;
					if (MaxMeters > 99) { MaxMeters = MaxMeters - 100; }
					break;
				}
				case 2:
				{
					if (MaxMeters % 10 == 9) { MaxMeters = MaxMeters - 9;}
					else { MaxMeters = MaxMeters + 1;}
					break;
				}
				case 3:
				{
					MaxCantimeters = MaxCantimeters + 10;
					if (MaxCantimeters > 99) { MaxCantimeters = MaxCantimeters - 100; }
					break;
				}
				case 4:
				{
					if (MaxCantimeters % 10 == 9) { MaxCantimeters = MaxCantimeters - 9;}
					else { MaxCantimeters = MaxCantimeters + 1;}
					break;
				}
			}
		}
		Button_delay(); 			
	}	
}

ISR(TIMER0_OVF_vect)
{
	Timer0Counter++;
	if (Timer0Counter == 15)
	{
		if (SetMaxResult == 1)
		{ OnOffDigit = SetMaxResultDisplay(Attempts, Percents, MaxMeters, MaxCantimeters, ChoiseDigitForSetMaxResult, OnOffDigit);}
		Timer0Counter = 0;
	}
}*/

ISR(TIMER1_CAPT_vect)
{
	//PORTC ^= (1<<PC3);
	TCNT1 = 0;
	Timer1calibrate = ICR1;
	if (Timer1calibrate == 0) { PORTC ^= (1<<PC3);}
	//StartDisplay(Attempts, Percents, ICR1L, Cantimeters);
}

/*void Button_delay(void)
{
	PCICR  &=~ (1<<PCIE1);
	PCMSK1 &=~ (1<<PCINT8);
	PCMSK1 &=~ (1<<PCINT9);
	
	sei();
	_delay_ms(500);
	cli();
	
	PCICR  |= (1<<PCIE1);
	PCMSK1 |= (1<<PCINT8)|(1<<PCINT9);
}*/

int main(void)
{
	DDRC |= (1<<PC3);
	PORTC |= (1<<PC0)|(1<<PC1);
	
	//PCICR  |= (1<<PCIE1);
	//PCMSK1 |= (1<<PCINT8)|(1<<PCINT9);//buttons
	
	//TCCR0B |= (1<<CS02)|(1<<CS00);
	
	TIMSK1 |= (1<<ICIE1);
	TCCR1B |= (ICES1)|(1<<ICNC1)|(1<<CS10);
	
	//InitSegment();
	//StartDisplay(Attempts, Percents, Meters, Cantimeters);
	
	sei();
	
    while(1)
    {
        asm("nop");
    }
}