/************************************************************************/ /* Circular buffer type define */ /* This file implements a circulair buffer type */ /* Steve Vandenbussche */ /************************************************************************/ #ifndef _CIRCULAR_BUFFER #define _CIRCULAR_BUFFER #include //Define buffer width #define BUFFER_SIZE 64 //Define buffer type //#define BUFFER_TYPE char //Define index type, this type must be big enough to hold BUFFER_SIZE //#define INDEX_TYPE unsigned char //Define circular buffer structure typedef struct { char buffer[BUFFER_SIZE]; //buffer unsigned char indexIn; //Write pointer witch holds the next free location unsigned char indexOut; //Read pointer wicht holds the first location to process unsigned char count; //current number of elements in the buffer }circular_buffer; //Function prototypes int Write_one_to_buff(circular_buffer* buffPtr, char item_to_add); int Read_one_from_buff(circular_buffer* buffPtr, char* item_read); int Read_block_from_buff(circular_buffer* buffPtr, char* block_read, unsigned char lenght); /**************************************************************************** Function: int Write_one_to_buffer(circular_buffer* buffPtr, char item_to_add) Description: This function adds one element to the circular buffer Precondition: Member of type circular_buffer is declared Parameters: - Pointer to a circular_buffer - Item to add Returns: 0 on succes -1 when the buffer is full Remarks: None ***************************************************************************/ int Write_one_to_buff(circular_buffer* buffPtr, char item_to_add) { //When the buffer is not full, then add the item if((BUFFER_SIZE - buffPtr->count) > 0) { buffPtr->buffer[buffPtr->indexIn] = item_to_add; buffPtr->indexIn = (buffPtr->indexOut + 1) % BUFFER_SIZE; buffPtr->count++; } else return -1; return 0; } /**************************************************************************** Function: int Read_one_from_buffer(circular_buffer* buffPtr, char* item_read Description: This function reads one element from the circular buffer Precondition: Member of type circular_buffer is declared Parameters: - Pointer to a circular_buffer - Pointer where to hold the readed item Returns: 0 on succes -1 when the buffer is empty Remarks: None ***************************************************************************/ int Read_one_from_buff(circular_buffer* buffPtr, char* item_read) { //When the buffer is not empty read the item if(buffPtr->count > 0) { *item_read = buffPtr->buffer[buffPtr->indexOut]; buffPtr->indexOut = (buffPtr->indexOut - 1) % BUFFER_SIZE; buffPtr->count--; } else return -1; return 0; } /**************************************************************************** Function: int Read_block_from_buffer(circular_buffer* buffPtr, char* block_read, unsigned char lenght) Description: This function reads a block of elements from the buffer Precondition: Member of type circular_buffer is declared Parameters: - Pointer to a circular_buffer - Pointer where to hold the readed block - Lenght of the block Returns: 0 on succes -1 when the buffer has less elements then the lenght of the block Remarks: None ***************************************************************************/ int Read_block_from_buffer(circular_buffer* buffPtr, char* block_read, unsigned char lenght) { int i; //When the number of elements in the buffer is equal to or greater then the block lenght then read the block if(buffPtr->count >= lenght) { for(i=0; ibuffer[buffPtr->indexOut]; buffPtr->indexOut = (buffPtr->indexOut - 1) % BUFFER_SIZE; buffPtr->count--; } } else return -1; return 0; } #endif /*_CIRCULAR_BUFFER*/