Simple buffer.

Dependents:   data_logger

Files at this revision

API Documentation at this revision

Comitter:
jhestolano
Date:
Tue Jun 04 01:39:13 2013 +0000
Commit message:
Simple data logger.

Changed in this revision

buffer.cpp Show annotated file Show diff for this revision Revisions of this file
buffer.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r f45f33d1febb buffer.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buffer.cpp	Tue Jun 04 01:39:13 2013 +0000
@@ -0,0 +1,105 @@
+#include "buffer.h"
+
+template<class T>
+Buffer<T>::Buffer(int buf_size)
+{
+    size = buf_size;
+    data = new T[buf_size];
+    full = false;
+    empty = true;
+    p_tail = &data[0];
+    p_head = &data[0];
+    p_start = &data[0];
+    p_end = &data[buf_size];
+}
+
+template<class T>
+void Buffer<T>::put(T val)
+{
+    if(!full) //Si hay espacio, escribe.
+    {
+        *p_head = val;
+        p_head++;
+        empty = false; //Si se pudo esciribir, el buffer no está vacío.
+        if(p_head == p_end) //Si ya llegó al final, volver a iniciar.
+        {
+            p_head = p_start;
+        }
+        if(p_head == p_tail)
+        {
+            full = true; // Si el indice de escritura alcanza el de lectura, el buffer se llenó.
+        }
+    }
+    else //Buffer lleno
+    {
+        //std::cout << "Buffer lleno..." << std::endl;
+    }
+}
+
+template<class T>
+T Buffer<T>::get()
+{
+    if(!empty)
+    {
+        T temp = *p_tail;
+        *p_tail = 0;
+        p_tail++;
+        full = false;
+        if(p_tail == p_end)
+        {
+            p_tail = p_start;
+        }
+        if(p_tail == p_head)
+        {
+            empty = true;
+        }
+        return temp;
+    }
+    else
+    {
+        //std::cout << "Buffer vacío..." << std::endl;
+        return 0;
+    }
+
+}
+
+template<class T>
+bool Buffer<T>::isEmpty()
+{
+    return empty;
+}
+
+template<class T>
+bool Buffer<T>::isFull()
+{
+    return full;
+}
+
+/*
+template<class T>
+void Buffer<T>::printData()
+{
+    for(int i = 0; i < size; i++)
+    {
+        std::cout << "Elemento " << i << ": " << data[i] << std::endl;
+    }
+}
+*/
+
+template<class T>
+Buffer<T>::~Buffer()
+{
+    delete[] data;
+}
+
+
+
+template class Buffer<float>;
+template class Buffer<char>;
+template class Buffer<uint8_t>;
+template class Buffer<uint16_t>;
+template class Buffer<uint32_t>;
+template class Buffer<int8_t>;
+template class Buffer<int16_t>;
+template class Buffer<int32_t>;
+template class Buffer<double>;
diff -r 000000000000 -r f45f33d1febb buffer.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buffer.h	Tue Jun 04 01:39:13 2013 +0000
@@ -0,0 +1,30 @@
+#ifndef BUFFER_H
+#define BUFFER_H
+
+#include "mbed.h"
+
+template<class T>
+class Buffer
+{
+public:
+    Buffer(int buf_size);
+    void put(T val);
+    T get();
+    bool isEmpty();
+    bool isFull();
+    //void printData();
+    ~Buffer();
+
+private:
+    int size;
+    T* p_start;
+    T* p_end;
+    T* p_tail;
+    T* p_head;
+    T* data;
+    int index;
+    bool full;
+    bool empty;
+};
+
+#endif // BUFFER_H