📄 buffer_8c-source.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>Procyon AVRlib: buffer.c Source File</title><link href="dox.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.4.2 --><div class="qindex"><a class="qindex" href="main.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related Pages</a></div><h1>buffer.c</h1><a href="buffer_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file buffer.c \brief Multipurpose byte buffer structure and methods. */</span>00002 <span class="comment">//*****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name : 'buffer.c'</span>00005 <span class="comment">// Title : Multipurpose byte buffer structure and methods</span>00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2001-2002</span>00007 <span class="comment">// Created : 9/23/2001</span>00008 <span class="comment">// Revised : 9/23/2001</span>00009 <span class="comment">// Version : 1.0</span>00010 <span class="comment">// Target MCU : any</span>00011 <span class="comment">// Editor Tabs : 4</span>00012 <span class="comment">//</span>00013 <span class="comment">// This code is distributed under the GNU Public License</span>00014 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span>00015 <span class="comment">//</span>00016 <span class="comment">//*****************************************************************************</span>00017 00018 <span class="preprocessor">#include "<a class="code" href="buffer_8h.html">buffer.h</a>"</span>00019 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>00020 <span class="preprocessor">#include "avr/io.h"</span>00021 00022 <span class="preprocessor">#ifndef CRITICAL_SECTION_START</span>00023 <span class="preprocessor"></span><span class="preprocessor">#define CRITICAL_SECTION_START unsigned char _sreg = SREG; cli()</span>00024 <span class="preprocessor"></span><span class="preprocessor">#define CRITICAL_SECTION_END SREG = _sreg</span>00025 <span class="preprocessor"></span><span class="preprocessor">#endif</span>00026 <span class="preprocessor"></span>00027 <span class="comment">// global variables</span>00028 00029 <span class="comment">// initialization</span>00030 <a name="l00031"></a><a class="code" href="group__buffer.html#ga1">00031</a> <span class="keywordtype">void</span> <a class="code" href="group__buffer.html#ga1">bufferInit</a>(<a class="code" href="structstruct__cBuffer.html">cBuffer</a>* buffer, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *start, <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> size)00032 {00033 <span class="comment">// begin critical section</span>00034 CRITICAL_SECTION_START;00035 <span class="comment">// set start pointer of the buffer</span>00036 buffer-><a class="code" href="structstruct__cBuffer.html#o0">dataptr</a> = start;00037 buffer-><a class="code" href="structstruct__cBuffer.html#o1">size</a> = size;00038 <span class="comment">// initialize index and length</span>00039 buffer-><a class="code" href="structstruct__cBuffer.html#o3">dataindex</a> = 0;00040 buffer-><a class="code" href="structstruct__cBuffer.html#o2">datalength</a> = 0;00041 <span class="comment">// end critical section</span>00042 CRITICAL_SECTION_END;00043 }00044 00045 <span class="comment">// access routines</span><a name="l00046"></a><a class="code" href="group__buffer.html#ga2">00046</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> <a class="code" href="group__buffer.html#ga2">bufferGetFromFront</a>(<a class="code" href="structstruct__cBuffer.html">cBuffer</a>* buffer)00047 {00048 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> data = 0;00049 <span class="comment">// begin critical section</span>00050 CRITICAL_SECTION_START;00051 <span class="comment">// check to see if there's data in the buffer</span>00052 <span class="keywordflow">if</span>(buffer-><a class="code" href="structstruct__cBuffer.html#o2">datalength</a>)00053 {00054 <span class="comment">// get the first character from buffer</span>00055 data = buffer-><a class="code" href="structstruct__cBuffer.html#o0">dataptr</a>[buffer-><a class="code" href="structstruct__cBuffer.html#o3">dataindex</a>];00056 <span class="comment">// move index down and decrement length</span>00057 buffer-><a class="code" href="structstruct__cBuffer.html#o3">dataindex</a>++;00058 <span class="keywordflow">if</span>(buffer-><a class="code" href="structstruct__cBuffer.html#o3">dataindex</a> >= buffer-><a class="code" href="structstruct__cBuffer.html#o1">size</a>)00059 {00060 buffer-><a class="code" href="structstruct__cBuffer.html#o3">dataindex</a> -= buffer-><a class="code" href="structstruct__cBuffer.html#o1">size</a>;00061 }00062 buffer-><a class="code" href="structstruct__cBuffer.html#o2">datalength</a>--;00063 }00064 <span class="comment">// end critical section</span>00065 CRITICAL_SECTION_END;00066 <span class="comment">// return</span>00067 <span class="keywordflow">return</span> data;00068 }00069 <a name="l00070"></a><a class="code" href="group__buffer.html#ga3">00070</a> <span class="keywordtype">void</span> <a class="code" href="group__buffer.html#ga3">bufferDumpFromFront</a>(<a class="code" href="structstruct__cBuffer.html">cBuffer</a>* buffer, <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> numbytes)00071 {00072 <span class="comment">// begin critical section</span>00073 CRITICAL_SECTION_START;00074 <span class="comment">// dump numbytes from the front of the buffer</span>00075 <span class="comment">// are we dumping less than the entire buffer?</span>00076 <span class="keywordflow">if</span>(numbytes < buffer-><a class="code" href="structstruct__cBuffer.html#o2">datalength</a>)00077 {00078 <span class="comment">// move index down by numbytes and decrement length by numbytes</span>00079 buffer-><a class="code" href="structstruct__cBuffer.html#o3">dataindex</a> += numbytes;00080 <span class="keywordflow">if</span>(buffer-><a class="code" href="structstruct__cBuffer.html#o3">dataindex</a> >= buffer-><a class="code" href="structstruct__cBuffer.html#o1">size</a>)00081 {00082 buffer-><a class="code" href="structstruct__cBuffer.html#o3">dataindex</a> -= buffer-><a class="code" href="structstruct__cBuffer.html#o1">size</a>;00083 }00084 buffer-><a class="code" href="structstruct__cBuffer.html#o2">datalength</a> -= numbytes;00085 }00086 <span class="keywordflow">else</span>00087 {00088 <span class="comment">// flush the whole buffer</span>00089 buffer-><a class="code" href="structstruct__cBuffer.html#o2">datalength</a> = 0;00090 }00091 <span class="comment">// end critical section</span>00092 CRITICAL_SECTION_END;00093 }00094 <a name="l00095"></a><a class="code" href="group__buffer.html#ga4">00095</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> <a class="code" href="group__buffer.html#ga4">bufferGetAtIndex</a>(<a class="code" href="structstruct__cBuffer.html">cBuffer</a>* buffer, <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> index)00096 {00097 <span class="comment">// begin critical section</span>00098 CRITICAL_SECTION_START;00099 <span class="comment">// return character at index in buffer</span>00100 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> data = buffer-><a class="code" href="structstruct__cBuffer.html#o0">dataptr</a>[(buffer-><a class="code" href="structstruct__cBuffer.html#o3">dataindex</a>+index)%(buffer-><a class="code" href="structstruct__cBuffer.html#o1">size</a>)];00101 <span class="comment">// end critical section</span>00102 CRITICAL_SECTION_END;00103 <span class="keywordflow">return</span> data;00104 }00105 <a name="l00106"></a><a class="code" href="group__buffer.html#ga5">00106</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> <a class="code" href="group__buffer.html#ga5">bufferAddToEnd</a>(<a class="code" href="structstruct__cBuffer.html">cBuffer</a>* buffer, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> data)00107 {00108 <span class="comment">// begin critical section</span>00109 CRITICAL_SECTION_START;00110 <span class="comment">// make sure the buffer has room</span>00111 <span class="keywordflow">if</span>(buffer-><a class="code" href="structstruct__cBuffer.html#o2">datalength</a> < buffer-><a class="code" href="structstruct__cBuffer.html#o1">size</a>)00112 {00113 <span class="comment">// save data byte at end of buffer</span>00114 buffer-><a class="code" href="structstruct__cBuffer.html#o0">dataptr</a>[(buffer-><a class="code" href="structstruct__cBuffer.html#o3">dataindex</a> + buffer-><a class="code" href="structstruct__cBuffer.html#o2">datalength</a>) % buffer-><a class="code" href="structstruct__cBuffer.html#o1">size</a>] = data;00115 <span class="comment">// increment the length</span>00116 buffer-><a class="code" href="structstruct__cBuffer.html#o2">datalength</a>++;00117 <span class="comment">// end critical section</span>00118 CRITICAL_SECTION_END;00119 <span class="comment">// return success</span>00120 <span class="keywordflow">return</span> -1;00121 }00122 <span class="comment">// end critical section</span>00123 CRITICAL_SECTION_END;00124 <span class="comment">// return failure</span>00125 <span class="keywordflow">return</span> 0;00126 }00127 <a name="l00128"></a><a class="code" href="group__buffer.html#ga6">00128</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> <a class="code" href="group__buffer.html#ga6">bufferIsNotFull</a>(<a class="code" href="structstruct__cBuffer.html">cBuffer</a>* buffer)00129 {00130 <span class="comment">// begin critical section</span>00131 CRITICAL_SECTION_START;00132 <span class="comment">// check to see if the buffer has room</span>00133 <span class="comment">// return true if there is room</span>00134 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> bytesleft = (buffer-><a class="code" href="structstruct__cBuffer.html#o1">size</a> - buffer-><a class="code" href="structstruct__cBuffer.html#o2">datalength</a>);00135 <span class="comment">// end critical section</span>00136 CRITICAL_SECTION_END;00137 <span class="keywordflow">return</span> bytesleft;00138 }00139 <a name="l00140"></a><a class="code" href="group__buffer.html#ga7">00140</a> <span class="keywordtype">void</span> <a class="code" href="group__buffer.html#ga7">bufferFlush</a>(<a class="code" href="structstruct__cBuffer.html">cBuffer</a>* buffer)00141 {00142 <span class="comment">// begin critical section</span>00143 CRITICAL_SECTION_START;00144 <span class="comment">// flush contents of the buffer</span>00145 buffer-><a class="code" href="structstruct__cBuffer.html#o2">datalength</a> = 0;00146 <span class="comment">// end critical section</span>00147 CRITICAL_SECTION_END;00148 }00149 </pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:06 2006 for Procyon AVRlib by <a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -