subghz_read_raw.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 <lib/subghz/protocols/subghz_protocol_princeton.h>
  9. #include <assets_icons.h>
  10. #define SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE 100
  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. //if(u_rssi > 34) u_rssi = 34;
  57. with_view_model(
  58. instance->view, (SubghzReadRAWModel * model) {
  59. model->rssi_history[model->ind_write++] = u_rssi;
  60. if(model->ind_write > SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE) {
  61. model->rssi_history_end = true;
  62. model->ind_write = 0;
  63. }
  64. return true;
  65. });
  66. }
  67. void subghz_read_raw_update_sample_write(SubghzReadRAW* instance, size_t sample) {
  68. furi_assert(instance);
  69. with_view_model(
  70. instance->view, (SubghzReadRAWModel * model) {
  71. string_printf(model->sample_write, "%d spl.", sample);
  72. return false;
  73. });
  74. }
  75. void subghz_read_raw_stop_send(SubghzReadRAW* instance) {
  76. furi_assert(instance);
  77. with_view_model(
  78. instance->view, (SubghzReadRAWModel * model) {
  79. if(model->satus == SubghzReadRAWStatusTXRepeat) {
  80. // Start TX
  81. instance->callback(SubghzCustomEventViewReadRAWSendStart, instance->context);
  82. } else {
  83. model->satus = SubghzReadRAWStatusIDLE;
  84. }
  85. return true;
  86. });
  87. }
  88. void subghz_read_raw_update_sin(SubghzReadRAW* instance) {
  89. furi_assert(instance);
  90. with_view_model(
  91. instance->view, (SubghzReadRAWModel * model) {
  92. if(model->ind_sin++ > 62) {
  93. model->ind_sin = 0;
  94. }
  95. return true;
  96. });
  97. }
  98. static int8_t subghz_read_raw_tab_sin(uint8_t x) {
  99. const uint8_t tab_sin[64] = {0, 3, 6, 9, 12, 16, 19, 22, 25, 28, 31, 34, 37,
  100. 40, 43, 46, 49, 51, 54, 57, 60, 63, 65, 68, 71, 73,
  101. 76, 78, 81, 83, 85, 88, 90, 92, 94, 96, 98, 100, 102,
  102. 104, 106, 107, 109, 111, 112, 113, 115, 116, 117, 118, 120, 121,
  103. 122, 122, 123, 124, 125, 125, 126, 126, 126, 127, 127, 127};
  104. int8_t r = tab_sin[((x & 0x40) ? -x - 1 : x) & 0x3f];
  105. if(x & 0x80) return -r;
  106. return r;
  107. }
  108. void subghz_read_raw_draw_sin(Canvas* canvas, SubghzReadRAWModel* model) {
  109. #define SUBGHZ_RAW_SIN_AMPLITUDE 11
  110. for(int i = 113; i > 0; i--) {
  111. canvas_draw_line(
  112. canvas,
  113. i,
  114. 32 - subghz_read_raw_tab_sin(i + model->ind_sin * 16) / SUBGHZ_RAW_SIN_AMPLITUDE,
  115. i + 1,
  116. 32 + subghz_read_raw_tab_sin((i + model->ind_sin * 16 + 1) * 2) /
  117. SUBGHZ_RAW_SIN_AMPLITUDE);
  118. canvas_draw_line(
  119. canvas,
  120. i + 1,
  121. 32 - subghz_read_raw_tab_sin((i + model->ind_sin * 16)) / SUBGHZ_RAW_SIN_AMPLITUDE,
  122. i + 2,
  123. 32 + subghz_read_raw_tab_sin((i + model->ind_sin * 16 + 1) * 2) /
  124. SUBGHZ_RAW_SIN_AMPLITUDE);
  125. }
  126. }
  127. void subghz_read_raw_draw_scale(Canvas* canvas, SubghzReadRAWModel* model) {
  128. #define SUBGHZ_RAW_TOP_SCALE 14
  129. #define SUBGHZ_RAW_END_SCALE 115
  130. if(model->rssi_history_end == false) {
  131. for(int i = SUBGHZ_RAW_END_SCALE; i > 0; i -= 15) {
  132. canvas_draw_line(canvas, i, SUBGHZ_RAW_TOP_SCALE, i, SUBGHZ_RAW_TOP_SCALE + 4);
  133. canvas_draw_line(canvas, i - 5, SUBGHZ_RAW_TOP_SCALE, i - 5, SUBGHZ_RAW_TOP_SCALE + 2);
  134. canvas_draw_line(
  135. canvas, i - 10, SUBGHZ_RAW_TOP_SCALE, i - 10, SUBGHZ_RAW_TOP_SCALE + 2);
  136. }
  137. } else {
  138. for(int i = SUBGHZ_RAW_END_SCALE - model->ind_write % 15; i > -15; i -= 15) {
  139. canvas_draw_line(canvas, i, SUBGHZ_RAW_TOP_SCALE, i, SUBGHZ_RAW_TOP_SCALE + 4);
  140. if(SUBGHZ_RAW_END_SCALE > i + 5)
  141. canvas_draw_line(
  142. canvas, i + 5, SUBGHZ_RAW_TOP_SCALE, i + 5, SUBGHZ_RAW_TOP_SCALE + 2);
  143. if(SUBGHZ_RAW_END_SCALE > i + 10)
  144. canvas_draw_line(
  145. canvas, i + 10, SUBGHZ_RAW_TOP_SCALE, i + 10, SUBGHZ_RAW_TOP_SCALE + 2);
  146. }
  147. }
  148. }
  149. void subghz_read_raw_draw_rssi(Canvas* canvas, SubghzReadRAWModel* model) {
  150. int ind = 0;
  151. int base = 0;
  152. if(model->rssi_history_end == false) {
  153. for(int i = model->ind_write; i >= 0; i--) {
  154. canvas_draw_line(canvas, i, 47, i, 47 - model->rssi_history[i]);
  155. }
  156. if(model->ind_write > 3) {
  157. canvas_draw_line(canvas, model->ind_write, 47, model->ind_write, 13);
  158. canvas_draw_line(canvas, model->ind_write - 2, 12, model->ind_write + 2, 12);
  159. canvas_draw_line(canvas, model->ind_write - 1, 13, model->ind_write + 1, 13);
  160. }
  161. } else {
  162. base = SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE - model->ind_write;
  163. for(int i = SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE; i >= 0; i--) {
  164. ind = i - base;
  165. if(ind < 0) ind += SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE;
  166. canvas_draw_line(canvas, i, 47, i, 47 - model->rssi_history[ind]);
  167. }
  168. canvas_draw_line(
  169. canvas, SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE, 47, SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE, 13);
  170. canvas_draw_line(
  171. canvas,
  172. SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE - 2,
  173. 12,
  174. SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE + 2,
  175. 12);
  176. canvas_draw_line(
  177. canvas,
  178. SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE - 1,
  179. 13,
  180. SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE + 1,
  181. 13);
  182. }
  183. }
  184. void subghz_read_raw_draw(Canvas* canvas, SubghzReadRAWModel* model) {
  185. canvas_set_color(canvas, ColorBlack);
  186. canvas_set_font(canvas, FontSecondary);
  187. canvas_draw_str(canvas, 5, 8, string_get_cstr(model->frequency_str));
  188. canvas_draw_str(canvas, 40, 8, string_get_cstr(model->preset_str));
  189. canvas_draw_str_aligned(
  190. canvas, 126, 0, AlignRight, AlignTop, string_get_cstr(model->sample_write));
  191. canvas_draw_line(canvas, 0, 14, 115, 14);
  192. canvas_draw_line(canvas, 0, 48, 115, 48);
  193. canvas_draw_line(canvas, 115, 14, 115, 48);
  194. if((model->satus == SubghzReadRAWStatusTX) || (model->satus == SubghzReadRAWStatusTXRepeat)) {
  195. subghz_read_raw_draw_sin(canvas, model);
  196. } else {
  197. subghz_read_raw_draw_rssi(canvas, model);
  198. subghz_read_raw_draw_scale(canvas, model);
  199. }
  200. if(model->satus == SubghzReadRAWStatusIDLE) {
  201. elements_button_left(canvas, "Erase");
  202. elements_button_center(canvas, "Send");
  203. elements_button_right(canvas, "Save");
  204. canvas_draw_str_aligned(
  205. canvas, 58, 28, AlignCenter, AlignTop, string_get_cstr(model->file_name));
  206. } else if(model->satus == SubghzReadRAWStatusStart) {
  207. elements_button_left(canvas, "Config");
  208. elements_button_center(canvas, "REC");
  209. } else if(
  210. (model->satus == SubghzReadRAWStatusTX) || (model->satus == SubghzReadRAWStatusTXRepeat)) {
  211. elements_button_center(canvas, "Send");
  212. } else {
  213. elements_button_center(canvas, "Stop");
  214. }
  215. canvas_set_font_direction(canvas, 3);
  216. canvas_draw_str(canvas, 126, 40, "RSSI");
  217. canvas_set_font_direction(canvas, 0);
  218. }
  219. bool subghz_read_raw_input(InputEvent* event, void* context) {
  220. furi_assert(context);
  221. SubghzReadRAW* instance = context;
  222. if((event->key == InputKeyOk) &&
  223. (event->type == InputTypeLong || event->type == InputTypeRepeat)) {
  224. //we check that if we hold the transfer button,
  225. //further check of events is not needed, we exit
  226. return false;
  227. } else if(event->key == InputKeyOk && event->type == InputTypePress) {
  228. with_view_model(
  229. instance->view, (SubghzReadRAWModel * model) {
  230. uint8_t ret = false;
  231. if(model->satus == SubghzReadRAWStatusIDLE) {
  232. // Start TX
  233. instance->callback(SubghzCustomEventViewReadRAWSendStart, instance->context);
  234. instance->callback(SubghzCustomEventViewReadRAWVibro, instance->context);
  235. model->satus = SubghzReadRAWStatusTXRepeat;
  236. ret = true;
  237. } else if(model->satus == SubghzReadRAWStatusTX) {
  238. model->satus = SubghzReadRAWStatusTXRepeat;
  239. }
  240. return ret;
  241. });
  242. } else if(event->key == InputKeyOk && event->type == InputTypeRelease) {
  243. with_view_model(
  244. instance->view, (SubghzReadRAWModel * model) {
  245. if(model->satus == SubghzReadRAWStatusTXRepeat) {
  246. // Stop repeat TX
  247. model->satus = SubghzReadRAWStatusTX;
  248. }
  249. return false;
  250. });
  251. } else if(event->key == InputKeyBack && event->type == InputTypeShort) {
  252. with_view_model(
  253. instance->view, (SubghzReadRAWModel * model) {
  254. if(model->satus == SubghzReadRAWStatusREC) {
  255. //Stop REC
  256. instance->callback(SubghzCustomEventViewReadRAWIDLE, instance->context);
  257. model->satus = SubghzReadRAWStatusIDLE;
  258. } else {
  259. //Exit
  260. instance->callback(SubghzCustomEventViewReadRAWBack, instance->context);
  261. }
  262. return true;
  263. });
  264. } else if(event->key == InputKeyLeft && event->type == InputTypeShort) {
  265. with_view_model(
  266. instance->view, (SubghzReadRAWModel * model) {
  267. if(model->satus == SubghzReadRAWStatusStart) {
  268. //Config
  269. instance->callback(SubghzCustomEventViewReadRAWConfig, instance->context);
  270. } else if(model->satus == SubghzReadRAWStatusIDLE) {
  271. //Erase
  272. model->satus = SubghzReadRAWStatusStart;
  273. model->rssi_history_end = false;
  274. model->ind_write = 0;
  275. string_set(model->sample_write, "0 spl.");
  276. string_reset(model->file_name);
  277. instance->callback(SubghzCustomEventViewReadRAWErase, instance->context);
  278. }
  279. return true;
  280. });
  281. } else if(event->key == InputKeyRight && event->type == InputTypeShort) {
  282. with_view_model(
  283. instance->view, (SubghzReadRAWModel * model) {
  284. //Save
  285. if(model->satus == SubghzReadRAWStatusIDLE) {
  286. instance->callback(SubghzCustomEventViewReadRAWSave, instance->context);
  287. }
  288. return true;
  289. });
  290. } else if(event->key == InputKeyOk && event->type == InputTypeShort) {
  291. with_view_model(
  292. instance->view, (SubghzReadRAWModel * model) {
  293. if(model->satus == SubghzReadRAWStatusStart) {
  294. //Record
  295. instance->callback(SubghzCustomEventViewReadRAWREC, instance->context);
  296. model->satus = SubghzReadRAWStatusREC;
  297. model->ind_write = 0;
  298. model->rssi_history_end = false;
  299. } else if(
  300. (model->satus != SubghzReadRAWStatusTX) &&
  301. (model->satus != SubghzReadRAWStatusTXRepeat)) {
  302. //Stop
  303. instance->callback(SubghzCustomEventViewReadRAWIDLE, instance->context);
  304. model->satus = SubghzReadRAWStatusIDLE;
  305. }
  306. return true;
  307. });
  308. }
  309. return true;
  310. }
  311. void subghz_read_raw_set_status(
  312. SubghzReadRAW* instance,
  313. SubghzReadRAWStatus satus,
  314. const char* file_name) {
  315. furi_assert(instance);
  316. if(satus == SubghzReadRAWStatusStart) {
  317. with_view_model(
  318. instance->view, (SubghzReadRAWModel * model) {
  319. model->satus = SubghzReadRAWStatusStart;
  320. model->rssi_history_end = false;
  321. model->ind_write = 0;
  322. string_reset(model->file_name);
  323. string_set(model->sample_write, "0 spl.");
  324. return true;
  325. });
  326. } else if(satus == SubghzReadRAWStatusIDLE) {
  327. with_view_model(
  328. instance->view, (SubghzReadRAWModel * model) {
  329. model->satus = SubghzReadRAWStatusIDLE;
  330. return true;
  331. });
  332. } else if(satus == SubghzReadRAWStatusTX) {
  333. with_view_model(
  334. instance->view, (SubghzReadRAWModel * model) {
  335. model->satus = SubghzReadRAWStatusIDLE;
  336. model->rssi_history_end = false;
  337. model->ind_write = 0;
  338. string_set(model->file_name, file_name);
  339. string_set(model->sample_write, "RAW");
  340. return true;
  341. });
  342. }
  343. }
  344. void subghz_read_raw_enter(void* context) {
  345. furi_assert(context);
  346. //SubghzReadRAW* instance = context;
  347. }
  348. void subghz_read_raw_exit(void* context) {
  349. furi_assert(context);
  350. SubghzReadRAW* instance = context;
  351. with_view_model(
  352. instance->view, (SubghzReadRAWModel * model) {
  353. if(model->satus != SubghzReadRAWStatusIDLE &&
  354. model->satus != SubghzReadRAWStatusStart) {
  355. instance->callback(SubghzCustomEventViewReadRAWIDLE, instance->context);
  356. model->satus = SubghzReadRAWStatusStart;
  357. }
  358. return true;
  359. });
  360. }
  361. SubghzReadRAW* subghz_read_raw_alloc() {
  362. SubghzReadRAW* instance = furi_alloc(sizeof(SubghzReadRAW));
  363. // View allocation and configuration
  364. instance->view = view_alloc();
  365. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(SubghzReadRAWModel));
  366. view_set_context(instance->view, instance);
  367. view_set_draw_callback(instance->view, (ViewDrawCallback)subghz_read_raw_draw);
  368. view_set_input_callback(instance->view, subghz_read_raw_input);
  369. view_set_enter_callback(instance->view, subghz_read_raw_enter);
  370. view_set_exit_callback(instance->view, subghz_read_raw_exit);
  371. with_view_model(
  372. instance->view, (SubghzReadRAWModel * model) {
  373. string_init(model->frequency_str);
  374. string_init(model->preset_str);
  375. string_init(model->sample_write);
  376. string_init(model->file_name);
  377. model->rssi_history = furi_alloc(SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE * sizeof(uint8_t));
  378. return true;
  379. });
  380. return instance;
  381. }
  382. void subghz_read_raw_free(SubghzReadRAW* instance) {
  383. furi_assert(instance);
  384. with_view_model(
  385. instance->view, (SubghzReadRAWModel * model) {
  386. string_clear(model->frequency_str);
  387. string_clear(model->preset_str);
  388. string_clear(model->sample_write);
  389. string_clear(model->file_name);
  390. free(model->rssi_history);
  391. return true;
  392. });
  393. view_free(instance->view);
  394. free(instance);
  395. }
  396. View* subghz_read_raw_get_view(SubghzReadRAW* instance) {
  397. furi_assert(instance);
  398. return instance->view;
  399. }