/*****************************************************************************/
/*                                                                           */
/* FILENAME                                                                  */
/* 	 main.c                                                                  */
/*                                                                           */
/* DESCRIPTION                                                               */
/*   TMS320C5515 USB Stick Application 20 Spectrum Analyser.                 */
/*   Take microphone input and send to headphones with ring modulation.      */
/*                                                                           */
/* REVISION                                                                  */
/*   Revision: 1.00	                                                         */
/*   Author  : Richard Sikora                                                */
/*---------------------------------------------------------------------------*/
/*                                                                           */
/* HISTORY                                                                   */
/*   Revision: 1.00                                                          */
/*   15th September 2010. Created from TMS320C5505 USB Stick code.           */
/*                                                                           */
/*****************************************************************************/
/*
 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/ 
 * 
 * 
 *  Redistribution and use in source and binary forms, with or without 
 *  modification, are permitted provided that the following conditions 
 *  are met:
 *
 *    Redistributions of source code must retain the above copyright 
 *    notice, this list of conditions and the following disclaimer.
 *
 *    Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the 
 *    documentation and/or other materials provided with the   
 *    distribution.
 *
 *    Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
*/

#include "stdio.h"
#include "usbstk5515.h"
#include "usbstk5515_led.h"
#include "aic3204.h"
#include "PLL.h"
#include "oled.h"
#include "pushbuttons.h"
#include "stereo.h"
#include "dsplib.h"
#include "sar.h"

#include "iir.h"

//#include "IIR_band_pass_filters.h"
//#include "IIR_band_stop_filters.h"
#include "IIR_filters_fourth_order.h"
#include "IIR_low_pass_filters.h"

Int16 left_input;
Int16 right_input;
Int16 left_output;
Int16 right_output;
Int16 mono_input;


Int16 AM;
Int16 nosna;
Int16 AM_x_nosna;
Int16 absAM;
Int16 dem_AM;
Int16 dem_AM_AS;
Int16 dem_AM_4;
Int16 dem_AM_AS_4;

#define SAMPLES_PER_SECOND 48000
#define GAIN_IN_dB 0

unsigned int Step = 0; 
unsigned int LastStep = 99;

int fo = 12000;
int B3dB = 500;

char *napis1 = "1                   ";
char *napis2 = "2                   ";

signed int wsp_BP[6] = {0, 0, 0, 0, 0, 0 };
signed int x_BP[3] = { 0, 0, 0 };  // próbki wejściowe
signed int y_BP[3] = { 0, 0, 0 };  // próbki wyjściowe

signed int wsp_LP_out[6] = {0, 0, 0, 0, 0, 0 };
signed int x_LP[3] = { 0, 0, 0 };  // próbki wejściowe
signed int y_LP[3] = { 0, 0, 0 };  // próbki wyjściowe

signed int x_LP_as[3] = { 0, 0, 0 };  // próbki wejściowe
signed int y_LP_as[3] = { 0, 0, 0 };  // próbki wyjściowe


/* ------------------------------------------------------------------------ *
 *                                                                          *
 *  main( )                                                                 *
 *                                                                          *
 * ------------------------------------------------------------------------ */
