buffer_stream.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. * @return bool
  54. */
  55. bool buffer_stream_send_from_isr(BufferStream* buffer_stream, const uint8_t* data, size_t size);
  56. /**
  57. * @brief Receive buffer from stream
  58. * @param buffer_stream
  59. * @param timeout
  60. * @return Buffer*
  61. */
  62. Buffer* buffer_stream_receive(BufferStream* buffer_stream, TickType_t timeout);
  63. /**
  64. * @brief Get stream overrun count
  65. * @param buffer_stream
  66. * @return size_t
  67. */
  68. size_t buffer_stream_get_overrun_count(BufferStream* buffer_stream);
  69. /**
  70. * @brief Reset stream and buffer pool
  71. * @param buffer_stream
  72. */
  73. void buffer_stream_reset(BufferStream* buffer_stream);
  74. #ifdef __cplusplus
  75. }
  76. #endif