Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
SNIC/CBuffer.h@0:edaa24c1f5cd, 2015-02-10 (annotated)
- Committer:
 - komoritan
 - Date:
 - Tue Feb 10 12:26:31 2015 +0000
 - Revision:
 - 0:edaa24c1f5cd
 
Debug Code
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| komoritan | 0:edaa24c1f5cd | 1 | /* Copyright (C) 2012 mbed.org, MIT License | 
| komoritan | 0:edaa24c1f5cd | 2 | * | 
| komoritan | 0:edaa24c1f5cd | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software | 
| komoritan | 0:edaa24c1f5cd | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, | 
| komoritan | 0:edaa24c1f5cd | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, | 
| komoritan | 0:edaa24c1f5cd | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is | 
| komoritan | 0:edaa24c1f5cd | 7 | * furnished to do so, subject to the following conditions: | 
| komoritan | 0:edaa24c1f5cd | 8 | * | 
| komoritan | 0:edaa24c1f5cd | 9 | * The above copyright notice and this permission notice shall be included in all copies or | 
| komoritan | 0:edaa24c1f5cd | 10 | * substantial portions of the Software. | 
| komoritan | 0:edaa24c1f5cd | 11 | * | 
| komoritan | 0:edaa24c1f5cd | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING | 
| komoritan | 0:edaa24c1f5cd | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | 
| komoritan | 0:edaa24c1f5cd | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | 
| komoritan | 0:edaa24c1f5cd | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
| komoritan | 0:edaa24c1f5cd | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | 
| komoritan | 0:edaa24c1f5cd | 17 | */ | 
| komoritan | 0:edaa24c1f5cd | 18 | |
| komoritan | 0:edaa24c1f5cd | 19 | #ifndef CIRCBUFFER_H_ | 
| komoritan | 0:edaa24c1f5cd | 20 | #define CIRCBUFFER_H_ | 
| komoritan | 0:edaa24c1f5cd | 21 | |
| komoritan | 0:edaa24c1f5cd | 22 | template <class T> | 
| komoritan | 0:edaa24c1f5cd | 23 | class CircBuffer { | 
| komoritan | 0:edaa24c1f5cd | 24 | public: | 
| komoritan | 0:edaa24c1f5cd | 25 | CircBuffer(int length) { | 
| komoritan | 0:edaa24c1f5cd | 26 | write = 0; | 
| komoritan | 0:edaa24c1f5cd | 27 | read = 0; | 
| komoritan | 0:edaa24c1f5cd | 28 | size = length + 1; | 
| komoritan | 0:edaa24c1f5cd | 29 | buf = (T *)malloc(size * sizeof(T)); | 
| komoritan | 0:edaa24c1f5cd | 30 | }; | 
| komoritan | 0:edaa24c1f5cd | 31 | |
| komoritan | 0:edaa24c1f5cd | 32 | bool isFull() { | 
| komoritan | 0:edaa24c1f5cd | 33 | return (((write + 1) % size) == read); | 
| komoritan | 0:edaa24c1f5cd | 34 | }; | 
| komoritan | 0:edaa24c1f5cd | 35 | |
| komoritan | 0:edaa24c1f5cd | 36 | bool isEmpty() { | 
| komoritan | 0:edaa24c1f5cd | 37 | return (read == write); | 
| komoritan | 0:edaa24c1f5cd | 38 | }; | 
| komoritan | 0:edaa24c1f5cd | 39 | |
| komoritan | 0:edaa24c1f5cd | 40 | void queue(T k) { | 
| komoritan | 0:edaa24c1f5cd | 41 | |
| komoritan | 0:edaa24c1f5cd | 42 | if (isFull()) { | 
| komoritan | 0:edaa24c1f5cd | 43 | read++; | 
| komoritan | 0:edaa24c1f5cd | 44 | read %= size; | 
| komoritan | 0:edaa24c1f5cd | 45 | } | 
| komoritan | 0:edaa24c1f5cd | 46 | buf[write++] = k; | 
| komoritan | 0:edaa24c1f5cd | 47 | write %= size; | 
| komoritan | 0:edaa24c1f5cd | 48 | } | 
| komoritan | 0:edaa24c1f5cd | 49 | |
| komoritan | 0:edaa24c1f5cd | 50 | void flush() { | 
| komoritan | 0:edaa24c1f5cd | 51 | read = 0; | 
| komoritan | 0:edaa24c1f5cd | 52 | write = 0; | 
| komoritan | 0:edaa24c1f5cd | 53 | } | 
| komoritan | 0:edaa24c1f5cd | 54 | |
| komoritan | 0:edaa24c1f5cd | 55 | |
| komoritan | 0:edaa24c1f5cd | 56 | uint32_t available() { | 
| komoritan | 0:edaa24c1f5cd | 57 | return (write >= read) ? write - read : size - read + write; | 
| komoritan | 0:edaa24c1f5cd | 58 | }; | 
| komoritan | 0:edaa24c1f5cd | 59 | |
| komoritan | 0:edaa24c1f5cd | 60 | bool dequeue(T * c) { | 
| komoritan | 0:edaa24c1f5cd | 61 | bool empty = isEmpty(); | 
| komoritan | 0:edaa24c1f5cd | 62 | if (!empty) { | 
| komoritan | 0:edaa24c1f5cd | 63 | *c = buf[read++]; | 
| komoritan | 0:edaa24c1f5cd | 64 | read %= size; | 
| komoritan | 0:edaa24c1f5cd | 65 | } | 
| komoritan | 0:edaa24c1f5cd | 66 | return(!empty); | 
| komoritan | 0:edaa24c1f5cd | 67 | }; | 
| komoritan | 0:edaa24c1f5cd | 68 | |
| komoritan | 0:edaa24c1f5cd | 69 | private: | 
| komoritan | 0:edaa24c1f5cd | 70 | volatile uint32_t write; | 
| komoritan | 0:edaa24c1f5cd | 71 | volatile uint32_t read; | 
| komoritan | 0:edaa24c1f5cd | 72 | uint32_t size; | 
| komoritan | 0:edaa24c1f5cd | 73 | T * buf; | 
| komoritan | 0:edaa24c1f5cd | 74 | }; | 
| komoritan | 0:edaa24c1f5cd | 75 | |
| komoritan | 0:edaa24c1f5cd | 76 | #endif |