y_ringbuf.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /// ------------------------------------------------------------------------------------------------------------------------------------
  2. ///
  3. /// MIT License
  4. ///
  5. /// Permission is hereby granted, free of charge, to any person obtaining a copy
  6. /// of this software and associated documentation files (the "Software"), to deal
  7. /// in the Software without restriction, including without limitation the rights
  8. /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. /// copies of the Software, and to permit persons to whom the Software is
  10. /// furnished to do so, subject to the following conditions:
  11. ///
  12. /// The above copyright notice and this permission notice shall be included in all
  13. /// copies or substantial portions of the Software.
  14. ///
  15. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. /// SOFTWARE.
  22. ///
  23. /// Copyright (c) 2022 ycz. All rights reserved.
  24. ///
  25. /// Created by ycz on 2022/1/12.
  26. ///
  27. /// @brief
  28. /// y_ringbuf 是一种用于嵌入式设备的 ringbuf,可以方便用于数据缓存与传递。
  29. ///
  30. /// ------------------------------------------------------------------------------------------------------------------------------------
  31. /// ------------------------------------------------------------------------------------------------------------------------------------
  32. /// 防止当前头文件被重复引用
  33. /// ------------------------------------------------------------------------------------------------------------------------------------
  34. #ifndef _Y_RINGBUF_H__
  35. #define _Y_RINGBUF_H__
  36. /// ------------------------------------------------------------------------------------------------------------------------------------
  37. /// 头文件
  38. /// ------------------------------------------------------------------------------------------------------------------------------------
  39. #include <stdbool.h>
  40. #include <stdint.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <stdio.h>
  44. /// -----------------------------------------------------------------------------------------------------------------------------------
  45. /// 兼容 c++
  46. /// ------------------------------------------------------------------------------------------------------------------------------------
  47. /// ------------------------------------------------------------------------------------------------------------------------------------
  48. /// 宏定义
  49. /// ------------------------------------------------------------------------------------------------------------------------------------
  50. #define Y_RINGBUF_VERSION_MAJOR 0 ///< 版本信息 主版本 ( 主架构变化 )
  51. #define Y_RINGBUF_VERSION_MINOR 1 ///< 版本信息 次版本 ( 单个功能增加或修改 )
  52. #define Y_RINGBUF_VERSION_PATCH 2 ///< 版本信息 补丁版本 ( bug修复 )
  53. #define Y_RINGBUF_USE_MUTEX_LOCK 1 ///< 是否使用互斥锁 在使用单线程的情况下可以关闭以降低开销 \n 0 : 不使用 1 : 使用
  54. /// ------------------------------------------------------------------------------------------------------------------------------------
  55. /// 结构体
  56. /// ------------------------------------------------------------------------------------------------------------------------------------
  57. typedef struct ringbuf RINGBUF_st; ///< ring_buf 实例结构体 对外隐藏具体实现 当不透明指针使用
  58. /// ------------------------------------------------------------------------------------------------------------------------------------
  59. /// 函数 API
  60. /// ------------------------------------------------------------------------------------------------------------------------------------
  61. void y_ringbuf_print_version(); ///< 打印 y_ringbuf 版本号
  62. uint16_t y_ringbuf_get_used_size(RINGBUF_st *ringbuf); ///< 获取 ringbuf 已使用字节数
  63. uint16_t y_ringbuf_get_free_size(RINGBUF_st *ringbuf); ///< 获取 ringbuf 可使用字节数
  64. uint16_t y_ringbuf_read_clear(RINGBUF_st *ringbuf, uint8_t *data, uint16_t size); ///< 从 ringbuf 中读取数据并删除
  65. uint16_t y_ringbuf_read_only(RINGBUF_st *ringbuf, uint8_t *data, uint16_t size); ///< 从 ringbuf 中读取数据但不删除
  66. bool y_ringbuf_write(RINGBUF_st *ringbuf, uint8_t *data, uint16_t size); ///< 向 ringbuf 中写入数据
  67. bool y_ringbuf_delete_data(RINGBUF_st *ringbuf, uint16_t size); ///< 删除 ringbuf 指定长度数据
  68. bool y_ringbuf_is_full(RINGBUF_st *ringbuf); ///< 判断 ringbuf 是否为满
  69. bool y_ringbuf_is_empty(RINGBUF_st *ringbuf); ///< 判断 ringbuf 是否为空
  70. bool y_ringbuf_reset(RINGBUF_st *ringbuf); ///< 重置 ringbuf
  71. bool y_ringbuf_destroy(RINGBUF_st *ringbuf); ///< 销毁 ringbuf
  72. RINGBUF_st *y_ringbuf_create(uint16_t length); ///< 创建 ringbuf 长度 (max 65535)
  73. bool y_ringbuf_write_remalloc_memory(RINGBUF_st *ringbuf, uint8_t *data, uint16_t size);
  74. /// ------------------------------------------------------------------------------------------------------------------------------------
  75. /// 条件编译结尾
  76. /// ------------------------------------------------------------------------------------------------------------------------------------
  77. #endif // _Y_RINGBUF_H 防止当前头文件被重复引用