scpi-parser, adopted from github

Dependents:   scpi_parser_test

Committer:
iesensor
Date:
Fri Nov 14 01:07:59 2014 +0000
Revision:
0:d8aca47812e0
tested on K64F

Who changed what in which revision?

UserRevisionLine numberNew contents of line
iesensor 0:d8aca47812e0 1
iesensor 0:d8aca47812e0 2 #include "scpi/fifo.h"
iesensor 0:d8aca47812e0 3
iesensor 0:d8aca47812e0 4 void fifo_init(fifo_t * fifo) {
iesensor 0:d8aca47812e0 5 fifo->wr = 0;
iesensor 0:d8aca47812e0 6 fifo->rd = 0;
iesensor 0:d8aca47812e0 7 fifo->size = FIFO_SIZE;
iesensor 0:d8aca47812e0 8 }
iesensor 0:d8aca47812e0 9
iesensor 0:d8aca47812e0 10 void fifo_clear(fifo_t * fifo) {
iesensor 0:d8aca47812e0 11 fifo->wr = 0;
iesensor 0:d8aca47812e0 12 fifo->rd = 0;
iesensor 0:d8aca47812e0 13 }
iesensor 0:d8aca47812e0 14
iesensor 0:d8aca47812e0 15 scpi_bool_t fifo_add(fifo_t * fifo, int16_t value) {
iesensor 0:d8aca47812e0 16 /* FIFO full? */
iesensor 0:d8aca47812e0 17 if (fifo->wr == ((fifo->rd + fifo->size) % (fifo->size + 1))) {
iesensor 0:d8aca47812e0 18 fifo_remove(fifo, NULL);
iesensor 0:d8aca47812e0 19 }
iesensor 0:d8aca47812e0 20
iesensor 0:d8aca47812e0 21 fifo->data[fifo->wr] = value;
iesensor 0:d8aca47812e0 22 fifo->wr = (fifo->wr + 1) % (fifo->size + 1);
iesensor 0:d8aca47812e0 23
iesensor 0:d8aca47812e0 24 return TRUE;
iesensor 0:d8aca47812e0 25 }
iesensor 0:d8aca47812e0 26
iesensor 0:d8aca47812e0 27 scpi_bool_t fifo_remove(fifo_t * fifo, int16_t * value) {
iesensor 0:d8aca47812e0 28 /* FIFO empty? */
iesensor 0:d8aca47812e0 29 if (fifo->wr == fifo->rd) {
iesensor 0:d8aca47812e0 30 return FALSE;
iesensor 0:d8aca47812e0 31 }
iesensor 0:d8aca47812e0 32
iesensor 0:d8aca47812e0 33 if(value) {
iesensor 0:d8aca47812e0 34 *value = fifo->data[fifo->rd];
iesensor 0:d8aca47812e0 35 }
iesensor 0:d8aca47812e0 36
iesensor 0:d8aca47812e0 37 fifo->rd = (fifo->rd + 1) % (fifo->size + 1);
iesensor 0:d8aca47812e0 38
iesensor 0:d8aca47812e0 39 return TRUE;
iesensor 0:d8aca47812e0 40 }
iesensor 0:d8aca47812e0 41
iesensor 0:d8aca47812e0 42 scpi_bool_t fifo_count(fifo_t * fifo, int16_t * value) {
iesensor 0:d8aca47812e0 43 *value = fifo->wr - fifo->rd;
iesensor 0:d8aca47812e0 44 if (*value < 0) {
iesensor 0:d8aca47812e0 45 *value += (fifo->size + 1);
iesensor 0:d8aca47812e0 46 }
iesensor 0:d8aca47812e0 47 return TRUE;
iesensor 0:d8aca47812e0 48 }