blynk & neopixelring & w7500
Fork of WIZwiki-7500_Blynk by
utility/BlynkFifo.h@18:bc3d69c01bf2, 2017-11-27 (annotated)
- Committer:
- jcm931213
- Date:
- Mon Nov 27 11:49:08 2017 +0000
- Revision:
- 18:bc3d69c01bf2
- Parent:
- 0:58b20b438383
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<typename T, unsigned SIZE> |
vshymanskyy | 0:58b20b438383 | 17 | class BlynkFifo { |
vshymanskyy | 0:58b20b438383 | 18 | |
vshymanskyy | 0:58b20b438383 | 19 | BlynkFifo(const BlynkFifo<T, SIZE> & rb); |
vshymanskyy | 0:58b20b438383 | 20 | |
vshymanskyy | 0:58b20b438383 | 21 | public: |
vshymanskyy | 0:58b20b438383 | 22 | |
vshymanskyy | 0:58b20b438383 | 23 | BlynkFifo() : fst(0), lst(0), flag(0) {} |
vshymanskyy | 0:58b20b438383 | 24 | ~BlynkFifo() {} |
vshymanskyy | 0:58b20b438383 | 25 | |
vshymanskyy | 0:58b20b438383 | 26 | void clear() { |
vshymanskyy | 0:58b20b438383 | 27 | fst = 0; |
vshymanskyy | 0:58b20b438383 | 28 | lst = 0; |
vshymanskyy | 0:58b20b438383 | 29 | flag = 0; |
vshymanskyy | 0:58b20b438383 | 30 | } |
vshymanskyy | 0:58b20b438383 | 31 | |
vshymanskyy | 0:58b20b438383 | 32 | size_t write(const T* data, size_t n) |
vshymanskyy | 0:58b20b438383 | 33 | { |
vshymanskyy | 0:58b20b438383 | 34 | if ((n = BlynkMin(n, getFree()))) { |
vshymanskyy | 0:58b20b438383 | 35 | const size_t ch1 = BlynkMin(n, SIZE - lst); |
vshymanskyy | 0:58b20b438383 | 36 | memcpy(buffer + lst, data, ch1 * sizeof(T)); |
vshymanskyy | 0:58b20b438383 | 37 | lst = (lst + ch1) % SIZE; |
vshymanskyy | 0:58b20b438383 | 38 | |
vshymanskyy | 0:58b20b438383 | 39 | if (ch1 < n) { |
vshymanskyy | 0:58b20b438383 | 40 | const size_t ch2 = n - ch1; |
vshymanskyy | 0:58b20b438383 | 41 | memcpy(buffer + lst, data + ch1, ch2 * sizeof(T)); |
vshymanskyy | 0:58b20b438383 | 42 | lst = (lst + ch2) % SIZE; |
vshymanskyy | 0:58b20b438383 | 43 | } |
vshymanskyy | 0:58b20b438383 | 44 | |
vshymanskyy | 0:58b20b438383 | 45 | if (fst == lst) { |
vshymanskyy | 0:58b20b438383 | 46 | flag = 1; |
vshymanskyy | 0:58b20b438383 | 47 | } |
vshymanskyy | 0:58b20b438383 | 48 | } |
vshymanskyy | 0:58b20b438383 | 49 | return n; |
vshymanskyy | 0:58b20b438383 | 50 | } |
vshymanskyy | 0:58b20b438383 | 51 | |
vshymanskyy | 0:58b20b438383 | 52 | size_t read(T* dest, size_t n) |
vshymanskyy | 0:58b20b438383 | 53 | { |
vshymanskyy | 0:58b20b438383 | 54 | if ((n = BlynkMin(n, getOccupied()))) { |
vshymanskyy | 0:58b20b438383 | 55 | flag = 0; |
vshymanskyy | 0:58b20b438383 | 56 | |
vshymanskyy | 0:58b20b438383 | 57 | const size_t ch1 = BlynkMin(n, SIZE - fst); |
vshymanskyy | 0:58b20b438383 | 58 | memcpy(dest, buffer + fst, ch1 * sizeof(T)); |
vshymanskyy | 0:58b20b438383 | 59 | fst = (fst + ch1) % SIZE; |
vshymanskyy | 0:58b20b438383 | 60 | |
vshymanskyy | 0:58b20b438383 | 61 | if (ch1 < n) { |
vshymanskyy | 0:58b20b438383 | 62 | const size_t ch2 = n - ch1; |
vshymanskyy | 0:58b20b438383 | 63 | memcpy(dest + ch1, buffer + fst, ch2 * sizeof(T)); |
vshymanskyy | 0:58b20b438383 | 64 | fst = (fst + ch2) % SIZE; |
vshymanskyy | 0:58b20b438383 | 65 | } |
vshymanskyy | 0:58b20b438383 | 66 | } |
vshymanskyy | 0:58b20b438383 | 67 | return n; |
vshymanskyy | 0:58b20b438383 | 68 | } |
vshymanskyy | 0:58b20b438383 | 69 | |
vshymanskyy | 0:58b20b438383 | 70 | bool push(const T& data) { |
vshymanskyy | 0:58b20b438383 | 71 | return write(&data, 1) == 1; |
vshymanskyy | 0:58b20b438383 | 72 | } |
vshymanskyy | 0:58b20b438383 | 73 | |
vshymanskyy | 0:58b20b438383 | 74 | size_t getOccupied() const { |
vshymanskyy | 0:58b20b438383 | 75 | if (lst == fst) { |
vshymanskyy | 0:58b20b438383 | 76 | return flag ? SIZE : 0; |
vshymanskyy | 0:58b20b438383 | 77 | } else if (lst > fst) { |
vshymanskyy | 0:58b20b438383 | 78 | return lst - fst; |
vshymanskyy | 0:58b20b438383 | 79 | } else { |
vshymanskyy | 0:58b20b438383 | 80 | return SIZE + lst - fst; |
vshymanskyy | 0:58b20b438383 | 81 | } |
vshymanskyy | 0:58b20b438383 | 82 | } |
vshymanskyy | 0:58b20b438383 | 83 | |
vshymanskyy | 0:58b20b438383 | 84 | size_t getFree() const { |
vshymanskyy | 0:58b20b438383 | 85 | return SIZE - getOccupied(); |
vshymanskyy | 0:58b20b438383 | 86 | } |
vshymanskyy | 0:58b20b438383 | 87 | |
vshymanskyy | 0:58b20b438383 | 88 | private: |
vshymanskyy | 0:58b20b438383 | 89 | T buffer[SIZE]; |
vshymanskyy | 0:58b20b438383 | 90 | size_t fst; |
vshymanskyy | 0:58b20b438383 | 91 | size_t lst; |
vshymanskyy | 0:58b20b438383 | 92 | uint8_t flag; |
vshymanskyy | 0:58b20b438383 | 93 | }; |
vshymanskyy | 0:58b20b438383 | 94 | |
vshymanskyy | 0:58b20b438383 | 95 | #endif |