USBMSD SD card Hello World for Mbed platforms
Dependencies: mbed USBMSD_SD USBDevice
USBDevice/USBSERIAL/CircBuffer.h@18:08b207d10056, 2011-12-11 (annotated)
- Committer:
- samux
- Date:
- Sun Dec 11 15:22:50 2011 +0000
- Revision:
- 18:08b207d10056
- Parent:
- 14:757226626acb
all is working: rename...
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux |
14:757226626acb | 1 | #ifndef CIRCBUFFER_H |
samux |
14:757226626acb | 2 | #define CIRCBUFFER_H |
samux |
14:757226626acb | 3 | |
samux |
14:757226626acb | 4 | template <class T> |
samux |
14:757226626acb | 5 | class CircBuffer { |
samux |
14:757226626acb | 6 | public: |
samux |
14:757226626acb | 7 | CircBuffer(int length) { |
samux |
14:757226626acb | 8 | write = 0; |
samux |
14:757226626acb | 9 | read = 0; |
samux |
14:757226626acb | 10 | size = length + 1; |
samux |
14:757226626acb | 11 | buf = (T *)malloc(size * sizeof(T)); |
samux |
14:757226626acb | 12 | }; |
samux |
14:757226626acb | 13 | |
samux |
14:757226626acb | 14 | bool isFull() { |
samux |
14:757226626acb | 15 | return ((write + 1) % size == read); |
samux |
14:757226626acb | 16 | }; |
samux |
14:757226626acb | 17 | |
samux |
14:757226626acb | 18 | bool isEmpty() { |
samux |
14:757226626acb | 19 | return (read == write); |
samux |
14:757226626acb | 20 | }; |
samux |
14:757226626acb | 21 | |
samux |
14:757226626acb | 22 | void queue(T k) { |
samux |
14:757226626acb | 23 | if (isFull()) { |
samux |
14:757226626acb | 24 | read++; |
samux |
14:757226626acb | 25 | read %= size; |
samux |
14:757226626acb | 26 | } |
samux |
14:757226626acb | 27 | buf[write++] = k; |
samux |
14:757226626acb | 28 | write %= size; |
samux |
14:757226626acb | 29 | } |
samux |
14:757226626acb | 30 | |
samux |
14:757226626acb | 31 | uint16_t available() { |
samux |
14:757226626acb | 32 | return (write >= read) ? write - read : size - read + write; |
samux |
14:757226626acb | 33 | }; |
samux |
14:757226626acb | 34 | |
samux |
14:757226626acb | 35 | bool dequeue(T * c) { |
samux |
14:757226626acb | 36 | bool empty = isEmpty(); |
samux |
14:757226626acb | 37 | if (!empty) { |
samux |
14:757226626acb | 38 | *c = buf[read++]; |
samux |
14:757226626acb | 39 | read %= size; |
samux |
14:757226626acb | 40 | } |
samux |
14:757226626acb | 41 | return(!empty); |
samux |
14:757226626acb | 42 | }; |
samux |
14:757226626acb | 43 | |
samux |
14:757226626acb | 44 | private: |
samux |
14:757226626acb | 45 | volatile uint16_t write; |
samux |
14:757226626acb | 46 | volatile uint16_t read; |
samux |
14:757226626acb | 47 | uint16_t size; |
samux |
14:757226626acb | 48 | T * buf; |
samux |
14:757226626acb | 49 | }; |
samux |
14:757226626acb | 50 | |
samux |
14:757226626acb | 51 | #endif |