#include "DSP2833x_Device.h"     // DSP281x Headerfile Include File
#include "filter_bat2.h"
#include "measure.h"
#include "RS485.h"
#include "message.h"

#include "package.h"
#include "pulto.h"

#include "peripher.h"
#include "GPIO_table.h"

int Mode,Desk,TermoAD=0,TermoRS=0,TermoSW=0,Currentoz=0;
long INcount0=0,INcount1=0;
LONGE KeyPressed;

void get_Mode()
{
	EALLOW;

	GpioCtrlRegs.GPAMUX1.all &= 0xFF000000;	//	00—11
	GpioCtrlRegs.GPAMUX2.all &= 0xFF00003F;	//	19—27
	GpioCtrlRegs.GPBMUX1.all &= 0xFFFFFFC0;	//	32—34
	GpioCtrlRegs.GPBMUX2.all &= 0x000FF000;	//	48—53, 58—63

	GpioCtrlRegs.GPADIR.bit.GPIO20 = 0;
	GpioCtrlRegs.GPADIR.bit.GPIO21 = 0;
	GpioCtrlRegs.GPADIR.bit.GPIO22 = 0;
	GpioCtrlRegs.GPBDIR.bit.GPIO51 = 0;

	EDIS; 

	Mode=1; 
	
	if(!GpioDataRegs.GPADAT.bit.GPIO21) Mode += 1;
	if(!GpioDataRegs.GPADAT.bit.GPIO20) Mode += 2;
	if(!GpioDataRegs.GPADAT.bit.GPIO22) Mode += 4;
	if(!GpioDataRegs.GPBDAT.bit.GPIO51) Mode += 8;
	
	if( (Mode==adr_POW1)||(Mode==adr_POW2)||	
		(Mode==adr_LOA1)||(Mode==adr_LOA2)	)				Currentoz = 1;

	if( (Mode==adr_POW1)||(Mode==adr_POW2))	Desk = dsk_COMM;
	if(	(Mode==adr_ENG1)||(Mode==adr_ENG2))	Desk = dsk_BKSD;
	if(	(Mode==adr_TRN1)||(Mode==adr_TRN2))	Desk = dsk_BKST;
	if(	(Mode==adr_LOA1)||(Mode==adr_LOA2))	Desk = dsk_LOAD;
	if	(Mode==adr_PLT3)					Desk = dsk_EPLT;
	if	(Mode==adr_SHKF)					Desk = dsk_SHKF;

	if(	(Desk==dsk_BKSD)||(Desk==dsk_BKST))	TermoRS = 1;

	if(	(Desk==dsk_COMM)||(Desk==dsk_SHKF))	TermoAD = 1;

	if(	(Desk==dsk_COMM)||(Desk==dsk_BKST))	TermoSW = 2;
	if	(Desk==dsk_BKSD)					TermoSW = 1;


	EALLOW;
	switch(Desk)
	{
		case dsk_COMM:	GpioCtrlRegs.GPADIR.all = COMM_GPADIR;
						GpioCtrlRegs.GPBDIR.all = COMM_GPBDIR;	break;

		case dsk_BKSD:	GpioCtrlRegs.GPADIR.all = BKSD_GPADIR;
						GpioCtrlRegs.GPBDIR.all = BKSD_GPBDIR;	break;

		case dsk_BKST:	GpioCtrlRegs.GPADIR.all = BKST_GPADIR;
						GpioCtrlRegs.GPBDIR.all = BKST_GPBDIR;	break;
		
		case dsk_EPLT:	GpioCtrlRegs.GPADIR.all = PLT2_GPADIR;
						GpioCtrlRegs.GPBDIR.all = PLT2_GPBDIR;	break;

		case dsk_LOAD:	GpioCtrlRegs.GPADIR.all = LOAD_GPADIR;
						GpioCtrlRegs.GPBDIR.all = LOAD_GPBDIR;	
						GpioDataRegs.GPASET.bit.GPIO3=1;		
						break;

		case dsk_SHKF:	GpioCtrlRegs.GPADIR.all = SHKF_GPADIR;
						GpioCtrlRegs.GPBDIR.all = SHKF_GPBDIR;	break;

	}
	EDIS;
}

