1 Star 0 Fork 4

jack/LiteQueue

forked from bagy/LiteQueue 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.c 3.19 KB
一键复制 编辑 原始数据 按行查看 历史
bagy 提交于 2023-12-14 21:25 . 修复写入时回调异常问题
#include "Lite_Queue.h"
#define QUEUE_ITEM_NUM 3
#define QUEUE_ITEM_SIZE 10
/*
* @ brief : Print the contents of each list item in the queue.
* @ param : {LiteQueue *} queue: Message queue handle pointer.
* @ return: None.
* @ author: bagy.
* @ note : None.
*/
static void PrintLiteQueue(LiteQueue *queue)
{
if(queue == NULL)
return;
for(int i = 0; i < queue->item_num; i++)
{
log_i("[item_num:%d] ", i);
for(int n = 0; n < queue->item_size; n++)
{
log_i("%d ", *((uint8_t *)queue + sizeof(LiteQueue) + i * queue->item_size + n));
}
log_i("\n");
}
}
/*
* @ brief : Print the data in buff.
* @ param : {const char *} mode : Print mode.
{LiteQueue *} queue: Message queue handle pointer.
* @ return: None.
* @ author: bagy.
* @ note : Used to observe buff data changes and test to verify the correctness of written or read data.
*/
static void PrintBuff(const char *mode, uint8_t *buff, size_t len)
{
if((buff == NULL) || (len < 1))
return;
if(strstr(mode, "write"))
log_i("Write buff>>>:");
else
log_i("Read buff <<<:");
for(size_t i = 0; i < len; i++)
{
log_i("%d ", buff[i]);
}
log_i("\n\n");
}
/*
* @ brief : Queue callback function.
* @ param : {LiteQueue_Status} sta: Queue status.
* @ return: None.
* @ author: bagy.
* @ note : When the queue writes or reads abnormally, the upper application will be notified through the callback function.
*/
#if(QUEUE_ERROR_CALLBACK == QUEUE_ENABLE)
void LiteQueue_Callback(LiteQueue_Status sta)
{
switch(sta)
{
case LITE_QUEUE_FULL : /*...*/ break;
case LITE_QUEUE_WRITE_BUSY: /*...*/ break;
case LITE_QUEUE_READ_BUSY : /*...*/ break;
default:break;
}
}
#endif
int main(void)
{
uint8_t writebuff[QUEUE_ITEM_SIZE] = {0};
uint8_t readbuff[QUEUE_ITEM_SIZE] = {0};
/* Create message queue, 4 list items, each list item has 10 bytes of memory space */
#if(QUEUE_ERROR_CALLBACK == QUEUE_ENABLE)
pLiteQueue msgQueue = LiteQueue_Create(QUEUE_ITEM_NUM, QUEUE_ITEM_SIZE, LiteQueue_Callback);
#else
pLiteQueue msgQueue = LiteQueue_Create(QUEUE_ITEM_NUM, QUEUE_ITEM_SIZE);
#endif
PrintLiteQueue(msgQueue);
log_i("\n");
/* Simulate writing and reading to the queue 6 times, and observe the data in the queue by printing */
for(int i = 0; i < 4; i++)
{
/* Simulate data, change the writebuff data and write it to the queue */
for(int n = 0; n < msgQueue->item_size; n++)
writebuff[n] = (i * msgQueue->item_size + n) % 256;
/* Data is written to the queue */
Write_Back_To_LiteQueue(msgQueue, writebuff);
PrintLiteQueue(msgQueue);
log_i("\n");
}
/* Read data from queue */
Read_From_LiteQueue(msgQueue, readbuff);
PrintBuff("read", readbuff, sizeof(readbuff));
/* Read data from queue */
Read_From_LiteQueue(msgQueue, readbuff);
PrintBuff("read", readbuff, sizeof(readbuff));
PrintBuff("write", writebuff, sizeof(writebuff));
/* Data is written to the queue */
Write_Back_To_LiteQueue(msgQueue, writebuff);
PrintLiteQueue(msgQueue);
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jack998/lite-queue.git
git@gitee.com:jack998/lite-queue.git
jack998
lite-queue
LiteQueue
master

搜索帮助