/**
@file seqproc.h
@brief This file contains the declarations of seqproc interfaces and data structures. There is a demo of sequence process in demo2task.c.

## changelog ##
______
Data        | Vesion   | Change Description
------------|----------|-------------------------------------
20240426    | 1.0      | First release
*/

/********************************************************************************************
Multi-Inlcude-Prevent Section
********************************************************************************************/
#ifndef _SEQPROC_H_
#define _SEQPROC_H_

/********************************************************************************************
Debug Switch Definition Section
********************************************************************************************/

/********************************************************************************************
Include File Section
********************************************************************************************/
#include "FreeRTOS.h"
#include "queue.h"
#include "resource_create.h"

/********************************************************************************************
Macro Definition Section
********************************************************************************************/
 /** wait time to the next stage */
#define SeqProc_SetWaitTime(x)  (SeqProc_WaitTime + ((x) << 8))

 /* return value of  function xSeqProc_Call */
#define SeqProc_CallOk	0U
#define SeqProc_CallError	1U

 /** Initliztion value of SeqProc Stage*/
#define SeqProc_StageNone	0xFF

 /** Max nest times of SeqProc */
#define SeqProc_MaxNestTimes	8

#define int8_s                 signed long

enum SeqProcReturnType {
	/** execute in the next stage immediately */
	SeqProc_Imdt  		= 0x0B00,  
	/** the process has complete, no further continuations */
	SeqProc_Complete	= 0x0E00,  
	/** Error occurred */
	SeqProc_Error	  	= 0x0F00,  
	/** yield the process and wait for specified timeout */
	SeqProc_WaitTime	= 0x1000,
};

/********************************************************************************************
Structure Type Declaration Section
********************************************************************************************/
typedef uint8_t SeqProcStage;

typedef uint32_t(*SeqProcfunc)(SeqProcStage, const void *);
 
 /** type define the struct of Seqproc, User only need to define this structure and  pass to vSeqProc_InitProc*/
typedef struct 
{
	tick_t  xWaitTick;  /**< Queue recieve wait ticks */
	tick_t  xComputing;	   /**< time calibration */
	SeqProcfunc xfunc[SeqProc_MaxNestTimes]; /**< current running process */
	SeqProcStage xStage[SeqProc_MaxNestTimes]; /**< stage of execution */
	int8_s xInProgress;						/**< current nest times */
}xSeqProc_st;


/********************************************************************************************
Prototype Declaration Section                                   
********************************************************************************************/
/**
@brief Initialize xSeqProc_st type of struct ,call this function before task blocked by QueueRecieve,refer to demo2Task.c
@param pxSeqProc [in] SeqProc structure
@return void
*/
 void vSeqProc_InitProc (xSeqProc_st* pxSeqProc);

/**
@brief Stop all running sequence process.
@param pxSeqProc [in] SeqProc structure
@return void
*/
 void vSeqProc_KillProc (xSeqProc_st* pxSeqProc);

/**
@brief call SeqProc_Call to start a sequence process , refer to demo2Task.c.
@param pxSeqProc [in] SeqProc structure
@param proc [in] Seqproc function pointer
@param arg [in] user arguments(optional)
@retval
	SeqProc_CallOk    The sequence process called ok
@retval
	SeqProc_CallError    The sequence process called error, and all called sequence have been stoped
*/
base_t xSeqProc_Call(xSeqProc_st* pxSeqProc,SeqProcfunc proc,const void* arg);

/**
@brief SeqProc_CalibriTimeout can calibrit time when task recieve queue in a timeout cycle, refer to demo2Task.c.
@param pxSeqProc [in] SeqProc parameters
@return void
*/
 void vSeqProc_CalibriTimeout (xSeqProc_st* pxSeqProc);

/**
@brief call SeqProc_Exec to execute next stage when queue timeout, refer to demo2Task.c.
@param pxSeqProc [in] SeqProc parameters
@return void
*/
 void vSeqProc_Exec (xSeqProc_st* pxSeqProc);

/**
@brief Returns the number of sequence processes currently being executed.
@param pxSeqProc [in] SeqProc parameters
@return 0 - 8
*/
 int8_s cSeqProc_IsInProgress(xSeqProc_st* pxSeqProc);

#endif /* _SEQPROC_H_ */
