//---------------------------------------------------------
// main.c
//---------------------------------------------------------
#include "alllabcfg.h"			//BIOS include file
#include "framework.h"

#define N 147
void dataIO(void);


//Variables
uint8_t sel=255;
uint32_t dip_stat;

//Arrays for holding the filter coefficients and the input samples from both channels
float h[N]; //4*147=588 bytes
float x[2][N]; //1176 bytes

//Pointers for the circular buffer
int8_t pt_cur=0,pt_conv=0;


//---------------------------------------------------------
//---------------------------------------------------------
void main(void)
{
	uint8_t i;
	for(i=0;i<N;i++)
	{
		x[0][i]=0;
		x[1][i]=0;
		h[i]=0;
	}
	initAll();				
	return;  		// return to BIOS scheduler
}


//---------------------------------------------------------
//---------------------------------------------------------
void dipPRD(void)					
{
	uint8_t dip_status8;
	uint32_t dip_stat_prev;
	
	DIP_getAll(&dip_stat);
	DIP_get(DIP_8, &dip_status8);
	if(dip_status8)
	{
		LED_turnOn(LED_2);
		sel=1;
		
	}
	else 
	{
		LED_turnOff(LED_2);
		//sel=0;
	}
	dip_stat_prev=dip_stat;
	//printf("The DIP switch status is read to be %c",dip_status8);
	dip_stat=(255-dip_stat)&0x0000000F;
	if(dip_stat!=dip_stat_prev)
	{
	if(dip_stat==1|dip_stat==2|dip_stat==3) 
	sel=255;
	else if(dip_stat==9) 
	sel=2;
	else if(dip_stat==13) 
	sel=3;
	else if(dip_stat==10) 
	sel=5;
	else if(dip_stat==14) 
	sel=6;
	else if(dip_stat==11) 
	sel=8;
	else if(dip_stat==15) 
	sel=9;
	else 
	sel=255;
	dataIO();//Put probe point for data input here
	sel=10;//a dummy value to make sure the multiplcation loop is executed
}
	
}


//---------------------------------------------------------
//---------------------------------------------------------
void audioHWI(void)
{
	uint8_t i;
	uint32_t s,out;
	uint16_t sl,sr,outr,outl;
	float output_left=0,output_right=0;

	s = read_audio_sample();
	if(sel!=1)
	{
	sl=(uint16_t)(s>>16);
	sr=(uint16_t)s;
	x[0][pt_cur]=(float)sl;
	x[1][pt_cur]=(float)sr;
	pt_conv=pt_conv;
	
	for(i=0;i<31;i++)
	{
	output_left=output_left*output_left;//Test multiplications to check how many can be performed 
										// in a single interrupt
		//output_left+=h[i]*x[0][pt_conv];
		//output_right+=h[i]*x[1][pt_conv];
		pt_conv--;
		if(pt_conv==-1)pt_conv=(N-1);
	}

	pt_cur++;
	outr=(uint32_t)output_right;
	outl=(uint32_t)output_left;
	out=(uint32_t)(outl&0xFFFF0000)|(outr>>16);
	}
	write_audio_sample(s); //Loop back the audio to test the program
	
	//}
}
void dataIO()
{
	return;
}
	
