123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include "signal.h"
- #include "lib_ringbuf.h"
- #include "cmsis_os.h"
- //用push_count 压入
- //检查pop_count-push_count ?
- //错误休眠
- //错误打印信息
- //数据流连续压入 不能断开
- char str[20] = {'1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','w','a','1','2','3'};
- bool run_over = false;
- void sighandler(int signum)
- {
- run_over = true;
- }
- void *push(void *arg)
- {
- int RetSize = 0;
- int Push_Ptr = 0;
- RingBuf tmp = (RingBuf)arg;
- if (tmp != NULL)
- {
- while (!run_over)
- {
- RetSize = RingBuf_Push(tmp, str + Push_Ptr, sizeof(str) - Push_Ptr);
- if (RetSize < sizeof(str) - Push_Ptr)
- {
- Push_Ptr += RetSize;
- osDelay(1);
- }
- else
- Push_Ptr = 0;
- }
- }
- else
- printf("RingBufOBJ ERR");
- return NULL;
- }
- void *pop(void *arg)
- {
- char buf[10];
- int RetSize = 0;
- int Pop_Ptr = 0;
- RingBuf tmp = (RingBuf)arg;
- if (tmp != NULL)
- {
- while (!run_over)
- {
- RetSize = RingBuf_Pop(tmp, buf, sizeof(buf) - 1);//size?
- if (RetSize == 0) //无有效数据
- osDelay(1);
- else //检查
- {
- // char str[20] = {'1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','w','a','1','2','3'};
- //1.开始比较 比较完之后 Pop_Ptr指针往后移动RetSize
- //2.需要回到模板头部时 即 RetSize > sizeof(str) - Pop_Ptr
- //分成两部分比较 第一部分比较 (sizeof(str) - Pop_Ptr)个长度,Pop_Ptr清零
- //第二部分比较 RetSize - (sizeof(str) - Pop_Ptr)个长度
- if (RetSize > sizeof(str) - Pop_Ptr)
- {
- int PreSize = sizeof(str) - Pop_Ptr;
- if (memcmp(str + Pop_Ptr, buf, PreSize) != 0 || memcmp(str, buf + PreSize, RetSize - PreSize) != 0)//?
- {
- puts("ERR 1");
- run_over = true;
- }
- Pop_Ptr = RetSize - PreSize;
- }
- else
- {
- if (memcmp(str + Pop_Ptr, buf, RetSize) != 0)
- {
- puts("ERR 2");
- run_over = true;
- }
- Pop_Ptr += RetSize;//到头处理
- if (Pop_Ptr == sizeof(str))
- Pop_Ptr = 0;
- }
- }
- }
- }
- else
- printf("RingBufOBJ ERR");
- return NULL;
- }
- #if 0
- RingBufOBJ _data;
- int main()
- {
- char buf[16];
- RingBuf tmp = RingBuf_Init(&_data, buf, sizeof(buf));
- if (tmp != NULL)
- {
- pthread_t th1, th2;
- signal(SIGINT, sighandler);
- pthread_create(&th1, NULL, push, tmp);
- pthread_create(&th2, NULL, pop, tmp);
-
- pthread_join(th1, NULL);
- pthread_join(th2, NULL);
- }
- else
- printf("Failed to initialize");
- return 0;
- }
- #endif
|