Merge Network code with webserver etc
[clinton/Smoothieware.git] / src / libs / RingBuffer.h
index 4e650e4..20c1128 100644 (file)
@@ -1,8 +1,8 @@
-/*  
+/*
       This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
       Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
       Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-      You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. 
+      You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
       
       With chucks taken from http://en.wikipedia.org/wiki/Circular_buffer, see licence there also
 */
@@ -20,15 +20,17 @@ template<class kind, int length> class RingBuffer {
         int          prev_block_index(int index);
         void         push_back(kind object);
         void         pop_front(kind &object);
+        kind*        get_tail_ref();
         void         get( int index, kind &object);
         kind*        get_ref( int index);
         void         delete_first();
 
         kind         buffer[length];
-        int          head;
-        int          tail;
+        volatile int          head;
+        volatile int          tail;
 };
 
+#include "sLPC17xx.h"
 
 template<class kind, int length> RingBuffer<kind, length>::RingBuffer(){
     this->head = this->tail = 0;
@@ -39,7 +41,11 @@ template<class kind, int length>  int RingBuffer<kind, length>::capacity(){
 }
 
 template<class kind, int length>  int RingBuffer<kind, length>::size(){
-return((this->head>this->tail)?length:0)+this->tail-head;
+       //return((this->head>this->tail)?length:0)+this->tail-head;
+       __disable_irq();
+       int i = tail - head + ((head > tail)?length:0);
+       __enable_irq();
+       return i;
 }
 
 template<class kind, int length> int RingBuffer<kind, length>::next_block_index(int index){
@@ -59,6 +65,10 @@ template<class kind, int length> void RingBuffer<kind, length>::push_back(kind o
     this->tail = (tail+1)&(length-1);
 }
 
+template<class kind, int length> kind* RingBuffer<kind, length>::get_tail_ref(){
+    return &(buffer[tail]);
+}
+
 template<class kind, int length> void RingBuffer<kind, length>::get(int index, kind &object){
     int j= 0;
     int k= this->head;
@@ -67,9 +77,9 @@ template<class kind, int length> void RingBuffer<kind, length>::get(int index, k
         j++;
         k= (k + 1) & (length - 1);
     }
-    // TODO : this checks wether we are asked a value out of range 
+    // TODO : this checks wether we are asked a value out of range
     //if (k == this->tail){
-    //    return NULL; 
+    //    return NULL;
     //}
     object = this->buffer[k];
 }
@@ -83,7 +93,7 @@ template<class kind, int length> kind* RingBuffer<kind, length>::get_ref(int ind
         j++;
         k= (k + 1) & (length - 1);
     }
-    // TODO : this checks wether we are asked a value out of range 
+    // TODO : this checks wether we are asked a value out of range
     if (k == this->tail){
         return NULL;
     }
@@ -91,7 +101,7 @@ template<class kind, int length> kind* RingBuffer<kind, length>::get_ref(int ind
 }
 
 template<class kind, int length> void RingBuffer<kind, length>::pop_front(kind &object){
-    object = this->buffer[this->head]; 
+    object = this->buffer[this->head];
     this->head = (this->head+1)&(length-1);
 }