两种缓存数据的写法 fifo

第一种:

/* **************************************************************************************
 * Function    : FIFO_Init
 * Description : FIFO structure initialisation, called once
 * Input data  : T_Fifo * t_Fifo = which FIFO structure to work on
 *               uint8_t *databuff = first address of array
 *               uint8_t length = data length
 *               uint8_t number = number of data
 * Ouptut data : none ; global variable (* t_Fifo) is updated
 * ************************************************************************************** */
void FIFO_Init(T_FIFO *t_Fifo,uint8_t *databuff,uint8_t length,uint8_t number)
{
 t_Fifo->WriteIndex = 0;
 t_Fifo->ReadIndex = 0;
 t_Fifo->DataBuff = databuff;
 t_Fifo->DataLength = length;
 t_Fifo->DataNumber = number;
}

 

/* **************************************************************************************
 * Function    : Write a array data into FIFO
 * Input data  :
 *              
 * Ouptut data :
 * ************************************************************************************** */
void FIFO_Put(T_FIFO *t_Fifo,uint8_t *databuff)
{
 uint8_t WriteIndexNext = t_Fifo->WriteIndex + t_Fifo->DataLength;
 
 if(WriteIndexNext >= ((t_Fifo->DataNumber-1)*t_Fifo->DataLength))
 {
  WriteIndexNext = 0;
 }
 if(WriteIndexNext!=t_Fifo->ReadIndex)
 {
  for(uint8_t i = 0;i<t_Fifo->DataLength;i++)
  (t_Fifo->DataBuff)[WriteIndexNext+i] = databuff[i];
  
   t_Fifo->WriteIndex = WriteIndexNext;
 }
}

 

/* **************************************************************************************
 * Function    : Get and remove a array data from FIFO
 * Input data  :
 *              
 * Ouptut data :
 * ************************************************************************************** */
uint8_t * FIFO_Get(T_FIFO *t_Fifo)
{
 uint8_t ReadIndexNext;
 uint8_t *databuff;
 if(t_Fifo->ReadIndex != t_Fifo->WriteIndex)
 {
  ReadIndexNext = t_Fifo->ReadIndex + t_Fifo->DataLength;
  if(ReadIndexNext>=((t_Fifo->DataNumber-1)*t_Fifo->DataLength))
  {
   ReadIndexNext = 0;
  }
  for(uint8_t i = 0;i<t_Fifo->DataLength;i++)
  databuff[i] = (t_Fifo->DataBuff)[ReadIndexNext+i];
  
  t_Fifo->ReadIndex = ReadIndexNext;
 }
 return databuff;
}

 

 

第二种写法:

uint8_t CommnadTohanlde[20][5]={0};
uint8_t iput = 0;
uint8_t iget = 0;
uint8_t nbrofCmd = 0;

uint8_t addring (uint8_t i)
{
 return (i+1) == NMAX ? 0 : i+1;
}
int32_t getCmd()
{
 uint8_t Pos;
   
 if(nbrofCmd > 0)
 {
     Pos = iget;
           iget = addring(iget);
           nbrofCmd--;
            return Pos;
 }
 else
  return -1;
}
uint8_t putCmd(uint8_t* cmd)
{
 if (nbrofCmd < NMAX)
 {
  memcpy(&CommnadTohanlde[iput],cmd,5);
  iput = addring(iput);
  nbrofCmd++;
 }
 return 0;
}

 

个人认为第一种方法要比第二种方法简单,然而第二种方法很好理解。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值