raw_samples.h 1.5 KB

1234567891011121314151617181920212223242526272829303132
  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 2048 /* Use a power of two: we take the modulo
  6. of the index quite often to normalize inside
  7. the range, and division is slow. */
  8. typedef struct RawSamplesBuffer {
  9. FuriMutex *mutex;
  10. struct {
  11. uint16_t level:1;
  12. uint16_t dur:15;
  13. } samples[RAW_SAMPLES_NUM];
  14. uint32_t idx; /* Current idx (next to write). */
  15. uint32_t total; /* Total samples: same as RAW_SAMPLES_NUM, we provide
  16. this field for a cleaner interface with the user, but
  17. we always use RAW_SAMPLES_NUM when taking the modulo so
  18. the compiler can optimize % as bit masking. */
  19. /* Signal features. */
  20. uint32_t short_pulse_dur; /* Duration of the shortest pulse. */
  21. } RawSamplesBuffer;
  22. RawSamplesBuffer *raw_samples_alloc(void);
  23. void raw_samples_reset(RawSamplesBuffer *s);
  24. void raw_samples_center(RawSamplesBuffer *s, uint32_t offset);
  25. void raw_samples_add(RawSamplesBuffer *s, bool level, uint32_t dur);
  26. void raw_samples_add_or_update(RawSamplesBuffer *s, bool level, uint32_t dur);
  27. void raw_samples_get(RawSamplesBuffer *s, uint32_t idx, bool *level, uint32_t *dur);
  28. void raw_samples_copy(RawSamplesBuffer *dst, RawSamplesBuffer *src);
  29. void raw_samples_free(RawSamplesBuffer *s);