void select_tpl_canal(int n_tpl)
{

	if(Desk==dsk_BKSD)
	{
#define BKSD_A_tpl_zero ((1L<<11) + (1L<<9) + (1L<<7) + (1L<<3))

		GpioDataRegs.GPASET.all = BKSD_A_tpl_zero;

		if(n_tpl&0x01)	GpioDataRegs.GPACLEAR.bit.GPIO11=1;
		if(n_tpl&0x02)	GpioDataRegs.GPACLEAR.bit.GPIO9=1;
		if(n_tpl&0x04)	GpioDataRegs.GPACLEAR.bit.GPIO7=1;
		if(n_tpl&0x08)	GpioDataRegs.GPACLEAR.bit.GPIO3=1;
	}

	if(Desk==dsk_BKST)
	{
#define BKST_A_tpl_zero ((1L<<11) + (1L<<9) + (1L<<7) + (1L<<3) + (1L<<1) + (1L<<8) + (1L<<6) + (1L<<0) + (1L<<27) + (1L<<24))
#define BKST_B_tpl_zero ((1L<<(53-32)) + (1L<<(50-32)))

		GpioDataRegs.GPASET.all = BKST_A_tpl_zero;
		GpioDataRegs.GPBSET.all = BKST_B_tpl_zero;

		if(n_tpl==0)	GpioDataRegs.GPACLEAR.bit.GPIO11=1;
		if(n_tpl==1)	GpioDataRegs.GPACLEAR.bit.GPIO9=1;
		if(n_tpl==2)	GpioDataRegs.GPACLEAR.bit.GPIO7=1;
		if(n_tpl==3)	GpioDataRegs.GPACLEAR.bit.GPIO3=1;
		if(n_tpl==4)	GpioDataRegs.GPACLEAR.bit.GPIO1=1;
		if(n_tpl==5)	GpioDataRegs.GPACLEAR.bit.GPIO8=1;
		if(n_tpl==6)	GpioDataRegs.GPACLEAR.bit.GPIO6=1;
		if(n_tpl==7)	GpioDataRegs.GPACLEAR.bit.GPIO0=1;
		if(n_tpl==8)	GpioDataRegs.GPACLEAR.bit.GPIO27=1;
		if(n_tpl==9)	GpioDataRegs.GPACLEAR.bit.GPIO24=1;
		if(n_tpl==10)	GpioDataRegs.GPBCLEAR.bit.GPIO53=1;
		if(n_tpl==11)	GpioDataRegs.GPBCLEAR.bit.GPIO50=1;
	}

	if(Desk==dsk_COMM)
	{
#define COMM_A_tpl_zero ((1L<<27) + (1L<<24) + (1L<<25))
#define COMM_B_tpl_zero ((1L<<(49-32)) + (1L<<(52-32)))

		GpioDataRegs.GPACLEAR.all = COMM_A_tpl_zero;
		GpioDataRegs.GPBCLEAR.all = COMM_B_tpl_zero;

		if(n_tpl&0x01)	GpioDataRegs.GPASET.bit.GPIO27=1;
		if(n_tpl&0x02)	GpioDataRegs.GPASET.bit.GPIO24=1;
		if(n_tpl&0x04)	GpioDataRegs.GPASET.bit.GPIO25=1;
		if(n_tpl&0x08)	GpioDataRegs.GPBSET.bit.GPIO49=1;
		if(n_tpl&0x10)	GpioDataRegs.GPBSET.bit.GPIO52=1;
}	}

