subghz_worker.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. void subghz_worker_rx_callback(
  14. ApiHalSubGhzCaptureLevel level,
  15. uint32_t duration,
  16. void* context) {
  17. SubGhzWorker* instance = context;
  18. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  19. LevelPair pair = {.level = level, .duration = duration};
  20. if(instance->overrun) {
  21. instance->overrun = false;
  22. pair.level = ApiHalSubGhzCaptureLevelOverrun;
  23. }
  24. size_t ret =
  25. xStreamBufferSendFromISR(instance->stream, &pair, sizeof(LevelPair), &xHigherPriorityTaskWoken);
  26. if(sizeof(LevelPair) != ret) instance->overrun = true;
  27. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  28. }
  29. static int32_t subghz_worker_thread_callback(void* context) {
  30. SubGhzWorker* instance = context;
  31. LevelPair pair;
  32. while(instance->running) {
  33. int ret = xStreamBufferReceive(instance->stream, &pair, sizeof(LevelPair), 10);
  34. if(ret == sizeof(LevelPair)) {
  35. if(pair.level == ApiHalSubGhzCaptureLevelOverrun) {
  36. printf(".");
  37. if (instance->overrun_callback) instance->overrun_callback(instance->context);
  38. } else {
  39. if (instance->pair_callback) instance->pair_callback(instance->context, pair);
  40. }
  41. }
  42. }
  43. return 0;
  44. }
  45. SubGhzWorker* subghz_worker_alloc() {
  46. SubGhzWorker* instance = furi_alloc(sizeof(SubGhzWorker));
  47. instance->thread = furi_thread_alloc();
  48. furi_thread_set_name(instance->thread, "subghz_worker");
  49. furi_thread_set_stack_size(instance->thread, 2048);
  50. furi_thread_set_context(instance->thread, instance);
  51. furi_thread_set_callback(instance->thread, subghz_worker_thread_callback);
  52. instance->stream = xStreamBufferCreate(sizeof(LevelPair) * 1024, sizeof(LevelPair));
  53. return instance;
  54. }
  55. void subghz_worker_free(SubGhzWorker* instance) {
  56. furi_assert(instance);
  57. vStreamBufferDelete(instance->stream);
  58. furi_thread_free(instance->thread);
  59. free(instance);
  60. }
  61. void subghz_worker_set_overrun_callback(SubGhzWorker* instance, SubGhzWorkerOverrunCallback callback) {
  62. furi_assert(instance);
  63. instance->overrun_callback = callback;
  64. }
  65. void subghz_worker_set_pair_callback(SubGhzWorker* instance, SubGhzWorkerPairCallback callback) {
  66. furi_assert(instance);
  67. instance->pair_callback = callback;
  68. }
  69. void subghz_worker_set_context(SubGhzWorker* instance, void* context) {
  70. furi_assert(instance);
  71. instance->context = context;
  72. }
  73. void subghz_worker_start(SubGhzWorker* instance) {
  74. furi_assert(instance);
  75. furi_assert(!instance->running);
  76. instance->running = true;
  77. furi_thread_start(instance->thread);
  78. }
  79. void subghz_worker_stop(SubGhzWorker* instance) {
  80. furi_assert(instance);
  81. furi_assert(instance->running);
  82. instance->running = false;
  83. furi_thread_join(instance->thread);
  84. }