error in seriapipe
Fork of C027_Support by
Pipe.h@9:e7a5959ffae1, 2013-11-10 (annotated)
- Committer:
- mazgch
- Date:
- Sun Nov 10 16:39:42 2013 +0000
- Revision:
- 9:e7a5959ffae1
- Parent:
- 8:2435cdff8015
- Child:
- 13:e2446fcdc246
update support library (tx pipe)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mazgch | 0:cb2d45baaca3 | 1 | #pragma once |
mazgch | 0:cb2d45baaca3 | 2 | |
mazgch | 0:cb2d45baaca3 | 3 | template <class T> |
mazgch | 0:cb2d45baaca3 | 4 | class Pipe |
mazgch | 0:cb2d45baaca3 | 5 | { |
mazgch | 0:cb2d45baaca3 | 6 | private: |
mazgch | 0:cb2d45baaca3 | 7 | inline int _inc(int i, int n = 1) |
mazgch | 0:cb2d45baaca3 | 8 | { |
mazgch | 0:cb2d45baaca3 | 9 | i += n; |
mazgch | 9:e7a5959ffae1 | 10 | if (i >= _s) |
mazgch | 9:e7a5959ffae1 | 11 | i -= _s; |
mazgch | 0:cb2d45baaca3 | 12 | return i; |
mazgch | 0:cb2d45baaca3 | 13 | } |
mazgch | 0:cb2d45baaca3 | 14 | public: |
mazgch | 9:e7a5959ffae1 | 15 | Pipe(int n, T* b = NULL) |
mazgch | 0:cb2d45baaca3 | 16 | { |
mazgch | 9:e7a5959ffae1 | 17 | _a = b ? NULL : new T[n]; |
mazgch | 9:e7a5959ffae1 | 18 | _r = 0; |
mazgch | 9:e7a5959ffae1 | 19 | _w = 0; |
mazgch | 9:e7a5959ffae1 | 20 | _b = b ? b : _a; |
mazgch | 9:e7a5959ffae1 | 21 | _s = n; |
mazgch | 0:cb2d45baaca3 | 22 | } |
mazgch | 0:cb2d45baaca3 | 23 | virtual ~Pipe() |
mazgch | 0:cb2d45baaca3 | 24 | { |
mazgch | 9:e7a5959ffae1 | 25 | if (_a) |
mazgch | 9:e7a5959ffae1 | 26 | delete [] _a; |
mazgch | 0:cb2d45baaca3 | 27 | } |
mazgch | 0:cb2d45baaca3 | 28 | // writing thread |
mazgch | 0:cb2d45baaca3 | 29 | bool writeable(void) // = not full |
mazgch | 0:cb2d45baaca3 | 30 | { |
mazgch | 9:e7a5959ffae1 | 31 | return free() > 0; |
mazgch | 0:cb2d45baaca3 | 32 | } |
mazgch | 0:cb2d45baaca3 | 33 | int free(void) // number of elements that can be added |
mazgch | 0:cb2d45baaca3 | 34 | { |
mazgch | 9:e7a5959ffae1 | 35 | int s = _r - _w; |
mazgch | 9:e7a5959ffae1 | 36 | if (s <= 0) |
mazgch | 9:e7a5959ffae1 | 37 | s += _s; |
mazgch | 9:e7a5959ffae1 | 38 | return s - 1; |
mazgch | 0:cb2d45baaca3 | 39 | } |
mazgch | 0:cb2d45baaca3 | 40 | void putc(T c) |
mazgch | 0:cb2d45baaca3 | 41 | { |
mazgch | 9:e7a5959ffae1 | 42 | int i = _w; |
mazgch | 0:cb2d45baaca3 | 43 | int j = i; |
mazgch | 0:cb2d45baaca3 | 44 | i = _inc(i); |
mazgch | 9:e7a5959ffae1 | 45 | while (i == _r) // = !writeable() |
mazgch | 9:e7a5959ffae1 | 46 | /*wait for space*/; |
mazgch | 9:e7a5959ffae1 | 47 | _b[j] = c; |
mazgch | 9:e7a5959ffae1 | 48 | _w = i; |
mazgch | 0:cb2d45baaca3 | 49 | } |
mazgch | 9:e7a5959ffae1 | 50 | int put(const T* p, int n, bool t = false) |
mazgch | 0:cb2d45baaca3 | 51 | { |
mazgch | 9:e7a5959ffae1 | 52 | int c = n; |
mazgch | 9:e7a5959ffae1 | 53 | while (c) |
mazgch | 0:cb2d45baaca3 | 54 | { |
mazgch | 9:e7a5959ffae1 | 55 | int f; |
mazgch | 9:e7a5959ffae1 | 56 | for (;;) // wait for space |
mazgch | 0:cb2d45baaca3 | 57 | { |
mazgch | 9:e7a5959ffae1 | 58 | f = free(); |
mazgch | 9:e7a5959ffae1 | 59 | if (f) break; // data avail |
mazgch | 9:e7a5959ffae1 | 60 | if (!t) return n - c; // no more space and not blocking |
mazgch | 0:cb2d45baaca3 | 61 | } |
mazgch | 9:e7a5959ffae1 | 62 | // check free space |
mazgch | 9:e7a5959ffae1 | 63 | if (c < f) f = c; |
mazgch | 9:e7a5959ffae1 | 64 | int w = _w; |
mazgch | 9:e7a5959ffae1 | 65 | int m = _s - w; |
mazgch | 9:e7a5959ffae1 | 66 | // check wrap |
mazgch | 9:e7a5959ffae1 | 67 | if (f > m) f = m; |
mazgch | 9:e7a5959ffae1 | 68 | memcpy(&_b[w], p, f); |
mazgch | 9:e7a5959ffae1 | 69 | _w = _inc(w, f); |
mazgch | 9:e7a5959ffae1 | 70 | c -= f; |
mazgch | 9:e7a5959ffae1 | 71 | p += f; |
mazgch | 8:2435cdff8015 | 72 | } |
mazgch | 9:e7a5959ffae1 | 73 | return n - c; |
mazgch | 0:cb2d45baaca3 | 74 | } |
mazgch | 0:cb2d45baaca3 | 75 | // reading thread |
mazgch | 0:cb2d45baaca3 | 76 | // -------------------------------------------------------- |
mazgch | 0:cb2d45baaca3 | 77 | //! check if there are any values available |
mazgch | 0:cb2d45baaca3 | 78 | bool readable(void) // = not empty |
mazgch | 0:cb2d45baaca3 | 79 | { |
mazgch | 9:e7a5959ffae1 | 80 | return (_r != _w); |
mazgch | 0:cb2d45baaca3 | 81 | } |
mazgch | 0:cb2d45baaca3 | 82 | //! get the number of values avialable in the buffer |
mazgch | 2:b6012cd91657 | 83 | virtual int size(void) |
mazgch | 0:cb2d45baaca3 | 84 | { |
mazgch | 9:e7a5959ffae1 | 85 | int s = _w - _r; |
mazgch | 9:e7a5959ffae1 | 86 | if (s < 0) |
mazgch | 9:e7a5959ffae1 | 87 | s += _s; |
mazgch | 9:e7a5959ffae1 | 88 | return s; |
mazgch | 0:cb2d45baaca3 | 89 | } |
mazgch | 0:cb2d45baaca3 | 90 | //! get a value from buffer (this function will block if no values available) |
mazgch | 0:cb2d45baaca3 | 91 | T getc(void) |
mazgch | 0:cb2d45baaca3 | 92 | { |
mazgch | 9:e7a5959ffae1 | 93 | int r = _r; |
mazgch | 9:e7a5959ffae1 | 94 | while (r == _w) // = !readable() |
mazgch | 9:e7a5959ffae1 | 95 | /*wait for data*/; |
mazgch | 9:e7a5959ffae1 | 96 | T t = _b[r]; |
mazgch | 9:e7a5959ffae1 | 97 | _r = _inc(r); |
mazgch | 0:cb2d45baaca3 | 98 | return t; |
mazgch | 0:cb2d45baaca3 | 99 | } |
mazgch | 9:e7a5959ffae1 | 100 | //! get values from buffer |
mazgch | 9:e7a5959ffae1 | 101 | virtual int get(T* p, int n, bool t = false) |
mazgch | 0:cb2d45baaca3 | 102 | { |
mazgch | 9:e7a5959ffae1 | 103 | int c = n; |
mazgch | 9:e7a5959ffae1 | 104 | while (c) |
mazgch | 0:cb2d45baaca3 | 105 | { |
mazgch | 9:e7a5959ffae1 | 106 | int f; |
mazgch | 9:e7a5959ffae1 | 107 | for (;;) // wait for data |
mazgch | 0:cb2d45baaca3 | 108 | { |
mazgch | 9:e7a5959ffae1 | 109 | f = size(); |
mazgch | 9:e7a5959ffae1 | 110 | if (f) break; // free space |
mazgch | 9:e7a5959ffae1 | 111 | if (!t) return n - c; // no space and not blocking |
mazgch | 0:cb2d45baaca3 | 112 | } |
mazgch | 9:e7a5959ffae1 | 113 | // check available data |
mazgch | 9:e7a5959ffae1 | 114 | if (c < f) f = c; |
mazgch | 9:e7a5959ffae1 | 115 | int r = _r; |
mazgch | 9:e7a5959ffae1 | 116 | int m = _s - r; |
mazgch | 9:e7a5959ffae1 | 117 | // check wrap |
mazgch | 9:e7a5959ffae1 | 118 | if (f > m) f = m; |
mazgch | 9:e7a5959ffae1 | 119 | memcpy(p, &_b[r], f); |
mazgch | 9:e7a5959ffae1 | 120 | _r = _inc(r, f); |
mazgch | 9:e7a5959ffae1 | 121 | c -= f; |
mazgch | 9:e7a5959ffae1 | 122 | p += f; |
mazgch | 0:cb2d45baaca3 | 123 | } |
mazgch | 9:e7a5959ffae1 | 124 | return n - c; |
mazgch | 0:cb2d45baaca3 | 125 | } |
mazgch | 9:e7a5959ffae1 | 126 | |
mazgch | 0:cb2d45baaca3 | 127 | // the following functions are useful if you like to inspect or parse the buffer |
mazgch | 9:e7a5959ffae1 | 128 | |
mazgch | 9:e7a5959ffae1 | 129 | //! reset the parsing index and return the number of available elments |
mazgch | 9:e7a5959ffae1 | 130 | virtual int start(void) |
mazgch | 9:e7a5959ffae1 | 131 | { |
mazgch | 9:e7a5959ffae1 | 132 | _o = _r; |
mazgch | 9:e7a5959ffae1 | 133 | return size(); |
mazgch | 9:e7a5959ffae1 | 134 | } |
mazgch | 9:e7a5959ffae1 | 135 | //! get the next element and increment |
mazgch | 9:e7a5959ffae1 | 136 | virtual T next(void) |
mazgch | 9:e7a5959ffae1 | 137 | { |
mazgch | 9:e7a5959ffae1 | 138 | int o = _o; |
mazgch | 9:e7a5959ffae1 | 139 | T t = _b[o]; |
mazgch | 9:e7a5959ffae1 | 140 | _o = _inc(o); |
mazgch | 9:e7a5959ffae1 | 141 | return t; |
mazgch | 9:e7a5959ffae1 | 142 | } |
mazgch | 9:e7a5959ffae1 | 143 | //! commit the index |
mazgch | 9:e7a5959ffae1 | 144 | virtual void done(void) |
mazgch | 9:e7a5959ffae1 | 145 | { |
mazgch | 9:e7a5959ffae1 | 146 | _r = _o; |
mazgch | 9:e7a5959ffae1 | 147 | } |
mazgch | 0:cb2d45baaca3 | 148 | |
mazgch | 0:cb2d45baaca3 | 149 | private: |
mazgch | 9:e7a5959ffae1 | 150 | T* _b; //!< buffer |
mazgch | 9:e7a5959ffae1 | 151 | T* _a; //!< allocated buffer |
mazgch | 9:e7a5959ffae1 | 152 | int _s; //!< size of buffer (s - 1) elements can be stored |
mazgch | 9:e7a5959ffae1 | 153 | volatile int _w; //!< write index |
mazgch | 9:e7a5959ffae1 | 154 | volatile int _r; //!< read index |
mazgch | 9:e7a5959ffae1 | 155 | int _o; //!< offest index used by parsing functions |
mazgch | 0:cb2d45baaca3 | 156 | }; |