VZTECH / Mbed 2 deprecated main_src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ring_buffer.cpp Source File

ring_buffer.cpp

00001 #include "ring_buffer.h"
00002 
00003 ring_buffer * ring_buffer_init( ring_buffer * rb ){
00004     if( rb == NULL ){
00005         rb = ( ring_buffer * )malloc( sizeof( ring_buffer ) );
00006         rb->begin = 0;
00007         rb->end = 0;
00008         rb->size = 0;
00009         rb->data = ( uint8_t * )malloc( sizeof( uint8_t ) * CB_BUFFER_SIZE * RING_BUFFER_SIZE );
00010         return( rb );
00011     }else return( rb );
00012 }
00013 
00014 uint8_t ring_buffer_add( ring_buffer * rb, uint8_t * data ){
00015     if( rb == NULL ) return 0x99;
00016 
00017     if( data == NULL ) return 0x98;
00018     
00019     if( rb->size >= RING_BUFFER_SIZE ) return 0x01;
00020 
00021     else{
00022         rb_xmemcpy( rb->data + ( CB_BUFFER_SIZE * rb->end ), data, CB_BUFFER_SIZE );           
00023         if( ++( rb->end ) >= RING_BUFFER_SIZE ) rb->end = 0;
00024         rb->size++;
00025     }
00026 
00027     return( 0x00 );
00028 }
00029 
00030 uint8_t * ring_buffer_get_next( ring_buffer * rb ){
00031     if( rb == NULL ) return( NULL );
00032 
00033     if( rb->size == 0 ) return( NULL );
00034 
00035     uint8_t * ret = rb->data + ( rb->begin * CB_BUFFER_SIZE );
00036     
00037     if( ++( rb->begin ) >= RING_BUFFER_SIZE ) rb->begin = 0;
00038 
00039     if( rb->size > 0 ) rb->size--;
00040     
00041     return( ret );
00042 }
00043 
00044 void rb_xmemcpy (uint8_t * dest, uint8_t * src, uint16_t size)
00045 {
00046   while ( size-- ) *dest++ = *src++;
00047 }