/*******************************************************************************
 * Aleix Riera                                                                 *
 * www.fjarre.com/~aleix                                                       *
 * aleix@fjarre.com                                                            *
 *******************************************************************************/
void init_panel_and_dsp(){
	panel = panel_init();
	if (!panel){
		TRACE("ERROR: cannot access the panel.\n");
		//return FALSE;
	}
	dsp = dsp_init(1, dspCode);
	if (!dsp){
		TRACE("ERROR: cannot access the DSP.\n");
		//return FALSE;
	}
}
//*****************************************************************
void init_panel_redefines(){
	panel_out_lcd_redefine(panel,0x06,char_active);
	panel_out_lcd_redefine(panel,0x07,char_param1);
	panel_out_lcd_redefine(panel,0x08,char_param2);
	panel_out_lcd_redefine(panel,0x01,char_noactive);
	panel_out_lcd_redefine(panel,0x02,char_line0);
	panel_out_lcd_redefine(panel,0x03,char_line1);
	panel_out_lcd_redefine(panel,0x04,char_line2);
	panel_out_lcd_redefine(panel,0x05,char_line3);
}
//*****************************************************************
void init_menu(){
	menu_index_part=0;
	menu_index_group=3*MAX_OPTIONS_PER_SECTION;
	menu_index_page=MAX_OPTIONS_PER_SECTION;
	menu_index_param=2*MAX_OPTIONS_PER_SECTION;
	//section:PART
	sprintf(menu[0],"%c",0x02);
	sprintf(menu[1],"%c",0x03);
	sprintf(menu[2],"%c",0x04);
	sprintf(menu[3],"%c",0x05);
	//section:GROUP
	sprintf(menu[3*MAX_OPTIONS_PER_SECTION],"%c",  	0x06);	//ACTIVE
	sprintf(menu[3*MAX_OPTIONS_PER_SECTION+1],"%c",	0x01);	//NO ACTIVE
	//section:PAGE
	strcpy(menu[MAX_OPTIONS_PER_SECTION],  		"FILTER");		//CONSTANTS
	strcpy(menu[MAX_OPTIONS_PER_SECTION+1],		"LFOs SPEED");		//FREQUENCIES
	strcpy(menu[MAX_OPTIONS_PER_SECTION+2],		"SEND DELAY");		//SEND
	strcpy(menu[MAX_OPTIONS_PER_SECTION+3],		"SATURATOR");	//SATURATION
	strcpy(menu[MAX_OPTIONS_PER_SECTION+4],		"LFO AMPS");		//AMPLITUDES
	strcpy(menu[MAX_OPTIONS_PER_SECTION+5],		"SEL.FILTER");		//FILTERLEVELS
	strcpy(menu[MAX_OPTIONS_PER_SECTION+6],		"LINE VOLUME");		//LINEVOL
	strcpy(menu[MAX_OPTIONS_PER_SECTION+7],		"DELAY PARAMS");	//DELAY
	strcpy(menu[MAX_OPTIONS_PER_SECTION+8],		"GLOBAL MIX");		//GLOBALMIX
	//section:PARAM
	strcpy(menu[2*MAX_OPTIONS_PER_SECTION],		"Amount:");	//SEND
	strcpy(menu[2*MAX_OPTIONS_PER_SECTION+1],	"Overdrive:");	//SATURATION
	strcpy(menu[2*MAX_OPTIONS_PER_SECTION+2],	"Level:");	//LINEVOL
	strcpy(menu[2*MAX_OPTIONS_PER_SECTION+3],	"Time:");	//DELAY
	strcpy(menu[2*MAX_OPTIONS_PER_SECTION+4],	"Feedback:");	//
	strcpy(menu[2*MAX_OPTIONS_PER_SECTION+5],	"Random:");	//
}
//*****************************************************************
void fill_tables(){
	fill_table(log_table,0,ONE,1);
	fill_table(nolog_table,0,ONE,0);
	fill_table(delaytime_table,TVALUE_DELAYTIME_MIN,ONE,1);
	fill_table(amp_table,	TVALUE_AMPLITUDE_INI,
				TVALUE_AMPLITUDE_FINAL,
				TVALUE_AMPLITUDE_LOG);
	fill_freq_tables(WAVE_TABLE_LEN,
				FS,
				TABLE_LEN,
				ifreq_table_LFO,
				ffreq_table_LFO,
				TVALUE_FREQ_INI,
				TVALUE_FREQ_FINAL,
				TVALUE_FREQ_LOG,
				TVALUE_FREQ_DMA);
	fill_table(offset_table,TVALUE_OFFSET_INI,
				TVALUE_OFFSET_FINAL,
				TVALUE_OFFSET_LOG);
	fill_table(offset_table_cutoff,TVALUE_OFFSET_CUTOFF_INI,
				TVALUE_OFFSET_CUTOFF_FINAL,
				TVALUE_OFFSET_CUTOFF_LOG);
}
//*****************************************************************
void init_variables(){
	flash_preset=0;
	led_bits=LEDS_ALWAYS;
	read_bank(TRUE);
}
//*****************************************************************
void leds_and_wait(){
	if(WAIT_TITLE)
		rtems_task_wake_after(WAIT_TITLE_TIME1);
	if(LEDS)
		panel_out_led(panel,PANEL01_LED_EDIT);
	if(WAIT_TITLE)
		rtems_task_wake_after(WAIT_TITLE_TIME1);
	if(LEDS)
		panel_out_led(panel,PANEL01_LED_EDIT|PANEL01_LED_SHIFT);
	if(WAIT_TITLE)
		rtems_task_wake_after(WAIT_TITLE_TIME1);
	print_title(2);
	if(LEDS)
		panel_out_led(panel,PANEL01_LED_EDIT|PANEL01_LED_SHIFT|PANEL01_LED_CTRL3);
	if(WAIT_TITLE)
		rtems_task_wake_after(WAIT_TITLE_TIME2);
	if(LEDS)
		panel_out_led(panel,PANEL01_LED_EDIT|PANEL01_LED_SHIFT|PANEL01_LED_CTRL2);
	if(WAIT_TITLE)
		rtems_task_wake_after(WAIT_TITLE_TIME2);
	if(LEDS)
		panel_out_led(panel,PANEL01_LED_EDIT|PANEL01_LED_SHIFT|PANEL01_LED_CTRL1);
	if(WAIT_TITLE)
		rtems_task_wake_after(WAIT_TITLE_TIME2);
	if(LEDS)
		panel_out_led(panel,PANEL01_LED_EDIT|PANEL01_LED_SHIFT|PANEL01_LED_CTRL2);
	if(WAIT_TITLE)
		rtems_task_wake_after(WAIT_TITLE_TIME2);
	if(LEDS)
		panel_out_led(panel,PANEL01_LED_EDIT|PANEL01_LED_SHIFT|PANEL01_LED_CTRL3);
	if(WAIT_TITLE)
		rtems_task_wake_after(WAIT_TITLE_TIME3);
	if(LEDS)
		panel_out_led(panel,PANEL01_LED_EDIT|PANEL01_LED_SHIFT|PANEL01_LED_CTRL1);
	if(WAIT_TITLE)
		rtems_task_wake_after(WAIT_TITLE_TIME3);
	if(LEDS)
		panel_out_led(panel,PANEL01_LED_EDIT|PANEL01_LED_SHIFT|PANEL01_LED_CTRL1|PANEL01_LED_CTRL2);
	if(WAIT_TITLE)
		rtems_task_wake_after(WAIT_TITLE_TIME3);
}
//*****************************************************************
void init_flash(){
	flash=flash_init();
	if(!flash)
		TRACE("ERROR: cannot access the flash.\n");
		//return FALSE;
}
//*****************************************************************
void exit_flash(){
	if(!flash_exit(flash))
		TRACE("ERROR: cannot close the flash.Were it already closed?\n");
		//return FALSE;
}
//*****************************************************************
void fill_table(rtems_signed32 *table,float margin1,float margin2,rtems_boolean log){
	int i;
	float value,valueini,valuetemp,m,dB;
	
	m=(margin2-margin1)/(TABLE_LEN-1);
	value=margin1;
	valueini=valuetemp=value;
	for(i=0;i<TABLE_LEN;i++){
		table[i]=float_to_fix(valuetemp);
		value+=m;
		if(log){
			if(i<27)	dB=-90+(float)40*i/27;
			else		dB=-50+(float)50*(i-27)/100;
			valuetemp=valueini+value*pow(10,dB/20);
		}
		else
			valuetemp=value;
	}
}
//*****************************************************************
void fill_freq_tables(int wlength,int fs,int tablelen,rtems_signed32 *itable,rtems_signed32 *ftable,float f1,float f2,rtems_boolean log,rtems_boolean dma){
	int i,tempint;
	float fn,fntemp,C,m,fnini;
	float tempfloat,dB;
	
	//y=mx+b
	m=(f2-f1)/(tablelen-1);
	
	//freq increments
	//	DMA 	=> out/1		(samples)
	//	noDMA 	=> out/BUFFER_SIZE	(samples)
	if(dma)	C=(float)wlength/fs;
	else	C=(float)wlength*BUFFER_SIZE/fs;
	
	//start compute
	fnini=f1*C;
	fntemp=fn=fnini;
	for(i=0;i<tablelen;i++){
		tempint=(int)fntemp;
		tempfloat=fntemp-tempint;
		if(tempfloat>=0.5) tempfloat-=1;
		itable[i]=float_to_fix(fix_to_float(tempint));
		ftable[i]=float_to_fix(2*tempfloat);
		//led_task (in ms)
			period[i]=C/(fntemp*2);//desnormalize & mult by 2 (+ and -)
			if(period[i]>MAX_WAIT_LFO_LEDS)	period[i]=MAX_WAIT_LFO_LEDS;
			period[i]*=1000;//in ms
		////
		fn+=m*C;
		if(log){
			if(i<27)	dB=-90+(float)40*i/27;
			else		dB=-50+(float)50*(i-27)/100;
			fntemp=fnini+fn*pow(10,dB/20);
		}
		else	fntemp=fn;
	}
}