/*
 * Copyright (c) 2018, Linz Center of Mechatronics GmbH (LCM), web: www.lcm.at
 * All rights reserved.
 */
/*
 * This file is licensed according to the BSD 3-clause license as follows:
 * 
 * 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 the "Linz Center of Mechatronics GmbH" and "LCM" 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 "Linz Center of Mechatronics GmbH" 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.
 */
/*
 * This file is part of X2C. web: x2c.lcm.at
 * $LastChangedRevision: 3674 $
 * $LastChangedDate:: 2025-03-07 12:00:30 +0100#$
 */
/* USERCODE-BEGIN:Description                                                                                         */
/* Description: Calculation of fundamental wave.                              */
/* USERCODE-END:Description                                                                                           */
#include <stddef.h>
#include "FundFreq_Float32.h"

/* all used update functions to ram for c2000                                                                         */
#if defined(__ALL_UPDATE_FUNC_2_RAM_C2000__)
    #pragma CODE_SECTION(FundFreq_Float32_Update, "ramfuncs")
#endif

/* USERCODE-BEGIN:PreProcessor                                                                                        */
#include <math.h>
#include "Sin_Data.h"

/* Inputs */
#define IN              (*block->In)
#define PHI             (*block->phi)

/* Outputs */
#define OUT             (block->Out)

/* Variables */
#define PHI_OLD         (block->phi_old)
#define CNT             (block->cnt)
#define SUM_A           (block->sum_a)
#define SUM_B           (block->sum_b)
#define A1              (block->a)
#define B1              (block->b)

/* USERCODE-END:PreProcessor                                                                                          */

/**********************************************************************************************************************/
/** Update                                                                                                           **/
/**********************************************************************************************************************/
void FundFreq_Float32_Update(FUNDFREQ_FLOAT32 *block)
{
/* USERCODE-BEGIN:UpdateFnc                                                                                           */
    float32 temp, sine, cosine;

    sine   = sinf(PHI);
    cosine = cosf(PHI);

    /* check for start of new period (jump from -pi to +pi or vice versa) */
    if ((ABS_R32(PHI - PHI_OLD)) > PI_R32)
    {
        /* calculate coefficients */
        A1 = (SUM_A / (float32)CNT) * 2.0f;
        B1 = (SUM_B / (float32)CNT) * 2.0f;

        /* reset counter and sum */
        CNT = 0;
        SUM_A = 0.0f;
        SUM_B = 0.0f;
    }

    /* check for counter limit */
    if (CNT != UINT32_MAX)
    {
        /* counter value hasn't reached its maximum value:
         * it's save to compute sum and to increase counter
         */
        SUM_A += (IN * cosine);
        SUM_B += (IN * sine);
        CNT++;
    }
    else
    {
        /* counter value has reached its maximum value:
         * reset sum to prevent false output on next jump of phi
         */
        SUM_A = 0.0f;
        SUM_B = 0.0f;
    }

    /* calculate first harmonic */
    temp  =  A1 * cosine;
    temp += (B1 * sine);
    OUT   = temp;

    /* save angle value for next cycle */
    PHI_OLD = PHI;

/* USERCODE-END:UpdateFnc                                                                                             */

}

/**********************************************************************************************************************/
/** Initialization                                                                                                   **/
/**********************************************************************************************************************/
void FundFreq_Float32_Init(FUNDFREQ_FLOAT32 *block)
{
    block->identifier = FUNDFREQ_FLOAT32_ID;
    block->Out = 0.0f;
/* USERCODE-BEGIN:InitFnc                                                                                             */
     PHI_OLD = 0.0f;
     CNT = 0;
     SUM_A = 0.0f;
     SUM_B = 0.0f;
     A1 = 0.0f;
     B1 = 0.0f;

/* USERCODE-END:InitFnc                                                                                               */
}

/**********************************************************************************************************************/
/** No Load function                                                                                                 **/
/**********************************************************************************************************************/

/**********************************************************************************************************************/
/** No Save function                                                                                                 **/
/**********************************************************************************************************************/

/**********************************************************************************************************************/
/** Get block element address                                                                                        **/
/**********************************************************************************************************************/
#if !defined(FUNDFREQ_FLOAT32_ISLINKED)
void* FundFreq_Float32_GetAddress(FUNDFREQ_FLOAT32* block, uint16 elementId)
{
    void* addr;
    switch (elementId)
    {
        case 1:
            addr = block->In;
            break;
        case 2:
            addr = block->phi;
            break;
        case 3:
            addr = &block->Out;
            break;
        default:
            addr = NULL;
            break;
    }
    return (addr);
}
#endif
