subghz_read_raw.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. #include "subghz_read_raw.h"
  2. #include "../subghz_i.h"
  3. #include <math.h>
  4. #include <furi.h>
  5. #include <furi_hal.h>
  6. #include <input/input.h>
  7. #include <gui/elements.h>
  8. #include <assets_icons.h>
  9. #define SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE 100
  10. #define TAG "SubGhzReadRAW"
  11. struct SubGhzReadRAW {
  12. View* view;
  13. SubGhzReadRAWCallback callback;
  14. void* context;
  15. };
  16. typedef struct {
  17. string_t frequency_str;
  18. string_t preset_str;
  19. string_t sample_write;
  20. string_t file_name;
  21. uint8_t* rssi_history;
  22. bool rssi_history_end;
  23. uint8_t ind_write;
  24. uint8_t ind_sin;
  25. SubGhzReadRAWStatus satus;
  26. } SubGhzReadRAWModel;
  27. void subghz_read_raw_set_callback(
  28. SubGhzReadRAW* subghz_read_raw,
  29. SubGhzReadRAWCallback callback,
  30. void* context) {
  31. furi_assert(subghz_read_raw);
  32. furi_assert(callback);
  33. subghz_read_raw->callback = callback;
  34. subghz_read_raw->context = context;
  35. }
  36. void subghz_read_raw_add_data_statusbar(
  37. SubGhzReadRAW* instance,
  38. const char* frequency_str,
  39. const char* preset_str) {
  40. furi_assert(instance);
  41. with_view_model(
  42. instance->view, (SubGhzReadRAWModel * model) {
  43. string_set(model->frequency_str, frequency_str);
  44. string_set(model->preset_str, preset_str);
  45. return true;
  46. });
  47. }
  48. void subghz_read_raw_add_data_rssi(SubGhzReadRAW* instance, float rssi) {
  49. furi_assert(instance);
  50. uint8_t u_rssi = 0;
  51. if(rssi < -90) {
  52. u_rssi = 0;
  53. } else {
  54. u_rssi = (uint8_t)((rssi + 90) / 2.7);
  55. }
  56. with_view_model(
  57. instance->view, (SubGhzReadRAWModel * model) {
  58. model->rssi_history[model->ind_write++] = u_rssi;
  59. if(model->ind_write > SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE) {
  60. model->rssi_history_end = true;
  61. model->ind_write = 0;
  62. }
  63. return true;
  64. });
  65. }
  66. void subghz_read_raw_update_sample_write(SubGhzReadRAW* instance, size_t sample) {
  67. furi_assert(instance);
  68. with_view_model(
  69. instance->view, (SubGhzReadRAWModel * model) {
  70. string_printf(model->sample_write, "%d spl.", sample);
  71. return false;
  72. });
  73. }
  74. void subghz_read_raw_stop_send(SubGhzReadRAW* instance) {
  75. furi_assert(instance);
  76. with_view_model(
  77. instance->view, (SubGhzReadRAWModel * model) {
  78. switch(model->satus) {
  79. case SubGhzReadRAWStatusTXRepeat:
  80. case SubGhzReadRAWStatusLoadKeyTXRepeat:
  81. instance->callback(SubGhzCustomEventViewReadRAWSendStart, instance->context);
  82. break;
  83. case SubGhzReadRAWStatusTX:
  84. model->satus = SubGhzReadRAWStatusIDLE;
  85. break;
  86. case SubGhzReadRAWStatusLoadKeyTX:
  87. model->satus = SubGhzReadRAWStatusLoadKeyIDLE;
  88. break;
  89. default:
  90. FURI_LOG_W(TAG, "unknown status");
  91. model->satus = SubGhzReadRAWStatusIDLE;
  92. break;
  93. }
  94. return true;
  95. });
  96. }
  97. void subghz_read_raw_update_sin(SubGhzReadRAW* instance) {
  98. furi_assert(instance);
  99. with_view_model(
  100. instance->view, (SubGhzReadRAWModel * model) {
  101. if(model->ind_sin++ > 62) {
  102. model->ind_sin = 0;
  103. }
  104. return true;
  105. });
  106. }
  107. static int8_t subghz_read_raw_tab_sin(uint8_t x) {
  108. const uint8_t tab_sin[64] = {0, 3, 6, 9, 12, 16, 19, 22, 25, 28, 31, 34, 37,
  109. 40, 43, 46, 49, 51, 54, 57, 60, 63, 65, 68, 71, 73,
  110. 76, 78, 81, 83, 85, 88, 90, 92, 94, 96, 98, 100, 102,
  111. 104, 106, 107, 109, 111, 112, 113, 115, 116, 117, 118, 120, 121,
  112. 122, 122, 123, 124, 125, 125, 126, 126, 126, 127, 127, 127};
  113. int8_t r = tab_sin[((x & 0x40) ? -x - 1 : x) & 0x3f];
  114. if(x & 0x80) return -r;
  115. return r;
  116. }
  117. void subghz_read_raw_draw_sin(Canvas* canvas, SubGhzReadRAWModel* model) {
  118. #define SUBGHZ_RAW_SIN_AMPLITUDE 11
  119. for(int i = 113; i > 0; i--) {
  120. canvas_draw_line(
  121. canvas,
  122. i,
  123. 32 - subghz_read_raw_tab_sin(i + model->ind_sin * 16) / SUBGHZ_RAW_SIN_AMPLITUDE,
  124. i + 1,
  125. 32 + subghz_read_raw_tab_sin((i + model->ind_sin * 16 + 1) * 2) /
  126. SUBGHZ_RAW_SIN_AMPLITUDE);
  127. canvas_draw_line(
  128. canvas,
  129. i + 1,
  130. 32 - subghz_read_raw_tab_sin((i + model->ind_sin * 16)) / SUBGHZ_RAW_SIN_AMPLITUDE,
  131. i + 2,
  132. 32 + subghz_read_raw_tab_sin((i + model->ind_sin * 16 + 1) * 2) /
  133. SUBGHZ_RAW_SIN_AMPLITUDE);
  134. }
  135. }
  136. void subghz_read_raw_draw_scale(Canvas* canvas, SubGhzReadRAWModel* model) {
  137. #define SUBGHZ_RAW_TOP_SCALE 14
  138. #define SUBGHZ_RAW_END_SCALE 115
  139. if(model->rssi_history_end == false) {
  140. for(int i = SUBGHZ_RAW_END_SCALE; i > 0; i -= 15) {
  141. canvas_draw_line(canvas, i, SUBGHZ_RAW_TOP_SCALE, i, SUBGHZ_RAW_TOP_SCALE + 4);
  142. canvas_draw_line(canvas, i - 5, SUBGHZ_RAW_TOP_SCALE, i - 5, SUBGHZ_RAW_TOP_SCALE + 2);
  143. canvas_draw_line(
  144. canvas, i - 10, SUBGHZ_RAW_TOP_SCALE, i - 10, SUBGHZ_RAW_TOP_SCALE + 2);
  145. }
  146. } else {
  147. for(int i = SUBGHZ_RAW_END_SCALE - model->ind_write % 15; i > -15; i -= 15) {
  148. canvas_draw_line(canvas, i, SUBGHZ_RAW_TOP_SCALE, i, SUBGHZ_RAW_TOP_SCALE + 4);
  149. if(SUBGHZ_RAW_END_SCALE > i + 5)
  150. canvas_draw_line(
  151. canvas, i + 5, SUBGHZ_RAW_TOP_SCALE, i + 5, SUBGHZ_RAW_TOP_SCALE + 2);
  152. if(SUBGHZ_RAW_END_SCALE > i + 10)
  153. canvas_draw_line(
  154. canvas, i + 10, SUBGHZ_RAW_TOP_SCALE, i + 10, SUBGHZ_RAW_TOP_SCALE + 2);
  155. }
  156. }
  157. }
  158. void subghz_read_raw_draw_rssi(Canvas* canvas, SubGhzReadRAWModel* model) {
  159. int ind = 0;
  160. int base = 0;
  161. if(model->rssi_history_end == false) {
  162. for(int i = model->ind_write; i >= 0; i--) {
  163. canvas_draw_line(canvas, i, 47, i, 47 - model->rssi_history[i]);
  164. }
  165. if(model->ind_write > 3) {
  166. canvas_draw_line(canvas, model->ind_write, 47, model->ind_write, 13);
  167. canvas_draw_line(canvas, model->ind_write - 2, 12, model->ind_write + 2, 12);
  168. canvas_draw_line(canvas, model->ind_write - 1, 13, model->ind_write + 1, 13);
  169. }
  170. } else {
  171. base = SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE - model->ind_write;
  172. for(int i = SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE; i >= 0; i--) {
  173. ind = i - base;
  174. if(ind < 0) ind += SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE;
  175. canvas_draw_line(canvas, i, 47, i, 47 - model->rssi_history[ind]);
  176. }
  177. canvas_draw_line(
  178. canvas, SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE, 47, SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE, 13);
  179. canvas_draw_line(
  180. canvas,
  181. SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE - 2,
  182. 12,
  183. SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE + 2,
  184. 12);
  185. canvas_draw_line(
  186. canvas,
  187. SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE - 1,
  188. 13,
  189. SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE + 1,
  190. 13);
  191. }
  192. }
  193. void subghz_read_raw_draw(Canvas* canvas, SubGhzReadRAWModel* model) {
  194. uint8_t graphics_mode = 1;
  195. canvas_set_color(canvas, ColorBlack);
  196. canvas_set_font(canvas, FontSecondary);
  197. canvas_draw_str(canvas, 5, 8, string_get_cstr(model->frequency_str));
  198. canvas_draw_str(canvas, 40, 8, string_get_cstr(model->preset_str));
  199. canvas_draw_str_aligned(
  200. canvas, 126, 0, AlignRight, AlignTop, string_get_cstr(model->sample_write));
  201. canvas_draw_line(canvas, 0, 14, 115, 14);
  202. canvas_draw_line(canvas, 0, 48, 115, 48);
  203. canvas_draw_line(canvas, 115, 14, 115, 48);
  204. switch(model->satus) {
  205. case SubGhzReadRAWStatusIDLE:
  206. elements_button_left(canvas, "Erase");
  207. elements_button_center(canvas, "Send");
  208. elements_button_right(canvas, "Save");
  209. break;
  210. case SubGhzReadRAWStatusLoadKeyIDLE:
  211. elements_button_left(canvas, "New");
  212. elements_button_center(canvas, "Send");
  213. elements_button_right(canvas, "More");
  214. elements_text_box(
  215. canvas, 4, 12, 110, 44, AlignCenter, AlignCenter, string_get_cstr(model->file_name));
  216. break;
  217. case SubGhzReadRAWStatusTX:
  218. case SubGhzReadRAWStatusTXRepeat:
  219. case SubGhzReadRAWStatusLoadKeyTX:
  220. case SubGhzReadRAWStatusLoadKeyTXRepeat:
  221. graphics_mode = 0;
  222. elements_button_center(canvas, "Send");
  223. break;
  224. case SubGhzReadRAWStatusStart:
  225. elements_button_left(canvas, "Config");
  226. elements_button_center(canvas, "REC");
  227. break;
  228. default:
  229. elements_button_center(canvas, "Stop");
  230. break;
  231. }
  232. if(graphics_mode == 0) {
  233. subghz_read_raw_draw_sin(canvas, model);
  234. } else {
  235. subghz_read_raw_draw_rssi(canvas, model);
  236. subghz_read_raw_draw_scale(canvas, model);
  237. canvas_set_font_direction(canvas, CanvasDirectionBottomToTop);
  238. canvas_draw_str(canvas, 126, 40, "RSSI");
  239. canvas_set_font_direction(canvas, CanvasDirectionLeftToRight);
  240. }
  241. }
  242. bool subghz_read_raw_input(InputEvent* event, void* context) {
  243. furi_assert(context);
  244. SubGhzReadRAW* instance = context;
  245. if((event->key == InputKeyOk) &&
  246. (event->type == InputTypeLong || event->type == InputTypeRepeat)) {
  247. //we check that if we hold the transfer button,
  248. //further check of events is not needed, we exit
  249. return false;
  250. } else if(event->key == InputKeyOk && event->type == InputTypePress) {
  251. with_view_model(
  252. instance->view, (SubGhzReadRAWModel * model) {
  253. uint8_t ret = false;
  254. switch(model->satus) {
  255. case SubGhzReadRAWStatusIDLE:
  256. // Start TX
  257. instance->callback(SubGhzCustomEventViewReadRAWSendStart, instance->context);
  258. instance->callback(SubGhzCustomEventViewReadRAWVibro, instance->context);
  259. model->satus = SubGhzReadRAWStatusTXRepeat;
  260. ret = true;
  261. break;
  262. case SubGhzReadRAWStatusTX:
  263. // Start TXRepeat
  264. model->satus = SubGhzReadRAWStatusTXRepeat;
  265. break;
  266. case SubGhzReadRAWStatusLoadKeyIDLE:
  267. // Start Load Key TX
  268. instance->callback(SubGhzCustomEventViewReadRAWSendStart, instance->context);
  269. instance->callback(SubGhzCustomEventViewReadRAWVibro, instance->context);
  270. model->satus = SubGhzReadRAWStatusLoadKeyTXRepeat;
  271. ret = true;
  272. break;
  273. case SubGhzReadRAWStatusLoadKeyTX:
  274. // Start Load Key TXRepeat
  275. model->satus = SubGhzReadRAWStatusLoadKeyTXRepeat;
  276. break;
  277. default:
  278. break;
  279. }
  280. return ret;
  281. });
  282. } else if(event->key == InputKeyOk && event->type == InputTypeRelease) {
  283. with_view_model(
  284. instance->view, (SubGhzReadRAWModel * model) {
  285. if(model->satus == SubGhzReadRAWStatusTXRepeat) {
  286. // Stop repeat TX
  287. model->satus = SubGhzReadRAWStatusTX;
  288. } else if(model->satus == SubGhzReadRAWStatusLoadKeyTXRepeat) {
  289. // Stop repeat TX
  290. model->satus = SubGhzReadRAWStatusLoadKeyTX;
  291. }
  292. return false;
  293. });
  294. } else if(event->key == InputKeyBack && event->type == InputTypeShort) {
  295. with_view_model(
  296. instance->view, (SubGhzReadRAWModel * model) {
  297. switch(model->satus) {
  298. case SubGhzReadRAWStatusREC:
  299. //Stop REC
  300. instance->callback(SubGhzCustomEventViewReadRAWIDLE, instance->context);
  301. model->satus = SubGhzReadRAWStatusIDLE;
  302. break;
  303. case SubGhzReadRAWStatusLoadKeyTX:
  304. //Stop TxRx
  305. instance->callback(SubGhzCustomEventViewReadRAWTXRXStop, instance->context);
  306. model->satus = SubGhzReadRAWStatusLoadKeyIDLE;
  307. break;
  308. case SubGhzReadRAWStatusTX:
  309. //Stop TxRx
  310. instance->callback(SubGhzCustomEventViewReadRAWTXRXStop, instance->context);
  311. model->satus = SubGhzReadRAWStatusIDLE;
  312. break;
  313. case SubGhzReadRAWStatusLoadKeyIDLE:
  314. //Exit
  315. instance->callback(SubGhzCustomEventViewReadRAWBack, instance->context);
  316. break;
  317. default:
  318. //Exit
  319. instance->callback(SubGhzCustomEventViewReadRAWBack, instance->context);
  320. break;
  321. }
  322. return true;
  323. });
  324. } else if(event->key == InputKeyLeft && event->type == InputTypeShort) {
  325. with_view_model(
  326. instance->view, (SubGhzReadRAWModel * model) {
  327. if(model->satus == SubGhzReadRAWStatusStart) {
  328. //Config
  329. instance->callback(SubGhzCustomEventViewReadRAWConfig, instance->context);
  330. } else if(
  331. (model->satus == SubGhzReadRAWStatusIDLE) ||
  332. (model->satus == SubGhzReadRAWStatusLoadKeyIDLE)) {
  333. //Erase
  334. model->satus = SubGhzReadRAWStatusStart;
  335. model->rssi_history_end = false;
  336. model->ind_write = 0;
  337. string_set(model->sample_write, "0 spl.");
  338. string_reset(model->file_name);
  339. instance->callback(SubGhzCustomEventViewReadRAWErase, instance->context);
  340. }
  341. return true;
  342. });
  343. } else if(event->key == InputKeyRight && event->type == InputTypeShort) {
  344. with_view_model(
  345. instance->view, (SubGhzReadRAWModel * model) {
  346. if(model->satus == SubGhzReadRAWStatusIDLE) {
  347. //Save
  348. instance->callback(SubGhzCustomEventViewReadRAWSave, instance->context);
  349. } else if(model->satus == SubGhzReadRAWStatusLoadKeyIDLE) {
  350. //More
  351. instance->callback(SubGhzCustomEventViewReadRAWMore, instance->context);
  352. }
  353. return true;
  354. });
  355. } else if(event->key == InputKeyOk && event->type == InputTypeShort) {
  356. with_view_model(
  357. instance->view, (SubGhzReadRAWModel * model) {
  358. if(model->satus == SubGhzReadRAWStatusStart) {
  359. //Record
  360. instance->callback(SubGhzCustomEventViewReadRAWREC, instance->context);
  361. model->satus = SubGhzReadRAWStatusREC;
  362. model->ind_write = 0;
  363. model->rssi_history_end = false;
  364. } else if(model->satus == SubGhzReadRAWStatusREC) {
  365. //Stop
  366. instance->callback(SubGhzCustomEventViewReadRAWIDLE, instance->context);
  367. model->satus = SubGhzReadRAWStatusIDLE;
  368. }
  369. return true;
  370. });
  371. }
  372. return true;
  373. }
  374. void subghz_read_raw_set_status(
  375. SubGhzReadRAW* instance,
  376. SubGhzReadRAWStatus satus,
  377. const char* file_name) {
  378. furi_assert(instance);
  379. switch(satus) {
  380. case SubGhzReadRAWStatusStart:
  381. with_view_model(
  382. instance->view, (SubGhzReadRAWModel * model) {
  383. model->satus = SubGhzReadRAWStatusStart;
  384. model->rssi_history_end = false;
  385. model->ind_write = 0;
  386. string_reset(model->file_name);
  387. string_set(model->sample_write, "0 spl.");
  388. return true;
  389. });
  390. break;
  391. case SubGhzReadRAWStatusIDLE:
  392. with_view_model(
  393. instance->view, (SubGhzReadRAWModel * model) {
  394. model->satus = SubGhzReadRAWStatusIDLE;
  395. return true;
  396. });
  397. break;
  398. case SubGhzReadRAWStatusLoadKeyTX:
  399. with_view_model(
  400. instance->view, (SubGhzReadRAWModel * model) {
  401. model->satus = SubGhzReadRAWStatusLoadKeyIDLE;
  402. model->rssi_history_end = false;
  403. model->ind_write = 0;
  404. string_set(model->file_name, file_name);
  405. string_set(model->sample_write, "RAW");
  406. return true;
  407. });
  408. break;
  409. case SubGhzReadRAWStatusSaveKey:
  410. with_view_model(
  411. instance->view, (SubGhzReadRAWModel * model) {
  412. model->satus = SubGhzReadRAWStatusLoadKeyIDLE;
  413. if(!model->ind_write) {
  414. string_set(model->file_name, file_name);
  415. string_set(model->sample_write, "RAW");
  416. } else {
  417. string_reset(model->file_name);
  418. }
  419. return true;
  420. });
  421. break;
  422. default:
  423. FURI_LOG_W(TAG, "unknown status");
  424. break;
  425. }
  426. }
  427. void subghz_read_raw_enter(void* context) {
  428. furi_assert(context);
  429. //SubGhzReadRAW* instance = context;
  430. }
  431. void subghz_read_raw_exit(void* context) {
  432. furi_assert(context);
  433. SubGhzReadRAW* instance = context;
  434. with_view_model(
  435. instance->view, (SubGhzReadRAWModel * model) {
  436. if(model->satus != SubGhzReadRAWStatusIDLE &&
  437. model->satus != SubGhzReadRAWStatusStart &&
  438. model->satus != SubGhzReadRAWStatusLoadKeyIDLE) {
  439. instance->callback(SubGhzCustomEventViewReadRAWIDLE, instance->context);
  440. model->satus = SubGhzReadRAWStatusStart;
  441. }
  442. return true;
  443. });
  444. }
  445. SubGhzReadRAW* subghz_read_raw_alloc() {
  446. SubGhzReadRAW* instance = malloc(sizeof(SubGhzReadRAW));
  447. // View allocation and configuration
  448. instance->view = view_alloc();
  449. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(SubGhzReadRAWModel));
  450. view_set_context(instance->view, instance);
  451. view_set_draw_callback(instance->view, (ViewDrawCallback)subghz_read_raw_draw);
  452. view_set_input_callback(instance->view, subghz_read_raw_input);
  453. view_set_enter_callback(instance->view, subghz_read_raw_enter);
  454. view_set_exit_callback(instance->view, subghz_read_raw_exit);
  455. with_view_model(
  456. instance->view, (SubGhzReadRAWModel * model) {
  457. string_init(model->frequency_str);
  458. string_init(model->preset_str);
  459. string_init(model->sample_write);
  460. string_init(model->file_name);
  461. model->rssi_history = malloc(SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE * sizeof(uint8_t));
  462. return true;
  463. });
  464. return instance;
  465. }
  466. void subghz_read_raw_free(SubGhzReadRAW* instance) {
  467. furi_assert(instance);
  468. with_view_model(
  469. instance->view, (SubGhzReadRAWModel * model) {
  470. string_clear(model->frequency_str);
  471. string_clear(model->preset_str);
  472. string_clear(model->sample_write);
  473. string_clear(model->file_name);
  474. free(model->rssi_history);
  475. return true;
  476. });
  477. view_free(instance->view);
  478. free(instance);
  479. }
  480. View* subghz_read_raw_get_view(SubGhzReadRAW* instance) {
  481. furi_assert(instance);
  482. return instance->view;
  483. }