add buffer_8 code
This commit is contained in:
60
buffer_8/buffer.c
Normal file
60
buffer_8/buffer.c
Normal file
@@ -0,0 +1,60 @@
|
||||
#include "buffer.h"
|
||||
// implementation for a FIFO-buffer with ints
|
||||
// this very simple FIFO-buffer can only hold one int
|
||||
#define FIFO_SIZE 8
|
||||
|
||||
// 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);
|
||||
}
|
||||
22
buffer_8/buffer.h
Normal file
22
buffer_8/buffer.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef _HR_BroJZ_buffer_
|
||||
#define _HR_BroJZ_buffer_
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
// interface for a buffer with int's
|
||||
|
||||
// put value i in buffer if buffer is not full
|
||||
// returns true on success or false otherways
|
||||
extern bool buffer_put(int i);
|
||||
|
||||
// get value from buffer and writes it to *p if buffer not empty
|
||||
// returns true on success or false otherways
|
||||
extern bool buffer_get(int *p);
|
||||
|
||||
// returns true when buffer is full or false otherways
|
||||
extern bool buffer_is_full(void);
|
||||
|
||||
// returns true when buffer is empty or false otherways
|
||||
extern bool buffer_is_empty(void);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user