数据结构-环形队列(FIFO)多字节读写

#ifndef SCIQUEUE_CPP

#define SCIQUEUE_CPP

#include "SciQueue.h"

PcCommProtocol ObjSciQueue;

void PcCommProtocol::Init(TSciQueue *pQueue)
{
pQueue->front= 0;
pQueue->rear = 0;
pQueue->maxSize =195;
pQueue->pDataBuff = (uint16 *)(&rxbuffer)
}


void PcCommProtocol::WriteSciQueue(TSciQueue *pQueue,uint16 *pDataBuff,uint16 count)
{
uint16 realCount;
int16 tempRear, tempFront;
tempFront = pQueue->front;
if(pQueue->rear > tempFront)
{
pQueue->inCount = pQueue->rear - tempFront;
}
else
{
pQueue->inCount = (pQueue->rear+pQueue->maxSize-tempFront)% pQueue->maxSize;
}
if(pQueue->inCount==(pQueue->maxSize-1))
{ pQueue->status=3; }
else
{
tempRear = pQueue->rear;
if ((pQueue->inCount + count) > (pQueue->maxSize-1))
{
pQueue->status=3;
realCount=pQueue->maxSize-pQueue->inCount-1;
}
else
{
realCount=count;
}
if ((tempRear+realCount) > pQueue->maxSize)
{
DataCopy(pQueue->pDataBuff+tempRear,pDataBuff,pQueue->maxSize-tempRear);
DataCopy(pQueue->pDataBuff,pDataBuff+(pQueue->maxSize-tempRear),realCount+tempRear-pQueue->maxSize);
}
else
{
DataCopy(pQueue->pDataBuff+tempRear,pDataBuff,realCount);
}
pQueue->rear = (tempRear + realCount) % pQueue->maxSize;
}



uint16 PcCommProtocol::ReadSciQueue(TSciQueue *pQueue,uint16 *pDataBuff,uint16 count)
{
int16 tempRear;
tempRear = pQueue->rear;

if(tempRear > pQueue->front)
{
pQueue->outCount = tempRear - pQueue->front;
}
else 
{
pQueue->outCount = (tempRear+pQueue->maxSize-pQueue->front)% pQueue->maxSize;
}

if(pQueue->outCount==0)
{
pQueue->status=0;
return 0;
}
else
{
if(pQueue->outCount < count)
{
pQueue->status=1;
return 0;
}
if ((pQueue->front+count) > pQueue->maxSize)
{
DataCopy(pDataBuff,pQueue->pDataBuff+pQueue->front,pQueue->maxSize-pQueue->front);
DataCopy(pDataBuff+(pQueue->maxSize-pQueue->front),pQueue->pDataBuff,count+pQueue->front-pQueue->maxSize);
}
else
{
DataCopy(pDataBuff,pQueue->pDataBuff+pQueue->front,count);
}
pQueue->front = (pQueue->front + count) % pQueue->maxSize;
pQueue->status=2;
return 1;
}



void PcCommProtocol::DataCopy(uint16 *pDestAddr, uint16 *pSrcAddr, int16 iLength)
{
if(iLength==0) 
return;
if(pDestAddr!=pSrcAddr)
{
for (int16 i = 0; i < iLength; i++)
{
*pDestAddr++ = *pSrcAddr++;
}
}

#endif



#ifndef SCIQUEUE_H

#define SCIQUEUE_H

typedef struct T_SCIQUEUE
{
int16 front;
int16 rear;
int16 inCount;
int16 outCount;
uint16 maxSize;
uint16 *pDataBuff;
uint16 status;
}TSciQueue;


typedef struct T_RX_BUFFER
{
uint16 uiDataSize;
uint16 uiBuff[16];
}TRxBuffer;


typedef enum 
{
Queue_EMPTY,

Queue_NoReady,

Queue_OK,

Queue_Full

}SciQueueSts;


class PcCommProtocol
{
uint16 rxbuffer[195];

TSciQueue WriteQueueBuffer;

TRxBuffer SciRxBuffer;
TRxBuffer SciTxBuffer;


public:

void Init(TSciQueue * pQueue);
void WriteSciQueue(TSciQueue * pQueue,uint16 * pDataBuff,uint16 count);
uint16 ReadSciQueue(TSciQueue * pQueue,uint16 * pDataBuff,uint16 count);


private:

void DataCopy(uint16 * pDestAddr,uint16 * pSrcAddr,int16 iLength);

};


extern PcCommProtocol ObjSciQueue;

#endif

你可能感兴趣的:(数据结构-环形队列(FIFO)多字节读写)