Naze32 clone with Frysky receiver
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

CircularBuffer.h 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2015 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MBED_CIRCULARBUFFER_H
  17. #define MBED_CIRCULARBUFFER_H
  18. namespace mbed {
  19. /** Templated Circular buffer class
  20. */
  21. template<typename T, uint32_t BufferSize, typename CounterType = uint32_t>
  22. class CircularBuffer {
  23. public:
  24. CircularBuffer() : _head(0), _tail(0), _full(false) {
  25. }
  26. ~CircularBuffer() {
  27. }
  28. /** Push the transaction to the buffer. This overwrites the buffer if it's
  29. * full
  30. *
  31. * @param data Data to be pushed to the buffer
  32. */
  33. void push(const T& data) {
  34. if (full()) {
  35. _tail++;
  36. _tail %= BufferSize;
  37. }
  38. _pool[_head++] = data;
  39. _head %= BufferSize;
  40. if (_head == _tail) {
  41. _full = true;
  42. }
  43. }
  44. /** Pop the transaction from the buffer
  45. *
  46. * @param data Data to be pushed to the buffer
  47. * @return True if the buffer is not empty and data contains a transaction, false otherwise
  48. */
  49. bool pop(T& data) {
  50. if (!empty()) {
  51. data = _pool[_tail++];
  52. _tail %= BufferSize;
  53. _full = false;
  54. return true;
  55. }
  56. return false;
  57. }
  58. /** Check if the buffer is empty
  59. *
  60. * @return True if the buffer is empty, false if not
  61. */
  62. bool empty() {
  63. return (_head == _tail) && !_full;
  64. }
  65. /** Check if the buffer is full
  66. *
  67. * @return True if the buffer is full, false if not
  68. */
  69. bool full() {
  70. return _full;
  71. }
  72. /** Reset the buffer
  73. *
  74. */
  75. void reset() {
  76. _head = 0;
  77. _tail = 0;
  78. _full = false;
  79. }
  80. private:
  81. T _pool[BufferSize];
  82. volatile CounterType _head;
  83. volatile CounterType _tail;
  84. volatile bool _full;
  85. };
  86. }
  87. #endif