/* ========================================
 *
 * Copyright YOUR COMPANY, THE YEAR
 * All Rights Reserved
 * UNPUBLISHED, LICENSED SOFTWARE.
 *
 * CONFIDENTIAL AND PROPRIETARY INFORMATION
 * WHICH IS THE PROPERTY OF your company.
 *
 * ========================================
*/

#include <device.h>
#include <math.h>

void main()
{
    uint8 i = 0;
    uint16 amplSin = 0x14;
    uint16 amplCos = 0x14;
    uint8 buff[15];
    float x = 0;
    float PI = 3.1415926535897932f;
    float sinValue = 0x00;
    float cosValue = 0x00;
    uint16 Meandr = 0;
    uint16 MeandrPointsCount = 50;
    uint8* pSinVal = (uint8*)&sinValue;
    uint8* pCosVal = (uint8*)&cosValue;
    
    CyGlobalIntEnable; //Enable global interrupts.
    UART_Start();    //Activate UART component
    
    for(;;)
    {
        x += PI / 36; //Calculate argument for sin/cos functions        
        sinValue = amplSin*sin(x); //Calculate sin
        cosValue = amplCos*cos(x); //Calculate cos
        i++;
        
        //Calculate meandr
        if(i <= MeandrPointsCount)
        {
          Meandr = 260;
        }
        else
        if(i<=2*MeandrPointsCount)
        {
          Meandr = 290;
        }
        else
        {
            i = 0;
        } 
        
        //Fill in output buffer by the sinValue
        buff[0]  = 0x0D;
        buff[1]  = 0x0A;
        buff[2]  = pSinVal[0];
        buff[3]  = pSinVal[1];
        buff[4]  = pSinVal[2];
        buff[5]  = pSinVal[3];
        buff[6]  = pCosVal[0];
        buff[7]  = pCosVal[1];
        buff[8]  = pCosVal[2];
        buff[9]  = pCosVal[3];        
        buff[10] = Meandr >>8;
        buff[11] = Meandr;
        buff[12] = 0x00;
        buff[13] = 0xFF;
        buff[14] = 0xFF;
        //Send data to host
        UART_PutArray(buff, 15);
    }
}