void main( void ) 
 {
    /* Initialize BSL */
    USBSTK5515_init( );
	
	/* Initialize PLL */
	pll_frequency_setup(120);

    /* Initialise hardware interface and I2C for code */
    aic3204_hardware_init();
    
    /* Initialise the AIC3204 codec */
	aic3204_init(); 
	
	/* Turn off the 4 coloured LEDs */
	USBSTK5515_ULED_init();
	
	/* Initialise the OLED LCD display */
	oled_init();
	SAR_init();
	/* Flush display buffer */
    oled_display_message("                 ", "                   ");
    
    printf("\n\nLaboratorium FCiPS\n");
    printf( "Ćwiczenie Demodulatory\n" );

	/* Setup sampling frequency and  gain for microphone */
    set_sampling_frequency_and_gain(SAMPLES_PER_SECOND, GAIN_IN_dB);

    oled_display_message("lab fcips          ", "DEMODULATORY  AM   ");

	printf( "Ćwiczenie Demodulatory AM asynchroniczny i synchroniczny\n" );
	printf( "Sygnał AM podawany z generatora na wejście kanału lwego\n" );
      
	/* Default to XF LED off */
	asm(" bclr XF");
	
	// projekt filtru LP
	wsp_LP(2400, SAMPLES_PER_SECOND, wsp_LP_out);
    
 	for ( ;; )
 	{

     aic3204_codec_read(&left_input, &right_input); // Configured for one interrupt per two channels.  
     
     // demodulacja AM
     AM = left_input;

     nosna = SOS_Q14(x_BP, y_BP, wsp_BP, AM);  // odfiltrownie nośnej
           
	 AM_x_nosna = _smpy(AM, nosna);	 
	 AM_x_nosna = _sshl(AM_x_nosna - (1 << 14), 1);
	 	 
	 absAM = _abss(AM);
	 absAM = _sshl(absAM - (1 << 14), 1);
		
	dem_AM =  IIR_I_order(x_LP, y_LP, wsp_LP_out, AM_x_nosna);
	dem_AM_AS = IIR_I_order(x_LP_as, y_LP_as, wsp_LP_out, absAM);
		
	dem_AM_4 = fourth_order_IIR_direct_form_II(IIR_low_pass_2400Hz, AM_x_nosna);
	dem_AM_AS_4 = fourth_order_IIR_direct_form_I(IIR_low_pass_2400Hz, absAM);
	
	// koniec demodulacji
        
     Step = pushbuttons_read(4);
     
     if ( Step == 1 )
        {
         if ( Step != LastStep )
          {
           oled_display_message("patrz na konsole  ", "i wpisz parametry  ");
            printf("Podaj częstotliwość nośną sygnału AM [Hz] fn = ");
    		scanf("%d", &fo);
    		B3dB = 30;
    		//wsp_IIR_BP(fo, B3dB, wsp1);
    		//wsp_IIR_BP(fo, B3dB, wsp2);	
    		
    		
    		wsp_SOS_BP_Q14(fo, 30, SAMPLES_PER_SECOND, wsp_BP);
           
           
           oled_display_message("L = AM            ", "P = nosna         ");
           printf( "1 Sygnały wyjściowe \nKanał lewy --> sygnał AM \nKanał prawy -> odfiltrowana nośna\n" );
           
           LastStep = Step;

          }	
         left_output = AM;      // Straight trough. No processing.
	     right_output = nosna;

	    } 
   
	  if ( Step == 2)
        {
         if ( Step != LastStep )
          {
           oled_display_message("L = nosna * AM    ", "P = abs AM        ");
           printf( "2 Sygnały wyjściowe \nKanał lewy --> sygnał AM * nośna\nKanał prawy -> wyprostowany sygnał AM\n" );
           
           LastStep = Step;
          }	
         left_output = AM_x_nosna;     // -60dB
	     right_output = absAM;	// -80dB

	    } 
	      if ( Step == 3)
        {
         if ( Step != LastStep )
          {
            oled_display_message("L = nosna * AM    ", "P = demodul. AM   ");
            printf( "3 Sygnały wyjściowe \nKanał lewy --> wynik demodulacji synchronicznej, po filtrze LP(2400Hz) I rzędu\nKanał prawy -> wynik demodulacji asynchronicznej, po filtrze LP(2400Hz) I rzędu\n" );
                     
            LastStep = Step;
          }         
         left_output = dem_AM;
	     right_output = dem_AM_AS;	// -80dB
	    }
	    
	    if ( Step == 4)
        {
         if ( Step != LastStep )
          {
            oled_display_message("L = nosna * AM    ", "P = demodul. AM   ");
            printf( "4 Sygnały wyjściowe \nKanał lewy --> wynik demodulacji synchronicznej, po filtrze LP(2400Hz) IV rzędu\nKanał prawy -> wynik demodulacji asynchronicznej, po filtrze LP(2400Hz) IV rzędu\n" );
                             
            LastStep = Step;
          }         
         left_output = dem_AM_4;
	     right_output = dem_AM_AS_4;	// -80dB
	    }
	         	    
     aic3204_codec_write(left_output, right_output);
 	}

   /* Disable I2S and put codec into reset */ 
    aic3204_disable();

    printf( "\n***Program has Terminated***\n" );
    
    oled_display_message("PROGRAM HAS        ", "TERMINATED        ");
    
    SW_BREAKPOINT;
}

/* ------------------------------------------------------------------------ *
 *                                                                          *
 *  End of main.c                                                           *
 *                                                                          *
 * ------------------------------------------------------------------------ */
