Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.27 KB | None | 0 0
  1. #ifndef CIRCULAR_BUFFER
  2. #define CIRCULAR_BUFFER
  3.  
  4. #include <avr/io.h>
  5. #include <util/delay.h>
  6. #include <avr/interrupt.h>
  7.  
  8. #include "USART.h"
  9.  
  10. #define CIRCULAR_SINGLE_BUFFER_SIZE 50
  11.  
  12. typedef struct {
  13.     char buffer[CIRCULAR_SINGLE_BUFFER_SIZE];
  14.     short int last_readed_byte;
  15.     short int last_saved_byte;
  16.     } circular_buffer;
  17.  
  18. /* Put data to circular buffer. */
  19. void put_data_circ_buffer(char _data, circular_buffer *buffer_struct)
  20.     {
  21.        
  22.     buffer_struct->last_saved_byte++;
  23.    
  24.     /* Checks that last saved haven't catched up last read pointer */
  25.     if ( buffer_struct->last_readed_byte == buffer_struct->last_saved_byte )
  26.         {
  27.         /* If it has, just increment last read by one (overwrites oldest data). */
  28.         buffer_struct->last_readed_byte++;
  29.         }
  30.        
  31.     /* Checks that incremented read pointer isnt over buffer array index range */
  32.     if (buffer_struct->last_readed_byte > CIRCULAR_SINGLE_BUFFER_SIZE-1 )
  33.         {
  34.         /* If it is, return to index 0 */
  35.         buffer_struct->last_readed_byte = 0;
  36.         }
  37.  
  38.     /* Check that last saved byte isnt going over buffers array index range */
  39.     if (buffer_struct->last_saved_byte > CIRCULAR_SINGLE_BUFFER_SIZE-1 )
  40.         {
  41.         /* If it is, return to index 0 */
  42.         buffer_struct->last_saved_byte = 0;
  43.         }
  44.        
  45.     /* In situation last_saved_byte is returned to index 0, is possible that
  46.      * last readed pointer is in index 0 too, then we have to move it */
  47.     if ( buffer_struct->last_readed_byte == buffer_struct->last_saved_byte )
  48.         {
  49.         buffer_struct->last_readed_byte++;
  50.         }
  51.        
  52.     /* Try gues what this do?*/
  53.     buffer_struct->buffer[buffer_struct->last_saved_byte] = _data;
  54.     }
  55.  
  56. /* Reads oldest data from buffer, check before read with is_buffer_clean
  57.  * that there is something to return, otherwise function just returns same
  58.  * data again and again. */
  59. char get_data_circ_buffer (circular_buffer *buffer_struct)
  60.     {
  61.     if ( buffer_struct->last_readed_byte+1 != buffer_struct->last_saved_byte )
  62.         {
  63.         buffer_struct->last_readed_byte++;
  64.         }
  65.        
  66.     return buffer_struct->buffer[buffer_struct->last_readed_byte];
  67.     }
  68.  
  69. /* Checks out is something to read from buffer */
  70. short int is_buffer_empty (circular_buffer *buffer_struct)
  71.     {
  72.     if ( buffer_struct->last_readed_byte+1 == buffer_struct->last_saved_byte )
  73.         {
  74.         usart_send('a');
  75.         return 1;
  76.         }
  77.     else
  78.         {
  79.         return 0;
  80.         }
  81.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement