66 lines
1.2 KiB
C
66 lines
1.2 KiB
C
#include "buffer.h"
|
|
// implementation for a FIFO-buffer with ints
|
|
// this very simple FIFO-buffer can only hold one int
|
|
#define FIFO_SIZE 100
|
|
|
|
// shared variables within this file
|
|
static int FIFO[FIFO_SIZE];
|
|
static unsigned int FIFO_count = 0;
|
|
|
|
// read and write pointers
|
|
unsigned int FIFO_write_p = 0;
|
|
unsigned int FIFO_read_p = 0;
|
|
|
|
/** incrementPointer
|
|
*
|
|
* add one and rotates if it overflows the buffersize
|
|
*/
|
|
unsigned int incrementPointer(unsigned int p)
|
|
{
|
|
p++;
|
|
if (p == FIFO_SIZE)
|
|
{
|
|
p = 0;
|
|
}
|
|
return p;
|
|
}
|
|
|
|
bool buffer_put(int i)
|
|
{
|
|
bool notFull = !buffer_is_full();
|
|
if (notFull)
|
|
{
|
|
FIFO[FIFO_write_p] = i;
|
|
FIFO_count++;
|
|
FIFO_write_p = incrementPointer(FIFO_write_p);
|
|
}
|
|
return notFull;
|
|
}
|
|
|
|
bool buffer_get(int *p)
|
|
{
|
|
bool notEmpty = !buffer_is_empty();
|
|
if (notEmpty)
|
|
{
|
|
*p = FIFO[FIFO_read_p];
|
|
FIFO_count--;
|
|
FIFO_read_p = incrementPointer(FIFO_read_p);
|
|
}
|
|
return notEmpty;
|
|
}
|
|
|
|
bool buffer_is_full(void)
|
|
{
|
|
return (FIFO_count >= FIFO_SIZE);
|
|
}
|
|
|
|
bool buffer_is_empty(void)
|
|
{
|
|
return (FIFO_count <= 0);
|
|
}
|
|
|
|
unsigned int number_of_elements_in_buffer()
|
|
{
|
|
return FIFO_count;
|
|
}
|