Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef CIRCULAR_BUFFER
- #define CIRCULAR_BUFFER
- #include <avr/io.h>
- #include <util/delay.h>
- #include <avr/interrupt.h>
- #include "USART.h"
- #define CIRCULAR_SINGLE_BUFFER_SIZE 50
- typedef struct {
- char buffer[CIRCULAR_SINGLE_BUFFER_SIZE];
- short int last_readed_byte;
- short int last_saved_byte;
- } circular_buffer;
- /* Put data to circular buffer. */
- void put_data_circ_buffer(char _data, circular_buffer *buffer_struct)
- {
- buffer_struct->last_saved_byte++;
- /* Checks that last saved haven't catched up last read pointer */
- if ( buffer_struct->last_readed_byte == buffer_struct->last_saved_byte )
- {
- /* If it has, just increment last read by one (overwrites oldest data). */
- buffer_struct->last_readed_byte++;
- }
- /* Checks that incremented read pointer isnt over buffer array index range */
- if (buffer_struct->last_readed_byte > CIRCULAR_SINGLE_BUFFER_SIZE-1 )
- {
- /* If it is, return to index 0 */
- buffer_struct->last_readed_byte = 0;
- }
- /* Check that last saved byte isnt going over buffers array index range */
- if (buffer_struct->last_saved_byte > CIRCULAR_SINGLE_BUFFER_SIZE-1 )
- {
- /* If it is, return to index 0 */
- buffer_struct->last_saved_byte = 0;
- }
- /* In situation last_saved_byte is returned to index 0, is possible that
- * last readed pointer is in index 0 too, then we have to move it */
- if ( buffer_struct->last_readed_byte == buffer_struct->last_saved_byte )
- {
- buffer_struct->last_readed_byte++;
- }
- /* Try gues what this do?*/
- buffer_struct->buffer[buffer_struct->last_saved_byte] = _data;
- }
- /* Reads oldest data from buffer, check before read with is_buffer_clean
- * that there is something to return, otherwise function just returns same
- * data again and again. */
- char get_data_circ_buffer (circular_buffer *buffer_struct)
- {
- if ( buffer_struct->last_readed_byte+1 != buffer_struct->last_saved_byte )
- {
- buffer_struct->last_readed_byte++;
- }
- return buffer_struct->buffer[buffer_struct->last_readed_byte];
- }
- /* Checks out is something to read from buffer */
- short int is_buffer_empty (circular_buffer *buffer_struct)
- {
- if ( buffer_struct->last_readed_byte+1 == buffer_struct->last_saved_byte )
- {
- usart_send('a');
- return 1;
- }
- else
- {
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement