/** I N C L U D E S **********************************************************/
#include <p18cxxx.h>
#include <timers.h>
#include "io_cfg.h"
#include "leddisplay.h" // 7 szegmenses LED kijelző driver

/** V A R I A B L E S ********************************************************/
#pragma udata
double seconds = 3662;
#define OSZTO 9375 //((48*1000000)/256)/4; // 8bit, 4-es előosztó
#define KEZDET 150
int old_sw2;
int mode = 0;
double temp = -999;

/** P R I V A T E  P R O T O T Y P E S ***************************************/
void InterruptHandlerHigh (void);
void init(void);
int Switch2IsPressed(void);

/** V E C T O R  R E M A P P I N G *******************************************/

/*extern void _startup (void);        // See c018i.c in your C18 compiler dir
#pragma code _RESET_INTERRUPT_VECTOR = 0x000800
void _reset (void)
{
    _asm goto _startup _endasm
}
#pragma code
*/
#pragma code _HIGH_INTERRUPT_VECTOR = 0x000808
void _high_ISR (void)
{
  _asm
    goto InterruptHandlerHigh //jump to interrupt routine
  _endasm
}

//----------------------------------------------------------------------------
// High priority interrupt routine
#pragma code
#pragma interrupt InterruptHandlerHigh

void InterruptHandlerHigh ()
{
  static unsigned int tmp = 0;
  if (INTCONbits.TMR0IF)
  {                                   //check for TMR0 overflow
     	INTCONbits.TMR0IF = 0;            //clear interrupt flag
		tmp++;
		TMR0L = KEZDET;
		if (tmp == OSZTO)
		{
			seconds++;
			if (seconds == 86400) seconds = 0;
			tmp = 0;
			sLED2_Toggle();
		}
  }
}

#pragma code _LOW_INTERRUPT_VECTOR = 0x000818
void _low_ISR (void)
{
    ;
}
#pragma code

/** D E C L A R A T I O N S **************************************************/
#pragma code
void main(void)
{
	int o = 0, p = 0, i = 0;
/*	double temps[6] = {-10.25,
					  -2.3,
					  -1.52,
					  1.93,
					  23,
					  3
		             };
*/
	init();
	init_temp();
    while(1)
    {
		//writeTime(o, p);
		/*if (Switch2IsPressed()) i++;
		if (i >= 6) i = 0;*/
		if (Switch2IsPressed()) mode = !mode;
		if (mode) {
			temp = getTemp();
			writeTemperature(temp);
			} else {
				o = seconds / 3600;
				p = (seconds - (o * 3600)) / 60;
				writeTime(o, p);
			}
    }//end while
}//end main

// inicializálás
void init(void)
{
  INTCON = 0x20;                //disable global and enable TMR0 interrupt
  INTCON2 = 0x84;               //TMR0 high priority
  RCONbits.IPEN = 1;            //enable priority levels
  TMR0H = 0;                    //clear timer
  TMR0L = KEZDET;                    //clear timer
//  T0CON = 0x82;                 //set up timer0 - prescaler 1:8
  T0CON = 0xC1;                 //set up timer0 - prescaler 1:4 81
  INTCONbits.GIEH = 1;          //enable interrupts
  ADCON1 |= 0x0F;                 // Default all pins to digital
  mInitAllSwitches();
  old_sw2 = sw2;
  mInitAllLEDs();
  clrscr(); // kijelző törlése
} // end init

int Switch2IsPressed(void)
{
    if(sw2 != old_sw2)
    {
        old_sw2 = sw2;                  // Save new value
        if(sw2 == 0)                    // If pressed
            return 1;                // Was pressed
    }//end if
    return 0;                       // Was not pressed
}//end Switch2IsPressed

/** EOF Demo02.c *************************************************************/
