Rev Author Line No. Line
1145 kaklik 1 /*! \file buffer.c \brief Multipurpose byte buffer structure and methods. */
2 //*****************************************************************************
3 //
4 // File Name : 'buffer.c'
5 // Title : Multipurpose byte buffer structure and methods
6 // Author : Pascal Stang - Copyright (C) 2001-2002
7 // Created : 9/23/2001
8 // Revised : 9/23/2001
9 // Version : 1.0
10 // Target MCU : any
11 // Editor Tabs : 4
12 //
13 // This code is distributed under the GNU Public License
14 // which can be found at http://www.gnu.org/licenses/gpl.txt
15 //
16 //*****************************************************************************
17  
18 #include "buffer.h"
19 #include "global.h"
20 #include "avr/io.h"
21  
22 #ifndef CRITICAL_SECTION_START
23 #define CRITICAL_SECTION_START unsigned char _sreg = SREG; cli()
24 #define CRITICAL_SECTION_END SREG = _sreg
25 #endif
26  
27 // global variables
28  
29 // initialization
30  
31 void bufferInit(cBuffer* buffer, unsigned char *start, unsigned short size)
32 {
33 // begin critical section
34 CRITICAL_SECTION_START;
35 // set start pointer of the buffer
36 buffer->dataptr = start;
37 buffer->size = size;
38 // initialize index and length
39 buffer->dataindex = 0;
40 buffer->datalength = 0;
41 // end critical section
42 CRITICAL_SECTION_END;
43 }
44  
45 // access routines
46 unsigned char bufferGetFromFront(cBuffer* buffer)
47 {
48 unsigned char data = 0;
49 // begin critical section
50 CRITICAL_SECTION_START;
51 // check to see if there's data in the buffer
52 if(buffer->datalength)
53 {
54 // get the first character from buffer
55 data = buffer->dataptr[buffer->dataindex];
56 // move index down and decrement length
57 buffer->dataindex++;
58 if(buffer->dataindex >= buffer->size)
59 {
60 buffer->dataindex -= buffer->size;
61 }
62 buffer->datalength--;
63 }
64 // end critical section
65 CRITICAL_SECTION_END;
66 // return
67 return data;
68 }
69  
70 void bufferDumpFromFront(cBuffer* buffer, unsigned short numbytes)
71 {
72 // begin critical section
73 CRITICAL_SECTION_START;
74 // dump numbytes from the front of the buffer
75 // are we dumping less than the entire buffer?
76 if(numbytes < buffer->datalength)
77 {
78 // move index down by numbytes and decrement length by numbytes
79 buffer->dataindex += numbytes;
80 if(buffer->dataindex >= buffer->size)
81 {
82 buffer->dataindex -= buffer->size;
83 }
84 buffer->datalength -= numbytes;
85 }
86 else
87 {
88 // flush the whole buffer
89 buffer->datalength = 0;
90 }
91 // end critical section
92 CRITICAL_SECTION_END;
93 }
94  
95 unsigned char bufferGetAtIndex(cBuffer* buffer, unsigned short index)
96 {
97 // begin critical section
98 CRITICAL_SECTION_START;
99 // return character at index in buffer
100 unsigned char data = buffer->dataptr[(buffer->dataindex+index)%(buffer->size)];
101 // end critical section
102 CRITICAL_SECTION_END;
103 return data;
104 }
105  
106 unsigned char bufferAddToEnd(cBuffer* buffer, unsigned char data)
107 {
108 // begin critical section
109 CRITICAL_SECTION_START;
110 // make sure the buffer has room
111 if(buffer->datalength < buffer->size)
112 {
113 // save data byte at end of buffer
114 buffer->dataptr[(buffer->dataindex + buffer->datalength) % buffer->size] = data;
115 // increment the length
116 buffer->datalength++;
117 // end critical section
118 CRITICAL_SECTION_END;
119 // return success
120 return -1;
121 }
122 // end critical section
123 CRITICAL_SECTION_END;
124 // return failure
125 return 0;
126 }
127  
128 unsigned short bufferIsNotFull(cBuffer* buffer)
129 {
130 // begin critical section
131 CRITICAL_SECTION_START;
132 // check to see if the buffer has room
133 // return true if there is room
134 unsigned short bytesleft = (buffer->size - buffer->datalength);
135 // end critical section
136 CRITICAL_SECTION_END;
137 return bytesleft;
138 }
139  
140 void bufferFlush(cBuffer* buffer)
141 {
142 // begin critical section
143 CRITICAL_SECTION_START;
144 // flush contents of the buffer
145 buffer->datalength = 0;
146 // end critical section
147 CRITICAL_SECTION_END;
148 }
149