buffer_stream.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * @file buffer_stream.h
  3. *
  4. * This file implements the concept of a buffer stream.
  5. * Data is written to the buffer until the buffer is full.
  6. * Then the buffer pointer is written to the stream, and the new write buffer is taken from the buffer pool.
  7. * After the buffer has been read by the receiving thread, it is sent to the free buffer pool.
  8. *
  9. * This will speed up sending large chunks of data between threads, compared to using a stream directly.
  10. */
  11. #pragma once
  12. #include <furi.h>
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. typedef struct Buffer Buffer;
  17. /**
  18. * @brief Get buffer data pointer
  19. * @param buffer
  20. * @return uint8_t*
  21. */
  22. uint8_t* buffer_get_data(Buffer* buffer);
  23. /**
  24. * @brief Get buffer size
  25. * @param buffer
  26. * @return size_t
  27. */
  28. size_t buffer_get_size(Buffer* buffer);
  29. /**
  30. * @brief Reset buffer and send to free buffer pool
  31. * @param buffer
  32. */
  33. void buffer_reset(Buffer* buffer);
  34. typedef struct BufferStream BufferStream;
  35. /**
  36. * @brief Allocate a new BufferStream instance
  37. * @param buffer_size
  38. * @param buffers_count
  39. * @return BufferStream*
  40. */
  41. BufferStream* buffer_stream_alloc(size_t buffer_size, size_t buffers_count);
  42. /**
  43. * @brief Free a BufferStream instance
  44. * @param buffer_stream
  45. */
  46. void buffer_stream_free(BufferStream* buffer_stream);
  47. /**
  48. * @brief Write data to buffer stream, from ISR context
  49. * Data will be written to the buffer until the buffer is full, and only then will the buffer be sent.
  50. * @param buffer_stream
  51. * @param data
  52. * @param size
  53. * @param task_woken
  54. * @return bool
  55. */
  56. bool buffer_stream_send_from_isr(
  57. BufferStream* buffer_stream,
  58. const uint8_t* data,
  59. size_t size,
  60. BaseType_t* const task_woken);
  61. /**
  62. * @brief Receive buffer from stream
  63. * @param buffer_stream
  64. * @param timeout
  65. * @return Buffer*
  66. */
  67. Buffer* buffer_stream_receive(BufferStream* buffer_stream, TickType_t timeout);
  68. /**
  69. * @brief Get stream overrun count
  70. * @param buffer_stream
  71. * @return size_t
  72. */
  73. size_t buffer_stream_get_overrun_count(BufferStream* buffer_stream);
  74. /**
  75. * @brief Reset stream and buffer pool
  76. * @param buffer_stream
  77. */
  78. void buffer_stream_reset(BufferStream* buffer_stream);
  79. #ifdef __cplusplus
  80. }
  81. #endif