PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

test-mocks_SerialMock.hpp 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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