lib_ringbuf_test.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include "signal.h"
  6. #include "lib_ringbuf.h"
  7. #include "cmsis_os.h"
  8. //用push_count 压入
  9. //检查pop_count-push_count ?
  10. //错误休眠
  11. //错误打印信息
  12. //数据流连续压入 不能断开
  13. char str[20] = {'1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','w','a','1','2','3'};
  14. bool run_over = false;
  15. void sighandler(int signum)
  16. {
  17. run_over = true;
  18. }
  19. void *push(void *arg)
  20. {
  21. int RetSize = 0;
  22. int Push_Ptr = 0;
  23. RingBuf tmp = (RingBuf)arg;
  24. if (tmp != NULL)
  25. {
  26. while (!run_over)
  27. {
  28. RetSize = RingBuf_Push(tmp, str + Push_Ptr, sizeof(str) - Push_Ptr);
  29. if (RetSize < sizeof(str) - Push_Ptr)
  30. {
  31. Push_Ptr += RetSize;
  32. osDelay(1);
  33. }
  34. else
  35. Push_Ptr = 0;
  36. }
  37. }
  38. else
  39. printf("RingBufOBJ ERR");
  40. return NULL;
  41. }
  42. void *pop(void *arg)
  43. {
  44. char buf[10];
  45. int RetSize = 0;
  46. int Pop_Ptr = 0;
  47. RingBuf tmp = (RingBuf)arg;
  48. if (tmp != NULL)
  49. {
  50. while (!run_over)
  51. {
  52. RetSize = RingBuf_Pop(tmp, buf, sizeof(buf) - 1);//size?
  53. if (RetSize == 0) //无有效数据
  54. osDelay(1);
  55. else //检查
  56. {
  57. // char str[20] = {'1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','w','a','1','2','3'};
  58. //1.开始比较 比较完之后 Pop_Ptr指针往后移动RetSize
  59. //2.需要回到模板头部时 即 RetSize > sizeof(str) - Pop_Ptr
  60. //分成两部分比较 第一部分比较 (sizeof(str) - Pop_Ptr)个长度,Pop_Ptr清零
  61. //第二部分比较 RetSize - (sizeof(str) - Pop_Ptr)个长度
  62. if (RetSize > sizeof(str) - Pop_Ptr)
  63. {
  64. int PreSize = sizeof(str) - Pop_Ptr;
  65. if (memcmp(str + Pop_Ptr, buf, PreSize) != 0 || memcmp(str, buf + PreSize, RetSize - PreSize) != 0)//?
  66. {
  67. puts("ERR 1");
  68. run_over = true;
  69. }
  70. Pop_Ptr = RetSize - PreSize;
  71. }
  72. else
  73. {
  74. if (memcmp(str + Pop_Ptr, buf, RetSize) != 0)
  75. {
  76. puts("ERR 2");
  77. run_over = true;
  78. }
  79. Pop_Ptr += RetSize;//到头处理
  80. if (Pop_Ptr == sizeof(str))
  81. Pop_Ptr = 0;
  82. }
  83. }
  84. }
  85. }
  86. else
  87. printf("RingBufOBJ ERR");
  88. return NULL;
  89. }
  90. #if 0
  91. RingBufOBJ _data;
  92. int main()
  93. {
  94. char buf[16];
  95. RingBuf tmp = RingBuf_Init(&_data, buf, sizeof(buf));
  96. if (tmp != NULL)
  97. {
  98. pthread_t th1, th2;
  99. signal(SIGINT, sighandler);
  100. pthread_create(&th1, NULL, push, tmp);
  101. pthread_create(&th2, NULL, pop, tmp);
  102. pthread_join(th1, NULL);
  103. pthread_join(th2, NULL);
  104. }
  105. else
  106. printf("Failed to initialize");
  107. return 0;
  108. }
  109. #endif