/*      mija 2009

        SW pro ATmega8 mereni + regulace teploty
        Fosc = 8192000Hz externi krystal
        
        ver.: 0.0 - start        
*/

#include        <avr/io.h>
#include        <avr/interrupt.h>
#include        <util/delay.h>
#include        <avr/eeprom.h>
#include        "reflow.h"
#include        "lcd.h"                 // musi se definovat PINy LCD
#include        "key.h"                 // musi se definovat PINy Tlacitek
#include        "clock.h"               // typ promenne Clockt_t
#include        "process.h"             // musi se definovat PIN topeni

//*************************************************************
// delay + interrupt 

void delay_ms(uint16_t time)
{
        while (time --) _delay_ms(1);
}

volatile static uint8_t ClockTik8ms=125;

ISR(TIMER0_OVF_vect)    //tik 8ms
{
        if (ClockTik8ms) ClockTik8ms --; 
}

//*************************************************************
//      gemeral cpu set

void GeneralCpuInit(void)
{
        //***   IO PIN  *** init,setup
        
        //***   TIMER0  *** 
        // 256 prescaler
        // overflow 8ms
        // enable interrupt overflow
        TCCR0 = _BV(CS02);
        TIMSK |= _BV(TOIE0); 
        
        //***   TIMER1  *** 

        //***   TIMER2  *** 

        //***   EXTERNAL INTERRUPT 
                
        //***   USART   *** 
        
        //*** ADC               *** 
                // input ADC2 (pin25 C2)
                // external reference 2.5V
                // clock adc 128k (div 64)
        ADMUX = _BV(MUX1);
        ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADPS1) | _BV(ADPS2);
}

//*************************************************************
//      analog read

int16_t ReadAdc(void)
{
        int16_t analog;
        uint8_t a;

        analog = 0;
        for (a=0;a<32;a++)
        {
                ADCSR &= ~(_BV(ADIF));
                ADCSR |= _BV(ADSC);
                while (!(ADCSR & _BV(ADIF)));
                analog += ADC;
        }
        return (analog >> 5  );                 // prumer = analog/32
}

//*************************************************************
//      main 

int main(void)
{
        uint8_t         proces = START;
        char            DisplBuffer[MAXDISPLBUFFER];
        Clockt_t        clock;
        uint16_t        temp = 0;
        uint8_t         key = NOKEY;    

        GeneralCpuInit();
        HeatInit();
        HeatOff();
        delay_ms(100);
        LCD_init();
        KeyInit();
        ClockInit(0,0,0,&clock);
        sei();

        for (;;)
        {
                switch(proces)
                {
                case START:             proces = ProcesStart(DisplBuffer,key,temp);break;
                case SOLDER:    proces = ProcesSolder(DisplBuffer,key,temp,&clock);break;
                case POSITIV:   proces = ProcesPositiv(DisplBuffer,key,temp,&clock);break;
                case DRY:               proces = ProcesDry(DisplBuffer,key,temp,&clock); break;
                default:                proces = START;
                }

                if (DisplBuffer[0]) ProcesDispl(DisplBuffer);
                
                ProcesKey(&key);
                ProcesClock((uint8_t *)&ClockTik8ms,&clock);
                ProcesTemp(ReadAdc(),&temp);
        }
        return 0;
}