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

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

short int faza1 = 0;
short int faza2 = 3000;

long int L_faza1 = 0;
long int L_faza2 = 3000;



int f1 = 1000, f2 = 20000;
int fn = 12000, f01, f02;
int DSB, AM;


int F1, F2, F; //Unormowane Częstotliwości cyfrowe
int deltaF;

long L_F1, L_F2, L_F; //Unormowane Częstotliwości cyfrowe
long L_deltaF;
long L_deltaF_podzielone;

int fp;

short int sinus1, sinus2;


#define SAMPLES_PER_SECOND 48000
#define GAIN_IN_dB 0

//unsigned long int i = 0;

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

char *napis1 = "1                   ";
char *napis2 = "2                   ";
 
/* ------------------------------------------------------------------------ *
 *                                                                          *
 *  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();

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

    oled_display_message("                 ", "                   ");
    oled_display_message("Cwiczenie 2      ", "Generatory i modul.");

	/* New. Add descriptive text */ 
	puts("\n\nLaboratorium CPS\n");
	puts("Ćwiczenie 2.3 - Efekt kaczego głosu - mnożenie sygnału przez sinus");

    puts(" 1 - Transmisja z wejścia na wyjście");
    puts(" 2 - Mnożenie przez sinus - 20 Hz");
    puts(" 3 - Mnożenie przez sinus - 50 Hz");
    puts(" 4 - Mnożenie przez sinus - 100 Hz");
	puts(" 5 - Mnożenie przez sinus - 200 Hz");
	puts(" 6 - Mnożenie przez sinus - 500 Hz");
	puts(" 7 - Mnożenie przez sinus - 1000 Hz");
	puts(" 8 - Mnożenie przez sinus - 2000 Hz");
      
	/* Default to XF LED off */
	asm(" bclr XF");
	
	for ( ;; )
 	{
     aic3204_codec_read(&left_input, &right_input); // Configured for one interrupt per two channels.
        
     Step = pushbuttons_read(8);
         
	  if ( Step == 1 )
        {
         if ( Step != LastStep )
          {
          	sprintf(napis2, "fn = %5d Hz        ", fn);
            oled_display_message("MODULATOR FM     ", napis2);
            puts("\n Krok 1 --> Transmisja z wejścia na wyjście");

           LastStep = Step;           
          }	
          
//        f01 = fn + (left_input >> 1);
//        f02 = fn + (right_input >> 1);
//                   
//        F1 = ((long)f01 * 22370 ) >> 14; 
//     	faza1 += F1;
//    	sine(&faza1, &sinus1, 1);
//     
//     	F2 = ((long)f02 * 22370 ) >> 14;
//     	faza2 += F2;
//     	sine(&faza2, &sinus2, 1);         
          
         left_output = left_input;      // Straight trough. No processing.
	     right_output = right_input;
	    } 
	  if ( Step == 2)
        {
         if ( Step != LastStep )
          {
          	sprintf(napis1, "CHIRP        ", f1);
          	sprintf(napis2, "%5d Hz, %5d Hz >   ",f1, f2);
            oled_display_message(napis1, napis2);
			 puts("\n Krok 2 --> Mnożenie przez sinus - 20 Hz");
			 
           	LastStep = Step;
			     
          }	
             	 
    	fn = 20;
        F1 = ((long)fn * 22370 ) >> 14; 
     	faza1 += F1;
    	sine(&faza1, &sinus1, 1);
     	
     	DSB = ((long)left_input * sinus1) >> 15;      
          
        left_output = DSB;      // Straight trough. No processing.
	    right_output = DSB;
	     
	    } 
      if ( Step == 3)
        {
         if ( Step != LastStep )
          {
          	sprintf(napis1, "CHIRP        ", f1);
          	sprintf(napis2, "%5d Hz, %5d Hz >   ",f1, f2);
            oled_display_message(napis1, napis2);
			 puts("\n Krok 3 --> Mnożenie przez sinus - 50 Hz");
			 
           	LastStep = Step;
			     
          }	
             	 
    	fn = 50;
        F1 = ((long)fn * 22370 ) >> 14; 
     	faza1 += F1;
    	sine(&faza1, &sinus1, 1);
     	
     	DSB = ((long)left_input * sinus1) >> 15;      
          
        left_output = DSB;      // Straight trough. No processing.
	    right_output = DSB;
	    } 
	    
	    if ( Step == 4)
        {
          if ( Step != LastStep )
          {
          	sprintf(napis1, "CHIRP        ", f1);
          	sprintf(napis2, "%5d Hz, %5d Hz >   ",f1, f2);
            oled_display_message(napis1, napis2);
			 puts("\n Krok 4 --> Mnożenie przez sinus - 100 Hz");
			 
           	LastStep = Step;
			     
          }	
             	 
	    	fn = 100;
	        F1 = ((long)fn * 22370 ) >> 14; 
	     	faza1 += F1;
	    	sine(&faza1, &sinus1, 1);
	     	
	     	DSB = ((long)left_input * sinus1) >> 15;      
	          
	        left_output = DSB;      // Straight trough. No processing.
		    right_output = DSB;
	    }  
	    if ( Step == 5)
        {
          if ( Step != LastStep )
          {
          	sprintf(napis1, "CHIRP        ", f1);
          	sprintf(napis2, "%5d Hz, %5d Hz >   ",f1, f2);
            oled_display_message(napis1, napis2);
			 puts("\n Krok 5 --> Mnożenie przez sinus - 200 Hz");
			 
           	LastStep = Step;
			     
          }	
             	 
	    	fn = 200;
	        F1 = ((long)fn * 22370 ) >> 14; 
	     	faza1 += F1;
	    	sine(&faza1, &sinus1, 1);
	     	
	     	DSB = ((long)left_input * sinus1) >> 15;      
	          
	        left_output = DSB;      // Straight trough. No processing.
		    right_output = DSB;
	    } 
	    if ( Step == 6)
        {
          if ( Step != LastStep )
          {
          	sprintf(napis1, "CHIRP        ", f1);
          	sprintf(napis2, "%5d Hz, %5d Hz >   ",f1, f2);
            oled_display_message(napis1, napis2);
			 puts("\n Krok 6 --> Mnożenie przez sinus - 500 Hz");
			 
           	LastStep = Step;
			     
          }	
             	 
    	fn = 500;
        F1 = ((long)fn * 22370 ) >> 14; 
     	faza1 += F1;
    	sine(&faza1, &sinus1, 1);
     	
     	DSB = ((long)left_input * sinus1) >> 15;      
          
        left_output = DSB;      // Straight trough. No processing.
	    right_output = DSB;
	    }  
	    
	    if ( Step == 7)
        {
          if ( Step != LastStep )
          {
          	sprintf(napis1, "CHIRP        ", f1);
          	sprintf(napis2, "%5d Hz, %5d Hz >   ",f1, f2);
            oled_display_message(napis1, napis2);
			 puts("\n Krok 7 --> Mnożenie przez sinus - 1000 Hz");
			 
           	LastStep = Step;
			     
          }	
             	 
    	fn = 1000;
        F1 = ((long)fn * 22370 ) >> 14; 
     	faza1 += F1;
    	sine(&faza1, &sinus1, 1);
     	
     	DSB = ((long)left_input * sinus1) >> 15;      
          
        left_output = DSB;      // Straight trough. No processing.
	    right_output = DSB;
	    }    
	    	
	    if ( Step == 8)
        {
          if ( Step != LastStep )
          {
          	sprintf(napis1, "CHIRP        ", f1);
          	sprintf(napis2, "%5d Hz, %5d Hz >   ",f1, f2);
            oled_display_message(napis1, napis2);
			 puts("\n Krok 8 --> Mnożenie przez sinus - 2000 Hz");
			 
           	LastStep = Step;
			     
          }	
             	 
    	fn = 2000;
        F1 = ((long)fn * 22370 ) >> 14; 
     	faza1 += F1;
    	sine(&faza1, &sinus1, 1);
     	
     	DSB = ((long)left_input * sinus1) >> 15;      
          
        left_output = DSB;      // Straight trough. No processing.
	    right_output = DSB;
	    }   	
	    	  	    
     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                                                           *
 *                                                                          *
 * ------------------------------------------------------------------------ */
