app.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
  2. * See the LICENSE file for information about the license. */
  3. #include "app.h"
  4. RawSamplesBuffer *RawSamples, *DetectedSamples;
  5. extern const SubGhzProtocolRegistry protoview_protocol_registry;
  6. /* Draw some text with a border. If the outside color is black and the inside
  7. * color is white, it just writes the border of the text, but the function can
  8. * also be used to write a bold variation of the font setting both the
  9. * colors to black, or alternatively to write a black text with a white
  10. * border so that it is visible if there are black stuff on the background. */
  11. /* The callback actually just passes the control to the actual active
  12. * view callback, after setting up basic stuff like cleaning the screen
  13. * and setting color to black. */
  14. static void render_callback(Canvas *const canvas, void *ctx) {
  15. ProtoViewApp *app = ctx;
  16. /* Clear screen. */
  17. canvas_set_color(canvas, ColorWhite);
  18. canvas_draw_box(canvas, 0, 0, 127, 63);
  19. canvas_set_color(canvas, ColorBlack);
  20. canvas_set_font(canvas, FontPrimary);
  21. /* Call who is in charge right now. */
  22. switch(app->current_view) {
  23. case ViewRawPulses: render_view_raw_pulses(canvas,app); break;
  24. case ViewFrequencySettings:
  25. case ViewModulationSettings:
  26. render_view_settings(canvas,app); break;
  27. case ViewLast: furi_crash(TAG " ViewLast selected"); break;
  28. }
  29. }
  30. /* Here all we do is putting the events into the queue that will be handled
  31. * in the while() loop of the app entry point function. */
  32. static void input_callback(InputEvent* input_event, void* ctx)
  33. {
  34. ProtoViewApp *app = ctx;
  35. furi_message_queue_put(app->event_queue,input_event,FuriWaitForever);
  36. }
  37. /* Allocate the application state and initialize a number of stuff.
  38. * This is called in the entry point to create the application state. */
  39. ProtoViewApp* protoview_app_alloc() {
  40. ProtoViewApp *app = malloc(sizeof(ProtoViewApp));
  41. // Init shared data structures
  42. RawSamples = raw_samples_alloc();
  43. DetectedSamples = raw_samples_alloc();
  44. //init setting
  45. app->setting = subghz_setting_alloc();
  46. subghz_setting_load(app->setting, EXT_PATH("subghz/assets/setting_user"));
  47. // GUI
  48. app->gui = furi_record_open(RECORD_GUI);
  49. app->view_port = view_port_alloc();
  50. view_port_draw_callback_set(app->view_port, render_callback, app);
  51. view_port_input_callback_set(app->view_port, input_callback, app);
  52. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  53. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  54. app->current_view = ViewRawPulses;
  55. // Signal found and visualization defaults
  56. app->signal_bestlen = 0;
  57. app->us_scale = PROTOVIEW_RAW_VIEW_DEFAULT_SCALE;
  58. app->signal_offset = 0;
  59. //init Worker & Protocol
  60. app->txrx = malloc(sizeof(ProtoViewTxRx));
  61. /* Setup rx worker and environment. */
  62. app->txrx->worker = subghz_worker_alloc();
  63. app->txrx->environment = subghz_environment_alloc();
  64. subghz_environment_set_protocol_registry(
  65. app->txrx->environment, (void*)&protoview_protocol_registry);
  66. app->txrx->receiver = subghz_receiver_alloc_init(app->txrx->environment);
  67. subghz_receiver_set_filter(app->txrx->receiver, SubGhzProtocolFlag_Decodable);
  68. subghz_worker_set_overrun_callback(
  69. app->txrx->worker, (SubGhzWorkerOverrunCallback)subghz_receiver_reset);
  70. subghz_worker_set_pair_callback(
  71. app->txrx->worker, (SubGhzWorkerPairCallback)subghz_receiver_decode);
  72. subghz_worker_set_context(app->txrx->worker, app->txrx->receiver);
  73. app->frequency = subghz_setting_get_default_frequency(app->setting);
  74. app->modulation = 0; /* Defaults to ProtoViewModulations[0]. */
  75. furi_hal_power_suppress_charge_enter();
  76. app->running = 1;
  77. return app;
  78. }
  79. /* Free what the application allocated. It is not clear to me if the
  80. * Flipper OS, once the application exits, will be able to reclaim space
  81. * even if we forget to free something here. */
  82. void protoview_app_free(ProtoViewApp *app) {
  83. furi_assert(app);
  84. // Put CC1101 on sleep.
  85. radio_sleep(app);
  86. // View related.
  87. view_port_enabled_set(app->view_port, false);
  88. gui_remove_view_port(app->gui, app->view_port);
  89. view_port_free(app->view_port);
  90. furi_record_close(RECORD_GUI);
  91. furi_message_queue_free(app->event_queue);
  92. app->gui = NULL;
  93. // Frequency setting.
  94. subghz_setting_free(app->setting);
  95. // Worker stuff.
  96. subghz_receiver_free(app->txrx->receiver);
  97. subghz_environment_free(app->txrx->environment);
  98. subghz_worker_free(app->txrx->worker);
  99. free(app->txrx);
  100. // Raw samples buffers.
  101. raw_samples_free(RawSamples);
  102. raw_samples_free(DetectedSamples);
  103. furi_hal_power_suppress_charge_exit();
  104. free(app);
  105. }
  106. /* Called periodically. Do signal processing here. Data we process here
  107. * will be later displayed by the render callback. The side effect of this
  108. * function is to scan for signals and set DetectedSamples. */
  109. static void timer_callback(void *ctx) {
  110. ProtoViewApp *app = ctx;
  111. scan_for_signal(app);
  112. }
  113. int32_t protoview_app_entry(void* p) {
  114. UNUSED(p);
  115. ProtoViewApp *app = protoview_app_alloc();
  116. /* Create a timer. We do data analysis in the callback. */
  117. FuriTimer *timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, app);
  118. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 4);
  119. /* Start listening to signals immediately. */
  120. radio_begin(app);
  121. radio_rx(app);
  122. /* This is the main event loop: here we get the events that are pushed
  123. * in the queue by input_callback(), and process them one after the
  124. * other. The timeout is 100 milliseconds, so if not input is received
  125. * before such time, we exit the queue_get() function and call
  126. * view_port_update() in order to refresh our screen content. */
  127. InputEvent input;
  128. while(app->running) {
  129. FuriStatus qstat = furi_message_queue_get(app->event_queue, &input, 100);
  130. if (qstat == FuriStatusOk) {
  131. if (DEBUG_MSG) FURI_LOG_E(TAG, "Main Loop - Input: type %d key %u",
  132. input.type, input.key);
  133. /* Handle navigation here. Then handle view-specific inputs
  134. * in the view specific handling function. */
  135. if (input.type == InputTypeShort &&
  136. input.key == InputKeyBack)
  137. {
  138. /* Exit the app. */
  139. app->running = 0;
  140. } else if (input.type == InputTypeShort &&
  141. input.key == InputKeyRight)
  142. {
  143. /* Go to the next view. */
  144. app->current_view++;
  145. if (app->current_view == ViewLast) app->current_view = 0;
  146. } else if (input.type == InputTypeShort &&
  147. input.key == InputKeyLeft)
  148. {
  149. /* Go to the previous view. */
  150. if (app->current_view == 0)
  151. app->current_view = ViewLast-1;
  152. else
  153. app->current_view--;
  154. } else {
  155. /* This is where we pass the control to the currently
  156. * active view input processing. */
  157. switch(app->current_view) {
  158. case ViewRawPulses:
  159. process_input_raw_pulses(app,input);
  160. break;
  161. case ViewFrequencySettings:
  162. case ViewModulationSettings:
  163. process_input_settings(app,input);
  164. break;
  165. case ViewLast: furi_crash(TAG " ViewLast selected"); break;
  166. }
  167. }
  168. } else {
  169. /* Useful to understand if the app is still alive when it
  170. * does not respond because of bugs. */
  171. if (DEBUG_MSG) {
  172. static int c = 0; c++;
  173. if (!(c % 20)) FURI_LOG_E(TAG, "Loop timeout");
  174. }
  175. }
  176. view_port_update(app->view_port);
  177. }
  178. /* App no longer running. Shut down and free. */
  179. if (app->txrx->txrx_state == TxRxStateRx) {
  180. FURI_LOG_E(TAG, "Putting CC1101 to sleep before exiting.");
  181. radio_rx_end(app);
  182. radio_sleep(app);
  183. }
  184. furi_timer_free(timer);
  185. protoview_app_free(app);
  186. return 0;
  187. }