posilani dat

Dependencies:   FatFileSystemCpp mbed PowerControl USBHostLite

Committer:
PavelKumpan
Date:
Tue May 23 18:42:14 2017 +0000
Revision:
26:5674b8978551
Parent:
7:805c16a071d0
Recreated communication protocol.

Who changed what in which revision?

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