Library to use a wifly module: RN 131 C/G
Dependents: RN-XV_simple_server
Diff: CBuffer.h
- Revision:
- 17:56f3c4da1ea8
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CBuffer.h Mon Jan 30 10:53:56 2012 +0000 @@ -0,0 +1,51 @@ +#ifndef CBUFFER_H +#define CBUFFER_H + +#define MAX_BUF 128 + +class CBuffer { +public: + CBuffer() { + write = 0; + read = 0; + size = MAX_BUF + 1; + }; + + bool isFull() { + return ((write + 1) % size == read); + }; + + bool isEmpty() { + return (read == write); + }; + + void queue(uint8_t k) { + if (isFull()) { + read++; + read %= size; + } + buf[write++] = k; + write %= size; + } + + uint16_t available() { + return (write >= read) ? write - read : size - read + write; + }; + + bool dequeue(uint8_t * c) { + if (!isEmpty()) { + *c = buf[read++]; + read %= size; + } + return(!isEmpty()); + }; + + +private: + volatile uint16_t write; + volatile uint16_t read; + uint16_t size; + uint8_t buf[MAX_BUF + 1]; +}; + +#endif \ No newline at end of file