SD card interface

Committer:
lharoon
Date:
Mon Oct 08 11:14:07 2012 +0000
Revision:
0:22612ae617a0
1st edition

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lharoon 0:22612ae617a0 1 #ifndef CIRCBUFFER_H
lharoon 0:22612ae617a0 2 #define CIRCBUFFER_H
lharoon 0:22612ae617a0 3
lharoon 0:22612ae617a0 4 template <class T>
lharoon 0:22612ae617a0 5 class CircBuffer {
lharoon 0:22612ae617a0 6 public:
lharoon 0:22612ae617a0 7 CircBuffer(int length) {
lharoon 0:22612ae617a0 8 write = 0;
lharoon 0:22612ae617a0 9 read = 0;
lharoon 0:22612ae617a0 10 size = length + 1;
lharoon 0:22612ae617a0 11 buf = (T *)malloc(size * sizeof(T));
lharoon 0:22612ae617a0 12 };
lharoon 0:22612ae617a0 13
lharoon 0:22612ae617a0 14 bool isFull() {
lharoon 0:22612ae617a0 15 return ((write + 1) % size == read);
lharoon 0:22612ae617a0 16 };
lharoon 0:22612ae617a0 17
lharoon 0:22612ae617a0 18 bool isEmpty() {
lharoon 0:22612ae617a0 19 return (read == write);
lharoon 0:22612ae617a0 20 };
lharoon 0:22612ae617a0 21
lharoon 0:22612ae617a0 22 void queue(T k) {
lharoon 0:22612ae617a0 23 if (isFull()) {
lharoon 0:22612ae617a0 24 read++;
lharoon 0:22612ae617a0 25 read %= size;
lharoon 0:22612ae617a0 26 }
lharoon 0:22612ae617a0 27 buf[write++] = k;
lharoon 0:22612ae617a0 28 write %= size;
lharoon 0:22612ae617a0 29 }
lharoon 0:22612ae617a0 30
lharoon 0:22612ae617a0 31 uint16_t available() {
lharoon 0:22612ae617a0 32 return (write >= read) ? write - read : size - read + write;
lharoon 0:22612ae617a0 33 };
lharoon 0:22612ae617a0 34
lharoon 0:22612ae617a0 35 bool dequeue(T * c) {
lharoon 0:22612ae617a0 36 bool empty = isEmpty();
lharoon 0:22612ae617a0 37 if (!empty) {
lharoon 0:22612ae617a0 38 *c = buf[read++];
lharoon 0:22612ae617a0 39 read %= size;
lharoon 0:22612ae617a0 40 }
lharoon 0:22612ae617a0 41 return(!empty);
lharoon 0:22612ae617a0 42 };
lharoon 0:22612ae617a0 43
lharoon 0:22612ae617a0 44 private:
lharoon 0:22612ae617a0 45 volatile uint16_t write;
lharoon 0:22612ae617a0 46 volatile uint16_t read;
lharoon 0:22612ae617a0 47 uint16_t size;
lharoon 0:22612ae617a0 48 T * buf;
lharoon 0:22612ae617a0 49 };
lharoon 0:22612ae617a0 50
lharoon 0:22612ae617a0 51 #endif