SerialLibrary for arrc
Buffer52.hpp@0:4801795a9073, 2021-12-11 (annotated)
- Committer:
- hamohamo
- Date:
- Sat Dec 11 02:37:36 2021 +0000
- Revision:
- 0:4801795a9073
SerialLibrary;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hamohamo | 0:4801795a9073 | 1 | #ifndef _52BUFFER |
hamohamo | 0:4801795a9073 | 2 | #define _52BUFFER |
hamohamo | 0:4801795a9073 | 3 | |
hamohamo | 0:4801795a9073 | 4 | #include <vector> |
hamohamo | 0:4801795a9073 | 5 | |
hamohamo | 0:4801795a9073 | 6 | namespace ARRC{ |
hamohamo | 0:4801795a9073 | 7 | template <class type> class Buffer{ |
hamohamo | 0:4801795a9073 | 8 | public: |
hamohamo | 0:4801795a9073 | 9 | Buffer(unsigned size):buf(size,0),size(size),read_ref(0),write_ref(0){} |
hamohamo | 0:4801795a9073 | 10 | type read(){ |
hamohamo | 0:4801795a9073 | 11 | type ret = 0; |
hamohamo | 0:4801795a9073 | 12 | if(read_ref < write_ref){ |
hamohamo | 0:4801795a9073 | 13 | ret = buf[read_ref % size]; |
hamohamo | 0:4801795a9073 | 14 | read_ref++; |
hamohamo | 0:4801795a9073 | 15 | } |
hamohamo | 0:4801795a9073 | 16 | return ret; |
hamohamo | 0:4801795a9073 | 17 | } |
hamohamo | 0:4801795a9073 | 18 | void write(type elem){ |
hamohamo | 0:4801795a9073 | 19 | buf[write_ref % size] = elem; |
hamohamo | 0:4801795a9073 | 20 | write_ref++; |
hamohamo | 0:4801795a9073 | 21 | } |
hamohamo | 0:4801795a9073 | 22 | bool readable(){return (read_ref < write_ref);} |
hamohamo | 0:4801795a9073 | 23 | int datacount(){return write_ref - read_ref;} |
hamohamo | 0:4801795a9073 | 24 | private: |
hamohamo | 0:4801795a9073 | 25 | std::vector<type> buf; |
hamohamo | 0:4801795a9073 | 26 | long unsigned read_ref,write_ref; |
hamohamo | 0:4801795a9073 | 27 | long unsigned size; |
hamohamo | 0:4801795a9073 | 28 | }; |
hamohamo | 0:4801795a9073 | 29 | |
hamohamo | 0:4801795a9073 | 30 | } |
hamohamo | 0:4801795a9073 | 31 | |
hamohamo | 0:4801795a9073 | 32 | #endif |