Library for Bluetooth Low Energy Module ble 4.0 HM-11

Embed: (wiki syntax)

« Back to documentation index

Buffer< T > Class Template Reference

Buffer< T > Class Template Reference

A templated software ring buffer. More...

#include <Buffer.h>

Public Member Functions

 Buffer (uint32_t size=0x100)
 Create a Buffer and allocate memory for it.
uint32_t getSize ()
 Get the size of the ring buffer.
 ~Buffer ()
 Destry a Buffer and release it's allocated memory.
void put (T data)
 Add a data element into the buffer.
get (void)
 Remove a data element from the buffer.
T * head (void)
 Get the address to the head of the buffer.
void clear (void)
 Reset the buffer to 0.
uint32_t available (void)
 Determine if anything is readable in the buffer.
Bufferoperator= (T data)
 Overloaded operator for writing to the buffer.
 operator int (void)
 Overloaded operator for reading from the buffer.

Detailed Description

template<typename T>
class Buffer< T >

A templated software ring buffer.

Example:

  #include "mbed.h"
  #include "Buffer.h"

  Buffer <char>  buf;

  int main()
  {
      buf = 'a';
      buf.put('b');
      char *head = buf.head();
      puts(head);

      char whats_in_there[2] = {0};
      int pos = 0;

      while(buf.available())
      {   
          whats_in_there[pos++] = buf;
      }
      printf("%c %c\n", whats_in_there[0], whats_in_there[1]);
      buf.clear();
      error("done\n\n\n");
  }

Definition at line 61 of file Buffer.h.


Constructor & Destructor Documentation

Buffer ( uint32_t  size = 0x100 )

Create a Buffer and allocate memory for it.

Parameters:
sizeThe size of the buffer

Definition at line 27 of file Buffer.cpp.

~Buffer (  )

Destry a Buffer and release it's allocated memory.

Definition at line 37 of file Buffer.cpp.


Member Function Documentation

uint32_t available ( void   )

Determine if anything is readable in the buffer.

Returns:
1 if something can be read, 0 otherwise

Definition at line 157 of file Buffer.h.

void clear ( void   )

Reset the buffer to 0.

Useful if using head() to parse packeted data

Definition at line 51 of file Buffer.cpp.

T get ( void   )

Remove a data element from the buffer.

Returns:
Pull the oldest element from the buffer

Definition at line 140 of file Buffer.h.

uint32_t getSize (  )

Get the size of the ring buffer.

Returns:
the size of the ring buffer

Definition at line 45 of file Buffer.cpp.

T * head ( void   )

Get the address to the head of the buffer.

Returns:
The address of element 0 in the buffer

Definition at line 149 of file Buffer.h.

operator int ( void   )

Overloaded operator for reading from the buffer.

Returns:
Pull the oldest element from the buffer

Definition at line 121 of file Buffer.h.

Buffer& operator= ( data )

Overloaded operator for writing to the buffer.

Parameters:
dataSomething to put in the buffer
Returns:

Definition at line 112 of file Buffer.h.

void put ( data )

Add a data element into the buffer.

Parameters:
dataSomething to add to the buffer

Definition at line 131 of file Buffer.h.