Add a bunch of APNs
Fork of C027_Support by
Pipe.h@2:b6012cd91657, 2013-10-25 (annotated)
- Committer:
- mazgch
- Date:
- Fri Oct 25 08:47:22 2013 +0000
- Revision:
- 2:b6012cd91657
- Parent:
- 0:cb2d45baaca3
- Child:
- 5:5362073f2689
GPS for Serial and I2c
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 | 0:cb2d45baaca3 | 10 | if (i >= s) |
mazgch | 0:cb2d45baaca3 | 11 | i -= s; |
mazgch | 0:cb2d45baaca3 | 12 | return i; |
mazgch | 0:cb2d45baaca3 | 13 | } |
mazgch | 0:cb2d45baaca3 | 14 | public: |
mazgch | 0:cb2d45baaca3 | 15 | Pipe(int n) |
mazgch | 0:cb2d45baaca3 | 16 | { |
mazgch | 0:cb2d45baaca3 | 17 | r = 0; |
mazgch | 0:cb2d45baaca3 | 18 | w = 0; |
mazgch | 0:cb2d45baaca3 | 19 | n ++; // we add one more element to be able to identify empty from full |
mazgch | 0:cb2d45baaca3 | 20 | b = new T[n]; |
mazgch | 0:cb2d45baaca3 | 21 | s = n; |
mazgch | 0:cb2d45baaca3 | 22 | } |
mazgch | 0:cb2d45baaca3 | 23 | virtual ~Pipe() |
mazgch | 0:cb2d45baaca3 | 24 | { |
mazgch | 0:cb2d45baaca3 | 25 | delete [] b; |
mazgch | 0:cb2d45baaca3 | 26 | } |
mazgch | 0:cb2d45baaca3 | 27 | // writing thread |
mazgch | 0:cb2d45baaca3 | 28 | bool writeable(void) // = not full |
mazgch | 0:cb2d45baaca3 | 29 | { |
mazgch | 0:cb2d45baaca3 | 30 | int i = _inc(w); |
mazgch | 0:cb2d45baaca3 | 31 | return (i = r); |
mazgch | 0:cb2d45baaca3 | 32 | } |
mazgch | 0:cb2d45baaca3 | 33 | int free(void) // number of elements that can be added |
mazgch | 0:cb2d45baaca3 | 34 | { |
mazgch | 0:cb2d45baaca3 | 35 | int t = r - w; |
mazgch | 0:cb2d45baaca3 | 36 | if (t <= 0) |
mazgch | 0:cb2d45baaca3 | 37 | t += s; |
mazgch | 0:cb2d45baaca3 | 38 | return t - 1; |
mazgch | 0:cb2d45baaca3 | 39 | } |
mazgch | 0:cb2d45baaca3 | 40 | void putc(T c) |
mazgch | 0:cb2d45baaca3 | 41 | { |
mazgch | 0:cb2d45baaca3 | 42 | int i = w; |
mazgch | 0:cb2d45baaca3 | 43 | int j = i; |
mazgch | 0:cb2d45baaca3 | 44 | i = _inc(i); |
mazgch | 0:cb2d45baaca3 | 45 | //assert(i != r); |
mazgch | 0:cb2d45baaca3 | 46 | b[j] = c; |
mazgch | 0:cb2d45baaca3 | 47 | w = i; |
mazgch | 0:cb2d45baaca3 | 48 | } |
mazgch | 0:cb2d45baaca3 | 49 | int put(T* p, int n) |
mazgch | 0:cb2d45baaca3 | 50 | { |
mazgch | 0:cb2d45baaca3 | 51 | int f = free(); |
mazgch | 0:cb2d45baaca3 | 52 | if (f < n) |
mazgch | 0:cb2d45baaca3 | 53 | n = f; |
mazgch | 0:cb2d45baaca3 | 54 | if (n) |
mazgch | 0:cb2d45baaca3 | 55 | { |
mazgch | 0:cb2d45baaca3 | 56 | int m = s - w; |
mazgch | 0:cb2d45baaca3 | 57 | if (m > n) |
mazgch | 0:cb2d45baaca3 | 58 | m = n; |
mazgch | 0:cb2d45baaca3 | 59 | memcpy(&b[w], &p[0], m); |
mazgch | 0:cb2d45baaca3 | 60 | int t = n - m; |
mazgch | 0:cb2d45baaca3 | 61 | if (t) |
mazgch | 0:cb2d45baaca3 | 62 | { |
mazgch | 0:cb2d45baaca3 | 63 | memcpy(&b[0], &p[m], t); |
mazgch | 0:cb2d45baaca3 | 64 | w = t; |
mazgch | 0:cb2d45baaca3 | 65 | } |
mazgch | 0:cb2d45baaca3 | 66 | else |
mazgch | 0:cb2d45baaca3 | 67 | w += m; |
mazgch | 0:cb2d45baaca3 | 68 | } |
mazgch | 0:cb2d45baaca3 | 69 | return n; |
mazgch | 0:cb2d45baaca3 | 70 | } |
mazgch | 0:cb2d45baaca3 | 71 | // reading thread |
mazgch | 0:cb2d45baaca3 | 72 | // -------------------------------------------------------- |
mazgch | 0:cb2d45baaca3 | 73 | //! check if there are any values available |
mazgch | 0:cb2d45baaca3 | 74 | bool readable(void) // = not empty |
mazgch | 0:cb2d45baaca3 | 75 | { |
mazgch | 0:cb2d45baaca3 | 76 | return (r != w); |
mazgch | 0:cb2d45baaca3 | 77 | } |
mazgch | 0:cb2d45baaca3 | 78 | //! get the number of values avialable in the buffer |
mazgch | 2:b6012cd91657 | 79 | virtual int size(void) |
mazgch | 0:cb2d45baaca3 | 80 | { |
mazgch | 0:cb2d45baaca3 | 81 | int t = w - r; |
mazgch | 0:cb2d45baaca3 | 82 | if (t < 0) |
mazgch | 0:cb2d45baaca3 | 83 | t += s; |
mazgch | 0:cb2d45baaca3 | 84 | return t; |
mazgch | 0:cb2d45baaca3 | 85 | } |
mazgch | 0:cb2d45baaca3 | 86 | //! get a value from buffer (this function will block if no values available) |
mazgch | 0:cb2d45baaca3 | 87 | T getc(void) |
mazgch | 0:cb2d45baaca3 | 88 | { |
mazgch | 0:cb2d45baaca3 | 89 | //while (r == w) |
mazgch | 0:cb2d45baaca3 | 90 | // /* just wait until w changes*/; |
mazgch | 0:cb2d45baaca3 | 91 | T t = b[r]; |
mazgch | 0:cb2d45baaca3 | 92 | r = _inc(r); |
mazgch | 0:cb2d45baaca3 | 93 | return t; |
mazgch | 0:cb2d45baaca3 | 94 | } |
mazgch | 0:cb2d45baaca3 | 95 | // get values from buffer (if the buffer has less values, only the values avilable are returned) |
mazgch | 2:b6012cd91657 | 96 | virtual int get(T* p, int n) |
mazgch | 0:cb2d45baaca3 | 97 | { |
mazgch | 0:cb2d45baaca3 | 98 | int f = size(); |
mazgch | 0:cb2d45baaca3 | 99 | if (f < n) |
mazgch | 0:cb2d45baaca3 | 100 | n = f; |
mazgch | 0:cb2d45baaca3 | 101 | if (n) |
mazgch | 0:cb2d45baaca3 | 102 | { |
mazgch | 0:cb2d45baaca3 | 103 | int m = s - r; |
mazgch | 0:cb2d45baaca3 | 104 | if (m > n) |
mazgch | 0:cb2d45baaca3 | 105 | m = n; |
mazgch | 0:cb2d45baaca3 | 106 | memcpy(&p[0], &b[r], m); |
mazgch | 0:cb2d45baaca3 | 107 | int t = n - m; |
mazgch | 0:cb2d45baaca3 | 108 | if (t) |
mazgch | 0:cb2d45baaca3 | 109 | { |
mazgch | 0:cb2d45baaca3 | 110 | memcpy(&p[m] ,&b[0], t); |
mazgch | 0:cb2d45baaca3 | 111 | r = t; |
mazgch | 0:cb2d45baaca3 | 112 | } |
mazgch | 0:cb2d45baaca3 | 113 | else |
mazgch | 0:cb2d45baaca3 | 114 | r += m; |
mazgch | 0:cb2d45baaca3 | 115 | } |
mazgch | 0:cb2d45baaca3 | 116 | return n; |
mazgch | 0:cb2d45baaca3 | 117 | } |
mazgch | 0:cb2d45baaca3 | 118 | // the following functions are useful if you like to inspect or parse the buffer |
mazgch | 2:b6012cd91657 | 119 | virtual int start(void) { o = r; return size(); } // reset the parsing index and return the number of available elments |
mazgch | 2:b6012cd91657 | 120 | virtual T next(void) { T t = b[o]; o = _inc(o); return t; } // get the next element and increment |
mazgch | 2:b6012cd91657 | 121 | virtual void done(void) { r = o; } // commit the index |
mazgch | 0:cb2d45baaca3 | 122 | |
mazgch | 0:cb2d45baaca3 | 123 | private: |
mazgch | 0:cb2d45baaca3 | 124 | // buffer |
mazgch | 0:cb2d45baaca3 | 125 | T* b; //!< buffer |
mazgch | 0:cb2d45baaca3 | 126 | int s; //!< size of buffer (s - 1) elements can be stored |
mazgch | 0:cb2d45baaca3 | 127 | volatile int w; //! write index |
mazgch | 0:cb2d45baaca3 | 128 | volatile int r; //! read index |
mazgch | 0:cb2d45baaca3 | 129 | int o; //! offest index used by parsing functions |
mazgch | 0:cb2d45baaca3 | 130 | }; |