xremote_signal.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. furi_assert(context);
  20. XRemoteSignalReceiver* rx_ctx = context;
  21. xremote_app_notification_blink(rx_ctx->notifications);
  22. if(infrared_worker_signal_is_decoded(ir_signal)) {
  23. const InfraredMessage* message;
  24. message = infrared_worker_get_decoded_signal(ir_signal);
  25. infrared_signal_set_message(rx_ctx->signal, message);
  26. } else {
  27. const uint32_t* timings;
  28. size_t timings_size = 0;
  29. infrared_worker_get_raw_signal(ir_signal, &timings, &timings_size);
  30. infrared_signal_set_raw_signal(
  31. rx_ctx->signal,
  32. timings,
  33. timings_size,
  34. INFRARED_COMMON_CARRIER_FREQUENCY,
  35. INFRARED_COMMON_DUTY_CYCLE);
  36. }
  37. if(rx_ctx->rx_callback != NULL) rx_ctx->rx_callback(rx_ctx->context, rx_ctx->signal);
  38. }
  39. static void xremote_signal_receiver_clear_context(XRemoteSignalReceiver* rx_ctx) {
  40. xremote_app_assert_void(rx_ctx);
  41. xremote_app_assert_void(rx_ctx->context);
  42. xremote_app_assert_void(rx_ctx->on_clear);
  43. rx_ctx->on_clear(rx_ctx->context);
  44. rx_ctx->context = NULL;
  45. }
  46. XRemoteSignalReceiver* xremote_signal_receiver_alloc(XRemoteAppContext* app_ctx) {
  47. XRemoteSignalReceiver* rx_ctx = malloc(sizeof(XRemoteSignalReceiver));
  48. rx_ctx->signal = infrared_signal_alloc();
  49. rx_ctx->worker = infrared_worker_alloc();
  50. rx_ctx->notifications = app_ctx->notifications;
  51. rx_ctx->rx_callback = NULL;
  52. rx_ctx->on_clear = NULL;
  53. rx_ctx->context = NULL;
  54. rx_ctx->started = false;
  55. return rx_ctx;
  56. }
  57. void xremote_signal_receiver_free(XRemoteSignalReceiver* rx_ctx) {
  58. xremote_app_assert_void(rx_ctx);
  59. xremote_signal_receiver_stop(rx_ctx);
  60. infrared_worker_free(rx_ctx->worker);
  61. infrared_signal_free(rx_ctx->signal);
  62. xremote_signal_receiver_clear_context(rx_ctx);
  63. free(rx_ctx);
  64. }
  65. void xremote_signal_receiver_set_context(
  66. XRemoteSignalReceiver* rx_ctx,
  67. void* context,
  68. XRemoteClearCallback on_clear) {
  69. xremote_signal_receiver_clear_context(rx_ctx);
  70. rx_ctx->on_clear = on_clear;
  71. rx_ctx->context = context;
  72. }
  73. void xremote_signal_receiver_set_rx_callback(
  74. XRemoteSignalReceiver* rx_ctx,
  75. XRemoteRxCallback rx_callback) {
  76. xremote_app_assert_void(rx_ctx);
  77. rx_ctx->rx_callback = rx_callback;
  78. }
  79. void xremote_signal_receiver_start(XRemoteSignalReceiver* rx_ctx) {
  80. xremote_app_assert_void((rx_ctx && rx_ctx->worker && !rx_ctx->started));
  81. infrared_worker_rx_set_received_signal_callback(
  82. rx_ctx->worker, xremote_signal_receiver_rx_callback, (void*)rx_ctx);
  83. infrared_worker_rx_start(rx_ctx->worker);
  84. xremote_app_notification_blink(rx_ctx->notifications);
  85. rx_ctx->started = true;
  86. }
  87. void xremote_signal_receiver_stop(XRemoteSignalReceiver* rx_ctx) {
  88. xremote_app_assert_void((rx_ctx && rx_ctx->worker && rx_ctx->started));
  89. infrared_worker_rx_set_received_signal_callback(rx_ctx->worker, NULL, NULL);
  90. infrared_worker_rx_stop(rx_ctx->worker);
  91. rx_ctx->started = false;
  92. }
  93. void xremote_signal_receiver_pause(XRemoteSignalReceiver* rx_ctx) {
  94. xremote_app_assert_void((rx_ctx && rx_ctx->worker));
  95. infrared_worker_rx_set_received_signal_callback(rx_ctx->worker, NULL, NULL);
  96. }
  97. void xremote_signal_receiver_resume(XRemoteSignalReceiver* rx_ctx) {
  98. xremote_app_assert_void((rx_ctx && rx_ctx->worker));
  99. infrared_worker_rx_set_received_signal_callback(
  100. rx_ctx->worker, xremote_signal_receiver_rx_callback, (void*)rx_ctx);
  101. }
  102. InfraredSignal* xremote_signal_receiver_get_signal(XRemoteSignalReceiver* rx_ctx) {
  103. xremote_app_assert(rx_ctx, NULL);
  104. return rx_ctx->signal;
  105. }