subghz_capture.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include "subghz_capture.h"
  2. #include "../subghz_i.h"
  3. #include <math.h>
  4. #include <furi.h>
  5. #include <api-hal.h>
  6. #include <input/input.h>
  7. #include <gui/elements.h>
  8. #include <notification/notification-messages.h>
  9. #include <lib/subghz/subghz_worker.h>
  10. #include <lib/subghz/protocols/subghz_protocol.h>
  11. #include <assets_icons.h>
  12. struct SubghzCapture {
  13. View* view;
  14. SubGhzWorker* worker;
  15. SubGhzProtocol* protocol;
  16. };
  17. typedef struct {
  18. uint8_t frequency;
  19. uint32_t real_frequency;
  20. uint32_t counter;
  21. string_t text;
  22. uint16_t scene;
  23. SubGhzProtocolCommon parser;
  24. } SubghzCaptureModel;
  25. static const char subghz_symbols[] = {'-', '\\', '|', '/'};
  26. void subghz_capture_draw(Canvas* canvas, SubghzCaptureModel* model) {
  27. char buffer[64];
  28. canvas_set_color(canvas, ColorBlack);
  29. canvas_set_font(canvas, FontPrimary);
  30. snprintf(
  31. buffer,
  32. sizeof(buffer),
  33. "Capture: %03ld.%03ldMHz %c",
  34. model->real_frequency / 1000000 % 1000,
  35. model->real_frequency / 1000 % 1000,
  36. subghz_symbols[model->counter % 4]);
  37. canvas_draw_str(canvas, 0, 8, buffer);
  38. switch(model->scene) {
  39. case 1:
  40. canvas_draw_icon(canvas, 0, 10, &I_RFIDDolphinReceive_97x61);
  41. canvas_invert_color(canvas);
  42. canvas_draw_box(canvas, 80, 12, 20, 20);
  43. canvas_invert_color(canvas);
  44. canvas_draw_icon(canvas, 75, 18, &I_sub1_10px);
  45. elements_multiline_text_aligned(
  46. canvas, 90, 38, AlignCenter, AlignTop, "Detecting\r\nSubGhz");
  47. break;
  48. default:
  49. canvas_set_font(canvas, FontSecondary);
  50. elements_multiline_text(canvas, 0, 20, string_get_cstr(model->text));
  51. break;
  52. }
  53. }
  54. bool subghz_capture_input(InputEvent* event, void* context) {
  55. furi_assert(context);
  56. SubghzCapture* subghz_capture = context;
  57. if(event->key == InputKeyBack) {
  58. return false;
  59. }
  60. with_view_model(
  61. subghz_capture->view, (SubghzCaptureModel * model) {
  62. bool reconfigure = false;
  63. if(event->type == InputTypeShort) {
  64. if(event->key == InputKeyLeft) {
  65. if(model->frequency > 0) model->frequency--;
  66. reconfigure = true;
  67. } else if(event->key == InputKeyRight) {
  68. if(model->frequency < subghz_frequencies_count - 1) model->frequency++;
  69. reconfigure = true;
  70. }
  71. }
  72. if(reconfigure) {
  73. api_hal_subghz_idle();
  74. model->real_frequency =
  75. api_hal_subghz_set_frequency_and_path(subghz_frequencies[model->frequency]);
  76. api_hal_subghz_rx();
  77. }
  78. return reconfigure;
  79. });
  80. return true;
  81. }
  82. void subghz_capture_text_callback(string_t text, void* context) {
  83. furi_assert(context);
  84. SubghzCapture* subghz_capture = context;
  85. with_view_model(
  86. subghz_capture->view, (SubghzCaptureModel * model) {
  87. model->counter++;
  88. string_set(model->text, text);
  89. model->scene = 0;
  90. return true;
  91. });
  92. }
  93. void subghz_capture_protocol_callback(SubGhzProtocolCommon* parser, void* context) {
  94. furi_assert(context);
  95. SubghzCapture* subghz_capture = context;
  96. char buffer[64];
  97. snprintf(
  98. buffer,
  99. sizeof(buffer),
  100. "%s\r\n"
  101. "K:%lX%lX\r\n"
  102. "SN:%lX\r\n"
  103. "BTN:%X",
  104. parser->name,
  105. (uint32_t)(parser->code_found >> 32),
  106. (uint32_t)(parser->code_found & 0x00000000FFFFFFFF),
  107. parser->serial,
  108. parser->btn);
  109. with_view_model(
  110. subghz_capture->view, (SubghzCaptureModel * model) {
  111. model->counter++;
  112. model->parser = *parser;
  113. string_set(model->text, buffer);
  114. model->scene = 0;
  115. return true;
  116. });
  117. }
  118. void subghz_capture_enter(void* context) {
  119. furi_assert(context);
  120. SubghzCapture* subghz_capture = context;
  121. api_hal_subghz_reset();
  122. api_hal_subghz_idle();
  123. api_hal_subghz_load_preset(ApiHalSubGhzPresetOokAsync);
  124. with_view_model(
  125. subghz_capture->view, (SubghzCaptureModel * model) {
  126. model->frequency = subghz_frequencies_433_92;
  127. model->real_frequency =
  128. api_hal_subghz_set_frequency_and_path(subghz_frequencies[model->frequency]);
  129. model->scene = 1;
  130. return true;
  131. });
  132. hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  133. api_hal_subghz_set_async_rx_callback(subghz_worker_rx_callback, subghz_capture->worker);
  134. api_hal_subghz_start_async_rx();
  135. subghz_worker_start(subghz_capture->worker);
  136. api_hal_subghz_flush_rx();
  137. api_hal_subghz_rx();
  138. }
  139. void subghz_capture_exit(void* context) {
  140. furi_assert(context);
  141. SubghzCapture* subghz_capture = context;
  142. subghz_worker_stop(subghz_capture->worker);
  143. api_hal_subghz_stop_async_rx();
  144. api_hal_subghz_sleep();
  145. }
  146. uint32_t subghz_capture_back(void* context) {
  147. return SubGhzViewMenu;
  148. }
  149. SubghzCapture* subghz_capture_alloc() {
  150. SubghzCapture* subghz_capture = furi_alloc(sizeof(SubghzCapture));
  151. // View allocation and configuration
  152. subghz_capture->view = view_alloc();
  153. view_allocate_model(subghz_capture->view, ViewModelTypeLocking, sizeof(SubghzCaptureModel));
  154. view_set_context(subghz_capture->view, subghz_capture);
  155. view_set_draw_callback(subghz_capture->view, (ViewDrawCallback)subghz_capture_draw);
  156. view_set_input_callback(subghz_capture->view, subghz_capture_input);
  157. view_set_enter_callback(subghz_capture->view, subghz_capture_enter);
  158. view_set_exit_callback(subghz_capture->view, subghz_capture_exit);
  159. view_set_previous_callback(subghz_capture->view, subghz_capture_back);
  160. with_view_model(
  161. subghz_capture->view, (SubghzCaptureModel * model) {
  162. string_init(model->text);
  163. return true;
  164. });
  165. subghz_capture->worker = subghz_worker_alloc();
  166. subghz_capture->protocol = subghz_protocol_alloc();
  167. subghz_worker_set_overrun_callback(
  168. subghz_capture->worker, (SubGhzWorkerOverrunCallback)subghz_protocol_reset);
  169. subghz_worker_set_pair_callback(
  170. subghz_capture->worker, (SubGhzWorkerPairCallback)subghz_protocol_parse);
  171. subghz_worker_set_context(subghz_capture->worker, subghz_capture->protocol);
  172. subghz_protocol_load_keeloq_file(
  173. subghz_capture->protocol, "/ext/assets/subghz/keeloq_mfcodes");
  174. subghz_protocol_load_nice_flor_s_file(
  175. subghz_capture->protocol, "/ext/assets/subghz/nice_floor_s_rx");
  176. subghz_protocol_enable_dump_text(
  177. subghz_capture->protocol, subghz_capture_text_callback, subghz_capture);
  178. return subghz_capture;
  179. }
  180. void subghz_capture_free(SubghzCapture* subghz_capture) {
  181. furi_assert(subghz_capture);
  182. subghz_protocol_free(subghz_capture->protocol);
  183. subghz_worker_free(subghz_capture->worker);
  184. with_view_model(
  185. subghz_capture->view, (SubghzCaptureModel * model) {
  186. string_clear(model->text);
  187. return true;
  188. });
  189. view_free(subghz_capture->view);
  190. free(subghz_capture);
  191. }
  192. View* subghz_capture_get_view(SubghzCapture* subghz_capture) {
  193. furi_assert(subghz_capture);
  194. return subghz_capture->view;
  195. }