raw_samples.h 1.5 KB

123456789101112131415161718192021222324252627282930313233
  1. /* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
  2. * See the LICENSE file for information about the license. */
  3. /* Our circular buffer of raw samples, used in order to display
  4. * the signal. */
  5. #define RAW_SAMPLES_NUM \
  6. 2048 /* Use a power of two: we take the modulo
  7. of the index quite often to normalize inside
  8. the range, and division is slow. */
  9. typedef struct RawSamplesBuffer {
  10. FuriMutex* mutex;
  11. struct {
  12. uint16_t level : 1;
  13. uint16_t dur : 15;
  14. } samples[RAW_SAMPLES_NUM];
  15. uint32_t idx; /* Current idx (next to write). */
  16. uint32_t total; /* Total samples: same as RAW_SAMPLES_NUM, we provide
  17. this field for a cleaner interface with the user, but
  18. we always use RAW_SAMPLES_NUM when taking the modulo so
  19. the compiler can optimize % as bit masking. */
  20. /* Signal features. */
  21. uint32_t short_pulse_dur; /* Duration of the shortest pulse. */
  22. } RawSamplesBuffer;
  23. RawSamplesBuffer* raw_samples_alloc(void);
  24. void raw_samples_reset(RawSamplesBuffer* s);
  25. void raw_samples_center(RawSamplesBuffer* s, uint32_t offset);
  26. void raw_samples_add(RawSamplesBuffer* s, bool level, uint32_t dur);
  27. void raw_samples_add_or_update(RawSamplesBuffer* s, bool level, uint32_t dur);
  28. void raw_samples_get(RawSamplesBuffer* s, uint32_t idx, bool* level, uint32_t* dur);
  29. void raw_samples_copy(RawSamplesBuffer* dst, RawSamplesBuffer* src);
  30. void raw_samples_free(RawSamplesBuffer* s);