lol

Dependencies:   MMA8451Q

Fork of Application by Mateusz Kowalik

Committer:
danix
Date:
Sun Jan 21 22:28:30 2018 +0000
Revision:
12:3a30cdffa27c
Parent:
10:41552d038a69
Working acelerometer and mouse

Who changed what in which revision?

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