UartReadBuffer.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (C) 2020 Embedded AMS B.V. - All Rights Reserved
  3. *
  4. * This file is part of Embedded Proto.
  5. *
  6. * Embedded Proto is open source software: you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as published
  8. * by the Free Software Foundation, version 3 of the license.
  9. *
  10. * Embedded Proto is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Embedded Proto. If not, see <https://www.gnu.org/licenses/>.
  17. *
  18. * For commercial and closed source application please visit:
  19. * <https://EmbeddedProto.com/license/>.
  20. *
  21. * Embedded AMS B.V.
  22. * Info:
  23. * info at EmbeddedProto dot com
  24. *
  25. * Postal adress:
  26. * Johan Huizingalaan 763a
  27. * 1066 VH, Amsterdam
  28. * the Netherlands
  29. */
  30. #ifndef INC_UARTREADBUFFER_H_
  31. #define INC_UARTREADBUFFER_H_
  32. #include <cstdint>
  33. #include <ReadBufferInterface.h>
  34. class UartReadBuffer : public ::EmbeddedProto::ReadBufferInterface
  35. {
  36. //! Store a maximum of MAX_SIZE bytes in the buffer
  37. static constexpr uint32_t MAX_SIZE = 50;
  38. public:
  39. UartReadBuffer();
  40. ~UartReadBuffer() override = default;
  41. /** \see ::EmbeddedProto::ReadBufferInterface::get_size() */
  42. uint32_t get_size() const override;
  43. /** \see ::EmbeddedProto::ReadBufferInterface::get_max_size() */
  44. uint32_t get_max_size() const override;
  45. /** \see ::EmbeddedProto::ReadBufferInterface::peak() */
  46. bool peek(uint8_t& byte) const override;
  47. /** \see ::EmbeddedProto::ReadBufferInterface::advance() */
  48. void advance() override;
  49. /** \see ::EmbeddedProto::ReadBufferInterface::advance(const uint32_t N) */
  50. void advance(const uint32_t N) override;
  51. /** \see ::EmbeddedProto::ReadBufferInterface::pop() */
  52. bool pop(uint8_t& byte) override;
  53. //! Return a pointer to the data array
  54. uint8_t* get_data_array();
  55. //! Return a non constant reference to the number of bytes written to the data array.
  56. uint32_t& get_bytes_written();
  57. //! Clear all indices, in effect allowing the data to be overwritten.
  58. void clear();
  59. //! Push new data into the buffer.
  60. bool push(uint8_t& byte);
  61. private:
  62. //! The array in which the data received over uart is stored.
  63. uint8_t data_[MAX_SIZE];
  64. //! The number of bytes currently received and stored in the data array.
  65. uint32_t write_index_;
  66. //! The number of bytes read from the data array.
  67. uint32_t read_index_;
  68. };
  69. #endif /* INC_UARTREADBUFFER_H_ */