Fork of https://developer.mbed.org/users/bscott/code/STM32_USBDevice/

Fork of STM32_USBDevice by Bradley Scott

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CircBuffer.h Source File

CircBuffer.h

00001 /* Copyright (c) 2010-2011 mbed.org, MIT License
00002 *
00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004 * and associated documentation files (the "Software"), to deal in the Software without
00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00007 * Software is furnished to do so, subject to the following conditions:
00008 *
00009 * The above copyright notice and this permission notice shall be included in all copies or
00010 * substantial portions of the Software.
00011 *
00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 */
00018 
00019 #ifndef CIRCBUFFER_H
00020 #define CIRCBUFFER_H
00021 
00022 template <class T, int Size>
00023 class CircBuffer {
00024 public:
00025     CircBuffer():write(0), read(0){}
00026     bool isFull() {
00027         return ((write + 1) % size == read);
00028     };
00029 
00030     bool isEmpty() {
00031         return (read == write);
00032     };
00033 
00034     void queue(T k) {
00035         if (isFull()) {
00036             read++;
00037             read %= size;
00038         }
00039         buf[write++] = k;
00040         write %= size;
00041     }
00042 
00043     uint16_t available() {
00044         return (write >= read) ? write - read : size - read + write;
00045     };
00046 
00047     bool dequeue(T * c) {
00048         bool empty = isEmpty();
00049         if (!empty) {
00050             *c = buf[read++];
00051             read %= size;
00052         }
00053         return(!empty);
00054     };
00055 
00056     void flush()
00057     {
00058         write = 0;
00059         read = 0;
00060     }
00061 
00062     // Queue a block of data of blockSize items
00063     void queue(const T *block, uint16_t blockSize)
00064     {
00065         if (blockSize >= size)
00066         {
00067             // Block is too big to fit in buffer, take the last size-1 items
00068             block = &block[blockSize - (size-1)];
00069             blockSize = size-1;
00070         }
00071 
00072         if (write + blockSize > size)
00073         {
00074             // Need to wrap around
00075             std::memcpy(&buf[write], block, sizeof(T)*(size-write));
00076             std::memcpy(buf, &block[size-write], sizeof(T)*(blockSize - (size-write)));
00077         }
00078         else
00079         {
00080             std::memcpy(&buf[write], block, sizeof(T)*blockSize);
00081         }
00082 
00083         // Update write position
00084         uint16_t wasFree = available() - size - 1;
00085         write = write + blockSize;
00086         write %= size;
00087         if (wasFree < blockSize)
00088         {
00089             // Update read position as well
00090             read = write + 1;
00091             read %= size;
00092         }
00093     }
00094 
00095     // Dequeue a block of data of at most blockSize items, writing them into block
00096     // Returns the number of items dequeued
00097     uint16_t dequeue(T *block, uint16_t blockSize)
00098     {
00099         if (isEmpty())
00100         {
00101             return 0;
00102         }
00103 
00104         uint16_t isAvailable = available();
00105         if (isAvailable < blockSize)
00106         {
00107             // Only return what we have
00108             blockSize = isAvailable;
00109         }
00110 
00111         if (read + blockSize > size)
00112         {
00113             // Need to wrap around
00114             std::memcpy(block, &buf[read], sizeof(T)*(size-read));
00115             std::memcpy(&block[size-read], buf, sizeof(T)*(blockSize - (size-read)));
00116         }
00117         else
00118         {
00119             std::memcpy(block, &buf[read], sizeof(T)*blockSize);
00120         }
00121 
00122         // Update read position
00123         read = read + blockSize;
00124         read %= size;
00125 
00126         return blockSize;
00127     }
00128 
00129 private:
00130     volatile uint16_t write;
00131     volatile uint16_t read;
00132     static const int size = Size+1;  //a modern optimizer should be able to remove this so it uses no ram.
00133     T buf[Size+1];
00134 };
00135 
00136 #endif