subghz_worker.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "subghz_worker.h"
  2. #include <stream_buffer.h>
  3. #include <furi.h>
  4. struct SubGhzWorker {
  5. FuriThread* thread;
  6. StreamBufferHandle_t stream;
  7. volatile bool running;
  8. volatile bool overrun;
  9. SubGhzWorkerOverrunCallback overrun_callback;
  10. SubGhzWorkerPairCallback pair_callback;
  11. void* context;
  12. };
  13. /** Rx callback timer
  14. *
  15. * @param level received signal level
  16. * @param duration received signal duration
  17. * @param context
  18. */
  19. void subghz_worker_rx_callback(
  20. ApiHalSubGhzCaptureLevel level,
  21. uint32_t duration,
  22. void* context) {
  23. SubGhzWorker* instance = context;
  24. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  25. LevelPair pair = {.level = level, .duration = duration};
  26. if(instance->overrun) {
  27. instance->overrun = false;
  28. pair.level = ApiHalSubGhzCaptureLevelOverrun;
  29. }
  30. size_t ret =
  31. xStreamBufferSendFromISR(instance->stream, &pair, sizeof(LevelPair), &xHigherPriorityTaskWoken);
  32. if(sizeof(LevelPair) != ret) instance->overrun = true;
  33. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  34. }
  35. /** Worker callback thread
  36. *
  37. * @param context
  38. * @return exit code
  39. */
  40. static int32_t subghz_worker_thread_callback(void* context) {
  41. SubGhzWorker* instance = context;
  42. LevelPair pair;
  43. while(instance->running) {
  44. int ret = xStreamBufferReceive(instance->stream, &pair, sizeof(LevelPair), 10);
  45. if(ret == sizeof(LevelPair)) {
  46. if(pair.level == ApiHalSubGhzCaptureLevelOverrun) {
  47. printf(".");
  48. if (instance->overrun_callback) instance->overrun_callback(instance->context);
  49. } else {
  50. if (instance->pair_callback) instance->pair_callback(instance->context, pair);
  51. }
  52. }
  53. }
  54. return 0;
  55. }
  56. SubGhzWorker* subghz_worker_alloc() {
  57. SubGhzWorker* instance = furi_alloc(sizeof(SubGhzWorker));
  58. instance->thread = furi_thread_alloc();
  59. furi_thread_set_name(instance->thread, "subghz_worker");
  60. furi_thread_set_stack_size(instance->thread, 2048);
  61. furi_thread_set_context(instance->thread, instance);
  62. furi_thread_set_callback(instance->thread, subghz_worker_thread_callback);
  63. instance->stream = xStreamBufferCreate(sizeof(LevelPair) * 1024, sizeof(LevelPair));
  64. return instance;
  65. }
  66. void subghz_worker_free(SubGhzWorker* instance) {
  67. furi_assert(instance);
  68. vStreamBufferDelete(instance->stream);
  69. furi_thread_free(instance->thread);
  70. free(instance);
  71. }
  72. void subghz_worker_set_overrun_callback(SubGhzWorker* instance, SubGhzWorkerOverrunCallback callback) {
  73. furi_assert(instance);
  74. instance->overrun_callback = callback;
  75. }
  76. void subghz_worker_set_pair_callback(SubGhzWorker* instance, SubGhzWorkerPairCallback callback) {
  77. furi_assert(instance);
  78. instance->pair_callback = callback;
  79. }
  80. void subghz_worker_set_context(SubGhzWorker* instance, void* context) {
  81. furi_assert(instance);
  82. instance->context = context;
  83. }
  84. void subghz_worker_start(SubGhzWorker* instance) {
  85. furi_assert(instance);
  86. furi_assert(!instance->running);
  87. instance->running = true;
  88. furi_thread_start(instance->thread);
  89. }
  90. void subghz_worker_stop(SubGhzWorker* instance) {
  91. furi_assert(instance);
  92. furi_assert(instance->running);
  93. instance->running = false;
  94. furi_thread_join(instance->thread);
  95. }