* AM2321的取温度间隔得大于2s,否则,i2c会不工作了 * SimpleTimer有个bug,会导致两次快速的读温度,现在读温度函数里加了保护 * Blynk有个bug,会导致无法把数据传到服务器 * 现在可以正常工作了
BlynkFifo2.h
00001 /** 00002 * @file BlynkFifo.h 00003 * @author Volodymyr Shymanskyy 00004 * @license This project is released under the MIT License (MIT) 00005 * @copyright Copyright (c) 2015 Volodymyr Shymanskyy 00006 * @date Feb 2015 00007 * @brief FIFO implementation 00008 * 00009 */ 00010 00011 #ifndef BlynkFifo_h 00012 #define BlynkFifo_h 00013 00014 #include <utility/BlynkUtility.h> 00015 00016 template <class T, unsigned N> 00017 class BlynkFifo 00018 { 00019 public: 00020 BlynkFifo() 00021 { 00022 clear(); 00023 } 00024 00025 void clear() 00026 { 00027 _r = 0; 00028 _w = 0; 00029 } 00030 00031 ~BlynkFifo(void) 00032 {} 00033 00034 // writing thread/context API 00035 //------------------------------------------------------------- 00036 00037 bool writeable(void) 00038 { 00039 return free() > 0; 00040 } 00041 00042 int free(void) 00043 { 00044 int s = _r - _w; 00045 if (s <= 0) 00046 s += N; 00047 return s - 1; 00048 } 00049 00050 T put(const T& c) 00051 { 00052 int i = _w; 00053 int j = i; 00054 i = _inc(i); 00055 while (i == _r) // = !writeable() 00056 /* nothing / just wait */; 00057 _b[j] = c; 00058 _w = i; 00059 return c; 00060 } 00061 00062 int put(const T* p, int n, bool t = false) 00063 { 00064 int c = n; 00065 while (c) 00066 { 00067 int f; 00068 while ((f = free()) == 0) // wait for space 00069 { 00070 if (!t) return n - c; // no more space and not blocking 00071 /* nothing / just wait */; 00072 } 00073 // check free space 00074 if (c < f) f = c; 00075 int w = _w; 00076 int m = N - w; 00077 // check wrap 00078 if (f > m) f = m; 00079 memcpy(&_b[w], p, f); 00080 _w = _inc(w, f); 00081 c -= f; 00082 p += f; 00083 } 00084 return n - c; 00085 } 00086 00087 // reading thread/context API 00088 // -------------------------------------------------------- 00089 00090 bool readable(void) 00091 { 00092 return (_r != _w); 00093 } 00094 00095 size_t size(void) 00096 { 00097 int s = _w - _r; 00098 if (s < 0) 00099 s += N; 00100 return s; 00101 } 00102 00103 T get(void) 00104 { 00105 int r = _r; 00106 while (r == _w) // = !readable() 00107 /* nothing / just wait */; 00108 T t = _b[r]; 00109 _r = _inc(r); 00110 return t; 00111 } 00112 00113 int get(T* p, int n, bool t = false) 00114 { 00115 int c = n; 00116 while (c) 00117 { 00118 int f; 00119 for (;;) // wait for data 00120 { 00121 f = size(); 00122 if (f) break; // free space 00123 if (!t) return n - c; // no space and not blocking 00124 /* nothing / just wait */; 00125 } 00126 // check available data 00127 if (c < f) f = c; 00128 int r = _r; 00129 int m = N - r; 00130 // check wrap 00131 if (f > m) f = m; 00132 memcpy(p, &_b[r], f); 00133 _r = _inc(r, f); 00134 c -= f; 00135 p += f; 00136 } 00137 return n - c; 00138 } 00139 00140 private: 00141 inline int _inc(int i, int n = 1) 00142 { 00143 return (i + n) % N; 00144 } 00145 00146 T _b[N]; 00147 volatile int _w; 00148 volatile int _r; 00149 }; 00150 00151 #endif 00152
Generated on Tue Jul 12 2022 13:01:26 by 1.7.2