max4146x_comp

Dependencies:   MAX14690

Committer:
sdivarci
Date:
Sun Oct 25 20:10:02 2020 +0000
Revision:
0:0061165683ee
sdivarci

Who changed what in which revision?

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