#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 "pulto.h"
#include "kanal.h"
#include "tools.h"
#include "peripher.h"

unsigned long LoneLeft=0,LoneRite=0;

interrupt void cpu_timer1_isr_PULT(void)
{
#define ALONE (20L * READY_FREQ)

  static unsigned int cownt,count_bright,count_blink;
  static unsigned long isMask;
  static int numb,isNumb,preTest;
  unsigned int light=0;
  int i,TST,loneL=1,loneR=1;

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

	if(!cReset)
	ServiceDog();

	toggle_STROBE();

	if(++CanPowse >= CANPOWSE)
	{
		CanPowse = 0;
		CanGO = 1;
	}

	if(++LoneLeft > ALONE) 
	{
		LoneLeft = ALONE;
		loneL = 0;
	}
	if(++LoneRite > ALONE) 
	{
		LoneRite = ALONE;
		loneR = 0;
	}
	modbus[0] = max(modbus[4]*loneL, modbus[10]*loneR);
	modbus[1] = max(modbus[5]*loneL, modbus[11]*loneR);
	modbus[2] = modbus[6]*loneL | modbus[12]*loneR;
	modbus[3] = modbus[7]*loneL | modbus[13]*loneR;

	if(++count_bright >= maximum_bright) count_bright = 0 ;

	for(i=0; i<QuaLamp; i++)
	if(count_bright < Bright[i])	light+=(1<<i);

	if(++cownt>2) cownt=0;

	TST = cTestLamp|bTestLamp;

	if(TST & !preTest) 
	{
		count_blink = BLINK_TIME; 
		isMask = 0;
		numb = 10;
	}
	preTest = TST;

	if(TST)
	{
		if(++count_blink >= BLINK_TIME)
		{
			count_blink=0;

			numb++; if(numb>=10) numb=0;
			isNumb = numb*1000 + numb*100 + numb*10 +numb;

			if(isMask)	isMask = 0;
			else		isMask = 0xFFFFFFFF;
		}

		if(cownt == 0)	kanal_Send(0,isNumb,4);
		if(cownt == 1)	kanal_Send(1,isNumb,4);
						kanal_Send(2,isMask,0);
		if(cownt == 2)	kanal_Send(3,isMask,0);
	}
	else
	{
		if(cownt == 0)	kanal_Send(0,modbus[0],0);
		if(cownt == 1)	kanal_Send(1,modbus[1],0);
						kanal_Send(2,modbus[2] & light,0);
		if(cownt == 2)	kanal_Send(3,modbus[3],0);
}	}
