nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nexpaq
Date:
Sat Sep 17 16:32:05 2016 +0000
Revision:
1:55a6170b404f
checking in for sharing

Who changed what in which revision?

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