void get_Buttons()
{
  unsigned long butt =0;
  unsigned long fluf =0;

	if(Desk==dsk_BKSD)
	{
		bTestLamp = !GpioDataRegs.GPBDAT.bit.GPIO50;
		bSecretBt = !GpioDataRegs.GPBDAT.bit.GPIO49;

		InputRep0 = bTestLamp;
		InputRep1 = bSecretBt;

		if(GpioDataRegs.GPADAT.bit.GPIO0) butt +=0x0001;
		if(GpioDataRegs.GPADAT.bit.GPIO2) butt +=0x0002;
		if(GpioDataRegs.GPADAT.bit.GPIO10)butt +=0x0004;
		if(GpioDataRegs.GPADAT.bit.GPIO4) butt +=0x0008;
	}

	if(Desk==dsk_BKST)
	{
		bSecretBt = !GpioDataRegs.GPBDAT.bit.GPIO63;

		InputRep1 = bSecretBt;

		return;
	}

	if(Desk==dsk_COMM)
	{
		bSecretBt = !GpioDataRegs.GPBDAT.bit.GPIO53;
		bTermoCal = er_anal(!GpioDataRegs.GPBDAT.bit.GPIO50, &INcount0, 500000, 0);

		InputRep1 = bSecretBt;
		InputRep2 = bTermoCal;

		return;
	}

	if(Desk==dsk_EPLT)
	{
		bTestLamp = er_anal(GpioDataRegs.GPADAT.bit.GPIO26, &INcount0, 1000, 0);

		if(GpioDataRegs.GPADAT.bit.GPIO27)	butt +=0x0001;
		if(GpioDataRegs.GPADAT.bit.GPIO24)	butt +=0x0002;
		if(GpioDataRegs.GPBDAT.bit.GPIO53)	butt +=0x0004;
		if(GpioDataRegs.GPBDAT.bit.GPIO50)	butt +=0x0008;

		if(GpioDataRegs.GPADAT.bit.GPIO26)	butt +=0x0010;
		if(GpioDataRegs.GPADAT.bit.GPIO23)	butt +=0x0020;
		if(GpioDataRegs.GPBDAT.bit.GPIO52)	butt +=0x0040;
		if(GpioDataRegs.GPADAT.bit.GPIO4 )	butt +=0x0080;

		if(GpioDataRegs.GPADAT.bit.GPIO25)	butt +=0x0100;
		if(GpioDataRegs.GPBDAT.bit.GPIO49)	butt +=0x0200;
		if(GpioDataRegs.GPADAT.bit.GPIO11)	butt +=0x0400;
		if(GpioDataRegs.GPADAT.bit.GPIO5 )	butt +=0x0800;

		if(GpioDataRegs.GPBDAT.bit.GPIO48)	butt +=0x1000;
		if(GpioDataRegs.GPADAT.bit.GPIO10)	butt +=0x2000;	//	Ñåêðåòíûé âõîä
	}

	if(Desk==dsk_LOAD)
	{
		if(!GpioDataRegs.GPADAT.bit.GPIO6)	butt +=0x0001;	//	OMEGA
		if(!GpioDataRegs.GPADAT.bit.GPIO8)	butt +=0x0002;	//	Start_1
		if(!GpioDataRegs.GPADAT.bit.GPIO11)	butt +=0x0004;	//	Res_in
	}	

	if(Desk==dsk_SHKF)
	{

		if(GpioDataRegs.GPBDAT.bit.GPIO53)	fluf += 0x0000010; else butt += 0x0001;
		if(GpioDataRegs.GPBDAT.bit.GPIO52)	fluf += 0x0000040; else butt += 0x0002;

		if(GpioDataRegs.GPADAT.bit.GPIO2)	fluf += 0x0001000; else butt += 0x0004;
		if(GpioDataRegs.GPADAT.bit.GPIO4)	fluf += 0x0002000; else butt += 0x0008;
		if(GpioDataRegs.GPADAT.bit.GPIO6)	fluf += 0x0004000; else butt += 0x0010;
		if(GpioDataRegs.GPADAT.bit.GPIO0)	fluf += 0x0008000; else butt += 0x0020;

		if(GpioDataRegs.GPADAT.bit.GPIO8)	fluf += 0x0010000; else butt += 0x0040;
		if(GpioDataRegs.GPADAT.bit.GPIO10)	fluf += 0x0020000; else butt += 0x0080;
		if(GpioDataRegs.GPADAT.bit.GPIO3)	fluf += 0x0040000; else butt += 0x0100;
		if(GpioDataRegs.GPADAT.bit.GPIO5)	fluf += 0x0080000; else butt += 0x0200;
		if(GpioDataRegs.GPADAT.bit.GPIO7)	fluf += 0x0100000; else butt += 0x0400;
		if(GpioDataRegs.GPADAT.bit.GPIO1)	fluf += 0x0200000; else butt += 0x0800;
		if(GpioDataRegs.GPADAT.bit.GPIO11)	fluf += 0x0400000; else butt += 0x1000;
		if(GpioDataRegs.GPADAT.bit.GPIO9)	fluf += 0x0800000; else butt += 0x2000;

		KeyPressed.all = fluf;
	}

	Inputs.all = butt;
}