app.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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->signal_decoded = false;
  58. app->us_scale = PROTOVIEW_RAW_VIEW_DEFAULT_SCALE;
  59. app->signal_offset = 0;
  60. //init Worker & Protocol
  61. app->txrx = malloc(sizeof(ProtoViewTxRx));
  62. /* Setup rx worker and environment. */
  63. app->txrx->worker = subghz_worker_alloc();
  64. app->txrx->environment = subghz_environment_alloc();
  65. subghz_environment_set_protocol_registry(
  66. app->txrx->environment, (void*)&protoview_protocol_registry);
  67. app->txrx->receiver = subghz_receiver_alloc_init(app->txrx->environment);
  68. subghz_receiver_set_filter(app->txrx->receiver, SubGhzProtocolFlag_Decodable);
  69. subghz_worker_set_overrun_callback(
  70. app->txrx->worker, (SubGhzWorkerOverrunCallback)subghz_receiver_reset);
  71. subghz_worker_set_pair_callback(
  72. app->txrx->worker, (SubGhzWorkerPairCallback)subghz_receiver_decode);
  73. subghz_worker_set_context(app->txrx->worker, app->txrx->receiver);
  74. app->frequency = subghz_setting_get_default_frequency(app->setting);
  75. app->modulation = 0; /* Defaults to ProtoViewModulations[0]. */
  76. furi_hal_power_suppress_charge_enter();
  77. app->running = 1;
  78. return app;
  79. }
  80. /* Free what the application allocated. It is not clear to me if the
  81. * Flipper OS, once the application exits, will be able to reclaim space
  82. * even if we forget to free something here. */
  83. void protoview_app_free(ProtoViewApp *app) {
  84. furi_assert(app);
  85. // Put CC1101 on sleep.
  86. radio_sleep(app);
  87. // View related.
  88. view_port_enabled_set(app->view_port, false);
  89. gui_remove_view_port(app->gui, app->view_port);
  90. view_port_free(app->view_port);
  91. furi_record_close(RECORD_GUI);
  92. furi_message_queue_free(app->event_queue);
  93. app->gui = NULL;
  94. // Frequency setting.
  95. subghz_setting_free(app->setting);
  96. // Worker stuff.
  97. subghz_receiver_free(app->txrx->receiver);
  98. subghz_environment_free(app->txrx->environment);
  99. subghz_worker_free(app->txrx->worker);
  100. free(app->txrx);
  101. // Raw samples buffers.
  102. raw_samples_free(RawSamples);
  103. raw_samples_free(DetectedSamples);
  104. furi_hal_power_suppress_charge_exit();
  105. free(app);
  106. }
  107. /* Called periodically. Do signal processing here. Data we process here
  108. * will be later displayed by the render callback. The side effect of this
  109. * function is to scan for signals and set DetectedSamples. */
  110. static void timer_callback(void *ctx) {
  111. ProtoViewApp *app = ctx;
  112. scan_for_signal(app);
  113. }
  114. int32_t protoview_app_entry(void* p) {
  115. UNUSED(p);
  116. ProtoViewApp *app = protoview_app_alloc();
  117. /* Create a timer. We do data analysis in the callback. */
  118. FuriTimer *timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, app);
  119. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 4);
  120. /* Start listening to signals immediately. */
  121. radio_begin(app);
  122. radio_rx(app);
  123. /* This is the main event loop: here we get the events that are pushed
  124. * in the queue by input_callback(), and process them one after the
  125. * other. The timeout is 100 milliseconds, so if not input is received
  126. * before such time, we exit the queue_get() function and call
  127. * view_port_update() in order to refresh our screen content. */
  128. InputEvent input;
  129. while(app->running) {
  130. FuriStatus qstat = furi_message_queue_get(app->event_queue, &input, 100);
  131. if (qstat == FuriStatusOk) {
  132. if (DEBUG_MSG) FURI_LOG_E(TAG, "Main Loop - Input: type %d key %u",
  133. input.type, input.key);
  134. /* Handle navigation here. Then handle view-specific inputs
  135. * in the view specific handling function. */
  136. if (input.type == InputTypeShort &&
  137. input.key == InputKeyBack)
  138. {
  139. /* Exit the app. */
  140. app->running = 0;
  141. } else if (input.type == InputTypeShort &&
  142. input.key == InputKeyRight)
  143. {
  144. /* Go to the next view. */
  145. app->current_view++;
  146. if (app->current_view == ViewLast) app->current_view = 0;
  147. } else if (input.type == InputTypeShort &&
  148. input.key == InputKeyLeft)
  149. {
  150. /* Go to the previous view. */
  151. if (app->current_view == 0)
  152. app->current_view = ViewLast-1;
  153. else
  154. app->current_view--;
  155. } else {
  156. /* This is where we pass the control to the currently
  157. * active view input processing. */
  158. switch(app->current_view) {
  159. case ViewRawPulses:
  160. process_input_raw_pulses(app,input);
  161. break;
  162. case ViewFrequencySettings:
  163. case ViewModulationSettings:
  164. process_input_settings(app,input);
  165. break;
  166. case ViewLast: furi_crash(TAG " ViewLast selected"); break;
  167. }
  168. }
  169. } else {
  170. /* Useful to understand if the app is still alive when it
  171. * does not respond because of bugs. */
  172. if (DEBUG_MSG) {
  173. static int c = 0; c++;
  174. if (!(c % 20)) FURI_LOG_E(TAG, "Loop timeout");
  175. }
  176. }
  177. view_port_update(app->view_port);
  178. }
  179. /* App no longer running. Shut down and free. */
  180. if (app->txrx->txrx_state == TxRxStateRx) {
  181. FURI_LOG_E(TAG, "Putting CC1101 to sleep before exiting.");
  182. radio_rx_end(app);
  183. radio_sleep(app);
  184. }
  185. furi_timer_free(timer);
  186. protoview_app_free(app);
  187. return 0;
  188. }