xremote_signal_view.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*!
  2. * @file flipper-xremote/views/xremote_signal_view.c
  3. @license This project is released under the GNU GPLv3 License
  4. * @copyright (c) 2023 Sandro Kalatozishvili (s.kalatoz@gmail.com)
  5. *
  6. * @brief Signal analyzer page view components and functionality.
  7. */
  8. #include "xremote_signal_view.h"
  9. #include "../xremote_analyzer.h"
  10. #include "../xremote_app.h"
  11. static void xremote_signal_view_draw_callback(Canvas* canvas, void* context)
  12. {
  13. furi_assert(context);
  14. XRemoteViewModel* model = context;
  15. XRemoteSignalAnalyzer* analyzer = model->context;
  16. XRemoteAppContext* app_ctx = xremote_signal_analyzer_get_app_context(analyzer);
  17. ViewOrientation orientation = app_ctx->app_settings->orientation;
  18. uint8_t y = orientation == ViewOrientationHorizontal ? 17 : 49;
  19. const char *text = "Press any\nbutton on\nthe remote.";
  20. xremote_canvas_draw_header(canvas, orientation, "Analyzer");
  21. elements_multiline_text_aligned(canvas, 0, y, AlignLeft, AlignTop, text);
  22. const char *exit_str = xremote_app_context_get_exit_str(app_ctx);
  23. xremote_canvas_draw_exit_footer(canvas, orientation, exit_str);
  24. }
  25. static void xremote_signal_success_view_draw_callback(Canvas* canvas, void* context)
  26. {
  27. furi_assert(context);
  28. XRemoteViewModel* model = context;
  29. XRemoteSignalAnalyzer* analyzer = model->context;
  30. XRemoteAppContext* app_ctx = xremote_signal_analyzer_get_app_context(analyzer);
  31. InfraredSignal *ir_signal = xremote_signal_analyzer_get_ir_signal(analyzer);
  32. xremote_canvas_draw_header(canvas, app_ctx->app_settings->orientation, "IR Signal");
  33. char signal_info[128];
  34. if (infrared_signal_is_raw(ir_signal))
  35. {
  36. InfraredRawSignal* raw = infrared_signal_get_raw_signal(ir_signal);
  37. snprintf(signal_info, sizeof(signal_info),
  38. "Type: RAW\n"
  39. "T-Size: %u\n"
  40. "D-Cycle: %.2f\n",
  41. raw->timings_size,
  42. (double)raw->duty_cycle);
  43. }
  44. else
  45. {
  46. InfraredMessage* message = infrared_signal_get_message(ir_signal);
  47. const char *infrared_protocol = infrared_get_protocol_name(message->protocol);
  48. snprintf(signal_info, sizeof(signal_info),
  49. "Proto: %s\n"
  50. "Addr: 0x%lX\n"
  51. "Cmd: 0x%lX\n",
  52. infrared_protocol,
  53. message->address,
  54. message->command);
  55. }
  56. if (app_ctx->app_settings->orientation == ViewOrientationHorizontal)
  57. {
  58. elements_multiline_text_aligned(canvas, 0, 17, AlignLeft, AlignTop, signal_info);
  59. xremote_canvas_draw_button_wide(canvas, model->ok_pressed, 68, 26, "Send", XRemoteIconEnter);
  60. xremote_canvas_draw_button_wide(canvas, model->back_pressed, 68, 44, "Retry", XRemoteIconBack);
  61. }
  62. else
  63. {
  64. elements_multiline_text_aligned(canvas, 0, 39, AlignLeft, AlignTop, signal_info);
  65. xremote_canvas_draw_button_wide(canvas, model->ok_pressed, 0, 88, "Send", XRemoteIconEnter);
  66. xremote_canvas_draw_button_wide(canvas, model->back_pressed, 0, 106, "Retry", XRemoteIconBack);
  67. }
  68. }
  69. static void xremote_signal_success_view_process(XRemoteView* view, InputEvent* event)
  70. {
  71. with_view_model(
  72. xremote_view_get_view(view),
  73. XRemoteViewModel* model,
  74. {
  75. XRemoteSignalAnalyzer *analyzer = xremote_view_get_context(view);
  76. model->context = analyzer;
  77. if (event->type == InputTypePress)
  78. {
  79. if (event->key == InputKeyOk)
  80. {
  81. model->ok_pressed = true;
  82. xremote_signal_analyzer_send_event(analyzer, XRemoteEventSignalSend);
  83. }
  84. else if (event->key == InputKeyBack)
  85. {
  86. model->back_pressed = true;
  87. xremote_signal_analyzer_send_event(analyzer, XRemoteEventSignalRetry);
  88. }
  89. }
  90. else if (event->type == InputTypeRelease)
  91. {
  92. if (event->key == InputKeyOk) model->ok_pressed = false;
  93. else if (event->key == InputKeyBack) model->back_pressed = false;
  94. }
  95. },
  96. true
  97. );
  98. }
  99. static void xremote_signal_view_process(XRemoteView* view, InputEvent* event)
  100. {
  101. with_view_model(
  102. xremote_view_get_view(view),
  103. XRemoteViewModel* model,
  104. {
  105. XRemoteSignalAnalyzer *analyzer = xremote_view_get_context(view);
  106. XRemoteAppContext *app_ctx = xremote_view_get_app_context(view);
  107. XRemoteAppExit exit = app_ctx->app_settings->exit_behavior;
  108. model->context = analyzer;
  109. if ((event->type == InputTypeShort ||
  110. event->type == InputTypeLong) &&
  111. event->key == InputKeyBack)
  112. {
  113. if ((event->type == InputTypeShort && exit == XRemoteAppExitPress) ||
  114. (event->type == InputTypeLong && exit == XRemoteAppExitHold))
  115. {
  116. model->back_pressed = true;
  117. xremote_signal_analyzer_send_event(analyzer, XRemoteEventSignalExit);
  118. }
  119. }
  120. else if (event->type == InputTypeRelease)
  121. {
  122. if (event->key == InputKeyOk) model->ok_pressed = false;
  123. else if (event->key == InputKeyBack) model->back_pressed = false;
  124. else if (event->key == InputKeyRight) model->right_pressed = false;
  125. }
  126. },
  127. true
  128. );
  129. }
  130. static bool xremote_signal_success_view_input_callback(InputEvent* event, void* context)
  131. {
  132. furi_assert(context);
  133. XRemoteView* view = (XRemoteView*)context;
  134. xremote_signal_success_view_process(view, event);
  135. return true;
  136. }
  137. static bool xremote_signal_view_input_callback(InputEvent* event, void* context)
  138. {
  139. furi_assert(context);
  140. XRemoteView* view = (XRemoteView*)context;
  141. xremote_signal_view_process(view, event);
  142. return true;
  143. }
  144. XRemoteView* xremote_signal_success_view_alloc(void* app_ctx, void *analyzer)
  145. {
  146. XRemoteView *view = xremote_view_alloc(app_ctx,
  147. xremote_signal_success_view_input_callback,
  148. xremote_signal_success_view_draw_callback);
  149. xremote_view_set_context(view, analyzer, NULL);
  150. with_view_model(
  151. xremote_view_get_view(view),
  152. XRemoteViewModel* model,
  153. {
  154. model->context = analyzer;
  155. model->back_pressed = false;
  156. model->ok_pressed = false;
  157. },
  158. true
  159. );
  160. return view;
  161. }
  162. XRemoteView* xremote_signal_view_alloc(void* app_ctx, void *analyzer)
  163. {
  164. XRemoteView *view = xremote_view_alloc(app_ctx,
  165. xremote_signal_view_input_callback,
  166. xremote_signal_view_draw_callback);
  167. xremote_view_set_context(view, analyzer, NULL);
  168. with_view_model(
  169. xremote_view_get_view(view),
  170. XRemoteViewModel* model,
  171. {
  172. model->context = analyzer;
  173. model->back_pressed = false;
  174. },
  175. true
  176. );
  177. return view;
  178. }