LP-56 - Better txpid option namings, fix tabs-spaces, tooltips. headers, variable...
[librepilot.git] / ground / openpilotgcs / src / plugins / gpsdisplay / buffer.cpp
blob032fec211afee4d7f2a813905f340fb9fdfcaf90
1 /**
2 ******************************************************************************
4 * @file buffer.c
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @brief see below
7 * As with all modules only the initialize function is exposed all other
8 * interactions with the module take place through the event queue and
9 * objects.
10 * @see The GNU Public License (GPL) Version 3
12 *****************************************************************************/
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 * for more details.
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 /*! \file buffer.c \brief Multipurpose byte buffer structure and methods. */
31 // *****************************************************************************
33 // File Name : 'buffer.c'
34 // Title : Multipurpose byte buffer structure and methods
35 // Author : Pascal Stang - Copyright (C) 2001-2002
36 // Created : 9/23/2001
37 // Revised : 9/23/2001
38 // Version : 1.0
39 // Target MCU : any
40 // Editor Tabs : 4
42 // This code is distributed under the GNU Public License
43 // which can be found at http://www.gnu.org/licenses/gpl.txt
45 // *****************************************************************************
47 #include "buffer.h"
49 // global variables
51 // initialization
53 void bufferInit(cBuffer *buffer, unsigned char *start, unsigned short size)
55 // set start pointer of the buffer
56 buffer->dataptr = start;
57 buffer->size = size;
58 // initialize index and length
59 buffer->dataindex = 0;
60 buffer->datalength = 0;
63 // access routines
64 unsigned char bufferGetFromFront(cBuffer *buffer)
66 unsigned char data = 0;
68 // check to see if there's data in the buffer
69 if (buffer->datalength) {
70 // get the first character from buffer
71 data = buffer->dataptr[buffer->dataindex];
72 // move index down and decrement length
73 buffer->dataindex++;
74 if (buffer->dataindex >= buffer->size) {
75 buffer->dataindex %= buffer->size;
77 buffer->datalength--;
79 // return
80 return data;
83 void bufferDumpFromFront(cBuffer *buffer, unsigned short numbytes)
85 // dump numbytes from the front of the buffer
86 // are we dumping less than the entire buffer?
87 if (numbytes < buffer->datalength) {
88 // move index down by numbytes and decrement length by numbytes
89 buffer->dataindex += numbytes;
90 if (buffer->dataindex >= buffer->size) {
91 buffer->dataindex %= buffer->size;
93 buffer->datalength -= numbytes;
94 } else {
95 // flush the whole buffer
96 buffer->datalength = 0;
100 unsigned char bufferGetAtIndex(cBuffer *buffer, unsigned short index)
102 // return character at index in buffer
103 return buffer->dataptr[(buffer->dataindex + index) % (buffer->size)];
106 unsigned char bufferAddToEnd(cBuffer *buffer, unsigned char data)
108 // make sure the buffer has room
109 if (buffer->datalength < buffer->size) {
110 // save data byte at end of buffer
111 buffer->dataptr[(buffer->dataindex + buffer->datalength) % buffer->size] = data;
112 // increment the length
113 buffer->datalength++;
114 // return success
115 return -1;
116 } else { return 0; }
119 unsigned char bufferIsNotFull(cBuffer *buffer)
121 // check to see if the buffer has room
122 // return true if there is room
123 return buffer->datalength < buffer->size;
126 void bufferFlush(cBuffer *buffer)
128 // flush contents of the buffer
129 buffer->datalength = 0;