久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3343|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

環(huán)形緩存區(qū)ring_buffer源程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:423034 發(fā)表于 2018-11-13 17:07 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
串口數(shù)據(jù)接收 及發(fā)送處理 節(jié)省空間 高效率

單片機源程序如下:
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "ring_buffer.h"

  4. /*****************************************************************************
  5. * Private types/enumerations/variables
  6. ****************************************************************************/

  7. #define RB_INDH(rb)                ((rb)->head & ((rb)->count - 1))
  8. #define RB_INDT(rb)                ((rb)->tail & ((rb)->count - 1))

  9. #if !defined(MAX)
  10. #define MAX(a, b) (((a) > (b)) ? (a) : (b))
  11. #endif
  12. #if !defined(MIN)
  13. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  14. #endif
  15. /*****************************************************************************
  16. * Public types/enumerations/variables
  17. ****************************************************************************/

  18. /*****************************************************************************
  19. * Private functions
  20. ****************************************************************************/

  21. /*****************************************************************************
  22. * Public functions
  23. ****************************************************************************/

  24. /**
  25. * @brief        Resets the ring buffer to empty
  26. * @param        RingBuff        : Pointer to ring buffer
  27. * @return        Nothing
  28. */
  29. void RingBuffer_Flush(RINGBUFF_T *RingBuff)
  30. {
  31.     RingBuff->head = RingBuff->tail = 0;
  32. }

  33. /**
  34. * @brief        Return size the ring buffer
  35. * @param        RingBuff        : Pointer to ring buffer
  36. * @return        Size of the ring buffer in bytes
  37. */
  38. int RingBuffer_GetSize(RINGBUFF_T *RingBuff)
  39. {
  40.     return RingBuff->count;
  41. }

  42. /**
  43. * @brief        Return number of items in the ring buffer
  44. * @param        RingBuff        : Pointer to ring buffer
  45. * @return        Number of items in the ring buffer
  46. */
  47. static int RingBuffer_GetCount(RINGBUFF_T *RingBuff)
  48. {
  49.     return RB_VHEAD(RingBuff) - RB_VTAIL(RingBuff);
  50. }

  51. /**
  52. * @brief        Return number of free items in the ring buffer
  53. * @param        RingBuff        : Pointer to ring buffer
  54. * @return        Number of free items in the ring buffer
  55. */
  56. static int RingBuffer_GetFree(RINGBUFF_T *RingBuff)
  57. {
  58.     return RingBuff->count - RingBuffer_GetCount(RingBuff);
  59. }

  60. /**
  61. * @brief        Return number of items in the ring buffer
  62. * @param        RingBuff        : Pointer to ring buffer
  63. * @return        1 if the ring buffer is full, otherwise 0
  64. */
  65. int RingBuffer_IsFull(RINGBUFF_T *RingBuff)
  66. {
  67.     return (RingBuffer_GetCount(RingBuff) >= RingBuff->count);
  68. }

  69. /**
  70. * @brief        Return empty status of ring buffer
  71. * @param        RingBuff        : Pointer to ring buffer
  72. * @return        1 if the ring buffer is empty, otherwise 0
  73. */
  74. int RingBuffer_IsEmpty(RINGBUFF_T *RingBuff)
  75. {
  76.     return RB_VHEAD(RingBuff) == RB_VTAIL(RingBuff);
  77. }
  78. /* Initialize ring buffer */
  79. int RingBuffer_Init(RINGBUFF_T *RingBuff, void *buffer, int itemSize, int count)
  80. {
  81.     RingBuff->data = buffer;
  82.     RingBuff->count = count;
  83.     RingBuff->itemSz = itemSize;
  84.     RingBuff->head = RingBuff->tail = 0;

  85.     return 1;
  86. }

  87. /* Insert a single item into Ring Buffer */
  88. int RingBuffer_Insert(RINGBUFF_T *RingBuff, const void *data)
  89. {
  90.     u8 *ptr = RingBuff->data;

  91.     /* We cannot insert when queue is full */
  92.     if (RingBuffer_IsFull(RingBuff))
  93.         return 0;
  94.     ptr += RB_INDH(RingBuff) * RingBuff->itemSz;
  95.     memcpy(ptr, data, RingBuff->itemSz);
  96.     RingBuff->head++;
  97.     return 1;
  98. }

  99. /* Insert multiple items into Ring Buffer */
  100. int RingBuffer_InsertMult(RINGBUFF_T *RingBuff, const void *data, int num)
  101. {
  102.     u8 *ptr = RingBuff->data;
  103.     int cnt1, cnt2;

  104.     /* We cannot insert when queue is full */
  105.     if (RingBuffer_IsFull(RingBuff))
  106.         return 0;

  107.     /* Calculate the segment lengths */
  108.     cnt1 = cnt2 = RingBuffer_GetFree(RingBuff);
  109.     if (RB_INDH(RingBuff) + cnt1 >= RingBuff->count)
  110.         cnt1 = RingBuff->count - RB_INDH(RingBuff);
  111.     cnt2 -= cnt1;

  112.     cnt1 = MIN(cnt1, num);
  113.     num -= cnt1;

  114.     cnt2 = MIN(cnt2, num);
  115.     num -= cnt2;

  116.     /* Write segment 1 */
  117.     ptr += RB_INDH(RingBuff) * RingBuff->itemSz;
  118.     memcpy(ptr, data, cnt1 * RingBuff->itemSz);
  119.     RingBuff->head += cnt1;

  120.     /* Write segment 2 */
  121.     ptr = (u8 *) RingBuff->data + RB_INDH(RingBuff) * RingBuff->itemSz;
  122.     data = (const u8 *) data + cnt1 * RingBuff->itemSz;
  123.     memcpy(ptr, data, cnt2 * RingBuff->itemSz);
  124.     RingBuff->head += cnt2;

  125.     return cnt1 + cnt2;
  126. }

  127. /* Pop single item from Ring Buffer */
  128. int RingBuffer_Pop(RINGBUFF_T *RingBuff, void *data)
  129. {
  130.     u8 *ptr = RingBuff->data;

  131.     /* We cannot pop when queue is empty */
  132.     if (RingBuffer_IsEmpty(RingBuff))
  133.         return 0;

  134.     ptr += RB_INDT(RingBuff) * RingBuff->itemSz;
  135.     memcpy(data, ptr, RingBuff->itemSz);
  136.     RingBuff->tail++;

  137.     return 1;
  138. }

  139. /* Pop multiple items from Ring buffer */
  140. int RingBuffer_PopMult(RINGBUFF_T *RingBuff, void *data, int num)
  141. {
  142.     u8 *ptr = RingBuff->data;
  143.     int cnt1, cnt2;

  144.     /* We cannot insert when queue is empty */
  145.     if (RingBuffer_IsEmpty(RingBuff))
  146.         return 0;

  147.     /* Calculate the segment lengths */
  148.     cnt1 = cnt2 = RingBuffer_GetCount(RingBuff);
  149.     if (RB_INDT(RingBuff) + cnt1 >= RingBuff->count)
  150.         cnt1 = RingBuff->count - RB_INDT(RingBuff);
  151.     cnt2 -= cnt1;

  152.     cnt1 = MIN(cnt1, num);
  153.     num -= cnt1;

  154.     cnt2 = MIN(cnt2, num);
  155.     num -= cnt2;

  156.     /* Write segment 1 */
  157.     ptr += RB_INDT(RingBuff) * RingBuff->itemSz;
  158.     memcpy(data, ptr, cnt1 * RingBuff->itemSz);
  159.     RingBuff->tail += cnt1;

  160.     /* Write segment 2 */
  161.     ptr = (u8 *) RingBuff->data + RB_INDT(RingBuff) * RingBuff->itemSz;
  162.     data = (u8 *) data + cnt1 * RingBuff->itemSz;
  163.     memcpy(data, ptr, cnt2 * RingBuff->itemSz);
  164.     RingBuff->tail += cnt2;

  165.     return cnt1 + cnt2;
  166. }
復(fù)制代碼

所有資料51hei提供下載:
ring_buffer.zip (3.77 KB, 下載次數(shù): 31)


評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:92520 發(fā)表于 2020-1-18 10:56 | 只看該作者
正是需要的,感謝分享!
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 午夜精品视频在线观看 | 在线a视频 | 日韩在线观看 | 九九热在线免费视频 | 天天操夜夜操 | 秋霞电影院午夜伦 | 久久国产婷婷国产香蕉 | 国产免费黄网 | 成人在线精品视频 | 天天草av | 国产一级特黄aaa大片评分 | 国产一区二区激情视频 | 毛片毛片毛片毛片毛片 | 日韩不卡在线观看 | 污污的网站在线观看 | 国产精品美女 | 国产在线一区二区 | 国产一级视频在线 | 免费观看www7722午夜电影 | 国产欧美性成人精品午夜 | av免费网站在线观看 | 亚洲网站在线播放 | 欧洲精品在线观看 | av网站免费在线观看 | 成人中文字幕在线 | 久久久免费电影 | 又黄又爽的网站 | 国产精品视屏 | 日韩av中文 | 中文字幕第九页 | 青青草一区 | 国产成人av免费看 | 亚洲欧美综合精品久久成人 | 中文字幕一区二区三区四区五区 | 欧美日韩久久 | 欧美一区二区三区视频在线观看 | 中文字幕在线一区二区三区 | 久久蜜桃资源一区二区老牛 | 日韩精品 电影一区 亚洲 | 国产91久久久久蜜臀青青天草二 | 日本aa毛片a级毛片免费观看 |