Cstyle的札记,Freertos内核详解,第3篇

<span style="white-space:pre">	</span>RTOS里面最常见的以及最核心的数据结构,队列的实现。可在VS2008 下编译测试。
</pre><pre name="code" class="cpp">/** @file
   
	Copyright (c) 2008 - 2014, MX.Studio 
	
	All rights reserved. 

	Created by Cstyle          
**/
#ifndef _QUEUE_H_
#define _QUEUE_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "Syslib.h"

#define Cfg_QUE_LEN 20

typedef struct
{
    UINT8 Data[Cfg_QUE_LEN];
    UINT8 pHead;
    UINT8 pTail;
    UINT16 size;
}Queue_t;

Queue_t * Queue_Creat();
UINT8 Queue_IsEmpty(Queue_t * const q);
UINT8 Queue_IsFull(Queue_t * const q);
UINT8 Queue_Push(Queue_t * const q,UINT8 const * const Dat);
UINT8 Queue_Pop(Queue_t * const q,UINT8 * const Dat);
UINT16 Queue_GetSize(Queue_t *const q);
void Queue_Test();

#ifdef __cplusplus
}
#endif
#endif
</pre><pre name="code" class="cpp">
</pre><pre name="code" class="cpp">
<pre name="code" class="cpp">/** @file
   
	Copyright (c) 2008 - 2014, MX.Studio 
	
	All rights reserved. 

	Created by Cstyle          
**/
#include "Queue.h"

Queue_t * Queue_Creat()
{
    Queue_t *p;
    p=malloc(sizeof(Queue_t));
    if(0!=p)
    {
        p->pHead=0;
        p->pTail=0;
        p->size=0;
        
        return p;
    }
    else return 0;
}


UINT8 Queue_IsEmpty(Queue_t * const q)
{
    if(!q->size) return 1;
    else return 0;
}

UINT8 Queue_IsFull(Queue_t * const q)
{
    if((q->size!=0)&&(q->pHead ==q->pTail)) return 1;//full
    else return 0;
}


UINT8 Queue_Push(Queue_t * const q,UINT8 const * const Dat)
{
        Assert(!Queue_IsFull(q));
        q->Data[q->pHead] = *Dat;
        q->pHead++;
        q->pHead = q->pHead % Cfg_QUE_LEN;
        q->size++;
        return 0;
}

UINT8 Queue_Pop(Queue_t * const q,UINT8 * const Dat)
{
    Assert(!Queue_IsEmpty(q));
    *Dat = q->Data[q->pTail];
    q->pTail++;
    q->pTail = q->pTail % Cfg_QUE_LEN;
    q->size--;
    return 0;
}

UINT16 Queue_GetSize(Queue_t *const q)
{
	return q->size;
}

void Queue_Test()
{
	
	
	 Queue_t *test;
	 UINT8 a=1,b,i;

	 printf("-----------------------------------------------
");
	 printf("------------Start Queue Test!------------------
");
	 printf("-----------------------------------------------
");

	 //creat queue
	 printf("creat queue test:

");
     test=Queue_Creat();
     printf("p=%x
p->pHead =%x
p->pTail =%x
p->size =%x
",test,test->pHead,test->pTail,test->size);

	 //push queue
	 printf("push queue test1:

");
     Queue_Push(test,&a);
     printf("p=%x
p->pHead =%x
p->pTail =%x
p->size =%x
",test,test->pHead,test->pTail,test->size);
     for(i=0;i<Queue_GetSize(test);i++)
     printf("Data[%d]=%x",i,test->Data[i]);
     printf("
 
");
	
	 //push queue
	 printf("push queue test2:

");
	 a++;
     Queue_Push(test,&a);
     printf("p=%x
p->pHead =%x
p->pTail =%x
p->size =%x
",test,test->pHead,test->pTail,test->size);
     for(i=0;i<Queue_GetSize(test);i++)
     printf("Data=%x",test->Data[i]);
     printf("
 
");

	 //pop queue
	 printf("push queue test:

");
     Queue_Pop(test,&b);
     printf("b=%x 
",b);

     printf("p=%x
p->pHead =%x
p->pTail =%x
p->size =%x
",test,test->pHead,test->pTail,test->size);
     for(i=0;i<Queue_GetSize(test);i++)
     printf("Data=%x",test->Data[i]);
	 printf("

");
	 
	
}
转载请注明出处 转载请注明出处
Cstyle.z.zhou@gmail.com  //  http://blog..net/CStyle_0x007 Cstyle.z.zhou@gmail.com // http://blog..net/CStyle_0x007
经验分享 程序员 微信小程序 职场和发展