pocsag_pager_receiver.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. #include "pocsag_pager_receiver.h"
  2. #include "../pocsag_pager_app_i.h"
  3. #include <pocsag_pager_icons.h>
  4. #include <math.h>
  5. #include <input/input.h>
  6. #include <gui/elements.h>
  7. #include <m-array.h>
  8. #define FRAME_HEIGHT 12
  9. #define MAX_LEN_PX 112
  10. #define MENU_ITEMS 4u
  11. #define UNLOCK_CNT 3
  12. #define SUBGHZ_RAW_THRESHOLD_MIN -90.0f
  13. typedef struct {
  14. FuriString* item_str;
  15. uint8_t type;
  16. } PCSGReceiverMenuItem;
  17. ARRAY_DEF(PCSGReceiverMenuItemArray, PCSGReceiverMenuItem, M_POD_OPLIST)
  18. #define M_OPL_PCSGReceiverMenuItemArray_t() ARRAY_OPLIST(PCSGReceiverMenuItemArray, M_POD_OPLIST)
  19. struct PCSGReceiverHistory {
  20. PCSGReceiverMenuItemArray_t data;
  21. };
  22. typedef struct PCSGReceiverHistory PCSGReceiverHistory;
  23. static const Icon* ReceiverItemIcons[] = {
  24. [SubGhzProtocolTypeUnknown] = &I_Quest_7x8,
  25. [SubGhzProtocolTypeStatic] = &I_Message_8x7,
  26. [SubGhzProtocolTypeDynamic] = &I_Lock_7x8,
  27. };
  28. typedef enum {
  29. PCSGReceiverBarShowDefault,
  30. PCSGReceiverBarShowLock,
  31. PCSGReceiverBarShowToUnlockPress,
  32. PCSGReceiverBarShowUnlock,
  33. } PCSGReceiverBarShow;
  34. struct PCSGReceiver {
  35. PCSGLock lock;
  36. uint8_t lock_count;
  37. FuriTimer* timer;
  38. View* view;
  39. PCSGReceiverCallback callback;
  40. void* context;
  41. };
  42. typedef struct {
  43. FuriString* frequency_str;
  44. FuriString* preset_str;
  45. FuriString* history_stat_str;
  46. PCSGReceiverHistory* history;
  47. uint16_t idx;
  48. uint16_t list_offset;
  49. uint16_t history_item;
  50. PCSGReceiverBarShow bar_show;
  51. uint8_t u_rssi;
  52. bool ext_module;
  53. } PCSGReceiverModel;
  54. void pcsg_receiver_rssi(PCSGReceiver* instance, float rssi) {
  55. furi_assert(instance);
  56. with_view_model(
  57. instance->view,
  58. PCSGReceiverModel * model,
  59. {
  60. if(rssi < SUBGHZ_RAW_THRESHOLD_MIN) {
  61. model->u_rssi = 0;
  62. } else {
  63. model->u_rssi = (uint8_t)(rssi - SUBGHZ_RAW_THRESHOLD_MIN);
  64. }
  65. },
  66. true);
  67. }
  68. void pcsg_view_receiver_set_lock(PCSGReceiver* pcsg_receiver, PCSGLock lock) {
  69. furi_assert(pcsg_receiver);
  70. pcsg_receiver->lock_count = 0;
  71. if(lock == PCSGLockOn) {
  72. pcsg_receiver->lock = lock;
  73. with_view_model(
  74. pcsg_receiver->view,
  75. PCSGReceiverModel * model,
  76. { model->bar_show = PCSGReceiverBarShowLock; },
  77. true);
  78. furi_timer_start(pcsg_receiver->timer, 1000);
  79. } else {
  80. with_view_model(
  81. pcsg_receiver->view,
  82. PCSGReceiverModel * model,
  83. { model->bar_show = PCSGReceiverBarShowDefault; },
  84. true);
  85. }
  86. }
  87. void pcsg_view_receiver_set_ext_module_state(PCSGReceiver* pcsg_receiver, bool is_external) {
  88. furi_assert(pcsg_receiver);
  89. with_view_model(
  90. pcsg_receiver->view, PCSGReceiverModel * model, { model->ext_module = is_external; }, true);
  91. }
  92. void pcsg_view_receiver_set_callback(
  93. PCSGReceiver* pcsg_receiver,
  94. PCSGReceiverCallback callback,
  95. void* context) {
  96. furi_assert(pcsg_receiver);
  97. furi_assert(callback);
  98. pcsg_receiver->callback = callback;
  99. pcsg_receiver->context = context;
  100. }
  101. static void pcsg_view_receiver_update_offset(PCSGReceiver* pcsg_receiver) {
  102. furi_assert(pcsg_receiver);
  103. with_view_model(
  104. pcsg_receiver->view,
  105. PCSGReceiverModel * model,
  106. {
  107. size_t history_item = model->history_item;
  108. uint16_t bounds = history_item > 3 ? 2 : history_item;
  109. if(history_item > 3 && model->idx >= (int16_t)(history_item - 1)) {
  110. model->list_offset = model->idx - 3;
  111. } else if(model->list_offset < model->idx - bounds) {
  112. model->list_offset =
  113. CLAMP(model->list_offset + 1, (int16_t)(history_item - bounds), 0);
  114. } else if(model->list_offset > model->idx - bounds) {
  115. model->list_offset = CLAMP(model->idx - 1, (int16_t)(history_item - bounds), 0);
  116. }
  117. },
  118. true);
  119. }
  120. void pcsg_view_receiver_add_item_to_menu(
  121. PCSGReceiver* pcsg_receiver,
  122. const char* name,
  123. uint8_t type) {
  124. furi_assert(pcsg_receiver);
  125. with_view_model(
  126. pcsg_receiver->view,
  127. PCSGReceiverModel * model,
  128. {
  129. PCSGReceiverMenuItem* item_menu =
  130. PCSGReceiverMenuItemArray_push_raw(model->history->data);
  131. item_menu->item_str = furi_string_alloc_set(name);
  132. item_menu->type = type;
  133. if((model->idx == model->history_item - 1)) {
  134. model->history_item++;
  135. model->idx++;
  136. } else {
  137. model->history_item++;
  138. }
  139. },
  140. true);
  141. pcsg_view_receiver_update_offset(pcsg_receiver);
  142. }
  143. void pcsg_view_receiver_add_data_statusbar(
  144. PCSGReceiver* pcsg_receiver,
  145. const char* frequency_str,
  146. const char* preset_str,
  147. const char* history_stat_str) {
  148. furi_assert(pcsg_receiver);
  149. with_view_model(
  150. pcsg_receiver->view,
  151. PCSGReceiverModel * model,
  152. {
  153. furi_string_set_str(model->frequency_str, frequency_str);
  154. furi_string_set_str(model->preset_str, preset_str);
  155. furi_string_set_str(model->history_stat_str, history_stat_str);
  156. },
  157. true);
  158. }
  159. static void pcsg_view_receiver_draw_frame(Canvas* canvas, uint16_t idx, bool scrollbar) {
  160. canvas_set_color(canvas, ColorBlack);
  161. canvas_draw_box(canvas, 0, 0 + idx * FRAME_HEIGHT, scrollbar ? 122 : 127, FRAME_HEIGHT);
  162. canvas_set_color(canvas, ColorWhite);
  163. canvas_draw_dot(canvas, 0, 0 + idx * FRAME_HEIGHT);
  164. canvas_draw_dot(canvas, 1, 0 + idx * FRAME_HEIGHT);
  165. canvas_draw_dot(canvas, 0, (0 + idx * FRAME_HEIGHT) + 1);
  166. canvas_draw_dot(canvas, 0, (0 + idx * FRAME_HEIGHT) + 11);
  167. canvas_draw_dot(canvas, scrollbar ? 121 : 126, 0 + idx * FRAME_HEIGHT);
  168. canvas_draw_dot(canvas, scrollbar ? 121 : 126, (0 + idx * FRAME_HEIGHT) + 11);
  169. }
  170. static void pcsg_view_rssi_draw(Canvas* canvas, PCSGReceiverModel* model) {
  171. for(uint8_t i = 1; i < model->u_rssi; i++) {
  172. if(i % 5) {
  173. canvas_draw_dot(canvas, 46 + i, 50);
  174. canvas_draw_dot(canvas, 47 + i, 51);
  175. canvas_draw_dot(canvas, 46 + i, 52);
  176. }
  177. }
  178. }
  179. void pcsg_view_receiver_draw(Canvas* canvas, PCSGReceiverModel* model) {
  180. canvas_clear(canvas);
  181. canvas_set_color(canvas, ColorBlack);
  182. canvas_set_font(canvas, FontSecondary);
  183. elements_button_left(canvas, "Config");
  184. //canvas_draw_line(canvas, 46, 51, 125, 51);
  185. bool scrollbar = model->history_item > 4;
  186. FuriString* str_buff;
  187. str_buff = furi_string_alloc();
  188. PCSGReceiverMenuItem* item_menu;
  189. for(size_t i = 0; i < MIN(model->history_item, MENU_ITEMS); ++i) {
  190. size_t idx = CLAMP((uint16_t)(i + model->list_offset), model->history_item, 0);
  191. item_menu = PCSGReceiverMenuItemArray_get(model->history->data, idx);
  192. furi_string_set(str_buff, item_menu->item_str);
  193. furi_string_replace_all(str_buff, "#", "");
  194. elements_string_fit_width(canvas, str_buff, scrollbar ? MAX_LEN_PX - 7 : MAX_LEN_PX);
  195. if(model->idx == idx) {
  196. pcsg_view_receiver_draw_frame(canvas, i, scrollbar);
  197. } else {
  198. canvas_set_color(canvas, ColorBlack);
  199. }
  200. canvas_draw_icon(canvas, 4, 2 + i * FRAME_HEIGHT, ReceiverItemIcons[item_menu->type]);
  201. canvas_draw_str(canvas, 15, 9 + i * FRAME_HEIGHT, furi_string_get_cstr(str_buff));
  202. furi_string_reset(str_buff);
  203. }
  204. if(scrollbar) {
  205. elements_scrollbar_pos(canvas, 128, 0, 49, model->idx, model->history_item);
  206. }
  207. furi_string_free(str_buff);
  208. canvas_set_color(canvas, ColorBlack);
  209. if(model->history_item == 0) {
  210. canvas_draw_icon(canvas, 0, 0, model->ext_module ? &I_Fishing_123x52 : &I_Scanning_123x52);
  211. canvas_set_font(canvas, FontPrimary);
  212. canvas_draw_str(canvas, 63, 46, "Scanning...");
  213. canvas_set_font(canvas, FontSecondary);
  214. canvas_draw_str(canvas, 44, 10, model->ext_module ? "Ext" : "Int");
  215. }
  216. // Draw RSSI
  217. pcsg_view_rssi_draw(canvas, model);
  218. switch(model->bar_show) {
  219. case PCSGReceiverBarShowLock:
  220. canvas_draw_icon(canvas, 64, 55, &I_Lock_7x8);
  221. canvas_draw_str(canvas, 74, 62, "Locked");
  222. break;
  223. case PCSGReceiverBarShowToUnlockPress:
  224. canvas_draw_str(canvas, 44, 62, furi_string_get_cstr(model->frequency_str));
  225. canvas_draw_str(canvas, 79, 62, furi_string_get_cstr(model->preset_str));
  226. canvas_draw_str(canvas, 96, 62, furi_string_get_cstr(model->history_stat_str));
  227. canvas_set_font(canvas, FontSecondary);
  228. elements_bold_rounded_frame(canvas, 14, 8, 99, 48);
  229. elements_multiline_text(canvas, 65, 26, "To unlock\npress:");
  230. canvas_draw_icon(canvas, 65, 42, &I_Pin_back_arrow_10x8);
  231. canvas_draw_icon(canvas, 80, 42, &I_Pin_back_arrow_10x8);
  232. canvas_draw_icon(canvas, 95, 42, &I_Pin_back_arrow_10x8);
  233. canvas_draw_icon(canvas, 16, 13, &I_WarningDolphin_45x42);
  234. canvas_draw_dot(canvas, 17, 61);
  235. break;
  236. case PCSGReceiverBarShowUnlock:
  237. canvas_draw_icon(canvas, 64, 55, &I_Unlock_7x8);
  238. canvas_draw_str(canvas, 74, 62, "Unlocked");
  239. break;
  240. default:
  241. canvas_draw_str(canvas, 44, 62, furi_string_get_cstr(model->frequency_str));
  242. canvas_draw_str(canvas, 79, 62, furi_string_get_cstr(model->preset_str));
  243. canvas_draw_str(canvas, 96, 62, furi_string_get_cstr(model->history_stat_str));
  244. break;
  245. }
  246. }
  247. static void pcsg_view_receiver_timer_callback(void* context) {
  248. furi_assert(context);
  249. PCSGReceiver* pcsg_receiver = context;
  250. with_view_model(
  251. pcsg_receiver->view,
  252. PCSGReceiverModel * model,
  253. { model->bar_show = PCSGReceiverBarShowDefault; },
  254. true);
  255. if(pcsg_receiver->lock_count < UNLOCK_CNT) {
  256. pcsg_receiver->callback(PCSGCustomEventViewReceiverOffDisplay, pcsg_receiver->context);
  257. } else {
  258. pcsg_receiver->lock = PCSGLockOff;
  259. pcsg_receiver->callback(PCSGCustomEventViewReceiverUnlock, pcsg_receiver->context);
  260. }
  261. pcsg_receiver->lock_count = 0;
  262. }
  263. bool pcsg_view_receiver_input(InputEvent* event, void* context) {
  264. furi_assert(context);
  265. PCSGReceiver* pcsg_receiver = context;
  266. if(pcsg_receiver->lock == PCSGLockOn) {
  267. with_view_model(
  268. pcsg_receiver->view,
  269. PCSGReceiverModel * model,
  270. { model->bar_show = PCSGReceiverBarShowToUnlockPress; },
  271. true);
  272. if(pcsg_receiver->lock_count == 0) {
  273. furi_timer_start(pcsg_receiver->timer, 1000);
  274. }
  275. if(event->key == InputKeyBack && event->type == InputTypeShort) {
  276. pcsg_receiver->lock_count++;
  277. }
  278. if(pcsg_receiver->lock_count >= UNLOCK_CNT) {
  279. pcsg_receiver->callback(PCSGCustomEventViewReceiverUnlock, pcsg_receiver->context);
  280. with_view_model(
  281. pcsg_receiver->view,
  282. PCSGReceiverModel * model,
  283. { model->bar_show = PCSGReceiverBarShowUnlock; },
  284. true);
  285. pcsg_receiver->lock = PCSGLockOff;
  286. furi_timer_start(pcsg_receiver->timer, 650);
  287. }
  288. return true;
  289. }
  290. if(event->key == InputKeyBack && event->type == InputTypeShort) {
  291. pcsg_receiver->callback(PCSGCustomEventViewReceiverBack, pcsg_receiver->context);
  292. } else if(
  293. event->key == InputKeyUp &&
  294. (event->type == InputTypeShort || event->type == InputTypeRepeat)) {
  295. with_view_model(
  296. pcsg_receiver->view,
  297. PCSGReceiverModel * model,
  298. {
  299. if(model->idx != 0) model->idx--;
  300. },
  301. true);
  302. } else if(
  303. event->key == InputKeyDown &&
  304. (event->type == InputTypeShort || event->type == InputTypeRepeat)) {
  305. with_view_model(
  306. pcsg_receiver->view,
  307. PCSGReceiverModel * model,
  308. {
  309. if(model->history_item && model->idx != model->history_item - 1) model->idx++;
  310. },
  311. true);
  312. } else if(event->key == InputKeyLeft && event->type == InputTypeShort) {
  313. pcsg_receiver->callback(PCSGCustomEventViewReceiverConfig, pcsg_receiver->context);
  314. } else if(event->key == InputKeyOk && event->type == InputTypeShort) {
  315. with_view_model(
  316. pcsg_receiver->view,
  317. PCSGReceiverModel * model,
  318. {
  319. if(model->history_item != 0) {
  320. pcsg_receiver->callback(PCSGCustomEventViewReceiverOK, pcsg_receiver->context);
  321. }
  322. },
  323. false);
  324. }
  325. pcsg_view_receiver_update_offset(pcsg_receiver);
  326. return true;
  327. }
  328. void pcsg_view_receiver_enter(void* context) {
  329. furi_assert(context);
  330. }
  331. void pcsg_view_receiver_exit(void* context) {
  332. furi_assert(context);
  333. PCSGReceiver* pcsg_receiver = context;
  334. with_view_model(
  335. pcsg_receiver->view,
  336. PCSGReceiverModel * model,
  337. {
  338. furi_string_reset(model->frequency_str);
  339. furi_string_reset(model->preset_str);
  340. furi_string_reset(model->history_stat_str);
  341. for
  342. M_EACH(item_menu, model->history->data, PCSGReceiverMenuItemArray_t) {
  343. furi_string_free(item_menu->item_str);
  344. item_menu->type = 0;
  345. }
  346. PCSGReceiverMenuItemArray_reset(model->history->data);
  347. model->idx = 0;
  348. model->list_offset = 0;
  349. model->history_item = 0;
  350. },
  351. false);
  352. furi_timer_stop(pcsg_receiver->timer);
  353. }
  354. PCSGReceiver* pcsg_view_receiver_alloc() {
  355. PCSGReceiver* pcsg_receiver = malloc(sizeof(PCSGReceiver));
  356. // View allocation and configuration
  357. pcsg_receiver->view = view_alloc();
  358. pcsg_receiver->lock = PCSGLockOff;
  359. pcsg_receiver->lock_count = 0;
  360. view_allocate_model(pcsg_receiver->view, ViewModelTypeLocking, sizeof(PCSGReceiverModel));
  361. view_set_context(pcsg_receiver->view, pcsg_receiver);
  362. view_set_draw_callback(pcsg_receiver->view, (ViewDrawCallback)pcsg_view_receiver_draw);
  363. view_set_input_callback(pcsg_receiver->view, pcsg_view_receiver_input);
  364. view_set_enter_callback(pcsg_receiver->view, pcsg_view_receiver_enter);
  365. view_set_exit_callback(pcsg_receiver->view, pcsg_view_receiver_exit);
  366. with_view_model(
  367. pcsg_receiver->view,
  368. PCSGReceiverModel * model,
  369. {
  370. model->frequency_str = furi_string_alloc();
  371. model->preset_str = furi_string_alloc();
  372. model->history_stat_str = furi_string_alloc();
  373. model->bar_show = PCSGReceiverBarShowDefault;
  374. model->history = malloc(sizeof(PCSGReceiverHistory));
  375. PCSGReceiverMenuItemArray_init(model->history->data);
  376. },
  377. true);
  378. pcsg_receiver->timer =
  379. furi_timer_alloc(pcsg_view_receiver_timer_callback, FuriTimerTypeOnce, pcsg_receiver);
  380. return pcsg_receiver;
  381. }
  382. void pcsg_view_receiver_free(PCSGReceiver* pcsg_receiver) {
  383. furi_assert(pcsg_receiver);
  384. with_view_model(
  385. pcsg_receiver->view,
  386. PCSGReceiverModel * model,
  387. {
  388. furi_string_free(model->frequency_str);
  389. furi_string_free(model->preset_str);
  390. furi_string_free(model->history_stat_str);
  391. for
  392. M_EACH(item_menu, model->history->data, PCSGReceiverMenuItemArray_t) {
  393. furi_string_free(item_menu->item_str);
  394. item_menu->type = 0;
  395. }
  396. PCSGReceiverMenuItemArray_clear(model->history->data);
  397. free(model->history);
  398. },
  399. false);
  400. furi_timer_free(pcsg_receiver->timer);
  401. view_free(pcsg_receiver->view);
  402. free(pcsg_receiver);
  403. }
  404. View* pcsg_view_receiver_get_view(PCSGReceiver* pcsg_receiver) {
  405. furi_assert(pcsg_receiver);
  406. return pcsg_receiver->view;
  407. }
  408. uint16_t pcsg_view_receiver_get_idx_menu(PCSGReceiver* pcsg_receiver) {
  409. furi_assert(pcsg_receiver);
  410. uint32_t idx = 0;
  411. with_view_model(
  412. pcsg_receiver->view, PCSGReceiverModel * model, { idx = model->idx; }, false);
  413. return idx;
  414. }
  415. void pcsg_view_receiver_set_idx_menu(PCSGReceiver* pcsg_receiver, uint16_t idx) {
  416. furi_assert(pcsg_receiver);
  417. with_view_model(
  418. pcsg_receiver->view,
  419. PCSGReceiverModel * model,
  420. {
  421. model->idx = idx;
  422. if(model->idx > 2) model->list_offset = idx - 2;
  423. },
  424. true);
  425. pcsg_view_receiver_update_offset(pcsg_receiver);
  426. }