blynk & neopixelring & w7500
Fork of WIZwiki-7500_Blynk by
utility/BlynkFifo2.h@18:bc3d69c01bf2, 2017-11-27 (annotated)
- Committer:
- jcm931213
- Date:
- Mon Nov 27 11:49:08 2017 +0000
- Revision:
- 18:bc3d69c01bf2
- Parent:
- 9:7369ec77a3ea
NeoPixel Ring Blynk Example final!!!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vshymanskyy | 0:58b20b438383 | 1 | /** |
vshymanskyy | 0:58b20b438383 | 2 | * @file BlynkFifo.h |
vshymanskyy | 0:58b20b438383 | 3 | * @author Volodymyr Shymanskyy |
vshymanskyy | 0:58b20b438383 | 4 | * @license This project is released under the MIT License (MIT) |
vshymanskyy | 0:58b20b438383 | 5 | * @copyright Copyright (c) 2015 Volodymyr Shymanskyy |
vshymanskyy | 0:58b20b438383 | 6 | * @date Feb 2015 |
vshymanskyy | 0:58b20b438383 | 7 | * @brief FIFO implementation |
vshymanskyy | 0:58b20b438383 | 8 | * |
vshymanskyy | 0:58b20b438383 | 9 | */ |
vshymanskyy | 0:58b20b438383 | 10 | |
vshymanskyy | 0:58b20b438383 | 11 | #ifndef BlynkFifo_h |
vshymanskyy | 0:58b20b438383 | 12 | #define BlynkFifo_h |
vshymanskyy | 0:58b20b438383 | 13 | |
vshymanskyy | 0:58b20b438383 | 14 | #include <utility/BlynkUtility.h> |
vshymanskyy | 0:58b20b438383 | 15 | |
vshymanskyy | 0:58b20b438383 | 16 | template <class T, unsigned N> |
vshymanskyy | 0:58b20b438383 | 17 | class BlynkFifo |
vshymanskyy | 0:58b20b438383 | 18 | { |
vshymanskyy | 0:58b20b438383 | 19 | public: |
vshymanskyy | 0:58b20b438383 | 20 | BlynkFifo() |
vshymanskyy | 0:58b20b438383 | 21 | { |
vshymanskyy | 0:58b20b438383 | 22 | clear(); |
vshymanskyy | 0:58b20b438383 | 23 | } |
vshymanskyy | 0:58b20b438383 | 24 | |
vshymanskyy | 0:58b20b438383 | 25 | void clear() |
vshymanskyy | 0:58b20b438383 | 26 | { |
vshymanskyy | 0:58b20b438383 | 27 | _r = 0; |
vshymanskyy | 0:58b20b438383 | 28 | _w = 0; |
vshymanskyy | 0:58b20b438383 | 29 | } |
vshymanskyy | 0:58b20b438383 | 30 | |
vshymanskyy | 0:58b20b438383 | 31 | ~BlynkFifo(void) |
vshymanskyy | 0:58b20b438383 | 32 | {} |
vshymanskyy | 0:58b20b438383 | 33 | |
vshymanskyy | 0:58b20b438383 | 34 | // writing thread/context API |
vshymanskyy | 0:58b20b438383 | 35 | //------------------------------------------------------------- |
vshymanskyy | 0:58b20b438383 | 36 | |
vshymanskyy | 0:58b20b438383 | 37 | bool writeable(void) |
vshymanskyy | 0:58b20b438383 | 38 | { |
vshymanskyy | 0:58b20b438383 | 39 | return free() > 0; |
vshymanskyy | 0:58b20b438383 | 40 | } |
vshymanskyy | 0:58b20b438383 | 41 | |
vshymanskyy | 0:58b20b438383 | 42 | int free(void) |
vshymanskyy | 0:58b20b438383 | 43 | { |
vshymanskyy | 0:58b20b438383 | 44 | int s = _r - _w; |
vshymanskyy | 0:58b20b438383 | 45 | if (s <= 0) |
vshymanskyy | 0:58b20b438383 | 46 | s += N; |
vshymanskyy | 0:58b20b438383 | 47 | return s - 1; |
vshymanskyy | 0:58b20b438383 | 48 | } |
vshymanskyy | 0:58b20b438383 | 49 | |
vshymanskyy | 0:58b20b438383 | 50 | T put(const T& c) |
vshymanskyy | 0:58b20b438383 | 51 | { |
vshymanskyy | 0:58b20b438383 | 52 | int i = _w; |
vshymanskyy | 0:58b20b438383 | 53 | int j = i; |
vshymanskyy | 0:58b20b438383 | 54 | i = _inc(i); |
vshymanskyy | 0:58b20b438383 | 55 | while (i == _r) // = !writeable() |
vshymanskyy | 0:58b20b438383 | 56 | /* nothing / just wait */; |
vshymanskyy | 0:58b20b438383 | 57 | _b[j] = c; |
vshymanskyy | 0:58b20b438383 | 58 | _w = i; |
vshymanskyy | 0:58b20b438383 | 59 | return c; |
vshymanskyy | 0:58b20b438383 | 60 | } |
vshymanskyy | 0:58b20b438383 | 61 | |
vshymanskyy | 0:58b20b438383 | 62 | int put(const T* p, int n, bool t = false) |
vshymanskyy | 0:58b20b438383 | 63 | { |
vshymanskyy | 0:58b20b438383 | 64 | int c = n; |
vshymanskyy | 0:58b20b438383 | 65 | while (c) |
vshymanskyy | 0:58b20b438383 | 66 | { |
vshymanskyy | 0:58b20b438383 | 67 | int f; |
vshymanskyy | 0:58b20b438383 | 68 | while ((f = free()) == 0) // wait for space |
vshymanskyy | 0:58b20b438383 | 69 | { |
vshymanskyy | 0:58b20b438383 | 70 | if (!t) return n - c; // no more space and not blocking |
vshymanskyy | 0:58b20b438383 | 71 | /* nothing / just wait */; |
vshymanskyy | 0:58b20b438383 | 72 | } |
vshymanskyy | 0:58b20b438383 | 73 | // check free space |
vshymanskyy | 0:58b20b438383 | 74 | if (c < f) f = c; |
vshymanskyy | 0:58b20b438383 | 75 | int w = _w; |
vshymanskyy | 0:58b20b438383 | 76 | int m = N - w; |
vshymanskyy | 0:58b20b438383 | 77 | // check wrap |
vshymanskyy | 0:58b20b438383 | 78 | if (f > m) f = m; |
vshymanskyy | 0:58b20b438383 | 79 | memcpy(&_b[w], p, f); |
vshymanskyy | 0:58b20b438383 | 80 | _w = _inc(w, f); |
vshymanskyy | 0:58b20b438383 | 81 | c -= f; |
vshymanskyy | 0:58b20b438383 | 82 | p += f; |
vshymanskyy | 0:58b20b438383 | 83 | } |
vshymanskyy | 0:58b20b438383 | 84 | return n - c; |
vshymanskyy | 0:58b20b438383 | 85 | } |
vshymanskyy | 0:58b20b438383 | 86 | |
vshymanskyy | 0:58b20b438383 | 87 | // reading thread/context API |
vshymanskyy | 0:58b20b438383 | 88 | // -------------------------------------------------------- |
vshymanskyy | 0:58b20b438383 | 89 | |
vshymanskyy | 0:58b20b438383 | 90 | bool readable(void) |
vshymanskyy | 0:58b20b438383 | 91 | { |
vshymanskyy | 0:58b20b438383 | 92 | return (_r != _w); |
vshymanskyy | 0:58b20b438383 | 93 | } |
vshymanskyy | 0:58b20b438383 | 94 | |
vshymanskyy | 0:58b20b438383 | 95 | size_t size(void) |
vshymanskyy | 0:58b20b438383 | 96 | { |
vshymanskyy | 0:58b20b438383 | 97 | int s = _w - _r; |
vshymanskyy | 0:58b20b438383 | 98 | if (s < 0) |
vshymanskyy | 0:58b20b438383 | 99 | s += N; |
vshymanskyy | 0:58b20b438383 | 100 | return s; |
vshymanskyy | 0:58b20b438383 | 101 | } |
vshymanskyy | 0:58b20b438383 | 102 | |
vshymanskyy | 0:58b20b438383 | 103 | T get(void) |
vshymanskyy | 0:58b20b438383 | 104 | { |
vshymanskyy | 0:58b20b438383 | 105 | int r = _r; |
vshymanskyy | 0:58b20b438383 | 106 | while (r == _w) // = !readable() |
vshymanskyy | 0:58b20b438383 | 107 | /* nothing / just wait */; |
vshymanskyy | 0:58b20b438383 | 108 | T t = _b[r]; |
vshymanskyy | 0:58b20b438383 | 109 | _r = _inc(r); |
vshymanskyy | 0:58b20b438383 | 110 | return t; |
vshymanskyy | 0:58b20b438383 | 111 | } |
vshymanskyy | 0:58b20b438383 | 112 | |
vshymanskyy | 0:58b20b438383 | 113 | int get(T* p, int n, bool t = false) |
vshymanskyy | 0:58b20b438383 | 114 | { |
vshymanskyy | 0:58b20b438383 | 115 | int c = n; |
vshymanskyy | 0:58b20b438383 | 116 | while (c) |
vshymanskyy | 0:58b20b438383 | 117 | { |
vshymanskyy | 0:58b20b438383 | 118 | int f; |
vshymanskyy | 0:58b20b438383 | 119 | for (;;) // wait for data |
vshymanskyy | 0:58b20b438383 | 120 | { |
vshymanskyy | 0:58b20b438383 | 121 | f = size(); |
vshymanskyy | 0:58b20b438383 | 122 | if (f) break; // free space |
vshymanskyy | 0:58b20b438383 | 123 | if (!t) return n - c; // no space and not blocking |
vshymanskyy | 0:58b20b438383 | 124 | /* nothing / just wait */; |
vshymanskyy | 0:58b20b438383 | 125 | } |
vshymanskyy | 0:58b20b438383 | 126 | // check available data |
vshymanskyy | 0:58b20b438383 | 127 | if (c < f) f = c; |
vshymanskyy | 0:58b20b438383 | 128 | int r = _r; |
vshymanskyy | 0:58b20b438383 | 129 | int m = N - r; |
vshymanskyy | 0:58b20b438383 | 130 | // check wrap |
vshymanskyy | 0:58b20b438383 | 131 | if (f > m) f = m; |
vshymanskyy | 0:58b20b438383 | 132 | memcpy(p, &_b[r], f); |
vshymanskyy | 0:58b20b438383 | 133 | _r = _inc(r, f); |
vshymanskyy | 0:58b20b438383 | 134 | c -= f; |
vshymanskyy | 0:58b20b438383 | 135 | p += f; |
vshymanskyy | 0:58b20b438383 | 136 | } |
vshymanskyy | 0:58b20b438383 | 137 | return n - c; |
vshymanskyy | 0:58b20b438383 | 138 | } |
vshymanskyy | 0:58b20b438383 | 139 | |
vshymanskyy | 0:58b20b438383 | 140 | private: |
Volodymyr Shymanskyy |
9:7369ec77a3ea | 141 | int _inc(int i, int n = 1) |
vshymanskyy | 0:58b20b438383 | 142 | { |
vshymanskyy | 0:58b20b438383 | 143 | return (i + n) % N; |
vshymanskyy | 0:58b20b438383 | 144 | } |
vshymanskyy | 0:58b20b438383 | 145 | |
vshymanskyy | 0:58b20b438383 | 146 | T _b[N]; |
vshymanskyy | 0:58b20b438383 | 147 | volatile int _w; |
vshymanskyy | 0:58b20b438383 | 148 | volatile int _r; |
vshymanskyy | 0:58b20b438383 | 149 | }; |
vshymanskyy | 0:58b20b438383 | 150 | |
vshymanskyy | 0:58b20b438383 | 151 | #endif |