A simple fifo for data

Files at this revision

API Documentation at this revision

Comitter:
gert_lauritsen
Date:
Wed May 21 18:30:45 2014 +0000
Commit message:
Just a simple Fifo

Changed in this revision

fifo.cpp Show annotated file Show diff for this revision Revisions of this file
fifo.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 3c704483eb79 fifo.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fifo.cpp	Wed May 21 18:30:45 2014 +0000
@@ -0,0 +1,49 @@
+#include "fifo.h"
+
+fifo::fifo()
+{
+    this->head = 0;
+    this->tail = 0;
+}
+uint32_t fifo::available()
+{
+    return (FIFO_SIZE + this->head - this->tail) % FIFO_SIZE;
+}
+uint32_t fifo::free()
+{
+    return (FIFO_SIZE - 1 - available());
+}
+uint8_t fifo::put(FIFO_TYPE data)
+{
+    uint32_t next;
+
+    // check if FIFO has room
+    next = (this->head + 1) % FIFO_SIZE;
+    if (next == this->tail)
+    {
+        // full
+        return 1;
+    }
+
+    this->buffer[this->head] = data;
+    this->head = next;
+
+    return 0;
+}
+uint8_t fifo::get(FIFO_TYPE* data)
+{
+    uint32_t next;
+
+    // check if FIFO has data
+    if (this->head == this->tail)
+    {
+        return 1; // FIFO empty
+    }
+
+    next = (this->tail + 1) % FIFO_SIZE;
+
+    *data = this->buffer[this->tail];
+    this->tail = next;
+
+    return 0;
+}
diff -r 000000000000 -r 3c704483eb79 fifo.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fifo.h	Wed May 21 18:30:45 2014 +0000
@@ -0,0 +1,23 @@
+#ifndef FIFO_H_
+#define FIFO_H_
+
+#include "LPC17xx.h"
+
+#define FIFO_SIZE 256
+#define FIFO_TYPE uint8_t
+
+class fifo
+{
+    FIFO_TYPE buffer[FIFO_SIZE];
+    uint32_t head, tail;
+
+public:
+    fifo();
+    uint8_t put(FIFO_TYPE data);// returns 0 on success
+    uint8_t get(FIFO_TYPE* data);
+    uint32_t available();
+    uint32_t free();
+};
+
+
+#endif /* FIFO_H_ */