#define TIMEOUTS 	98		// no. of timeouts to occur for waiting (1 timeout = 200x256us), 98 timeouts=5sec

#include<pic.h>
#include<pic1684.h>

__CONFIG(0x3ff1);

void ringcounter(void);
void dowakeon(void);
void dowakeoff(void);
void delay(void);
void wait5sec(void);
void getdipswitch(void);

char WAKEON = 4;
char WAKEOFF = 5;
char WAITDELAY = 2;
char ringcount=0;
unsigned int x=0;

void main(void)
{

GIE=0;						//disable interrupts
TRISA=0b00011111;				//PORTA=in
TRISB=0b00000000;				//B0 to B7 output
OPTION=0b11010111;				// tmr0/prescaler 256/tmr0 --> internal
PORTB =0;					//turn all relays off


// getdipswitch function sets the intial on/off rings and also the delay for rings to turn device on through A4,A3,A2,A1
//  A4	A3 wakeon wakeoff
//   0	0	2	3
//   0	1	3	4
//   1	0	4	5
//   1	1	5	6
//
//	A2 A1  waitdelay
//	0  0	5
//	0  1	10
//	1  0	15
//	1  1	20	

getdipswitch();				//configure rings and delay


while(1)
	{
 	ringcounter();
	if(ringcount==WAKEON)
		dowakeon();
	
	else if(ringcount==WAKEOFF)
		dowakeoff();
	
	}

}

void ringcounter(void)
{
ringcount=0;
while(1)
	{


	x=0;
	TMR0=0;
	while(RA0==0)		 
	{	
		if(TMR0>=200)					// to keep some margin in case TMR0 rolls back to 00, keep it to C8(200dec)!!
		{
			TMR0=0;
			x++;
			if(x>=TIMEOUTS)			//if timeout 5s return. Total timeout = 200us*TIMEOUTS*256(prescaler)
				return;
		}
	}
		while(RA0==1);					//while A0=1 wait
	ringcount++;
	}
}


void dowakeon(void)
{
char i;
for(i=0;i<WAITDELAY;i++)
	{
	ringcounter();
	if(ringcount==0)
		continue;
	else 
		switch(ringcount)
		{
		
			case 1: RB0=1;
					break;
			case 2: RB1=1;
					break;
			case 3: RB2=1;
					break;
			case 4: RB3=1;
					break;

			case 5: RB4=1;
					break;

			case 6: RB5=1;
					break;

			case 7: RB6=1;
					break;

			case 8: RB7=1;
					break;
			default: break;
		}
	}
ringcount=0;	
}


void dowakeoff(void)
{
char i;
for(i=0;i<WAITDELAY;i++)
	{
	ringcounter();
	if(ringcount==0)
		continue;
	else 
		switch(ringcount)
		{
		
			case 1: RB0=0;
					break;
			case 2: RB1=0;
					break;
			case 3: RB2=0;
					break;
			case 4: RB3=0;
					break;
			case 5: RB4=0;
					break;

			case 6: RB5=0;
					break;

			case 7: RB6=0;
					break;

			case 8: RB7=0;
					break;
			default: break;
		}
	}
ringcount=0;
}



void wait5sec(void)
{
x=0;
TMR0=0;
while(x<TIMEOUTS)
	if(TMR0>=200)					// to keep some margin in case TMR0 rolls back to 00, keep it to C8(200dec)!!
		{
			TMR0=0;
			x++;
		}
return;
 

}


void getdipswitch(void)
{
char temp1,temp2;
temp1 = (PORTA&0b00011000)>>3;				//get the A4,A3 bits into temp1
temp2 = (PORTA&0b00000110)>>1;				//get the A2,A1 bits into temp2
switch(temp1) 
	{
		case 0: WAKEON=2;WAKEOFF=3;
				break;
		case 1: WAKEON=3;WAKEOFF=4;
				break;
		case 2: WAKEON=4;WAKEOFF=5;
				break;
		case 3: WAKEON=5;WAKEOFF=6;
				break;
		default:break;
	}
			
switch(temp2) 
	{
		case 0: WAITDELAY=1;
				break;
		case 1: WAITDELAY=2;
				break;
		case 2: WAITDELAY=3;
				break;
		case 3: WAITDELAY=4;
				break;
		default:break;
	}

}

