/*
 * Copyright (c) 2014, 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: 4306 $
 * $LastChangedDate:: 2026-07-02 15:48:24 +0200#$
 */
/* USERCODE-BEGIN:Description                                                                                         */
/* Description: Generation of time delayed (enable) sequence.                 */
/* USERCODE-END:Description                                                                                           */
#include <stddef.h>
#include "Sequencer_Float32.h"

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

/* USERCODE-BEGIN:PreProcessor                                                                                        */
/* Inputs */
#define START       (*block->Start)

/* Outputs */
#define OUT1        (block->Out1)
#define OUT2        (block->Out2)
#define OUT3        (block->Out3)
#define OUT4        (block->Out4)

/* Parameters */
#define DELAY1      (block->delay1)
#define DELAY2      (block->delay2)
#define DELAY3      (block->delay3)
#define DELAY4      (block->delay4)

/* Variables */
#define CNT         (block->cnt)
#define START_OLD   (block->start_old)

/* USERCODE-END:PreProcessor                                                                                          */

/**********************************************************************************************************************/
/** Declare private functions with the C inline keyword to ensure function inlining for better runtime performance.  **/
/** Since all tested compilers DO NOT perform inlining at the LOWEST optimization level, compiler optimizations may  **/
/** need to be adjusted accordingly.                                                                                 **/
/**********************************************************************************************************************/
/* USERCODE-BEGIN:PrivateFncPrototypeOrDefinition                                                                     */
/* USERCODE-END:PrivateFncPrototypeOrDefinition                                                                       */

/**********************************************************************************************************************/
/** Update                                                                                                           **/
/**********************************************************************************************************************/
void Sequencer_Float32_Update(SEQUENCER_FLOAT32 *block)
{
/* USERCODE-BEGIN:UpdateFnc                                                                                           */
    float32 out1 = 0.0f;
    float32 out2 = 0.0f;
    float32 out3 = 0.0f;
    float32 out4 = 0.0f;

    if (START != 0.0f) /* start signal */
    {
        /* check for rising flank */
        if (START_OLD == 0.0f) /* rising edge of start signal occurred */
        {
            /* reset timer value */
            CNT  = 0;
        }

        /* compare delay values with timer value */
        if (CNT >= DELAY1)
        {
            out1 = 1.0f;
        }
        if (CNT >= DELAY2)
        {
            out2 = 1.0f;
        }
        if (CNT >= DELAY3)
        {
            out3 = 1.0f;
        }
        if (CNT >= DELAY4)
        {
            out4 = 1.0f;
        }

        /* check for timer overflow */
        if (CNT < (uint16)UINT16_MAX)
        {
            CNT++;
        }
        else
        {
            CNT = (uint16)UINT16_MAX;
        }
    }

    /* assign outputs */
    OUT1 = out1;
    OUT2 = out2;
    OUT3 = out3;
    OUT4 = out4;

    /* save start signal for next cycle */
    START_OLD = START;

/* USERCODE-END:UpdateFnc                                                                                             */
}

/**********************************************************************************************************************/
/** Initialization                                                                                                   **/
/**********************************************************************************************************************/
void Sequencer_Float32_Init(SEQUENCER_FLOAT32 *block)
{
    block->identifier = SEQUENCER_FLOAT32_ID;
    block->Out1 = 0.0f;
    block->Out2 = 0.0f;
    block->Out3 = 0.0f;
    block->Out4 = 0.0f;
/* USERCODE-BEGIN:InitFnc                                                                                             */
     CNT = 0;
     START_OLD = 0.0f;

/* USERCODE-END:InitFnc                                                                                               */
}

/**********************************************************************************************************************/
/** Load block data                                                                                                  **/
/**********************************************************************************************************************/
uint8 Sequencer_Float32_Load(const SEQUENCER_FLOAT32 *block, uint8 data[], uint16 *dataLength, uint16 maxSize)
{
    uint8 error = 0u;
    if (maxSize < 8u)
    {
        error = 1u;
    }
    else
    {
        data[0] = (uint8)(block->delay1 & 0xFFu);
        data[1] = (uint8)((block->delay1 >> 8) & 0xFFu);
        data[2] = (uint8)(block->delay2 & 0xFFu);
        data[3] = (uint8)((block->delay2 >> 8) & 0xFFu);
        data[4] = (uint8)(block->delay3 & 0xFFu);
        data[5] = (uint8)((block->delay3 >> 8) & 0xFFu);
        data[6] = (uint8)(block->delay4 & 0xFFu);
        data[7] = (uint8)((block->delay4 >> 8) & 0xFFu);
        *dataLength = 8u;
/* USERCODE-BEGIN:LoadFnc                                                                                             */
/* USERCODE-END:LoadFnc                                                                                               */
    }
    return (error);
}

/**********************************************************************************************************************/
/** Save block data                                                                                                  **/
/**********************************************************************************************************************/
uint8 Sequencer_Float32_Save(SEQUENCER_FLOAT32 *block, const uint8 data[], uint16 dataLength)
{
    uint8 error;
    if (dataLength != 8u)
    {
        error = 1u;
    }
    else
    {
        block->delay1 = (uint16)((uint16)data[0] | ((uint16)data[1] << 8));
        block->delay2 = (uint16)((uint16)data[2] | ((uint16)data[3] << 8));
        block->delay3 = (uint16)((uint16)data[4] | ((uint16)data[5] << 8));
        block->delay4 = (uint16)((uint16)data[6] | ((uint16)data[7] << 8));
        error = 0u;
/* USERCODE-BEGIN:SaveFnc                                                                                             */
/* USERCODE-END:SaveFnc                                                                                               */
    }
    return (error);
}

/**********************************************************************************************************************/
/** Get block element address                                                                                        **/
/**********************************************************************************************************************/
#if !defined(SEQUENCER_FLOAT32_ISLINKED)
void* Sequencer_Float32_GetAddress(SEQUENCER_FLOAT32* block, uint16 elementId)
{
    void* addr;
    switch (elementId)
    {
        case 1:
            addr = block->Start;
            break;
        case 2:
            addr = &block->Out1;
            break;
        case 3:
            addr = &block->Out2;
            break;
        case 4:
            addr = &block->Out3;
            break;
        case 5:
            addr = &block->Out4;
            break;
        default:
            addr = NULL;
            break;
    }
    return (addr);
}
#endif

/**********************************************************************************************************************/
/** Declare private functions with the C inline keyword to ensure function inlining for better runtime performance.  **/
/** Since all tested compilers DO NOT perform inlining at the LOWEST optimization level, compiler optimizations may  **/
/** need to be adjusted accordingly.                                                                                 **/
/**********************************************************************************************************************/
/* USERCODE-BEGIN:PrivateFncDefinition                                                                                */
/* USERCODE-END:PrivateFncDefinition                                                                                  */
