PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

154 linhas
3.2KB

  1. #pragma once
  2. BEGIN_TEST_MOCKS_NAMESPACE
  3. template<typename DataType, int Size>
  4. RingBuffer<DataType, Size>::RingBuffer()
  5. : mWriteHead(mData)
  6. , mReadHead(mData)
  7. {
  8. memset(mData, DataType(0), Size * sizeof(DataType));
  9. }
  10. template<typename DataType, int Size>
  11. RingBuffer<DataType, Size>::~RingBuffer()
  12. {
  13. }
  14. // -----------------------------------------------------------------------------
  15. template<typename DataType, int Size>
  16. int RingBuffer<DataType, Size>::getLength() const
  17. {
  18. if (mReadHead == mWriteHead)
  19. {
  20. return 0;
  21. }
  22. else if (mWriteHead > mReadHead)
  23. {
  24. return int(mWriteHead - mReadHead);
  25. }
  26. else
  27. {
  28. return int(mWriteHead - mData) + Size - int(mReadHead - mData);
  29. }
  30. }
  31. template<typename DataType, int Size>
  32. bool RingBuffer<DataType, Size>::isEmpty() const
  33. {
  34. return mReadHead == mWriteHead;
  35. }
  36. // -----------------------------------------------------------------------------
  37. template<typename DataType, int Size>
  38. void RingBuffer<DataType, Size>::write(DataType inData)
  39. {
  40. *mWriteHead++ = inData;
  41. if (mWriteHead >= mData + Size)
  42. {
  43. mWriteHead = mData;
  44. }
  45. }
  46. template<typename DataType, int Size>
  47. void RingBuffer<DataType, Size>::write(const DataType* inData, int inSize)
  48. {
  49. for (int i = 0; i < inSize; ++i)
  50. {
  51. write(inData[i]);
  52. }
  53. }
  54. template<typename DataType, int Size>
  55. void RingBuffer<DataType, Size>::clear()
  56. {
  57. memset(mData, DataType(0), Size * sizeof(DataType));
  58. mReadHead = mData;
  59. mWriteHead = mData;
  60. }
  61. // -----------------------------------------------------------------------------
  62. template<typename DataType, int Size>
  63. DataType RingBuffer<DataType, Size>::peek() const
  64. {
  65. return *mReadHead;
  66. }
  67. template<typename DataType, int Size>
  68. DataType RingBuffer<DataType, Size>::read()
  69. {
  70. const DataType data = *mReadHead++;
  71. if (mReadHead >= mData + Size)
  72. {
  73. mReadHead = mData;
  74. }
  75. return data;
  76. }
  77. template<typename DataType, int Size>
  78. void RingBuffer<DataType, Size>::read(DataType* outData, int inSize)
  79. {
  80. for (int i = 0; i < inSize; ++i)
  81. {
  82. outData[i] = read();
  83. }
  84. }
  85. // =============================================================================
  86. template<int BufferSize>
  87. SerialMock<BufferSize>::SerialMock()
  88. {
  89. }
  90. template<int BufferSize>
  91. SerialMock<BufferSize>::~SerialMock()
  92. {
  93. }
  94. // -----------------------------------------------------------------------------
  95. template<int BufferSize>
  96. void SerialMock<BufferSize>::begin(int inBaudrate)
  97. {
  98. mBaudrate = inBaudrate;
  99. mTxBuffer.clear();
  100. mRxBuffer.clear();
  101. }
  102. template<int BufferSize>
  103. int SerialMock<BufferSize>::available() const
  104. {
  105. return mRxBuffer.getLength();
  106. }
  107. template<int BufferSize>
  108. void SerialMock<BufferSize>::write(uint8_t inData)
  109. {
  110. mTxBuffer.write(inData);
  111. }
  112. template<int BufferSize>
  113. uint8_t SerialMock<BufferSize>::read()
  114. {
  115. return mRxBuffer.read();
  116. }
  117. // -----------------------------------------------------------------------------
  118. template<int BufferSize>
  119. void SerialMock<BufferSize>::moveTxToRx()
  120. {
  121. mRxBuffer.clear();
  122. const int size = mTxBuffer.getSize();
  123. for (int i = 0; i < size; ++i)
  124. {
  125. mRxBuffer.write(mTxBuffer.read());
  126. }
  127. }
  128. END_TEST_MOCKS_NAMESPACE