xremote_signal.c 4.0 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. xremote_app_assert(rx_ctx, NULL);
  49. rx_ctx->signal = infrared_signal_alloc();
  50. rx_ctx->worker = infrared_worker_alloc();
  51. rx_ctx->notifications = app_ctx->notifications;
  52. rx_ctx->rx_callback = NULL;
  53. rx_ctx->on_clear = NULL;
  54. rx_ctx->context = NULL;
  55. rx_ctx->started = false;
  56. return rx_ctx;
  57. }
  58. void xremote_signal_receiver_free(XRemoteSignalReceiver* rx_ctx) {
  59. xremote_app_assert_void(rx_ctx);
  60. xremote_signal_receiver_stop(rx_ctx);
  61. infrared_worker_free(rx_ctx->worker);
  62. infrared_signal_free(rx_ctx->signal);
  63. xremote_signal_receiver_clear_context(rx_ctx);
  64. free(rx_ctx);
  65. }
  66. void xremote_signal_receiver_set_context(
  67. XRemoteSignalReceiver* rx_ctx,
  68. void* context,
  69. XRemoteClearCallback on_clear) {
  70. xremote_signal_receiver_clear_context(rx_ctx);
  71. rx_ctx->on_clear = on_clear;
  72. rx_ctx->context = context;
  73. }
  74. void xremote_signal_receiver_set_rx_callback(
  75. XRemoteSignalReceiver* rx_ctx,
  76. XRemoteRxCallback rx_callback) {
  77. xremote_app_assert_void(rx_ctx);
  78. rx_ctx->rx_callback = rx_callback;
  79. }
  80. void xremote_signal_receiver_attach(XRemoteSignalReceiver* rx_ctx) {
  81. xremote_app_assert_void((rx_ctx && rx_ctx->worker));
  82. infrared_worker_rx_set_received_signal_callback(
  83. rx_ctx->worker, xremote_signal_receiver_rx_callback, rx_ctx);
  84. }
  85. void xremote_signal_receiver_detach(XRemoteSignalReceiver* rx_ctx) {
  86. xremote_app_assert_void((rx_ctx && rx_ctx->worker));
  87. infrared_worker_rx_set_received_signal_callback(rx_ctx->worker, NULL, NULL);
  88. }
  89. void xremote_signal_receiver_start(XRemoteSignalReceiver* rx_ctx) {
  90. xremote_app_assert_void((rx_ctx && !rx_ctx->started));
  91. xremote_signal_receiver_attach(rx_ctx);
  92. infrared_worker_rx_start(rx_ctx->worker);
  93. xremote_app_notification_blink(rx_ctx->notifications);
  94. rx_ctx->started = true;
  95. }
  96. void xremote_signal_receiver_stop(XRemoteSignalReceiver* rx_ctx) {
  97. xremote_app_assert_void((rx_ctx && rx_ctx->started));
  98. xremote_signal_receiver_detach(rx_ctx);
  99. infrared_worker_rx_stop(rx_ctx->worker);
  100. rx_ctx->started = false;
  101. }
  102. InfraredSignal* xremote_signal_receiver_get_signal(XRemoteSignalReceiver* rx_ctx) {
  103. xremote_app_assert(rx_ctx, NULL);
  104. return rx_ctx->signal;
  105. }