/*******************************************************************************
* File Name: main.c
*
* Version: 2.20
*
* Description:
*   This is a source code for example project of ADC single ended mode.
*
*   Variable resistor(pot) is connected to +ve input of ADC using the I/O pin.
*   P0.0. When voltage to positive terminal of ADC is 0, the output displayed
*   on the LCD pannel is 0x0000. As voltage on positive terminal goes on
*   increasing, the  converted value goes on increasing from 0x0000 and reaches
*   0xFFFF when voltage becomes 1.024V. Futher increase in voltage value,
*   doesn't cause any changes to values displayed in the LCD.
*
* Hardware Connections:
*  Connect analog input from Variable resistor to port P0[0] of DVK1 board.
*
********************************************************************************
* Copyright 2012-2015, Cypress Semiconductor Corporation. All rights reserved.
* This software is owned by Cypress Semiconductor Corporation and is protected
* by and subject to worldwide patent and copyright laws and treaties.
* Therefore, you may use this software only as provided in the license agreement
* accompanying the software package from which you obtained this software.
* CYPRESS AND ITS SUPPLIERS MAKE NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* WITH REGARD TO THIS SOFTWARE, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT,
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*******************************************************************************/

#include <device.h>

/* Function name: downsample
*/

#define DOWNRATE 128
#define DOWNSCALE 2
int downcount = 0;
uint16 downvalue = 0, downtemp = 0;

int downsample(uint16 sample) {
    downtemp += (uint16)(sample >> DOWNSCALE);
    downcount++;
    if (downcount >= DOWNRATE) { downvalue = downtemp; downcount = 0; downtemp = 0; return 1; }
    else { return 0; }
}

/* Function name: pulse_detect
*/
#define NPULSE 4
uint16 pulsevalue[NPULSE] = {0, 0, 0, 0};
#define PULSETHRESHOLD (0x3fff)

uint16 down_detect() {
    return ((pulsevalue[0] < PULSETHRESHOLD) && (pulsevalue[1] > PULSETHRESHOLD));
}

uint16 up_detect() {
    return ((pulsevalue[0] > PULSETHRESHOLD) && (pulsevalue[1] < PULSETHRESHOLD));
}

#define PULSE_WIDTH 10
uint16 rising = 0, delay = 0;

uint16 pulse_detect() {
    uint16 falling, pulse;
    falling = down_detect();
    pulse = (rising && falling);
    if (delay >= PULSE_WIDTH) { delay = 0; rising = 0; }
    rising |= up_detect();
    return pulse;
}

uint16 pulse_sample() {
    int i;
    for (i=1; i<NPULSE; i++)
        pulsevalue[i] = pulsevalue[i-1];
    pulsevalue[0] = downvalue;
    return ((pulsevalue[0] < PULSETHRESHOLD) && (pulsevalue[1] > PULSETHRESHOLD));
}

/*******************************************************************************
* Function Name: main
********************************************************************************
*
* Summary:
*  main() performs following functions:
*  1: Initializes the LCD
*  2: Starts ADC
*  3: Starts ADC converstion.
*  4: Gets the converted result and displays it in LCD.
*
* Parameters:
*  None.
*
* Return:
*  None.
*
*******************************************************************************/
int main()
{
    int16 output, npulses = 0;

    /* Start the components */

    LCD_Start();
    ADC_DelSig_1_Start();

    /* Start the ADC conversion */
    ADC_DelSig_1_StartConvert();

    /* Display the value of ADC output on LCD */
    LCD_Position(0u, 0u);
    LCD_PrintString("number of pulses");

    for(;;)
    {
        if(ADC_DelSig_1_IsEndConversion(ADC_DelSig_1_RETURN_STATUS))
        {
            output = ADC_DelSig_1_GetResult16();
            
            /* Saturate ADC result to positive numbers. */
            if(output < 0)
            {
                output = 0;
            }
            if (downsample(output)) {
                pulse_sample();
                npulses += pulse_detect();
            }
            
            LCD_Position(1u, 0u);
            LCD_PrintInt16(pulsevalue[0]); LCD_PrintString(" ");
            LCD_PrintInt16(pulsevalue[1]); LCD_PrintString(" ");
            LCD_PrintInt16(npulses); ;
            }
    }
}


/* [] END OF FILE */
