Microduino

Dependencies:   mbed

Fork of Io_moon by Li Weiyi

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?

UserRevisionLine numberNew 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 <class T, unsigned N>
lixianyu 0:740c1eb2df13 17 class BlynkFifo
lixianyu 0:740c1eb2df13 18 {
lixianyu 0:740c1eb2df13 19 public:
lixianyu 0:740c1eb2df13 20 BlynkFifo()
lixianyu 0:740c1eb2df13 21 {
lixianyu 0:740c1eb2df13 22 clear();
lixianyu 0:740c1eb2df13 23 }
lixianyu 0:740c1eb2df13 24
lixianyu 0:740c1eb2df13 25 void clear()
lixianyu 0:740c1eb2df13 26 {
lixianyu 0:740c1eb2df13 27 _r = 0;
lixianyu 0:740c1eb2df13 28 _w = 0;
lixianyu 0:740c1eb2df13 29 }
lixianyu 0:740c1eb2df13 30
lixianyu 0:740c1eb2df13 31 ~BlynkFifo(void)
lixianyu 0:740c1eb2df13 32 {}
lixianyu 0:740c1eb2df13 33
lixianyu 0:740c1eb2df13 34 // writing thread/context API
lixianyu 0:740c1eb2df13 35 //-------------------------------------------------------------
lixianyu 0:740c1eb2df13 36
lixianyu 0:740c1eb2df13 37 bool writeable(void)
lixianyu 0:740c1eb2df13 38 {
lixianyu 0:740c1eb2df13 39 return free() > 0;
lixianyu 0:740c1eb2df13 40 }
lixianyu 0:740c1eb2df13 41
lixianyu 0:740c1eb2df13 42 int free(void)
lixianyu 0:740c1eb2df13 43 {
lixianyu 0:740c1eb2df13 44 int s = _r - _w;
lixianyu 0:740c1eb2df13 45 if (s <= 0)
lixianyu 0:740c1eb2df13 46 s += N;
lixianyu 0:740c1eb2df13 47 return s - 1;
lixianyu 0:740c1eb2df13 48 }
lixianyu 0:740c1eb2df13 49
lixianyu 0:740c1eb2df13 50 T put(const T& c)
lixianyu 0:740c1eb2df13 51 {
lixianyu 0:740c1eb2df13 52 int i = _w;
lixianyu 0:740c1eb2df13 53 int j = i;
lixianyu 0:740c1eb2df13 54 i = _inc(i);
lixianyu 0:740c1eb2df13 55 while (i == _r) // = !writeable()
lixianyu 0:740c1eb2df13 56 /* nothing / just wait */;
lixianyu 0:740c1eb2df13 57 _b[j] = c;
lixianyu 0:740c1eb2df13 58 _w = i;
lixianyu 0:740c1eb2df13 59 return c;
lixianyu 0:740c1eb2df13 60 }
lixianyu 0:740c1eb2df13 61
lixianyu 0:740c1eb2df13 62 int put(const T* p, int n, bool t = false)
lixianyu 0:740c1eb2df13 63 {
lixianyu 0:740c1eb2df13 64 int c = n;
lixianyu 0:740c1eb2df13 65 while (c)
lixianyu 0:740c1eb2df13 66 {
lixianyu 0:740c1eb2df13 67 int f;
lixianyu 0:740c1eb2df13 68 while ((f = free()) == 0) // wait for space
lixianyu 0:740c1eb2df13 69 {
lixianyu 0:740c1eb2df13 70 if (!t) return n - c; // no more space and not blocking
lixianyu 0:740c1eb2df13 71 /* nothing / just wait */;
lixianyu 0:740c1eb2df13 72 }
lixianyu 0:740c1eb2df13 73 // check free space
lixianyu 0:740c1eb2df13 74 if (c < f) f = c;
lixianyu 0:740c1eb2df13 75 int w = _w;
lixianyu 0:740c1eb2df13 76 int m = N - w;
lixianyu 0:740c1eb2df13 77 // check wrap
lixianyu 0:740c1eb2df13 78 if (f > m) f = m;
lixianyu 0:740c1eb2df13 79 memcpy(&_b[w], p, f);
lixianyu 0:740c1eb2df13 80 _w = _inc(w, f);
lixianyu 0:740c1eb2df13 81 c -= f;
lixianyu 0:740c1eb2df13 82 p += f;
lixianyu 0:740c1eb2df13 83 }
lixianyu 0:740c1eb2df13 84 return n - c;
lixianyu 0:740c1eb2df13 85 }
lixianyu 0:740c1eb2df13 86
lixianyu 0:740c1eb2df13 87 // reading thread/context API
lixianyu 0:740c1eb2df13 88 // --------------------------------------------------------
lixianyu 0:740c1eb2df13 89
lixianyu 0:740c1eb2df13 90 bool readable(void)
lixianyu 0:740c1eb2df13 91 {
lixianyu 0:740c1eb2df13 92 return (_r != _w);
lixianyu 0:740c1eb2df13 93 }
lixianyu 0:740c1eb2df13 94
lixianyu 0:740c1eb2df13 95 size_t size(void)
lixianyu 0:740c1eb2df13 96 {
lixianyu 0:740c1eb2df13 97 int s = _w - _r;
lixianyu 0:740c1eb2df13 98 if (s < 0)
lixianyu 0:740c1eb2df13 99 s += N;
lixianyu 0:740c1eb2df13 100 return s;
lixianyu 0:740c1eb2df13 101 }
lixianyu 0:740c1eb2df13 102
lixianyu 0:740c1eb2df13 103 T get(void)
lixianyu 0:740c1eb2df13 104 {
lixianyu 0:740c1eb2df13 105 int r = _r;
lixianyu 0:740c1eb2df13 106 while (r == _w) // = !readable()
lixianyu 0:740c1eb2df13 107 /* nothing / just wait */;
lixianyu 0:740c1eb2df13 108 T t = _b[r];
lixianyu 0:740c1eb2df13 109 _r = _inc(r);
lixianyu 0:740c1eb2df13 110 return t;
lixianyu 0:740c1eb2df13 111 }
lixianyu 0:740c1eb2df13 112
lixianyu 0:740c1eb2df13 113 int get(T* p, int n, bool t = false)
lixianyu 0:740c1eb2df13 114 {
lixianyu 0:740c1eb2df13 115 int c = n;
lixianyu 0:740c1eb2df13 116 while (c)
lixianyu 0:740c1eb2df13 117 {
lixianyu 0:740c1eb2df13 118 int f;
lixianyu 0:740c1eb2df13 119 for (;;) // wait for data
lixianyu 0:740c1eb2df13 120 {
lixianyu 0:740c1eb2df13 121 f = size();
lixianyu 0:740c1eb2df13 122 if (f) break; // free space
lixianyu 0:740c1eb2df13 123 if (!t) return n - c; // no space and not blocking
lixianyu 0:740c1eb2df13 124 /* nothing / just wait */;
lixianyu 0:740c1eb2df13 125 }
lixianyu 0:740c1eb2df13 126 // check available data
lixianyu 0:740c1eb2df13 127 if (c < f) f = c;
lixianyu 0:740c1eb2df13 128 int r = _r;
lixianyu 0:740c1eb2df13 129 int m = N - r;
lixianyu 0:740c1eb2df13 130 // check wrap
lixianyu 0:740c1eb2df13 131 if (f > m) f = m;
lixianyu 0:740c1eb2df13 132 memcpy(p, &_b[r], f);
lixianyu 0:740c1eb2df13 133 _r = _inc(r, f);
lixianyu 0:740c1eb2df13 134 c -= f;
lixianyu 0:740c1eb2df13 135 p += f;
lixianyu 0:740c1eb2df13 136 }
lixianyu 0:740c1eb2df13 137 return n - c;
lixianyu 0:740c1eb2df13 138 }
lixianyu 0:740c1eb2df13 139
lixianyu 0:740c1eb2df13 140 private:
lixianyu 0:740c1eb2df13 141 inline int _inc(int i, int n = 1)
lixianyu 0:740c1eb2df13 142 {
lixianyu 0:740c1eb2df13 143 return (i + n) % N;
lixianyu 0:740c1eb2df13 144 }
lixianyu 0:740c1eb2df13 145
lixianyu 0:740c1eb2df13 146 T _b[N];
lixianyu 0:740c1eb2df13 147 volatile int _w;
lixianyu 0:740c1eb2df13 148 volatile int _r;
lixianyu 0:740c1eb2df13 149 };
lixianyu 0:740c1eb2df13 150
lixianyu 0:740c1eb2df13 151 #endif
lixianyu 0:740c1eb2df13 152