p kj
/
LPC824-BlynkWeatherstation
Microduino
Fork of Io_moon by
Blynk_v0_3_7/Blynk/utility/BlynkFifo.h@0:740c1eb2df13, 2016-06-23 (annotated)
- Committer:
- lixianyu
- Date:
- Thu Jun 23 11:16:14 2016 +0000
- Revision:
- 0:740c1eb2df13
* AM2321?????????2s????i2c?????; * SimpleTimer??bug?????????????????????????; * Blynk??bug??????????????; * ?????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lixianyu | 0:740c1eb2df13 | 1 | /** |
lixianyu | 0:740c1eb2df13 | 2 | * @file BlynkFifo.h |
lixianyu | 0:740c1eb2df13 | 3 | * @author Volodymyr Shymanskyy |
lixianyu | 0:740c1eb2df13 | 4 | * @license This project is released under the MIT License (MIT) |
lixianyu | 0:740c1eb2df13 | 5 | * @copyright Copyright (c) 2015 Volodymyr Shymanskyy |
lixianyu | 0:740c1eb2df13 | 6 | * @date Feb 2015 |
lixianyu | 0:740c1eb2df13 | 7 | * @brief FIFO implementation |
lixianyu | 0:740c1eb2df13 | 8 | * |
lixianyu | 0:740c1eb2df13 | 9 | */ |
lixianyu | 0:740c1eb2df13 | 10 | |
lixianyu | 0:740c1eb2df13 | 11 | #ifndef BlynkFifo_h |
lixianyu | 0:740c1eb2df13 | 12 | #define BlynkFifo_h |
lixianyu | 0:740c1eb2df13 | 13 | |
lixianyu | 0:740c1eb2df13 | 14 | #include <utility/BlynkUtility.h> |
lixianyu | 0:740c1eb2df13 | 15 | |
lixianyu | 0:740c1eb2df13 | 16 | template<typename T, unsigned SIZE> |
lixianyu | 0:740c1eb2df13 | 17 | class BlynkFifo { |
lixianyu | 0:740c1eb2df13 | 18 | |
lixianyu | 0:740c1eb2df13 | 19 | BlynkFifo(const BlynkFifo<T, SIZE> & rb); |
lixianyu | 0:740c1eb2df13 | 20 | |
lixianyu | 0:740c1eb2df13 | 21 | public: |
lixianyu | 0:740c1eb2df13 | 22 | |
lixianyu | 0:740c1eb2df13 | 23 | BlynkFifo() : fst(0), lst(0), flag(0) {} |
lixianyu | 0:740c1eb2df13 | 24 | ~BlynkFifo() {} |
lixianyu | 0:740c1eb2df13 | 25 | |
lixianyu | 0:740c1eb2df13 | 26 | void clear() { |
lixianyu | 0:740c1eb2df13 | 27 | fst = 0; |
lixianyu | 0:740c1eb2df13 | 28 | lst = 0; |
lixianyu | 0:740c1eb2df13 | 29 | flag = 0; |
lixianyu | 0:740c1eb2df13 | 30 | } |
lixianyu | 0:740c1eb2df13 | 31 | |
lixianyu | 0:740c1eb2df13 | 32 | size_t write(const T* data, size_t n) |
lixianyu | 0:740c1eb2df13 | 33 | { |
lixianyu | 0:740c1eb2df13 | 34 | if ((n = BlynkMin(n, getFree()))) { |
lixianyu | 0:740c1eb2df13 | 35 | const size_t ch1 = BlynkMin(n, SIZE - lst); |
lixianyu | 0:740c1eb2df13 | 36 | memcpy(buffer + lst, data, ch1 * sizeof(T)); |
lixianyu | 0:740c1eb2df13 | 37 | lst = (lst + ch1) % SIZE; |
lixianyu | 0:740c1eb2df13 | 38 | |
lixianyu | 0:740c1eb2df13 | 39 | if (ch1 < n) { |
lixianyu | 0:740c1eb2df13 | 40 | const size_t ch2 = n - ch1; |
lixianyu | 0:740c1eb2df13 | 41 | memcpy(buffer + lst, data + ch1, ch2 * sizeof(T)); |
lixianyu | 0:740c1eb2df13 | 42 | lst = (lst + ch2) % SIZE; |
lixianyu | 0:740c1eb2df13 | 43 | } |
lixianyu | 0:740c1eb2df13 | 44 | |
lixianyu | 0:740c1eb2df13 | 45 | if (fst == lst) { |
lixianyu | 0:740c1eb2df13 | 46 | flag = 1; |
lixianyu | 0:740c1eb2df13 | 47 | } |
lixianyu | 0:740c1eb2df13 | 48 | } |
lixianyu | 0:740c1eb2df13 | 49 | return n; |
lixianyu | 0:740c1eb2df13 | 50 | } |
lixianyu | 0:740c1eb2df13 | 51 | |
lixianyu | 0:740c1eb2df13 | 52 | size_t read(T* dest, size_t n) |
lixianyu | 0:740c1eb2df13 | 53 | { |
lixianyu | 0:740c1eb2df13 | 54 | if ((n = BlynkMin(n, getOccupied()))) { |
lixianyu | 0:740c1eb2df13 | 55 | flag = 0; |
lixianyu | 0:740c1eb2df13 | 56 | |
lixianyu | 0:740c1eb2df13 | 57 | const size_t ch1 = BlynkMin(n, SIZE - fst); |
lixianyu | 0:740c1eb2df13 | 58 | memcpy(dest, buffer + fst, ch1 * sizeof(T)); |
lixianyu | 0:740c1eb2df13 | 59 | fst = (fst + ch1) % SIZE; |
lixianyu | 0:740c1eb2df13 | 60 | |
lixianyu | 0:740c1eb2df13 | 61 | if (ch1 < n) { |
lixianyu | 0:740c1eb2df13 | 62 | const size_t ch2 = n - ch1; |
lixianyu | 0:740c1eb2df13 | 63 | memcpy(dest + ch1, buffer + fst, ch2 * sizeof(T)); |
lixianyu | 0:740c1eb2df13 | 64 | fst = (fst + ch2) % SIZE; |
lixianyu | 0:740c1eb2df13 | 65 | } |
lixianyu | 0:740c1eb2df13 | 66 | } |
lixianyu | 0:740c1eb2df13 | 67 | return n; |
lixianyu | 0:740c1eb2df13 | 68 | } |
lixianyu | 0:740c1eb2df13 | 69 | |
lixianyu | 0:740c1eb2df13 | 70 | void dump(void) |
lixianyu | 0:740c1eb2df13 | 71 | { |
lixianyu | 0:740c1eb2df13 | 72 | for (int i = fst; i < lst; i++) { |
lixianyu | 0:740c1eb2df13 | 73 | pc.printf("0x%02X ", buffer[i]); |
lixianyu | 0:740c1eb2df13 | 74 | if (i % 16 == 0) { |
lixianyu | 0:740c1eb2df13 | 75 | pc.printf("\r\n"); |
lixianyu | 0:740c1eb2df13 | 76 | } |
lixianyu | 0:740c1eb2df13 | 77 | } |
lixianyu | 0:740c1eb2df13 | 78 | } |
lixianyu | 0:740c1eb2df13 | 79 | |
lixianyu | 0:740c1eb2df13 | 80 | bool push(const T& data) { |
lixianyu | 0:740c1eb2df13 | 81 | return write(&data, 1) == 1; |
lixianyu | 0:740c1eb2df13 | 82 | } |
lixianyu | 0:740c1eb2df13 | 83 | |
lixianyu | 0:740c1eb2df13 | 84 | size_t getOccupied() const { |
lixianyu | 0:740c1eb2df13 | 85 | if (lst == fst) { |
lixianyu | 0:740c1eb2df13 | 86 | return flag ? SIZE : 0; |
lixianyu | 0:740c1eb2df13 | 87 | } else if (lst > fst) { |
lixianyu | 0:740c1eb2df13 | 88 | return lst - fst; |
lixianyu | 0:740c1eb2df13 | 89 | } else { |
lixianyu | 0:740c1eb2df13 | 90 | return SIZE + lst - fst; |
lixianyu | 0:740c1eb2df13 | 91 | } |
lixianyu | 0:740c1eb2df13 | 92 | } |
lixianyu | 0:740c1eb2df13 | 93 | |
lixianyu | 0:740c1eb2df13 | 94 | size_t getFree() const { |
lixianyu | 0:740c1eb2df13 | 95 | return SIZE - getOccupied(); |
lixianyu | 0:740c1eb2df13 | 96 | } |
lixianyu | 0:740c1eb2df13 | 97 | |
lixianyu | 0:740c1eb2df13 | 98 | private: |
lixianyu | 0:740c1eb2df13 | 99 | T buffer[SIZE]; |
lixianyu | 0:740c1eb2df13 | 100 | size_t fst; |
lixianyu | 0:740c1eb2df13 | 101 | size_t lst; |
lixianyu | 0:740c1eb2df13 | 102 | uint8_t flag; |
lixianyu | 0:740c1eb2df13 | 103 | }; |
lixianyu | 0:740c1eb2df13 | 104 | |
lixianyu | 0:740c1eb2df13 | 105 | #endif |
lixianyu | 0:740c1eb2df13 | 106 |