blablabla

Dependencies:   MAG3110 MMA8451Q SLCD- TSI USBDevice mbed

Committer:
Osator
Date:
Wed Apr 16 12:20:00 2014 +0000
Revision:
0:339b7abfa147
blablabla

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Osator 0:339b7abfa147 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
Osator 0:339b7abfa147 2 *
Osator 0:339b7abfa147 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Osator 0:339b7abfa147 4 * and associated documentation files (the "Software"), to deal in the Software without
Osator 0:339b7abfa147 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
Osator 0:339b7abfa147 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Osator 0:339b7abfa147 7 * Software is furnished to do so, subject to the following conditions:
Osator 0:339b7abfa147 8 *
Osator 0:339b7abfa147 9 * The above copyright notice and this permission notice shall be included in all copies or
Osator 0:339b7abfa147 10 * substantial portions of the Software.
Osator 0:339b7abfa147 11 *
Osator 0:339b7abfa147 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Osator 0:339b7abfa147 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Osator 0:339b7abfa147 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Osator 0:339b7abfa147 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Osator 0:339b7abfa147 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Osator 0:339b7abfa147 17 */
Osator 0:339b7abfa147 18
Osator 0:339b7abfa147 19 #ifndef CIRCBUFFER_H
Osator 0:339b7abfa147 20 #define CIRCBUFFER_H
Osator 0:339b7abfa147 21
Osator 0:339b7abfa147 22 template <class T>
Osator 0:339b7abfa147 23 class CircBuffer {
Osator 0:339b7abfa147 24 public:
Osator 0:339b7abfa147 25 CircBuffer(int length) {
Osator 0:339b7abfa147 26 write = 0;
Osator 0:339b7abfa147 27 read = 0;
Osator 0:339b7abfa147 28 size = length + 1;
Osator 0:339b7abfa147 29 buf = (T *)malloc(size * sizeof(T));
Osator 0:339b7abfa147 30 };
Osator 0:339b7abfa147 31
Osator 0:339b7abfa147 32 ~CircBuffer() {
Osator 0:339b7abfa147 33 free(buf);
Osator 0:339b7abfa147 34 }
Osator 0:339b7abfa147 35
Osator 0:339b7abfa147 36 bool isFull() {
Osator 0:339b7abfa147 37 return ((write + 1) % size == read);
Osator 0:339b7abfa147 38 };
Osator 0:339b7abfa147 39
Osator 0:339b7abfa147 40 bool isEmpty() {
Osator 0:339b7abfa147 41 return (read == write);
Osator 0:339b7abfa147 42 };
Osator 0:339b7abfa147 43
Osator 0:339b7abfa147 44 void queue(T k) {
Osator 0:339b7abfa147 45 if (isFull()) {
Osator 0:339b7abfa147 46 read++;
Osator 0:339b7abfa147 47 read %= size;
Osator 0:339b7abfa147 48 }
Osator 0:339b7abfa147 49 buf[write++] = k;
Osator 0:339b7abfa147 50 write %= size;
Osator 0:339b7abfa147 51 }
Osator 0:339b7abfa147 52
Osator 0:339b7abfa147 53 uint16_t available() {
Osator 0:339b7abfa147 54 return (write >= read) ? write - read : size - read + write;
Osator 0:339b7abfa147 55 };
Osator 0:339b7abfa147 56
Osator 0:339b7abfa147 57 bool dequeue(T * c) {
Osator 0:339b7abfa147 58 bool empty = isEmpty();
Osator 0:339b7abfa147 59 if (!empty) {
Osator 0:339b7abfa147 60 *c = buf[read++];
Osator 0:339b7abfa147 61 read %= size;
Osator 0:339b7abfa147 62 }
Osator 0:339b7abfa147 63 return(!empty);
Osator 0:339b7abfa147 64 };
Osator 0:339b7abfa147 65
Osator 0:339b7abfa147 66 private:
Osator 0:339b7abfa147 67 volatile uint16_t write;
Osator 0:339b7abfa147 68 volatile uint16_t read;
Osator 0:339b7abfa147 69 uint16_t size;
Osator 0:339b7abfa147 70 T * buf;
Osator 0:339b7abfa147 71 };
Osator 0:339b7abfa147 72
Osator 0:339b7abfa147 73 #endif