⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 queue.c

📁 C语言源代码及相关资料
💻 C
📖 第 1 页 / 共 2 页
字号:
        }        
#if defined(QUEUE_SEMCONTROL)
        OSSemPost(Queue->pQueueSem);
#else
        //OS_EXIT_CRITICAL();
#endif // end #if defined(QUEUE_SEMCONTROL)
        
    }
    return err;
}
#endif                                                                  /* EN_QUEUE_WRITE               */

/*********************************************************************************************************
** Function name:           QueueWriteFront
** Descriptions:            LIFO方式发送数据
** Input parameters:        Buf :指向队列的指针
**                          Data:发送的数据
** Output parameters:       NONE
** Returned value:          NOT_OK    : 参数错误
**                          QUEUE_FULL: 队列满
**                          QUEUE_OK  : 发送成功
*********************************************************************************************************/
#ifndef EN_QUEUE_WRITE_FRONT
#define EN_QUEUE_WRITE_FRONT    0
#endif

#if EN_QUEUE_WRITE_FRONT > 0

uint8 QueueWriteFront (void *Buf, QUEUE_DATA_TYPE Data)
{
    uint8 err;
    DataQueue *Queue;
#if defined(QUEUE_SEMCONTROL)
    INT8U QueueErr;
#endif // end #if defined(QUEUE_SEMCONTROL)

    err = NOT_OK;
    if (Buf != NULL) {                                                  /* 队列是否有效                 */
        Queue = (DataQueue *)Buf;
        
#if defined(QUEUE_SEMCONTROL)
        OSSemPend(Queue->pQueueSem, 0, &QueueErr);
#else
        //OS_ENTER_CRITICAL();
#endif // end #if defined(QUEUE_SEMCONTROL)
#if defined(QUEUE_NEW_OPRT)
#if 0
				uint16 shNumData = 0;
				if ( Queue->In >= Queue->Out ) {
					shNumData = ( Queue->In - Queue->Out );
				} else {
					shNumData = Queue->MaxData - ( Queue->Out - Queue->In );
				}
				if ( shNumData < Queue->MaxData )
#else
				if ( Queue->In >= Queue->Out ) {
					if ( (Queue->In - Queue->Out) < Queue->MaxData ) {
						// Case 1: Stil have free space, in Index >= out Index
						err = QUEUE_OK;
					}
				} else {
					// Queue->In < Queue->Out
					// it means that ((Queue->Out - Queue->In) > 0), has free space
					// Case 2: Stil have free space, in Index < out Index
					err = QUEUE_OK;
				}
				if ( err == QUEUE_OK )
				{
					Queue->Out--; 
					if (Queue->Out < Queue->Buf) {
						Queue->Out = Queue->End - 1;
					}
					Queue->Out[0] = Data;
					err = QUEUE_OK;
#endif
#else
        if (Queue->NData < Queue->MaxData) {                            /* 队列是否满                   */
            Queue->Out--; 
                                                          
            if (Queue->Out < Queue->Buf) {                              /* 调整出队指针                 */
            
                Queue->Out = Queue->End - 1;
            }
            
            Queue->Out[0] = Data;                                       /* 数据入队                     */
            Queue->NData++;                                             /* 数据数目增加                 */
            err = QUEUE_OK;
#endif // end of #if !defined(QUEUE_NEW_OPRT)            
        } else {                                                        /* 满                           */
            err = QUEUE_FULL;
            if (Queue->WriteFull != NULL) {                             /* 调用用户处理函数             */
                err = Queue->WriteFull(Queue, Data, Q_WRITE_FRONT_MODE);
            }
        }
#if defined(QUEUE_SEMCONTROL)
        OSSemPost(Queue->pQueueSem);
#else
        //OS_EXIT_CRITICAL();
#endif // end #if defined(QUEUE_SEMCONTROL)
    }
    return err;
}

#endif                                                                  /* EN_QUEUE_WRITE_FRONT         */

/*********************************************************************************************************
** Function name:           QueueNData
** Descriptions:            取得队列中数据数
** Input parameters:        Buf :指向队列的指针
** Output parameters:       NONE
** Returned value:          队列包含数据数
*********************************************************************************************************/
#ifndef EN_QUEUE_NDATA
#define EN_QUEUE_NDATA    0
#endif

#if EN_QUEUE_NDATA > 0

uint16 QueueNData (void *Buf)
{
    uint16 temp;
#if defined(QUEUE_SEMCONTROL)
    INT8U QueueErr;
#endif // end #if defined(QUEUE_SEMCONTROL)
#if defined(QUEUE_NEW_OPRT)
		QUEUE_DATA_TYPE *pIn = 0;
		QUEUE_DATA_TYPE *pOut = 0;
		uint16 shMaxData = 0;
#endif // end of #if defined(QUEUE_NEW_OPRT)

    temp = 0;                                                           /* 队列无效返回0                */
    
    if (Buf != NULL) {
#if defined(QUEUE_SEMCONTROL)
        OSSemPend(Queue->pQueueSem, 0, &QueueErr);
#else
        //OS_ENTER_CRITICAL(); 
#endif // end #if defined(QUEUE_SEMCONTROL)
#if defined(QUEUE_NEW_OPRT)
				pIn = ((DataQueue *)Buf)->In ; 
				pOut = ((DataQueue *)Buf)->Out;
				shMaxData = ((DataQueue *)Buf)->MaxData;
				if ( pIn >= pOut ) {
					temp = ( pIn - pOut );
				} else {
					temp = shMaxData - ( pOut - pIn );
				}
#else
        temp = ((DataQueue *)Buf)->NData;
#endif // end of #if defined(QUEUE_NEW_OPRT)
#if defined(QUEUE_SEMCONTROL)
        OSSemPost(Queue->pQueueSem);
#else
        //OS_EXIT_CRITICAL();
#endif // end #if defined(QUEUE_SEMCONTROL)
    }
    return temp;
}

#endif                                                                  /* EN_QUEUE_NDATA               */

/*********************************************************************************************************
** Function name:           QueueSize
** Descriptions:            取得队列总容量
** Input parameters:        Buf :指向队列的指针
** Output parameters:       NONE
** Returned value:          队列总容量
*********************************************************************************************************/
#ifndef EN_QUEUE_SIZE
#define EN_QUEUE_SIZE    0
#endif

#if EN_QUEUE_SIZE > 0

uint16 QueueSize (void *Buf)
{
    uint16 temp;
#if defined(QUEUE_SEMCONTROL)
    INT8U QueueErr;
#endif // end #if defined(QUEUE_SEMCONTROL)
    
    temp = 0;                                                           /* 队列无效返回0                */
    
    if (Buf != NULL) {
#if defined(QUEUE_SEMCONTROL)
        OSSemPend(Queue->pQueueSem, 0, &QueueErr);
#else
        //OS_ENTER_CRITICAL();
#endif // end #if defined(QUEUE_SEMCONTROL)
        temp = ((DataQueue *)Buf)->MaxData;
#if defined(QUEUE_SEMCONTROL)
        OSSemPost(Queue->pQueueSem);
#else
        //OS_EXIT_CRITICAL();
#endif // end #if defined(QUEUE_SEMCONTROL)
    }
    return temp;
}

#endif                                                                  /* EN_QUEUE_SIZE                */

/*********************************************************************************************************
** Function name:           OSQFlush
** Descriptions:            清空队列
** Input parameters:        Buf :指向队列的指针
** Output parameters:       NONE
** Returned value:          NONE
*********************************************************************************************************/
#ifndef EN_QUEUE_FLUSH
#define EN_QUEUE_FLUSH    1
#endif

#if EN_QUEUE_FLUSH > 0

void QueueFlush (void *Buf)
{
    DataQueue *Queue;
#if defined(QUEUE_SEMCONTROL)
    INT8U QueueErr;
#endif // end #if defined(QUEUE_SEMCONTROL)
    
    if (Buf != NULL) {                                                  /* 队列是否有效                 */
        Queue = (DataQueue *)Buf;
#if defined(QUEUE_SEMCONTROL)
        OSSemPend(Queue->pQueueSem, 0, &QueueErr);
#else
        //OS_ENTER_CRITICAL();
#endif // end #if defined(QUEUE_SEMCONTROL)
        Queue->Out = Queue->Buf;
        Queue->In = Queue->Buf;
#if !defined(QUEUE_NEW_OPRT)
        Queue->NData = 0;                                               /* 数据数目为0                  */
#endif // end of #if !defined(QUEUE_NEW_OPRT)

#if defined(QUEUE_SEMCONTROL)
        OSSemPost(Queue->pQueueSem);
#else
        //OS_EXIT_CRITICAL();
#endif // end #if defined(QUEUE_SEMCONTROL)
    }
}

#endif                                                                  /* EN_QUEUE_FLUSH               */

/*********************************************************************************************************
**                            End Of File
********************************************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -