vt100: include pgmspace.h as we use PROGMEM macro
[aversive.git] / modules / base / cirbuf / cirbuf.h
1 /*  
2  *  Copyright Droids Corporation (2007)
3  *  Olivier MATZ <zer0@droids-corp.org>
4  * 
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  *  Revision : $Id: cirbuf.h,v 1.1.2.5 2009-01-03 16:24:50 zer0 Exp $
20  *
21  *
22  */
23
24 /** 
25  * Circular buffer implementation. You should not use a circular buffer
26  * size > 127.
27  *
28  * Funcs are not atomic, so if * necessary, each call should be
29  * protected by IRQ_LOCKs.
30  */
31
32
33 #ifndef _CIRBUF_H_
34 #define _CIRBUF_H_
35
36 #include <aversive.h>
37 #include <stdio.h>
38
39 #ifdef CONFIG_MODULE_CIRBUF_LARGE
40 typedef signed int cirbuf_int;
41 typedef unsigned int cirbuf_uint;
42 #else
43 typedef signed char cirbuf_int;
44 typedef unsigned char cirbuf_uint;
45 #endif
46
47 /**
48  * This structure is the header of a cirbuf type.
49  */
50 struct cirbuf {
51         cirbuf_uint maxlen;             /**< total len of the fifo (number of elements) */
52         volatile cirbuf_uint start;     /**< indice of the first elt */
53         volatile cirbuf_uint end;       /**< indice of the last elt */
54         volatile cirbuf_uint len;       /**< current len of fifo */
55         char *buf;
56 };
57
58 /* #define CIRBUF_DEBUG */
59
60 #ifdef CIRBUF_DEBUG
61 #define dprintf(fmt, ...) printf("line %3.3d - " fmt, __LINE__, ##__VA_ARGS__)
62 #else
63 #define dprintf(args...) do {} while(0)
64 #endif
65
66
67 /**
68  * Init the circular buffer
69  */
70 void cirbuf_init(struct cirbuf *cbuf, char *buf, cirbuf_uint start, cirbuf_uint maxlen);
71
72
73 /**
74  * Return 1 if the circular buffer is full
75  */
76 #define CIRBUF_IS_FULL(cirbuf) ((cirbuf)->maxlen == (cirbuf)->len)
77
78 /**
79  * Return 1 if the circular buffer is empty
80  */
81 #define CIRBUF_IS_EMPTY(cirbuf) ((cirbuf)->len == 0)
82
83 /**
84  * return current size of the circular buffer (number of used elements)
85  */
86 #define CIRBUF_GET_LEN(cirbuf) ((cirbuf)->len)
87
88 /**
89  * return size of the circular buffer (used + free elements)
90  */
91 #define CIRBUF_GET_MAXLEN(cirbuf) ((cirbuf)->maxlen)
92
93 /**
94  * return the number of free elts 
95  */
96 #define CIRBUF_GET_FREELEN(cirbuf) ((cirbuf)->maxlen - (cirbuf)->len)
97
98 /**
99  * Iterator for a circular buffer
100  *   c: struct cirbuf pointer
101  *   i: an integer type (cirbuf_uint is enough) internally used in the macro
102  *   e: char that takes the value for each iteration
103  */
104 #define CIRBUF_FOREACH(c, i, e)                                 \
105         for ( i=0, e=(c)->buf[(c)->start] ;                     \
106               i<((c)->len) ;                                    \
107               i ++,  e=(c)->buf[((c)->start+i)%((c)->maxlen)])
108
109
110 /** 
111  * Add a character at head of the circular buffer. Return 0 on success, or
112  * a negative value on error.
113  */
114 cirbuf_int cirbuf_add_head_safe(struct cirbuf *cbuf, char c);
115
116 /** 
117  * Add a character at head of the circular buffer. You _must_ check that you
118  * have enough free space in the buffer before calling this func.
119  */
120 void cirbuf_add_head(struct cirbuf *cbuf, char c);
121
122 /** 
123  * Add a character at tail of the circular buffer. Return 0 on success, or
124  * a negative value on error.
125  */
126 cirbuf_int cirbuf_add_tail_safe(struct cirbuf *cbuf, char c);
127
128 /** 
129  * Add a character at tail of the circular buffer. You _must_ check that you
130  * have enough free space in the buffer before calling this func.
131  */
132 void cirbuf_add_tail(struct cirbuf *cbuf, char c);
133
134 /** 
135  * Remove a char at the head of the circular buffer. Return 0 on
136  * success, or a negative value on error.
137  */
138 cirbuf_int cirbuf_del_head_safe(struct cirbuf *cbuf);
139
140 /** 
141  * Remove a char at the head of the circular buffer. You _must_ check
142  * that buffer is not empty before calling the function.
143  */
144 void cirbuf_del_head(struct cirbuf *cbuf);
145
146 /** 
147  * Remove a char at the tail of the circular buffer. Return 0 on
148  * success, or a negative value on error.
149  */
150 cirbuf_int cirbuf_del_tail_safe(struct cirbuf *cbuf);
151
152 /** 
153  * Remove a char at the tail of the circular buffer. You _must_ check
154  * that buffer is not empty before calling the function.
155  */
156 void cirbuf_del_tail(struct cirbuf *cbuf);
157
158 /**
159  * Return the head of the circular buffer. You _must_ check that
160  * buffer is not empty before calling the function.
161  */
162 char cirbuf_get_head(struct cirbuf *cbuf);
163
164 /**
165  * Return the tail of the circular buffer. You _must_ check that
166  * buffer is not empty before calling the function.
167  */
168 char cirbuf_get_tail(struct cirbuf *cbuf);
169
170
171
172 /** 
173  * Add a buffer at head of the circular buffer. 'c' is a pointer to a
174  * buffer, and n is the number of char to add. Return the number of
175  * copied bytes on success, or a negative value on error.
176  */
177 cirbuf_int cirbuf_add_buf_head(struct cirbuf *cbuf, const char *c, cirbuf_uint n);
178
179 /** 
180  * Add a buffer at tail of the circular buffer. 'c' is a pointer to a
181  * buffer, and n is the number of char to add. Return the number of
182  * copied bytes on success, or a negative value on error.
183  */
184 cirbuf_int cirbuf_add_buf_tail(struct cirbuf *cbuf, const char *c, cirbuf_uint n);
185
186 /** 
187  * Remove chars at the head of the circular buffer. Return 0 on
188  * success, or a negative value on error.
189  */
190 cirbuf_int cirbuf_del_buf_head(struct cirbuf *cbuf, cirbuf_uint size);
191
192 /** 
193  * Remove chars at the tail of the circular buffer. Return 0 on
194  * success, or a negative value on error.
195  */
196 cirbuf_int cirbuf_del_buf_tail(struct cirbuf *cbuf, cirbuf_uint size);
197
198 /** 
199  * Copy a maximum of 'size' characters from the head of the circular
200  * buffer to a flat one pointed by 'c'. Return the number of copied
201  * chars.
202  */
203 cirbuf_int cirbuf_get_buf_head(struct cirbuf *cbuf, char *c, cirbuf_uint size);
204
205 /** 
206  * Copy a maximum of 'size' characters from the tail of the circular
207  * buffer to a flat one pointed by 'c'. Return the number of copied
208  * chars.
209  */
210 cirbuf_int cirbuf_get_buf_tail(struct cirbuf *cbuf, char *c, cirbuf_uint size);
211
212
213 /** 
214  * Set the start of the data to the index 0 of the internal buffer.
215  */
216 void cirbuf_align_left(struct cirbuf *cbuf);
217
218 /** 
219  * Set the end of the data to the last index of the internal buffer.
220  */
221 void cirbuf_align_right(struct cirbuf *cbuf);
222
223 #endif /* _CIRBUF_H_ */