scpi-parser, adopted from github
Diff: libscpi/fifo.c
- Revision:
- 0:d8aca47812e0
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libscpi/fifo.c Fri Nov 14 01:07:59 2014 +0000 @@ -0,0 +1,48 @@ + +#include "scpi/fifo.h" + +void fifo_init(fifo_t * fifo) { + fifo->wr = 0; + fifo->rd = 0; + fifo->size = FIFO_SIZE; +} + +void fifo_clear(fifo_t * fifo) { + fifo->wr = 0; + fifo->rd = 0; +} + +scpi_bool_t fifo_add(fifo_t * fifo, int16_t value) { + /* FIFO full? */ + if (fifo->wr == ((fifo->rd + fifo->size) % (fifo->size + 1))) { + fifo_remove(fifo, NULL); + } + + fifo->data[fifo->wr] = value; + fifo->wr = (fifo->wr + 1) % (fifo->size + 1); + + return TRUE; +} + +scpi_bool_t fifo_remove(fifo_t * fifo, int16_t * value) { + /* FIFO empty? */ + if (fifo->wr == fifo->rd) { + return FALSE; + } + + if(value) { + *value = fifo->data[fifo->rd]; + } + + fifo->rd = (fifo->rd + 1) % (fifo->size + 1); + + return TRUE; +} + +scpi_bool_t fifo_count(fifo_t * fifo, int16_t * value) { + *value = fifo->wr - fifo->rd; + if (*value < 0) { + *value += (fifo->size + 1); + } + return TRUE; +}