#include "DSP2833x_Device.h"     // DSP2833x Headerfile Include File
#include "DSP2833x_SWPrioritizedIsrLevels.h" 

#include "RS485.h"
#include "message.h"
#include "filter_bat2.h"
#include "measure.h"
#include "package.h"
#include "test.h"
#include "kanal.h"
#include "peripher.h"

WORDE PRES;

int isMask = 1;
int isLamp = 0;
int isBrit = 0;
int isNumb = 1111;

long cownt=0;

unsigned int Light = 0xFFFF;

int quaLamp = 6;

void what_is()
{
  static int numb=0;

	if(keyTest)
	{
		if(keyNext & !preNext)
		{
			if(!isBrit)
			{
				isBrit=1;
				isMask=1;
				isLamp=0;
			}
			else
			{
				isMask<<=1;
	
				if(++isLamp >= quaLamp)
				{
					isMask=1;
					isLamp=0;
		}	}	}

		if(isBrit)
		{
			if(keyUp && !preUp)
			if(Bright[isLamp]<10) Bright[isLamp]++;

			if(keyDown & !preDown)
			if(Bright[isLamp]>0) Bright[isLamp]--;
		}
		else
		{
			if(cownt) cownt--;
			else
			{
				cownt = BLN_FREQ/4;

				numb++; if(numb==10) numb=1;
				isNumb = numb*1111;

				if(!isMask)	isMask = 0xFFFF;
				else		isMask = 0;
	}	}	}

	else
	{
		if(isBrit) 
		{
			isBrit=0;
			Save_params();
	}	}

	PRES = KEYS;
}


interrupt void cpu_timer1_isr_PULT(void)
{
  static int count_bright=0;
  unsigned int light=0, i;
  static LONGE Diod1,Diod2;
  static unsigned int Cownt,cownt;
  int dig1,dig2;

	EALLOW;
	CpuTimer1.InterruptCount++;
	IER |= MINT13;                 // Set "global" priority
	EINT;
  	EDIS;    // This is needed to disable write to EALLOW protected registers

	GpioDataRegs.GPATOGGLE.bit.GPIO0=1; //	Ready

	if(count_bright) count_bright --;
	else count_bright = 9;
	for(i=0; i<quaLamp; i++)
	{
		if(count_bright < Bright[i])	light+=(1<<i);
	}
	Light = light;

	Modbus[16].all = Inputs.all | cTestLamp;

	if(Modbus[16].bit.bit4)	GpioDataRegs.GPASET.bit.GPIO2=1;	//	
	else					GpioDataRegs.GPACLEAR.bit.GPIO2=1;	//

	Cownt++; cownt = Cownt&3;

	what_is();

	if(isBrit)
	{
		if(cownt==0) kanal_Send(adr_digg1,isLamp,0);
		if(cownt==1) kanal_Send(adr_digg2,Bright[isLamp],0);
	}
	else
	{
		if(keyTest)
		{
			dig1 = isNumb; 
			dig2 = isNumb;
		}
		else
		{
			dig1 = Modbus[0].all; 
			dig2 = Modbus[1].all;
		}

		if(cownt==0) kanal_Send(adr_digg1,dig1,0);
		if(cownt==1) kanal_Send(adr_digg2,dig2,0);
	}

	if(keyTest)	kanal_Send(adr_lamps, isMask		& Light,0);
	else		kanal_Send(adr_lamps, Modbus[2].all	& Light,0);

	if(Mode==adr_PLT1)
	{
		if(keyTest)
		{
			Diod1.wrd.word_0 = isMask;
			Diod1.wrd.word_1 = isMask;
			Diod2.wrd.word_0 = isMask;
			Diod2.wrd.word_1 = isMask;
		}
		else
		{
			Diod1.wrd.word_0 = Modbus[3].all;
			Diod1.wrd.word_1 = Modbus[4].all;
			Diod2.wrd.word_0 = Modbus[5].all;
			Diod2.wrd.word_1 = Modbus[6].all;
		}
		if(cownt==2) kanal_Send(adr_diod1,Diod1.all,0);
		if(cownt==3) kanal_Send(adr_diod2,Diod2.all,0);
	}

	ServiceDog();
}

void timer_Init()
{

#ifdef TUBER
	if(Mode==adr_PLT2) quaLamp = 4;
#endif
#ifdef P20183
	if(Mode==adr_PLT2) quaLamp = 4;
	if(Mode==adr_PLT3) quaLamp = 4;
#endif

	EALLOW;  // This is needed to write to EALLOW protected registers
	PieVectTable.XINT13 = &cpu_timer1_isr_PULT;
	EDIS;    // This is needed to disable write to EALLOW protected registers

	ConfigCpuTimer(&CpuTimer1, 	SYSCLKOUT/1000000, 	1000000/BLN_FREQ);
	CpuTimer1Regs.TCR.all = 0x4020; // Use write-only instruction to set TSS bit = 0
	IER |= M_INT13;
}
