/*****************************************************************************/
/*                                                                           */
/* 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 "bargraph.h"
#include "oled.h"
#include "pushbuttons.h"
#include "stereo.h"
#include "dsplib.h"
#include "gsm.h"
#include "FIR_LP_4_kHz.h"

DATA buforLP1[N_LP + 2];
DATA buforLP2[N_LP + 2];
DATA buforLP3[N_LP + 2];

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


#define SAMPLES_PER_SECOND 48000
#define GAIN_IN_dB 0

//unsigned long int i = 0;

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

unsigned int decymator = 0;

//int j = 0;
//int k = 0;
//int m = 0;
//int display_counter = 0;
//int waveform_counter;
//long delay;

//DATA input_buffer[1024]; /* Must be declared as DATA for dsplib compatibility */

//DATA buffer2[1024];
//int display_buffer[96];
//int buffer1[96];

 
/* ------------------------------------------------------------------------ *
 *                                                                          *
 *  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("                 ", "                   ");
    oled_display_message("Cwiczenie 1      ", "Transmisja sygnalu ");
    
    printf("\n\nLaboratorium CPS\n");
    printf("Badanie decymacji i interpolacji\n" );
	printf("\n Decymacja i interpolacja");
	printf("\n Przyciskami przełącza się skład toru sygnału - włącza i wyłącza filtry\n");
	/* Setup sampling frequency and  gain for microphone */
    set_sampling_frequency_and_gain(SAMPLES_PER_SECOND, GAIN_IN_dB);

      
	/* Default to XF LED off */
	asm(" bclr XF");
    
 	for ( ;; )
 	{

     aic3204_codec_read(&left_input, &right_input); // Configured for one interrupt per two channels.
   
     mono_input = stereo_to_mono(left_input, right_input);
     
     Step = pushbuttons_read(7);
         
	  if ( Step == 1 )
        {
         if ( Step != LastStep )
          {
           oled_display_message("Zwarcie              ", "FP = 48 KHZ          ");
           printf("\n1\nNa obu wyjściach sygnał wejściowy z lewego kanału\n");
           LastStep = Step;
          }	
         left_output = left_input;      // Straight trough. No processing.
	     right_output = left_input;

	    } 
	  if ( Step == 2)
        {
         if ( Step != LastStep )
          {
           oled_display_message("L DECYMACJA        ", "P DECYMACJA + LP    ");
           printf("\n2 Decymacja bez filtru antyalizingowego\nKanał lewy:  left input -> próbkowanie (decymacja) 6:1 --------------> left output\n");
           printf(     "Kanał prawy: left input -> próbkowanie (decymacja) 6:1 -> LP(4 kHz) -> right output\n");
           LastStep = Step;
          }	
         if(decymator >= 5)
         {                  
         	decymator = 0;
         	left_output = left_input;      // Straight trough. No processing.
	     	right_output = left_input;
         }
         else
         {
         	decymator++;
         	left_output = 0;      // próbki zerowe
	     	right_output = 0;
         }
         fir(&right_output, H_LP, &right_output, buforLP1, 1, N_LP);
         right_output = shl(right_output, 2);

	    } 
	    
	    if ( Step == 3)
        {
         if ( Step != LastStep )
          {
           oled_display_message("L LP + DECYMACJA +LP ", "P DECYMACJA + LP    ");
           printf("\n3 Decymacja z filtrem antyalizingowym i bez\nKanał lewy:  left input -> LP(4 kHz) -> próbkowanie (decymacja) 6:1 -> LP(4 kHz) -> left output\n");
           printf(     "Kanał prawy: left input --------------> próbkowanie (decymacja) 6:1 -> LP(4 kHz) -> right output\n");
           LastStep = Step;
          }
          
         right_input = left_input;
         fir(&left_input, H_LP, &left_input, buforLP1, 1, N_LP);         	
         if(decymator >= 5)
         {                  
         	decymator = 0;
         	left_output = left_input;      // Straight trough. No processing.
	     	right_output = right_input;
         }
         else
         {
         	decymator++;
         	left_output = 0;      // próbki zerowe
	     	right_output = 0;
         }
         fir(&left_output, H_LP, &left_output, buforLP2, 1, N_LP);
         fir(&right_output, H_LP, &right_output, buforLP3, 1, N_LP);
	    } 
      if ( Step == 4)
        {
         if ( Step != LastStep )
          {
           oled_display_message("decymacja 12         ", "FP = 48 KHZ           ");
           printf("\n4\nKanał lewy:  left input  -> próbkowanie (decymacja) 12:1 -> left output\n");
           printf(     "Kanał prawy: right input -> próbkowanie (decymacja) 12:1 -> right output\n");
           LastStep = Step;
          }	
         if(decymator >= 11)
         {                  
         	decymator = 0;
         	left_output = left_input;      // Straight trough. No processing.
	     	right_output = right_input;
         }
         else
         {
         	decymator++;
         	left_output = 0;      // Straight trough. No processing.
	     	right_output = 0;
         }

	    } 
	    if ( Step == 5)
        {
         if ( Step != LastStep )
          {
           oled_display_message("impulsy 12:1         ", "FP = 48 KHZ           ");
           printf("\n5\nKanał lewy:  impulsy 12:1\n");
           printf(     "Kanał prawy: impulsy 12:1\n");
           LastStep = Step;
          }	
         if(decymator >= 11)
         {                  
         	decymator = 0;
         	left_output = -32000;      // Straight trough. No processing.
	     	right_output = -32000;
         }
         else
         {
         	decymator++;
         	left_output = 0;      // Straight trough. No processing.
	     	right_output = 0;
         }

	    }
	    
	    if ( Step == 6)
        {
         if ( Step != LastStep )
          {
           oled_display_message("impulsy 24:1         ", "FP = 48 KHZ           ");
           printf("\n6\nKanał lewy:  impulsy 24:1\n");
           printf(     "Kanał prawy: impulsy 24:1\n");
           LastStep = Step;
          }	
         if(decymator >= 23)
         {                  
         	decymator = 0;
         	left_output = -32000;      // Straight trough. No processing.
	     	right_output = -32000;
         }
         else
         {
         	decymator++;
         	left_output = 0;      // Straight trough. No processing.
	     	right_output = 0;
         }

	    }  
	    if ( Step == 7)
        {
         if ( Step != LastStep )
          {
           oled_display_message("impulsy 48:1         ", "FP = 48 KHZ           ");
           printf("\n7\nKanał lewy:  impulsy 48:1\n");
           printf(     "Kanał prawy: impulsy 48:1\n");
           LastStep = Step;
          }	
         if(decymator >= 47)
         {                  
         	decymator = 0;
         	left_output = -32000;      // Straight trough. No processing.
	     	right_output = -32000;
         }
         else
         {
         	decymator++;
         	left_output = 0;      // Straight trough. No processing.
	     	right_output = 0;
         }
         //if(decymator == 24) right_output = -32000;

	    }  
	    	  
	    
     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                                                           *
 *                                                                          *
 * ------------------------------------------------------------------------ */
