app.c 8.3 KB

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