/*********************************************************************
 *                    Flowcode DSP FFT Code
 *
 * File: DSP_FFT.c
 *
 * (c) 2009 Matrix Multimedia Ltd.
 * http://www.matrixmultimedia.com
 *
 * Software License Agreement
 *
 * The software supplied herewith by Matrix Multimedia Ltd (the
 * “Company”) for its Flowcode graphical programming language is
 * intended and supplied to you, the Company’s customer, for use
 * solely and exclusively on the Company's products. The software
 * is owned by the Company, and is protected under applicable
 * copyright laws. All rights are reserved. Any use in violation
 * of the foregoing restrictions may subject the user to criminal
 * sanctions under applicable laws, as well as to civil liability
 * for the breach of the terms and conditions of this licence.
 *
 * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 *
 * Changelog:
 *
 *  date  | by | description
 * -------+----+-----------------------------------------------------
 * 051012 | BR | Created
 * 301012 | ST | Fixed some minor defines issues
 * 301012 | ST | Removed Inverse_FTT and Rewrote to call global FFT function in the System.c file
 * 011112 | ST | Made #errors clearer
 *        |    |
 *        |    |
 ********************************************************************/

/*********************************************************************

 Return & parameter types:
   void
   char
   short
   char*

 Pin directions:
   0 = OUTPUT
   2 = INPUT
   3 = BIDIRECTIONAL

**********************************************************************

[Settings]
CLSID={49046B6C-5003-4FED-ABAE-671F5CD1C203}
IsAnalogue=0
MultipleAllowed=1
Description=DSP FFT Flowcode Component

**********************************************************************

[Port]
DesiredPort=0
PortMandatory=0
SamePortAsID=0

[Pins]
Count=0

[PinPort]
# PORTA = 0
# PORTB = 1
# PORTC = 2
# PORTD = 3
# PORTE = 4

[PinDesiredBit]

[PinDirection]
# DIR_INPUT = 2
# DIR_OUTPUT = 0
# DIR_BIDIRECTIONAL = 3

[PinMustUsePort]
# boolean

[SamePortAsPinX]
# -1 = any pin
# otherwise, the pin number

[PinMustUsePin]
# boolean

[PinValue]
# boolean (0 = off, 1 = on)

**********************************************************************

[MacroNames]
Count=1
1=FFT

[MacroReturns]
1=void

[MacroIsPrivate]
1=0

[MacroParameters_FFT]
Count=0

[MacroParamTypes_FFT]


 ********************************************************************/

/********************************************************************
 * ADDITIONAL CODE
 ********************************************************************/

/*DefinesCode_Start*/

/**** Macro Substitutions ****
a = Unique Reference
b = Input data buffer (time domain)
c = Output data buffer (frequency domain)
******************************/

//common defines
#define %a_INPUT_BUFFER_NAME        MXBUF_%b
#define %a_INPUT_BUFFER_LEN         MXBUFSZ_%b
#define %a_INPUT_BUFFER_SIGN        MXBUFSIGN_%b
#define %a_INPUT_BUFFER_BITS        MXBUFBITS_%b
#define %a_INPUT_BUFFER_DOM         MXBUFDOM_%b

#define %a_OUTPUT_BUFFER_REAL_NAME  MXBUF_%c
#define %a_OUTPUT_BUFFER_IMAG_NAME  MXBUF_IM_%c
#define %a_OUTPUT_BUFFER_LEN        MXBUFSZ_%c
#define %a_OUTPUT_BUFFER_SIGN       MXBUFSIGN_%c
#define %a_OUTPUT_BUFFER_BITS       MXBUFBITS_%c
#define %a_OUTPUT_BUFFER_DOM        MXBUFDOM_%c


//Sample Buffer Size Must Be A Power Of 2!!!
#if(%a_INPUT_BUFFER_LEN == 4)
    #define %a_FFT_SAMPLE_POWER     2
#endif
#if(%a_INPUT_BUFFER_LEN == 8)
    #define %a_FFT_SAMPLE_POWER     3
#endif
#if(%a_INPUT_BUFFER_LEN == 16)
    #define %a_FFT_SAMPLE_POWER     4
#endif
#if(%a_INPUT_BUFFER_LEN == 32)
    #define %a_FFT_SAMPLE_POWER     5
#endif
#if(%a_INPUT_BUFFER_LEN == 64)
    #define %a_FFT_SAMPLE_POWER     6
#endif
#if(%a_INPUT_BUFFER_LEN == 128)
    #define %a_FFT_SAMPLE_POWER     7
#endif
#if(%a_INPUT_BUFFER_LEN == 256)
    #define %a_FFT_SAMPLE_POWER     8
#endif
#if(%a_INPUT_BUFFER_LEN == 512)
    #define %a_FFT_SAMPLE_POWER     9
#endif
#if(%a_INPUT_BUFFER_LEN == 1024)
    #define %a_FFT_SAMPLE_POWER    10
#endif
#if(%a_INPUT_BUFFER_LEN == 2048)
    #define %a_FFT_SAMPLE_POWER    11
#endif
#if(%a_INPUT_BUFFER_LEN == 4096)
    #define %a_FFT_SAMPLE_POWER    12
#endif
#ifndef %a_FFT_SAMPLE_POWER
    #define %a_FFT_SAMPLE_POWER     1    //Solves other less specific compilation errs
    #error "%a: input buffer size must be a power of 2 and contain at least 4 samples."
#endif

#if (%a_INPUT_BUFFER_LEN != %a_OUTPUT_BUFFER_LEN)
    #error "%a: output buffer size must match the input buffer."
#endif
#if (%a_INPUT_BUFFER_BITS != %a_OUTPUT_BUFFER_BITS)
    #error "%a: output buffer type must match the input buffer."
#endif
#if (%a_INPUT_BUFFER_SIGN != 1)
    #error "%a: input buffer size must be signed."
#endif
#if (%a_OUTPUT_BUFFER_SIGN != 1)
    #error "%a: output buffer size must be signed."
#endif
#if (%a_INPUT_BUFFER_DOM != 0)
    #error "%a: input buffer must be time-domain."
#endif
#if (%a_OUTPUT_BUFFER_DOM != 1)
    #error "%a: output buffer must be frequency-domain."
#endif

//#warning "DSP_FFT general note - Buffers should be signed and input waveform should be scaled so it is shifted around 0."


/*DefinesCode_End*/

/*InitialisationCode_Start*/
/*InitialisationCode_End*/

/*InterruptCode_Start*/
/*InterruptCode_End*/

/********************************************************************
 * FUNCTIONS
 ********************************************************************/


void FFT()
{
/*Macro_FFT_Start*/

    #if (%a_INPUT_BUFFER_BITS == 8)
        MXDSP_FFT_8(1, %a_FFT_SAMPLE_POWER, %a_INPUT_BUFFER_NAME, %a_OUTPUT_BUFFER_REAL_NAME, %a_OUTPUT_BUFFER_IMAG_NAME);
    #endif
    #if (%a_INPUT_BUFFER_BITS == 16)
        MXDSP_FFT_16(1, %a_FFT_SAMPLE_POWER, %a_INPUT_BUFFER_NAME, %a_OUTPUT_BUFFER_REAL_NAME, %a_OUTPUT_BUFFER_IMAG_NAME);
    #endif

/*Macro_FFT_End*/
}


