view_info.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. #include <gui/view.h>
  5. #include <lib/toolbox/random_name.h>
  6. /* This view has subviews accessible navigating up/down. This
  7. * enumaration is used to track the currently active subview. */
  8. enum {
  9. SubViewInfoMain,
  10. SubViewInfoSave,
  11. SubViewInfoLast, /* Just a sentinel. */
  12. };
  13. /* Our view private data. */
  14. #define SAVE_FILENAME_LEN 64
  15. typedef struct {
  16. /* Our save view displays an oscilloscope-alike resampled signal,
  17. * so that the user can see what they are saving. With left/right
  18. * you can move to next rows. Here we store where we are. */
  19. uint32_t signal_display_start_row;
  20. char *filename;
  21. uint8_t cur_info_page; // Info page to display. Useful when there are
  22. // too many fields populated by the decoder that
  23. // a single page is not enough.
  24. } InfoViewPrivData;
  25. /* Draw the text label and value of the specified info field at x,y. */
  26. static void render_info_field(Canvas *const canvas,
  27. ProtoViewField *f, uint8_t x, uint8_t y)
  28. {
  29. char buf[64];
  30. char strval[32];
  31. field_to_string(strval,sizeof(strval),f);
  32. snprintf(buf,sizeof(buf),"%s: %s", f->name, strval);
  33. canvas_set_font(canvas, FontSecondary);
  34. canvas_draw_str(canvas, x, y, buf);
  35. }
  36. /* Render the view with the detected message information. */
  37. #define INFO_LINES_PER_PAGE 5
  38. static void render_subview_main(Canvas *const canvas, ProtoViewApp *app) {
  39. InfoViewPrivData *privdata = app->view_privdata;
  40. uint8_t pages = (app->msg_info->fieldset->numfields
  41. +(INFO_LINES_PER_PAGE-1)) / INFO_LINES_PER_PAGE;
  42. privdata->cur_info_page %= pages;
  43. uint8_t current_page = privdata->cur_info_page;
  44. char buf[32];
  45. /* Protocol name as title. */
  46. canvas_set_font(canvas, FontPrimary);
  47. uint8_t y = 8, lineheight = 10;
  48. if (pages > 1) {
  49. snprintf(buf,sizeof(buf),"%s %u/%u", app->msg_info->decoder->name,
  50. current_page+1, pages);
  51. canvas_draw_str(canvas, 0, y, buf);
  52. } else {
  53. canvas_draw_str(canvas, 0, y, app->msg_info->decoder->name);
  54. }
  55. y += lineheight;
  56. /* Draw the info fields. */
  57. uint8_t max_lines = INFO_LINES_PER_PAGE;
  58. uint32_t j = current_page*max_lines;
  59. while (j < app->msg_info->fieldset->numfields) {
  60. render_info_field(canvas,app->msg_info->fieldset->fields[j++],0,y);
  61. y += lineheight;
  62. if (--max_lines == 0) break;
  63. }
  64. /* Draw a vertical "save" label. Temporary solution, to switch to
  65. * something better ASAP. */
  66. y = 37;
  67. lineheight = 7;
  68. canvas_draw_str(canvas, 119, y, "s"); y += lineheight;
  69. canvas_draw_str(canvas, 119, y, "a"); y += lineheight;
  70. canvas_draw_str(canvas, 119, y, "v"); y += lineheight;
  71. canvas_draw_str(canvas, 119, y, "e"); y += lineheight;
  72. }
  73. /* Render view with save option. */
  74. static void render_subview_save(Canvas *const canvas, ProtoViewApp *app) {
  75. InfoViewPrivData *privdata = app->view_privdata;
  76. /* Display our signal in digital form: here we don't show the
  77. * signal with the exact timing of the received samples, but as it
  78. * is in its logic form, in exact multiples of the short pulse length. */
  79. uint8_t rows = 6;
  80. uint8_t rowheight = 11;
  81. uint8_t bitwidth = 4;
  82. uint8_t bitheight = 5;
  83. uint32_t idx = privdata->signal_display_start_row * (128/4);
  84. bool prevbit = false;
  85. for (uint8_t y = bitheight+12; y <= rows*rowheight; y += rowheight) {
  86. for (uint8_t x = 0; x < 128; x += 4) {
  87. bool bit = bitmap_get(app->msg_info->bits,
  88. app->msg_info->bits_bytes,idx);
  89. uint8_t prevy = y + prevbit*(bitheight*-1) - 1;
  90. uint8_t thisy = y + bit*(bitheight*-1) - 1;
  91. canvas_draw_line(canvas,x,prevy,x,thisy);
  92. canvas_draw_line(canvas,x,thisy,x+bitwidth-1,thisy);
  93. prevbit = bit;
  94. if (idx >= app->msg_info->pulses_count) {
  95. canvas_set_color(canvas, ColorWhite);
  96. canvas_draw_dot(canvas, x+1,thisy);
  97. canvas_draw_dot(canvas, x+3,thisy);
  98. canvas_set_color(canvas, ColorBlack);
  99. }
  100. idx++; // Draw next bit
  101. }
  102. }
  103. canvas_set_font(canvas, FontSecondary);
  104. canvas_draw_str(canvas, 0, 6, "ok: send, long ok: save");
  105. }
  106. /* Render the selected subview of this view. */
  107. void render_view_info(Canvas *const canvas, ProtoViewApp *app) {
  108. if (app->signal_decoded == false) {
  109. canvas_set_font(canvas, FontSecondary);
  110. canvas_draw_str(canvas, 30,36,"No signal decoded");
  111. return;
  112. }
  113. ui_show_available_subviews(canvas,app,SubViewInfoLast);
  114. switch(app->current_subview[app->current_view]) {
  115. case SubViewInfoMain: render_subview_main(canvas,app); break;
  116. case SubViewInfoSave: render_subview_save(canvas,app); break;
  117. }
  118. }
  119. /* The user typed the file name. Let's save it and remove the keyboard
  120. * view. */
  121. static void text_input_done_callback(void* context) {
  122. ProtoViewApp *app = context;
  123. InfoViewPrivData *privdata = app->view_privdata;
  124. FuriString *save_path = furi_string_alloc_printf(
  125. "%s/%s.sub", EXT_PATH("subghz"), privdata->filename);
  126. save_signal(app, furi_string_get_cstr(save_path));
  127. furi_string_free(save_path);
  128. free(privdata->filename);
  129. privdata->filename = NULL; // Don't free it again on view exit
  130. ui_dismiss_keyboard(app);
  131. ui_show_alert(app, "Signal saved", 1500);
  132. }
  133. /* Replace all the occurrences of character c1 with c2 in the specified
  134. * string. */
  135. void str_replace(char *buf, char c1, char c2) {
  136. char *p = buf;
  137. while(*p) {
  138. if (*p == c1) *p = c2;
  139. p++;
  140. }
  141. }
  142. /* Set a random filename the user can edit. */
  143. void set_signal_random_filename(ProtoViewApp *app, char *buf, size_t buflen) {
  144. char suffix[6];
  145. set_random_name(suffix,sizeof(suffix));
  146. snprintf(buf,buflen,"%.10s-%s-%d",app->msg_info->decoder->name,suffix,rand()%1000);
  147. str_replace(buf,' ','_');
  148. str_replace(buf,'-','_');
  149. str_replace(buf,'/','_');
  150. }
  151. /* ========================== Signal transmission =========================== */
  152. /* This is the context we pass to the data yield callback for
  153. * asynchronous tx. */
  154. typedef enum {
  155. SendSignalSendStartGap,
  156. SendSignalSendBits,
  157. SendSignalSendEndGap,
  158. SendSignalEndTransmission
  159. } SendSignalState;
  160. #define PROTOVIEW_SENDSIGNAL_START_GAP 10000 /* microseconds. */
  161. #define PROTOVIEW_SENDSIGNAL_END_GAP 10000 /* microseconds. */
  162. typedef struct {
  163. SendSignalState state; // Current state.
  164. uint32_t curpos; // Current bit position of data to send.
  165. ProtoViewApp *app; // App reference.
  166. uint32_t start_gap_dur; // Gap to send at the start.
  167. uint32_t end_gap_dur; // Gap to send at the end.
  168. } SendSignalCtx;
  169. /* Setup the state context for the callback responsible to feed data
  170. * to the subghz async tx system. */
  171. static void send_signal_init(SendSignalCtx *ss, ProtoViewApp *app) {
  172. ss->state = SendSignalSendStartGap;
  173. ss->curpos = 0;
  174. ss->app = app;
  175. ss->start_gap_dur = PROTOVIEW_SENDSIGNAL_START_GAP;
  176. ss->end_gap_dur = PROTOVIEW_SENDSIGNAL_END_GAP;
  177. }
  178. /* Send signal data feeder callback. When the asynchronous transmission is
  179. * active, this function is called to return new samples from the currently
  180. * decoded signal in app->msg_info. The subghz subsystem aspects this function,
  181. * that is the data feeder, to return LevelDuration types (that is a structure
  182. * with level, that is pulse or gap, and duration in microseconds).
  183. *
  184. * The position into the transmission is stored in the context 'ctx', that
  185. * references a SendSignalCtx structure.
  186. *
  187. * In the SendSignalCtx structure 'ss' we remember at which bit of the
  188. * message we are, in ss->curoff. We also send a start and end gap in order
  189. * to make sure the transmission is clear.
  190. */
  191. LevelDuration radio_tx_feed_data(void *ctx) {
  192. SendSignalCtx *ss = ctx;
  193. /* Send start gap. */
  194. if (ss->state == SendSignalSendStartGap) {
  195. ss->state = SendSignalSendBits;
  196. return level_duration_make(0,ss->start_gap_dur);
  197. }
  198. /* Send data. */
  199. if (ss->state == SendSignalSendBits) {
  200. uint32_t dur = 0, j;
  201. uint32_t level = 0;
  202. /* Let's see how many consecutive bits we have with the same
  203. * level. */
  204. for (j = 0; ss->curpos+j < ss->app->msg_info->pulses_count; j++) {
  205. uint32_t l = bitmap_get(ss->app->msg_info->bits,
  206. ss->app->msg_info->bits_bytes,
  207. ss->curpos+j);
  208. if (j == 0) {
  209. /* At the first bit of this sequence, we store the
  210. * level of the sequence. */
  211. level = l;
  212. dur += ss->app->msg_info->short_pulse_dur;
  213. continue;
  214. }
  215. /* As long as the level is the same, we update the duration.
  216. * Otherwise stop the loop and return this sample. */
  217. if (l != level) break;
  218. dur += ss->app->msg_info->short_pulse_dur;
  219. }
  220. ss->curpos += j;
  221. /* If this was the last set of bits, change the state to
  222. * send the final gap. */
  223. if (ss->curpos >= ss->app->msg_info->pulses_count)
  224. ss->state = SendSignalSendEndGap;
  225. return level_duration_make(level, dur);
  226. }
  227. /* Send end gap. */
  228. if (ss->state == SendSignalSendEndGap) {
  229. ss->state = SendSignalEndTransmission;
  230. return level_duration_make(0,ss->end_gap_dur);
  231. }
  232. /* End transmission. Here state is guaranteed
  233. * to be SendSignalEndTransmission */
  234. return level_duration_reset();
  235. }
  236. /* Vibrate and produce a click sound when a signal is sent. */
  237. void notify_signal_sent(ProtoViewApp *app) {
  238. static const NotificationSequence sent_seq = {
  239. &message_blue_255,
  240. &message_vibro_on,
  241. &message_note_g1,
  242. &message_delay_10,
  243. &message_sound_off,
  244. &message_vibro_off,
  245. &message_blue_0,
  246. NULL
  247. };
  248. notification_message(app->notification, &sent_seq);
  249. }
  250. /* Handle input for the info view. */
  251. void process_input_info(ProtoViewApp *app, InputEvent input) {
  252. /* If we don't have a decoded signal, we don't allow to go up/down
  253. * in the subviews: they are only useful when a loaded signal. */
  254. if (app->signal_decoded &&
  255. ui_process_subview_updown(app,input,SubViewInfoLast)) return;
  256. InfoViewPrivData *privdata = app->view_privdata;
  257. int subview = ui_get_current_subview(app);
  258. /* Main subview. */
  259. if (subview == SubViewInfoMain) {
  260. if (input.type == InputTypeLong && input.key == InputKeyOk) {
  261. /* Reset the current sample to capture the next. */
  262. reset_current_signal(app);
  263. } else if (input.type == InputTypeShort && input.key == InputKeyOk) {
  264. /* Show next info page. */
  265. privdata->cur_info_page++;
  266. }
  267. } else if (subview == SubViewInfoSave) {
  268. /* Save subview. */
  269. if (input.type == InputTypePress && input.key == InputKeyRight) {
  270. privdata->signal_display_start_row++;
  271. } else if (input.type == InputTypePress && input.key == InputKeyLeft) {
  272. if (privdata->signal_display_start_row != 0)
  273. privdata->signal_display_start_row--;
  274. } else if (input.type == InputTypeLong && input.key == InputKeyOk)
  275. {
  276. // We have have the buffer already allocated, in case the
  277. // user aborted with BACK a previous saving.
  278. if (privdata->filename == NULL)
  279. privdata->filename = malloc(SAVE_FILENAME_LEN);
  280. set_signal_random_filename(app,privdata->filename,SAVE_FILENAME_LEN);
  281. ui_show_keyboard(app, privdata->filename, SAVE_FILENAME_LEN,
  282. text_input_done_callback);
  283. } else if (input.type == InputTypeShort && input.key == InputKeyOk) {
  284. SendSignalCtx send_state;
  285. send_signal_init(&send_state,app);
  286. radio_tx_signal(app,radio_tx_feed_data,&send_state);
  287. notify_signal_sent(app);
  288. }
  289. }
  290. }
  291. /* Called on view exit. */
  292. void view_exit_info(ProtoViewApp *app) {
  293. InfoViewPrivData *privdata = app->view_privdata;
  294. // When the user aborts the keyboard input, we are left with the
  295. // filename buffer allocated.
  296. if (privdata->filename) free(privdata->filename);
  297. }