A simple fifo for data

Committer:
gert_lauritsen
Date:
Wed May 21 18:30:45 2014 +0000
Revision:
0:3c704483eb79
Just a simple Fifo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gert_lauritsen 0:3c704483eb79 1 #include "fifo.h"
gert_lauritsen 0:3c704483eb79 2
gert_lauritsen 0:3c704483eb79 3 fifo::fifo()
gert_lauritsen 0:3c704483eb79 4 {
gert_lauritsen 0:3c704483eb79 5 this->head = 0;
gert_lauritsen 0:3c704483eb79 6 this->tail = 0;
gert_lauritsen 0:3c704483eb79 7 }
gert_lauritsen 0:3c704483eb79 8 uint32_t fifo::available()
gert_lauritsen 0:3c704483eb79 9 {
gert_lauritsen 0:3c704483eb79 10 return (FIFO_SIZE + this->head - this->tail) % FIFO_SIZE;
gert_lauritsen 0:3c704483eb79 11 }
gert_lauritsen 0:3c704483eb79 12 uint32_t fifo::free()
gert_lauritsen 0:3c704483eb79 13 {
gert_lauritsen 0:3c704483eb79 14 return (FIFO_SIZE - 1 - available());
gert_lauritsen 0:3c704483eb79 15 }
gert_lauritsen 0:3c704483eb79 16 uint8_t fifo::put(FIFO_TYPE data)
gert_lauritsen 0:3c704483eb79 17 {
gert_lauritsen 0:3c704483eb79 18 uint32_t next;
gert_lauritsen 0:3c704483eb79 19
gert_lauritsen 0:3c704483eb79 20 // check if FIFO has room
gert_lauritsen 0:3c704483eb79 21 next = (this->head + 1) % FIFO_SIZE;
gert_lauritsen 0:3c704483eb79 22 if (next == this->tail)
gert_lauritsen 0:3c704483eb79 23 {
gert_lauritsen 0:3c704483eb79 24 // full
gert_lauritsen 0:3c704483eb79 25 return 1;
gert_lauritsen 0:3c704483eb79 26 }
gert_lauritsen 0:3c704483eb79 27
gert_lauritsen 0:3c704483eb79 28 this->buffer[this->head] = data;
gert_lauritsen 0:3c704483eb79 29 this->head = next;
gert_lauritsen 0:3c704483eb79 30
gert_lauritsen 0:3c704483eb79 31 return 0;
gert_lauritsen 0:3c704483eb79 32 }
gert_lauritsen 0:3c704483eb79 33 uint8_t fifo::get(FIFO_TYPE* data)
gert_lauritsen 0:3c704483eb79 34 {
gert_lauritsen 0:3c704483eb79 35 uint32_t next;
gert_lauritsen 0:3c704483eb79 36
gert_lauritsen 0:3c704483eb79 37 // check if FIFO has data
gert_lauritsen 0:3c704483eb79 38 if (this->head == this->tail)
gert_lauritsen 0:3c704483eb79 39 {
gert_lauritsen 0:3c704483eb79 40 return 1; // FIFO empty
gert_lauritsen 0:3c704483eb79 41 }
gert_lauritsen 0:3c704483eb79 42
gert_lauritsen 0:3c704483eb79 43 next = (this->tail + 1) % FIFO_SIZE;
gert_lauritsen 0:3c704483eb79 44
gert_lauritsen 0:3c704483eb79 45 *data = this->buffer[this->tail];
gert_lauritsen 0:3c704483eb79 46 this->tail = next;
gert_lauritsen 0:3c704483eb79 47
gert_lauritsen 0:3c704483eb79 48 return 0;
gert_lauritsen 0:3c704483eb79 49 }