1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-12-13 20:48:42 +01:00
LibrePilot/ground/openpilotgcs/src/plugins/gpsdisplay/buffer.cpp

131 lines
4.1 KiB
C++
Raw Normal View History

2013-04-05 22:46:56 +02:00
/**
******************************************************************************
*
* @file buffer.c
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @brief see below
* As with all modules only the initialize function is exposed all other
* interactions with the module take place through the event queue and
2013-04-05 22:46:56 +02:00
* objects.
* @see The GNU Public License (GPL) Version 3
*
*****************************************************************************/
/*
* This program 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.
*
* This program 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.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*! \file buffer.c \brief Multipurpose byte buffer structure and methods. */
// *****************************************************************************
2013-04-05 22:46:56 +02:00
//
// File Name : 'buffer.c'
// Title : Multipurpose byte buffer structure and methods
// Author : Pascal Stang - Copyright (C) 2001-2002
// Created : 9/23/2001
// Revised : 9/23/2001
// Version : 1.0
// Target MCU : any
// Editor Tabs : 4
//
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
2013-04-05 22:46:56 +02:00
//
// *****************************************************************************
2013-04-05 22:46:56 +02:00
#include "buffer.h"
// global variables
// initialization
void bufferInit(cBuffer *buffer, unsigned char *start, unsigned short size)
2013-04-05 22:46:56 +02:00
{
// set start pointer of the buffer
buffer->dataptr = start;
buffer->size = size;
// initialize index and length
buffer->dataindex = 0;
buffer->datalength = 0;
2013-04-05 22:46:56 +02:00
}
// access routines
unsigned char bufferGetFromFront(cBuffer *buffer)
2013-04-05 22:46:56 +02:00
{
unsigned char data = 0;
2013-04-05 22:46:56 +02:00
// check to see if there's data in the buffer
if (buffer->datalength) {
// get the first character from buffer
data = buffer->dataptr[buffer->dataindex];
// move index down and decrement length
buffer->dataindex++;
if (buffer->dataindex >= buffer->size) {
buffer->dataindex %= buffer->size;
}
buffer->datalength--;
}
// return
return data;
2013-04-05 22:46:56 +02:00
}
void bufferDumpFromFront(cBuffer *buffer, unsigned short numbytes)
2013-04-05 22:46:56 +02:00
{
// dump numbytes from the front of the buffer
// are we dumping less than the entire buffer?
if (numbytes < buffer->datalength) {
// move index down by numbytes and decrement length by numbytes
buffer->dataindex += numbytes;
if (buffer->dataindex >= buffer->size) {
buffer->dataindex %= buffer->size;
}
buffer->datalength -= numbytes;
} else {
// flush the whole buffer
buffer->datalength = 0;
}
2013-04-05 22:46:56 +02:00
}
unsigned char bufferGetAtIndex(cBuffer *buffer, unsigned short index)
2013-04-05 22:46:56 +02:00
{
// return character at index in buffer
return buffer->dataptr[(buffer->dataindex + index) % (buffer->size)];
2013-04-05 22:46:56 +02:00
}
unsigned char bufferAddToEnd(cBuffer *buffer, unsigned char data)
2013-04-05 22:46:56 +02:00
{
// make sure the buffer has room
if (buffer->datalength < buffer->size) {
// save data byte at end of buffer
buffer->dataptr[(buffer->dataindex + buffer->datalength) % buffer->size] = data;
// increment the length
buffer->datalength++;
// return success
return -1;
} else { return 0; }
2013-04-05 22:46:56 +02:00
}
unsigned char bufferIsNotFull(cBuffer *buffer)
2013-04-05 22:46:56 +02:00
{
// check to see if the buffer has room
// return true if there is room
return buffer->datalength < buffer->size;
2013-04-05 22:46:56 +02:00
}
void bufferFlush(cBuffer *buffer)
2013-04-05 22:46:56 +02:00
{
// flush contents of the buffer
buffer->datalength = 0;
2013-04-05 22:46:56 +02:00
}