Added a test case for the switch module
[clinton/Smoothieware.git] / src / libs / RingBuffer.h
1 /*
2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
3 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.
4 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.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
6
7 With chucks taken from http://en.wikipedia.org/wiki/Circular_buffer, see licence there also
8 */
9
10 #ifndef RINGBUFFER_H
11 #define RINGBUFFER_H
12
13
14 template<class kind, int length> class RingBuffer {
15 public:
16 RingBuffer();
17 int size();
18 int capacity();
19 int next_block_index(int index);
20 int prev_block_index(int index);
21 void push_back(kind object);
22 void pop_front(kind &object);
23 kind* get_head_ref();
24 kind* get_tail_ref();
25 void get( int index, kind &object);
26 kind* get_ref( int index);
27 void delete_tail();
28
29 kind buffer[length];
30 volatile int tail;
31 volatile int head;
32 };
33
34 #include "sLPC17xx.h"
35
36 template<class kind, int length> RingBuffer<kind, length>::RingBuffer(){
37 this->tail = this->head = 0;
38 }
39
40 template<class kind, int length> int RingBuffer<kind, length>::capacity(){
41 return length-1;
42 }
43
44 template<class kind, int length> int RingBuffer<kind, length>::size(){
45 //return((this->tail>this->head)?length:0)+this->head-tail;
46 __disable_irq();
47 int i = head - tail + ((tail > head)?length:0);
48 __enable_irq();
49 return i;
50 }
51
52 template<class kind, int length> int RingBuffer<kind, length>::next_block_index(int index){
53 index++;
54 if (index == length) { index = 0; }
55 return(index);
56 }
57
58 template<class kind, int length> int RingBuffer<kind, length>::prev_block_index(int index){
59 if (index == 0) { index = length; }
60 index--;
61 return(index);
62 }
63
64 template<class kind, int length> void RingBuffer<kind, length>::push_back(kind object){
65 this->buffer[this->head] = object;
66 this->head = (head+1)&(length-1);
67 }
68
69 template<class kind, int length> kind* RingBuffer<kind, length>::get_head_ref(){
70 return &(buffer[head]);
71 }
72
73 template<class kind, int length> kind* RingBuffer<kind, length>::get_tail_ref(){
74 return &(buffer[tail]);
75 }
76
77 template<class kind, int length> void RingBuffer<kind, length>::get(int index, kind &object){
78 int j= 0;
79 int k= this->tail;
80 while (k != this->head){
81 if (j == index) break;
82 j++;
83 k= (k + 1) & (length - 1);
84 }
85 // TODO : this checks wether we are asked a value out of range
86 //if (k == this->head){
87 // return NULL;
88 //}
89 object = this->buffer[k];
90 }
91
92
93 template<class kind, int length> kind* RingBuffer<kind, length>::get_ref(int index){
94 int j= 0;
95 int k= this->tail;
96 while (k != this->head){
97 if (j == index) break;
98 j++;
99 k= (k + 1) & (length - 1);
100 }
101 // TODO : this checks wether we are asked a value out of range
102 if (k == this->head){
103 return 0;
104 }
105 return &(this->buffer[k]);
106 }
107
108 template<class kind, int length> void RingBuffer<kind, length>::pop_front(kind &object){
109 object = this->buffer[this->tail];
110 this->tail = (this->tail+1)&(length-1);
111 }
112
113 template<class kind, int length> void RingBuffer<kind, length>::delete_tail(){
114 //kind dummy;
115 //this->pop_front(dummy);
116 this->tail = (this->tail+1)&(length-1);
117 }
118
119
120 #endif