xremote_signal.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*!
  2. * @file flipper-xremote/xremote_signal.h
  3. @license This project is released under the GNU GPLv3 License
  4. * @copyright (c) 2023 Sandro Kalatozishvili (s.kalatoz@gmail.com)
  5. *
  6. * @brief Implementation of infrared signal receiver functionality
  7. */
  8. #include "xremote_signal.h"
  9. struct XRemoteSignalReceiver {
  10. XRemoteClearCallback on_clear;
  11. XRemoteRxCallback rx_callback;
  12. NotificationApp* notifications;
  13. InfraredWorker* worker;
  14. InfraredSignal* signal;
  15. void* context;
  16. bool started;
  17. };
  18. static void xremote_signal_receiver_rx_callback(void* context, InfraredWorkerSignal* ir_signal)
  19. {
  20. furi_assert(context);
  21. XRemoteSignalReceiver *rx_ctx = context;
  22. xremote_app_notification_blink(rx_ctx->notifications);
  23. if (infrared_worker_signal_is_decoded(ir_signal))
  24. {
  25. const InfraredMessage* message;
  26. message = infrared_worker_get_decoded_signal(ir_signal);
  27. infrared_signal_set_message(rx_ctx->signal, message);
  28. }
  29. else
  30. {
  31. const uint32_t* timings;
  32. size_t timings_size = 0;
  33. infrared_worker_get_raw_signal(ir_signal, &timings, &timings_size);
  34. infrared_signal_set_raw_signal(rx_ctx->signal, timings, timings_size,
  35. INFRARED_COMMON_CARRIER_FREQUENCY, INFRARED_COMMON_DUTY_CYCLE);
  36. }
  37. if (rx_ctx->rx_callback != NULL)
  38. rx_ctx->rx_callback(rx_ctx->context, rx_ctx->signal);
  39. }
  40. static void xremote_signal_receiver_clear_context(XRemoteSignalReceiver* rx_ctx)
  41. {
  42. xremote_app_assert_void(rx_ctx);
  43. xremote_app_assert_void(rx_ctx->context);
  44. xremote_app_assert_void(rx_ctx->on_clear);
  45. rx_ctx->on_clear(rx_ctx->context);
  46. rx_ctx->context = NULL;
  47. }
  48. XRemoteSignalReceiver* xremote_signal_receiver_alloc(XRemoteAppContext* app_ctx)
  49. {
  50. XRemoteSignalReceiver *rx_ctx = malloc(sizeof(XRemoteSignalReceiver));
  51. rx_ctx->signal = infrared_signal_alloc();
  52. rx_ctx->worker = infrared_worker_alloc();
  53. rx_ctx->notifications = app_ctx->notifications;
  54. rx_ctx->rx_callback = NULL;
  55. rx_ctx->on_clear = NULL;
  56. rx_ctx->context = NULL;
  57. rx_ctx->started = false;
  58. return rx_ctx;
  59. }
  60. void xremote_signal_receiver_free(XRemoteSignalReceiver* rx_ctx)
  61. {
  62. xremote_app_assert_void(rx_ctx);
  63. xremote_signal_receiver_stop(rx_ctx);
  64. infrared_worker_free(rx_ctx->worker);
  65. infrared_signal_free(rx_ctx->signal);
  66. xremote_signal_receiver_clear_context(rx_ctx);
  67. free(rx_ctx);
  68. }
  69. void xremote_signal_receiver_set_context(XRemoteSignalReceiver* rx_ctx, void *context, XRemoteClearCallback on_clear)
  70. {
  71. xremote_signal_receiver_clear_context(rx_ctx);
  72. rx_ctx->on_clear = on_clear;
  73. rx_ctx->context = context;
  74. }
  75. void xremote_signal_receiver_set_rx_callback(XRemoteSignalReceiver* rx_ctx, XRemoteRxCallback rx_callback)
  76. {
  77. xremote_app_assert_void(rx_ctx);
  78. rx_ctx->rx_callback = rx_callback;
  79. }
  80. void xremote_signal_receiver_start(XRemoteSignalReceiver *rx_ctx)
  81. {
  82. xremote_app_assert_void((rx_ctx && rx_ctx->worker && !rx_ctx->started));
  83. infrared_worker_rx_set_received_signal_callback(rx_ctx->worker,
  84. xremote_signal_receiver_rx_callback, (void*)rx_ctx);
  85. infrared_worker_rx_start(rx_ctx->worker);
  86. xremote_app_notification_blink(rx_ctx->notifications);
  87. rx_ctx->started = true;
  88. }
  89. void xremote_signal_receiver_stop(XRemoteSignalReceiver *rx_ctx)
  90. {
  91. xremote_app_assert_void((rx_ctx && rx_ctx->worker && rx_ctx->started));
  92. infrared_worker_rx_set_received_signal_callback(rx_ctx->worker, NULL, NULL);
  93. infrared_worker_rx_stop(rx_ctx->worker);
  94. rx_ctx->started = false;
  95. }
  96. void xremote_signal_receiver_pause(XRemoteSignalReceiver *rx_ctx)
  97. {
  98. xremote_app_assert_void((rx_ctx && rx_ctx->worker));
  99. infrared_worker_rx_set_received_signal_callback(rx_ctx->worker, NULL, NULL);
  100. }
  101. void xremote_signal_receiver_resume(XRemoteSignalReceiver *rx_ctx)
  102. {
  103. xremote_app_assert_void((rx_ctx && rx_ctx->worker));
  104. infrared_worker_rx_set_received_signal_callback(rx_ctx->worker,
  105. xremote_signal_receiver_rx_callback, (void*)rx_ctx);
  106. }
  107. InfraredSignal* xremote_signal_receiver_get_signal(XRemoteSignalReceiver *rx_ctx)
  108. {
  109. xremote_app_assert(rx_ctx, NULL);
  110. return rx_ctx->signal;
  111. }