You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.2KB

  1. // Copyright 2009 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. //
  16. // -----------------------------------------------------------------------------
  17. //
  18. // Important: All buffer sizes are expected to be less than 256! (fit in 8
  19. // bits), and must be powers of 2.
  20. #ifndef AVRLIB_RING_BUFFER_H_
  21. #define AVRLIB_RING_BUFFER_H_
  22. #include "avrlib/base.h"
  23. #include "avrlib/avrlib.h"
  24. namespace avrlib {
  25. // Circular buffer, used for example for Serial input, Software serial output,
  26. // Audio rendering... A buffer is created for each Owner - for example,
  27. // Buffer<AudioClient> represents the audio buffer used by AudioClient.
  28. template<typename Owner>
  29. class RingBuffer : public Input, Output {
  30. public:
  31. typedef typename Owner::Value Value;
  32. enum {
  33. size = Owner::buffer_size,
  34. data_size = Owner::data_size
  35. };
  36. RingBuffer() { }
  37. static inline uint8_t capacity() { return size; }
  38. static inline void Write(Value v) {
  39. while (!writable());
  40. Overwrite(v);
  41. }
  42. static inline uint8_t writable() {
  43. return (read_ptr_ - write_ptr_ - 1) & (size - 1);
  44. }
  45. static inline uint8_t NonBlockingWrite(Value v) {
  46. if (writable()) {
  47. Overwrite(v);
  48. return 1;
  49. } else {
  50. return 0;
  51. }
  52. }
  53. static inline void Overwrite(Value v) {
  54. uint8_t w = write_ptr_;
  55. buffer_[w] = v;
  56. write_ptr_ = (w + 1) & (size - 1);
  57. }
  58. static void Overwrite2(Value v1, Value v2) {
  59. uint8_t w = write_ptr_;
  60. buffer_[w] = v1;
  61. buffer_[w + 1] = v2;
  62. write_ptr_ = (w + 2) & (size - 1);
  63. }
  64. static inline uint8_t Requested() { return 0; }
  65. static inline Value Read() {
  66. while (!readable());
  67. return ImmediateRead();
  68. }
  69. static inline uint8_t readable() {
  70. return (write_ptr_ - read_ptr_) & (size - 1);
  71. }
  72. static inline int16_t NonBlockingRead() {
  73. if (readable()) {
  74. return ImmediateRead();
  75. } else {
  76. return -1;
  77. }
  78. }
  79. static inline Value ImmediateRead() {
  80. uint8_t r = read_ptr_;
  81. Value result = buffer_[r];
  82. read_ptr_ = (r + 1) & (size - 1);
  83. return result;
  84. }
  85. static inline void Flush() {
  86. write_ptr_ = read_ptr_;
  87. }
  88. private:
  89. static Value buffer_[size];
  90. static volatile uint8_t read_ptr_;
  91. static volatile uint8_t write_ptr_;
  92. DISALLOW_COPY_AND_ASSIGN(RingBuffer);
  93. };
  94. // Static variables created for each buffer.
  95. template<typename T> volatile uint8_t RingBuffer<T>::read_ptr_ = 0;
  96. template<typename T> volatile uint8_t RingBuffer<T>::write_ptr_ = 0;
  97. template<typename T> typename T::Value RingBuffer<T>::buffer_[];
  98. } // namespace avrlib
  99. #endif // AVRLIB_RING_BUFFER_H_