#include "DataHelper.h" /** * 初始化一个先入先出缓冲 * * @author lxz (041620 15:15:44) * * @param fifo 缓冲对象 * @param buffer 数据缓冲 * @param total 总数据大小 */ void dh_fifo_init(fifo_buffer_t *fifo, unsigned char *buffer, int total) { fifo->buffer = buffer; fifo->total = total; fifo->count = 0; fifo->head = 0; fifo->tail = 0; } /** * 缓冲保存一个数据 * * @author lxz (041620 15:16:14) * * @param fifo 缓冲对象 * @param dat 缓冲数据 */ void dh_fifo_push(fifo_buffer_t *fifo, unsigned char dat) { if (fifo->count + 1 < fifo->total) { DH_ENTER_CRITCAL(); fifo->buffer[fifo->head] = dat; if (++fifo->head >= fifo->total) { fifo->head = 0; } fifo->count++; DH_EXIT_CRITCAL(); } } /** * * * @author lxz (041620 15:16:43) * * @param fifo * @param dat * * @return int */ int dh_fifo_pop(fifo_buffer_t *fifo, unsigned char *dat) { if (fifo->count > 0) { DH_ENTER_CRITCAL(); *dat = fifo->buffer[fifo->tail]; if (++fifo->tail >= fifo->total) { fifo->tail = 0; } fifo->count--; DH_EXIT_CRITCAL(); return 1; } return 0; }