PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

circular_buffer.h 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. MIT License
  3. Copyright (c) 2018 Antonio Alexander Brewer (tonton81) - https://github.com/tonton81
  4. Contributors:
  5. Tim - https://github.com/Defragster
  6. Mike - https://github.com/mjs513
  7. Designed and tested for PJRC Teensy 3.2, 3.5, and 3.6 boards.
  8. May or may not work on other microcontrollers, support for them will not be provided.
  9. Use at your own risk.
  10. Forum link : https://forum.pjrc.com/threads/50395-Circular_Buffer
  11. Permission is hereby granted, free of charge, to any person obtaining a copy
  12. of this software and associated documentation files (the "Software"), to deal
  13. in the Software without restriction, including without limitation the rights
  14. to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
  15. copies of the Software, and to permit persons to whom the Software is
  16. furnished to do so, subject to the following conditions:
  17. The above copyright notice and this permission notice shall be included in all
  18. copies or substantial portions of the Software.
  19. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. SOFTWARE.
  26. */
  27. #ifndef CIRCULAR_BUFFER_H
  28. #define CIRCULAR_BUFFER_H
  29. #include <algorithm>
  30. template<typename T, uint16_t _size, uint16_t multi = 0>
  31. class Circular_Buffer {
  32. public:
  33. void push_back(T value) { return write(value); }
  34. void push_front(T value);
  35. T pop_front() { return read(); }
  36. T pop_back();
  37. void write(T value);
  38. void push_back(const T *buffer, uint16_t length) { write(buffer, length); }
  39. void write(const T *buffer, uint16_t length);
  40. void push_front(const T *buffer, uint16_t length);
  41. T peek(uint16_t pos = 0);
  42. T peekBytes(T *buffer, uint16_t length);
  43. T read();
  44. T pop_front(T *buffer, uint16_t length) { return readBytes(buffer,length); }
  45. T peek_front(T *buffer, uint16_t length, uint32_t entry = 0);
  46. T read(T *buffer, uint16_t length) { return readBytes(buffer,length); }
  47. T readBytes(T *buffer, uint16_t length);
  48. void flush() { clear(); }
  49. void clear() { head = tail = _available = 0; }
  50. void print(const char *p);
  51. void println(const char *p);
  52. uint16_t size() { return _available; }
  53. uint16_t available() { return _available; }
  54. uint16_t capacity() { return _size; }
  55. uint16_t length_back() { return (((int)_cabuf[((head+size()-1)&(_size-1))][0] << 8*sizeof(T)) | (int)_cabuf[((head+size()-1)&(_size-1))][1]); }
  56. uint16_t length_front() { return (((int)_cabuf[((head)&(_size-1))][0] << 8*sizeof(T)) | (int)_cabuf[((head)&(_size-1))][1]); }
  57. T list();
  58. T variance();
  59. T deviation();
  60. T average();
  61. bool remove(uint16_t pos);
  62. T median(bool override = 0);
  63. void sort_ascending();
  64. void sort_descending();
  65. T sum();
  66. T min();
  67. T max();
  68. T mean() { return average(); }
  69. T max_size() { return multi; }
  70. T pop_back(T *buffer, uint16_t length);
  71. T* peek_front() { return front(); }
  72. T* peek_back() { return back(); }
  73. T* front() { return _cabuf[((head)&(_size-1))]+2; }
  74. T* back() { return _cabuf[((tail-1)&(_size-1))]+2; }
  75. bool replace(T *buffer, uint16_t length, int pos1, int pos2, int pos3, int pos4 = -1, int pos5 = -1);
  76. bool isEqual(const T *buffer);
  77. bool find(T *buffer, uint16_t length, int pos1, int pos2, int pos3, int pos4 = -1, int pos5 = -1);
  78. bool findRemove(T *buffer, uint16_t length, int pos1, int pos2, int pos3, int pos4 = -1, int pos5 = -1);
  79. protected:
  80. private:
  81. volatile uint16_t head = 0;
  82. volatile uint16_t tail = 0;
  83. volatile uint16_t _available = 0;
  84. T _cbuf[_size];
  85. T _cabuf[_size][multi+2];
  86. };
  87. template<typename T, uint16_t _size, uint16_t multi>
  88. bool Circular_Buffer<T,_size,multi>::remove(uint16_t pos) {
  89. if ( multi ) {
  90. if ( pos >= _size ) return 0;
  91. int32_t find_area = -1;
  92. for ( uint16_t i = 0; i < _size; i++ ) {
  93. if ( ((head+i)&(_size-1)) == pos ) {
  94. find_area = i;
  95. break;
  96. }
  97. }
  98. if ( find_area == -1 ) return 0;
  99. while ( ((head+find_area)&(_size-1)) != ((head)&(_size-1)) ) {
  100. memmove(_cabuf[((head+find_area)&(_size-1))],_cabuf[((head+find_area-1)&(_size-1))], (2+multi)*sizeof(T));
  101. find_area--;
  102. }
  103. head = ((head + 1)&(2*_size-1));
  104. _available--;
  105. return 1;
  106. }
  107. return 0;
  108. }
  109. template<typename T, uint16_t _size, uint16_t multi>
  110. bool Circular_Buffer<T, _size, multi>::findRemove(T *buffer, uint16_t length, int pos1, int pos2, int pos3, int pos4, int pos5) {
  111. uint8_t input_count = 3;
  112. int32_t found = -1;
  113. if ( pos4 != -1 ) input_count = 4;
  114. if ( pos5 != -1 ) input_count = 5;
  115. for ( uint16_t j = 0; j < _available; j++ ) {
  116. switch ( input_count ) {
  117. case 3: {
  118. if ( _cabuf[ ((head+j)&(_size-1)) ][pos1+2] == buffer[pos1] && _cabuf[ ((head+j)&(_size-1)) ][pos2+2] == buffer[pos2] &&
  119. _cabuf[ ((head+j)&(_size-1)) ][pos3+2] == buffer[pos3] ) {
  120. found = j;
  121. break;
  122. }
  123. }
  124. case 4: {
  125. if ( _cabuf[ ((head+j)&(_size-1)) ][pos1+2] == buffer[pos1] && _cabuf[ ((head+j)&(_size-1)) ][pos2+2] == buffer[pos2] &&
  126. _cabuf[ ((head+j)&(_size-1)) ][pos3+2] == buffer[pos3] && _cabuf[ ((head+j)&(_size-1)) ][pos4+2] == buffer[pos4] ) {
  127. found = j;
  128. break;
  129. }
  130. }
  131. case 5: {
  132. if ( _cabuf[ ((head+j)&(_size-1)) ][pos1+2] == buffer[pos1] && _cabuf[ ((head+j)&(_size-1)) ][pos2+2] == buffer[pos2] &&
  133. _cabuf[ ((head+j)&(_size-1)) ][pos3+2] == buffer[pos3] && _cabuf[ ((head+j)&(_size-1)) ][pos4+2] == buffer[pos4] &&
  134. _cabuf[ ((head+j)&(_size-1)) ][pos5+2] == buffer[pos5] ) {
  135. found = j;
  136. break;
  137. }
  138. }
  139. }
  140. if ( found >= 0 ) {
  141. remove(found);
  142. return 1;
  143. }
  144. }
  145. return 0;
  146. }
  147. template<typename T, uint16_t _size, uint16_t multi>
  148. bool Circular_Buffer<T, _size, multi>::find(T *buffer, uint16_t length, int pos1, int pos2, int pos3, int pos4, int pos5) {
  149. uint8_t input_count = 3;
  150. bool found = 0;
  151. if ( pos4 != -1 ) input_count = 4;
  152. if ( pos5 != -1 ) input_count = 5;
  153. for ( uint16_t j = 0; j < _available; j++ ) {
  154. switch ( input_count ) {
  155. case 3: {
  156. if ( _cabuf[ ((head+j)&(_size-1)) ][pos1+2] == buffer[pos1] && _cabuf[ ((head+j)&(_size-1)) ][pos2+2] == buffer[pos2] &&
  157. _cabuf[ ((head+j)&(_size-1)) ][pos3+2] == buffer[pos3] ) {
  158. found = 1;
  159. break;
  160. }
  161. }
  162. case 4: {
  163. if ( _cabuf[ ((head+j)&(_size-1)) ][pos1+2] == buffer[pos1] && _cabuf[ ((head+j)&(_size-1)) ][pos2+2] == buffer[pos2] &&
  164. _cabuf[ ((head+j)&(_size-1)) ][pos3+2] == buffer[pos3] && _cabuf[ ((head+j)&(_size-1)) ][pos4+2] == buffer[pos4] ) {
  165. found = 1;
  166. break;
  167. }
  168. }
  169. case 5: {
  170. if ( _cabuf[ ((head+j)&(_size-1)) ][pos1+2] == buffer[pos1] && _cabuf[ ((head+j)&(_size-1)) ][pos2+2] == buffer[pos2] &&
  171. _cabuf[ ((head+j)&(_size-1)) ][pos3+2] == buffer[pos3] && _cabuf[ ((head+j)&(_size-1)) ][pos4+2] == buffer[pos4] &&
  172. _cabuf[ ((head+j)&(_size-1)) ][pos5+2] == buffer[pos5] ) {
  173. found = 1;
  174. break;
  175. }
  176. }
  177. }
  178. if ( found ) {
  179. memmove(buffer, _cabuf[ ((head+j)&(_size-1)) ]+2,length*sizeof(T));
  180. break;
  181. }
  182. }
  183. return found;
  184. }
  185. template<typename T, uint16_t _size, uint16_t multi>
  186. bool Circular_Buffer<T, _size, multi>::replace(T *buffer, uint16_t length, int pos1, int pos2, int pos3, int pos4, int pos5) {
  187. uint8_t input_count = 3;
  188. bool found = 0;
  189. if ( pos4 != -1 ) input_count = 4;
  190. if ( pos5 != -1 ) input_count = 5;
  191. for ( uint16_t j = 0; j < _available; j++ ) {
  192. switch ( input_count ) {
  193. case 3: {
  194. if ( _cabuf[ ((head+j)&(_size-1)) ][pos1+2] == buffer[pos1] && _cabuf[ ((head+j)&(_size-1)) ][pos2+2] == buffer[pos2] &&
  195. _cabuf[ ((head+j)&(_size-1)) ][pos3+2] == buffer[pos3] ) {
  196. found = 1;
  197. break;
  198. }
  199. }
  200. case 4: {
  201. if ( _cabuf[ ((head+j)&(_size-1)) ][pos1+2] == buffer[pos1] && _cabuf[ ((head+j)&(_size-1)) ][pos2+2] == buffer[pos2] &&
  202. _cabuf[ ((head+j)&(_size-1)) ][pos3+2] == buffer[pos3] && _cabuf[ ((head+j)&(_size-1)) ][pos4+2] == buffer[pos4] ) {
  203. found = 1;
  204. break;
  205. }
  206. }
  207. case 5: {
  208. if ( _cabuf[ ((head+j)&(_size-1)) ][pos1+2] == buffer[pos1] && _cabuf[ ((head+j)&(_size-1)) ][pos2+2] == buffer[pos2] &&
  209. _cabuf[ ((head+j)&(_size-1)) ][pos3+2] == buffer[pos3] && _cabuf[ ((head+j)&(_size-1)) ][pos4+2] == buffer[pos4] &&
  210. _cabuf[ ((head+j)&(_size-1)) ][pos5+2] == buffer[pos5] ) {
  211. found = 1;
  212. break;
  213. }
  214. }
  215. }
  216. if ( found ) {
  217. _cabuf[ ((head+j)&(_size-1)) ][0] = length & 0xFF00;
  218. _cabuf[ ((head+j)&(_size-1)) ][1] = length & 0xFF;
  219. memmove(_cabuf[ ((head+j)&(_size-1)) ]+2,buffer,length*sizeof(T));
  220. break;
  221. }
  222. }
  223. return found;
  224. }
  225. template<typename T, uint16_t _size, uint16_t multi>
  226. bool Circular_Buffer<T,_size,multi>::isEqual(const T *buffer) {
  227. if ( multi ) {
  228. bool success = 1;
  229. for ( uint16_t j = 0; j < _available; j++ ) {
  230. success = 1;
  231. for ( uint16_t k = 0; k < (_cabuf[ ((head+j)&(_size-1)) ][0] | _cabuf[ ((head+j)&(_size-1)) ][1]); k++ ) {
  232. if ( _cabuf[ ((head+j)&(_size-1)) ][k+2] != buffer[k] ) {
  233. success = 0;
  234. break;
  235. }
  236. }
  237. if ( success ) return 1;
  238. }
  239. }
  240. return 0;
  241. }
  242. template<typename T, uint16_t _size, uint16_t multi>
  243. void Circular_Buffer<T,_size,multi>::print(const char *p) {
  244. if ( multi ) return;
  245. write((T*)p,strlen(p));
  246. }
  247. template<typename T, uint16_t _size, uint16_t multi>
  248. void Circular_Buffer<T,_size,multi>::println(const char *p) {
  249. if ( multi ) return;
  250. write((T*)p,strlen(p));
  251. write('\n');
  252. }
  253. template<typename T, uint16_t _size, uint16_t multi>
  254. void Circular_Buffer<T,_size,multi>::push_front(const T *buffer, uint16_t length) {
  255. if ( multi ) {
  256. if ( tail == (head ^ _size) ) tail = ((tail - 1)&(2*_size-1));
  257. head = ((head - 1)&(2*_size-1));
  258. _cabuf[(head&(_size-1))][0] = length & 0xFF00;
  259. _cabuf[(head&(_size-1))][1] = length & 0xFF;
  260. memmove(_cabuf[((head)&(_size-1))]+2,buffer,length*sizeof(T));
  261. if ( _available < _size ) _available++;
  262. return;
  263. }
  264. for ( uint16_t i = length-1; i > 0; i-- ) push_front(buffer[i]);
  265. push_front(buffer[0]);
  266. }
  267. template<typename T, uint16_t _size, uint16_t multi>
  268. T Circular_Buffer<T,_size,multi>::pop_back() {
  269. if ( _available ) {
  270. if ( _available ) _available--;
  271. tail = ((tail - 1)&(2*_size-1));
  272. return _cbuf[((tail)&(_size-1))];
  273. }
  274. return -1;
  275. }
  276. template<typename T, uint16_t _size, uint16_t multi>
  277. void Circular_Buffer<T,_size,multi>::push_front(T value) {
  278. if ( multi ) return;
  279. head = ((head - 1)&(2*_size-1));
  280. _cbuf[((head)&(_size-1))] = value;
  281. if ( _available < _size ) _available++;
  282. }
  283. template<typename T, uint16_t _size, uint16_t multi>
  284. void Circular_Buffer<T,_size,multi>::write(const T *buffer, uint16_t length) {
  285. if ( multi ) {
  286. _cabuf[((tail)&(_size-1))][0] = length & 0xFF00;
  287. _cabuf[((tail)&(_size-1))][1] = length & 0xFF;
  288. memmove(_cabuf[((tail)&(_size-1))]+2,buffer,length*sizeof(T));
  289. if ( tail == ((head ^ _size)) ) head = ((head + 1)&(2*_size-1));
  290. tail = ((tail + 1)&(2*_size-1));
  291. if ( _available < _size ) _available++;
  292. return;
  293. }
  294. if ( ( _available += length ) >= _size ) _available = _size;
  295. if ( length < ( _size - tail ) ) {
  296. memmove(_cbuf+tail,buffer,length*sizeof(T));
  297. tail = ((tail + length)&(2*_size-1));
  298. }
  299. else for ( uint16_t i = 0; i < length; i++ ) write(buffer[i]);
  300. }
  301. template<typename T, uint16_t _size, uint16_t multi>
  302. void Circular_Buffer<T,_size,multi>::write(T value) {
  303. if ( multi ) return;
  304. if ( _available < _size ) _available++;
  305. _cbuf[((tail)&(_size-1))] = value;
  306. if ( tail == ((head ^ _size)) ) head = ((head + 1)&(2*_size-1));
  307. tail = ((tail + 1)&(2*_size-1));
  308. }
  309. template<typename T, uint16_t _size, uint16_t multi>
  310. T Circular_Buffer<T,_size,multi>::list() {
  311. if ( multi ) {
  312. if ( !size() ) {
  313. Serial.println("There are no queues available..."); return 0;
  314. }
  315. Serial.print("\nCircular Array Buffer Queue Size: "); Serial.print(size()); Serial.print(" / "); Serial.println(_size);
  316. Serial.print("\nFirst Entry: ");
  317. for ( uint16_t i = 2; i <= (((int)_cabuf[((head)&(_size-1))][0] << 8*sizeof(T)) | (int)_cabuf[((head)&(_size-1))][1])+1; i++ ) {
  318. if ( (int)(_cabuf[((head+i)&(_size-1))][i]) != (T)(_cabuf[((head+i)&(_size-1))][i]) ) { // possible float?
  319. Serial.print(_cabuf[((head)&(_size-1))][i],7);
  320. }
  321. else Serial.print(_cabuf[((head)&(_size-1))][i]);
  322. Serial.print(" ");
  323. } Serial.print("("); Serial.print((((int)_cabuf[((head)&(_size-1))][0] << 8*sizeof(T)) | (int)_cabuf[((head)&(_size-1))][1])); Serial.println(" entries.)");
  324. Serial.print("Last Entry: ");
  325. for ( uint16_t i = 2; i <= (((int)_cabuf[((head+size()-1)&(_size-1))][0] << 8*sizeof(T)) | (int)_cabuf[((head+size()-1)&(_size-1))][1])+1; i++ ) {
  326. if ( (int)(_cabuf[((head+size()-1)&(_size-1))][i]) != (T)(_cabuf[((head+size()-1)&(_size-1))][i]) ) { // possible float?
  327. Serial.print(_cabuf[((head+size()-1)&(_size-1))][i],7);
  328. }
  329. else Serial.print(_cabuf[((head+size()-1)&(_size-1))][i]);
  330. Serial.print(" ");
  331. } Serial.print("("); Serial.print((((int)_cabuf[((head+size()-1)&(_size-1))][0] << 8*sizeof(T)) | (int)_cabuf[((head+size()-1)&(_size-1))][1])); Serial.println(" entries.)");
  332. Serial.print("\n[Indice] [Entries]\n\n");
  333. for ( uint16_t i = 0; i < size(); i++ ) {
  334. Serial.print(" ");
  335. Serial.print(((head+i)&(_size-1)));
  336. Serial.print("\t\t");
  337. for ( uint16_t j = 2; j <= (((int)_cabuf[((head+i)&(_size-1))][0] << 8*sizeof(T)) | (int)_cabuf[((head+i)&(_size-1))][1])+1; j++ ) {
  338. if ( (int)(_cabuf[((head+i)&(_size-1))][j]) != (T)(_cabuf[((head+i)&(_size-1))][j]) ) { // possible float?
  339. Serial.print(_cabuf[((head+i)&(_size-1))][j],7); Serial.print("\t");
  340. }
  341. else {
  342. Serial.print(_cabuf[((head+i)&(_size-1))][j]); Serial.print("\t");
  343. }
  344. } Serial.print("("); Serial.print((((int)_cabuf[((head+i)&(_size-1))][0] << 8*sizeof(T)) | (int)_cabuf[((head+i)&(_size-1))][1])); Serial.println(" entries.)");
  345. }
  346. return _available;
  347. }
  348. if ( !size() ) {
  349. Serial.println("There are no queues available..."); return 0;
  350. }
  351. Serial.print("\nCircular Ring Buffer Queue Size: "); Serial.print(size()); Serial.print(" / "); Serial.println(_size);
  352. Serial.print("\nIndice: \t");
  353. for ( uint16_t i = 0; i < _available; i++ ) {
  354. Serial.print("[");
  355. Serial.print((head+i)&(_size-1)); Serial.print("] \t");
  356. }
  357. Serial.print("\nEntries:\t");
  358. for ( uint16_t i = 0; i < _available; i++ ) {
  359. if ( (int)_cbuf[((head+i)&(_size-1))] != (T)_cbuf[((head+i)&(_size-1))] ) { // possible float?
  360. Serial.print(_cbuf[((head+i)&(_size-1))],7); Serial.print("\t");
  361. }
  362. else {
  363. Serial.print(_cbuf[((head+i)&(_size-1))]); Serial.print("\t\t");
  364. }
  365. } Serial.println('\n');
  366. return _available;
  367. }
  368. template<typename T, uint16_t _size, uint16_t multi>
  369. T Circular_Buffer<T,_size,multi>::read() {
  370. if ( multi ) {
  371. head = ((head + 1)&(2*_size-1));
  372. if ( _available ) _available--;
  373. return 0;
  374. }
  375. if ( _available ) _available--;
  376. T value = _cbuf[((head)&(_size-1))];
  377. head = ((head + 1)&(2*_size-1));
  378. return value;
  379. }
  380. template<typename T, uint16_t _size, uint16_t multi>
  381. T Circular_Buffer<T,_size,multi>::sum() {
  382. if ( multi || !_available ) return 0;
  383. T value = 0;
  384. for ( uint16_t i = 0; i < _available; i++ ) value += _cbuf[((head+i)&(_size-1))];
  385. return value;
  386. }
  387. template<typename T, uint16_t _size, uint16_t multi>
  388. T Circular_Buffer<T,_size,multi>::average() {
  389. if ( multi || !_available ) return 0;
  390. return sum()/_available;
  391. }
  392. template<typename T, uint16_t _size, uint16_t multi>
  393. T Circular_Buffer<T,_size,multi>::variance() {
  394. if ( multi || !_available ) return 0;
  395. T _mean = average();
  396. T value = 0;
  397. for ( uint16_t i = 0; i < _available; i++ ) {
  398. value += ((_cbuf[((head+i)&(_size-1))] - _mean) * (_cbuf[((head+i)&(_size-1))] - _mean));
  399. }
  400. value /= _available;
  401. return value;
  402. }
  403. template<typename T, uint16_t _size, uint16_t multi>
  404. T Circular_Buffer<T,_size,multi>::deviation() {
  405. if ( multi || !_available ) return 0;
  406. return sqrt(variance());
  407. }
  408. template<typename T, uint16_t _size, uint16_t multi>
  409. T Circular_Buffer<T,_size,multi>::peek(uint16_t pos) {
  410. if ( multi ) return 0;
  411. if ( pos > _size ) return 0;
  412. return _cbuf[((head+pos)&(_size-1))];
  413. }
  414. template<typename T, uint16_t _size, uint16_t multi>
  415. void Circular_Buffer<T,_size,multi>::sort_ascending() {
  416. if ( multi || !_available ) return;
  417. T buffer[_available];
  418. for ( uint16_t i = 0; i < _available; i++ ) buffer[i] = _cbuf[((head+i)&(_size-1))];
  419. std::sort(&buffer[0], &buffer[_available]); // sort ascending
  420. for ( uint16_t i = 0; i < _available; i++ ) _cbuf[((head+i)&(_size-1))] = buffer[i];
  421. }
  422. template<typename T, uint16_t _size, uint16_t multi>
  423. void Circular_Buffer<T,_size,multi>::sort_descending() {
  424. if ( multi || !_available ) return;
  425. sort_ascending();
  426. T buffer[_available];
  427. for ( uint16_t i = 0; i < _available; i++ ) buffer[i] = _cbuf[((head+i)&(_size-1))];
  428. std::reverse(&buffer[0], &buffer[_available]); // sort descending
  429. for ( uint16_t i = 0; i < _available; i++ ) _cbuf[((head+i)&(_size-1))] = buffer[i];
  430. }
  431. template<typename T, uint16_t _size, uint16_t multi>
  432. T Circular_Buffer<T,_size,multi>::median(bool override) {
  433. if ( multi || !_available ) return 0;
  434. if ( override ) sort_ascending();
  435. else {
  436. T buffer[_available];
  437. for ( uint16_t i = 0; i < _available; i++ ) buffer[i] = _cbuf[((head+i)&(_size-1))];
  438. std::sort(&buffer[0], &buffer[_available]); // sort ascending
  439. }
  440. if ( !(_available % 2) ) {
  441. return ( _cbuf[((head+((_available/2)-1))&(_size-1))] + _cbuf[((head+(_available/2))&(_size-1))] ) /2;
  442. }
  443. else return _cbuf[((head+((_available/2)))&(_size-1))];
  444. return 0;
  445. }
  446. template<typename T, uint16_t _size, uint16_t multi>
  447. T Circular_Buffer<T,_size,multi>::max() {
  448. if ( multi || !_available ) return 0;
  449. T buffer[_available];
  450. for ( uint16_t i = 0; i < _available; i++ ) buffer[i] = _cbuf[((head+i)&(_size-1))];
  451. std::sort(&buffer[0], &buffer[_available]); // sort ascending
  452. return buffer[_available-1];
  453. }
  454. template<typename T, uint16_t _size, uint16_t multi>
  455. T Circular_Buffer<T,_size,multi>::min() {
  456. if ( multi || !_available ) return 0;
  457. T buffer[_available];
  458. for ( uint16_t i = 0; i < _available; i++ ) buffer[i] = _cbuf[((head+i)&(_size-1))];
  459. std::sort(&buffer[0], &buffer[_available]); // sort ascending
  460. return buffer[0];
  461. }
  462. template<typename T, uint16_t _size, uint16_t multi>
  463. T Circular_Buffer<T,_size,multi>::peekBytes(T *buffer, uint16_t length) {
  464. if ( multi ) return 0;
  465. uint16_t _count;
  466. ( _available < length ) ? _count = _available : _count = length;
  467. if ( _count < ( _size - head ) ) memmove(buffer,_cbuf,_count*sizeof(T));
  468. else for ( uint16_t i = 0; i < _count; i++ ) buffer[i] = peek(i);
  469. return _count;
  470. }
  471. template<typename T, uint16_t _size, uint16_t multi>
  472. T Circular_Buffer<T,_size,multi>::peek_front(T *buffer, uint16_t length, uint32_t entry) {
  473. if ( multi ) {
  474. memmove(&buffer[0],&_cabuf[((head+entry)&(_size-1))][2],length*sizeof(T)); // update CA buffer
  475. return 0;
  476. }
  477. }
  478. template<typename T, uint16_t _size, uint16_t multi>
  479. T Circular_Buffer<T,_size,multi>::readBytes(T *buffer, uint16_t length) {
  480. if ( multi ) {
  481. memmove(&buffer[0],&_cabuf[((head)&(_size-1))][2],length*sizeof(T)); // update CA buffer
  482. read();
  483. return 0;
  484. }
  485. uint16_t _count;
  486. ( _available < length ) ? _count = _available : _count = length; // memmove if aligned
  487. if ( _count < ( _size - head ) ) {
  488. _available -= length;
  489. memmove(buffer,_cbuf,_count*sizeof(T));
  490. head = ((head + _count)&(2*_size-1));
  491. }
  492. else for ( uint16_t i = 0; i < _count; i++ ) buffer[i] = read(); // if buffer rollover
  493. return _count;
  494. }
  495. template<typename T, uint16_t _size, uint16_t multi>
  496. T Circular_Buffer<T,_size,multi>::pop_back(T *buffer, uint16_t length) {
  497. if ( multi ) {
  498. memmove(&buffer[0],&_cabuf[((tail-1)&(_size-1))][2],length*sizeof(T));
  499. tail = (tail - 1)&(2*_size-1);
  500. if ( _available ) _available--;
  501. return 0;
  502. }
  503. }
  504. #endif // Circular_Buffer_H