app_buffer.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
  2. * See the LICENSE file for information about the license. */
  3. #include <inttypes.h>
  4. #include <furi/core/string.h>
  5. #include <furi.h>
  6. #include <furi_hal.h>
  7. #include "app_buffer.h"
  8. /* Allocate and initialize a samples buffer. */
  9. RawSamplesBuffer *raw_samples_alloc(void) {
  10. RawSamplesBuffer *buf = malloc(sizeof(*buf));
  11. buf->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  12. raw_samples_reset(buf);
  13. return buf;
  14. }
  15. /* Free a sample buffer. Should be called when the mutex is released. */
  16. void raw_samples_free(RawSamplesBuffer *s) {
  17. furi_mutex_free(s->mutex);
  18. free(s);
  19. }
  20. /* This just set all the samples to zero and also resets the internal
  21. * index. There is no need to call it after raw_samples_alloc(), but only
  22. * when one wants to reset the whole buffer of samples. */
  23. void raw_samples_reset(RawSamplesBuffer *s) {
  24. furi_mutex_acquire(s->mutex,FuriWaitForever);
  25. s->total = RAW_SAMPLES_NUM;
  26. s->idx = 0;
  27. s->short_pulse_dur = 0;
  28. memset(s->level,0,sizeof(s->level));
  29. memset(s->dur,0,sizeof(s->dur));
  30. furi_mutex_release(s->mutex);
  31. }
  32. /* Set the raw sample internal index so that what is currently at
  33. * offset 'offset', will appear to be at 0 index. */
  34. void raw_samples_center(RawSamplesBuffer *s, uint32_t offset) {
  35. s->idx = (s->idx+offset) % RAW_SAMPLES_NUM;
  36. }
  37. /* Add the specified sample in the circular buffer. */
  38. void raw_samples_add(RawSamplesBuffer *s, bool level, uint32_t dur) {
  39. furi_mutex_acquire(s->mutex,FuriWaitForever);
  40. s->level[s->idx] = level;
  41. s->dur[s->idx] = dur;
  42. s->idx = (s->idx+1) % RAW_SAMPLES_NUM;
  43. furi_mutex_release(s->mutex);
  44. }
  45. /* Get the sample from the buffer. It is possible to use out of range indexes
  46. * as 'idx' because the modulo operation will rewind back from the start. */
  47. void raw_samples_get(RawSamplesBuffer *s, uint32_t idx, bool *level, uint32_t *dur)
  48. {
  49. furi_mutex_acquire(s->mutex,FuriWaitForever);
  50. idx = (s->idx + idx) % RAW_SAMPLES_NUM;
  51. *level = s->level[idx];
  52. *dur = s->dur[idx];
  53. furi_mutex_release(s->mutex);
  54. }
  55. /* Copy one buffer to the other, including current index. */
  56. void raw_samples_copy(RawSamplesBuffer *dst, RawSamplesBuffer *src) {
  57. furi_mutex_acquire(src->mutex,FuriWaitForever);
  58. furi_mutex_acquire(dst->mutex,FuriWaitForever);
  59. dst->idx = src->idx;
  60. dst->short_pulse_dur = src->short_pulse_dur;
  61. memcpy(dst->level,src->level,sizeof(dst->level));
  62. memcpy(dst->dur,src->dur,sizeof(dst->dur));
  63. furi_mutex_release(src->mutex);
  64. furi_mutex_release(dst->mutex);
  65. }