Mbed Cloud example program for workshop in W27 2018.

Dependencies:   MMA7660 LM75B

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MyBuffer.h Source File

MyBuffer.h

00001 
00002 /**
00003  * @file    Buffer.h
00004  * @brief   Software Buffer - Templated Ring Buffer for most data types
00005  * @author  sam grove
00006  * @version 1.0
00007  * @see     
00008  *
00009  * Copyright (c) 2013
00010  *
00011  * Licensed under the Apache License, Version 2.0 (the "License");
00012  * you may not use this file except in compliance with the License.
00013  * You may obtain a copy of the License at
00014  *
00015  *     http://www.apache.org/licenses/LICENSE-2.0
00016  *
00017  * Unless required by applicable law or agreed to in writing, software
00018  * distributed under the License is distributed on an "AS IS" BASIS,
00019  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020  * See the License for the specific language governing permissions and
00021  * limitations under the License.
00022  */
00023  
00024 #ifndef MYBUFFER_H
00025 #define MYBUFFER_H
00026 
00027 #include <stdint.h>
00028 #include <string.h>
00029 
00030 /** A templated software ring buffer
00031  *
00032  * Example:
00033  * @code
00034  *  #include "mbed.h"
00035  *  #include "MyBuffer.h"
00036  *
00037  *  MyBuffer <char> buf;
00038  *
00039  *  int main()
00040  *  {
00041  *      buf = 'a';
00042  *      buf.put('b');
00043  *      char *head = buf.head();
00044  *      puts(head);
00045  *
00046  *      char whats_in_there[2] = {0};
00047  *      int pos = 0;
00048  *
00049  *      while(buf.available())
00050  *      {   
00051  *          whats_in_there[pos++] = buf;
00052  *      }
00053  *      printf("%c %c\n", whats_in_there[0], whats_in_there[1]);
00054  *      buf.clear();
00055  *      error("done\n\n\n");
00056  *  }
00057  * @endcode
00058  */
00059 
00060 template <typename T>
00061 class MyBuffer
00062 {
00063 private:
00064     T   *_buf;
00065     volatile uint32_t   _wloc;
00066     volatile uint32_t   _rloc;
00067     uint32_t            _size;
00068 
00069 public:
00070     /** Create a Buffer and allocate memory for it
00071      *  @param size The size of the buffer
00072      */
00073     MyBuffer(uint32_t size = 0x100);
00074     
00075     /** Get the size of the ring buffer
00076      * @return the size of the ring buffer
00077      */
00078      uint32_t getSize();
00079      uint32_t getNbAvailable();
00080     
00081     /** Destry a Buffer and release it's allocated memory
00082      */
00083     ~MyBuffer();
00084     
00085     /** Add a data element into the buffer
00086      *  @param data Something to add to the buffer
00087      */
00088     void put(T data);
00089     
00090     /** Remove a data element from the buffer
00091      *  @return Pull the oldest element from the buffer
00092      */
00093     T get(void);
00094     
00095     /** Get the address to the head of the buffer
00096      *  @return The address of element 0 in the buffer
00097      */
00098     T *head(void);
00099     
00100     /** Reset the buffer to 0. Useful if using head() to parse packeted data
00101      */
00102     void clear(void);
00103     
00104     /** Determine if anything is readable in the buffer
00105      *  @return 1 if something can be read, 0 otherwise
00106      */
00107     uint32_t available(void);
00108     
00109     /** Overloaded operator for writing to the buffer
00110      *  @param data Something to put in the buffer
00111      *  @return
00112      */
00113     MyBuffer &operator= (T data)
00114     {
00115         put(data);
00116         return *this;
00117     }
00118     
00119     /** Overloaded operator for reading from the buffer
00120      *  @return Pull the oldest element from the buffer 
00121      */  
00122     operator int(void)
00123     {
00124         return get();
00125     }
00126     
00127      uint32_t peek(char c);
00128     
00129 };
00130 
00131 template <class T>
00132 inline void MyBuffer<T>::put(T data)
00133 {
00134     _buf[_wloc++] = data;
00135     _wloc %= (_size-1);
00136     
00137     return;
00138 }
00139 
00140 template <class T>
00141 inline T MyBuffer<T>::get(void)
00142 {
00143     T data_pos = _buf[_rloc++];
00144     _rloc %= (_size-1);
00145     
00146     return data_pos;
00147 }
00148 
00149 template <class T>
00150 inline T *MyBuffer<T>::head(void)
00151 {
00152     T *data_pos = &_buf[0];
00153     
00154     return data_pos;
00155 }
00156 
00157 template <class T>
00158 inline uint32_t MyBuffer<T>::available(void)
00159 {
00160     return (_wloc == _rloc) ? 0 : 1;
00161     //return 1;
00162 }
00163 
00164 #endif
00165