#define queue_max 1200
typedef struct
{
int data[512];
} item_t;
item_t *mp3_data = NULL;
int front = 0;
int rear = 0;
bool half(void)
{
return abs(rear - front) >= 10;
}
static int en_Queue(item_t *q, int *front, int *rear, uint8_t *data, int len)
{
if ((*rear + 1) % queue_max == *front)
{
ESP_LOGE(TAG, "queue full");
return -1;
}
if ((*rear + 1) >= queue_max)
{
*rear = 0;
}
memset((uint8_t *)&q[(*rear) % queue_max], 0, sizeof(item_t));
memcpy((uint8_t *)&q[(*rear) % queue_max], data, len);
(*rear)++;
return (*rear);
}
static int de_Queue(item_t *q, int *front, int *rear, uint8_t *data, int *len)
{
if (*front == *rear % queue_max)
{
ESP_LOGE(TAG, "queue null");
return -1;
}
if ((*front + 1) >= queue_max)
{
*front = 0;
}
*len = 512;
memcpy(data, (uint8_t *)&q[*front], *len);
*front = (*front + 1) % queue_max;
return *front;
}
static void cle_Queue(int *front, int *rear)
{
*front = 0;
*rear = 0;
free(mp3_data);
}
void mp3_ble_receive(uint8_t *rec, uint16_t len)
{
if (mp3_data == NULL)
{
mp3_data = (item_t *)malloc(sizeof(item_t) * queue_max);
}
ESP_LOGI(TAG, "mp3 len=%d rear=%d front=%d", len, rear, front);
en_Queue(mp3_data, &front, &rear, rec, len);
}
int mp3_ble_read(uint8_t *data, int *len)
{
int state;
state = de_Queue(mp3_data, &front, &rear, data, len);
return state;
}
FIFO-原理以及实现
最新推荐文章于 2024-07-05 16:20:17 发布