view_info.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 <toolbox/name_generator.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. name_generator_make_auto(buf, buflen, app->msg_info->decoder->name);
  145. }
  146. /* ========================== Signal transmission =========================== */
  147. /* This is the context we pass to the data yield callback for
  148. * asynchronous tx. */
  149. typedef enum {
  150. SendSignalSendStartGap,
  151. SendSignalSendBits,
  152. SendSignalSendEndGap,
  153. SendSignalEndTransmission
  154. } SendSignalState;
  155. #define PROTOVIEW_SENDSIGNAL_START_GAP 10000 /* microseconds. */
  156. #define PROTOVIEW_SENDSIGNAL_END_GAP 10000 /* microseconds. */
  157. typedef struct {
  158. SendSignalState state; // Current state.
  159. uint32_t curpos; // Current bit position of data to send.
  160. ProtoViewApp *app; // App reference.
  161. uint32_t start_gap_dur; // Gap to send at the start.
  162. uint32_t end_gap_dur; // Gap to send at the end.
  163. } SendSignalCtx;
  164. /* Setup the state context for the callback responsible to feed data
  165. * to the subghz async tx system. */
  166. static void send_signal_init(SendSignalCtx *ss, ProtoViewApp *app) {
  167. ss->state = SendSignalSendStartGap;
  168. ss->curpos = 0;
  169. ss->app = app;
  170. ss->start_gap_dur = PROTOVIEW_SENDSIGNAL_START_GAP;
  171. ss->end_gap_dur = PROTOVIEW_SENDSIGNAL_END_GAP;
  172. }
  173. /* Send signal data feeder callback. When the asynchronous transmission is
  174. * active, this function is called to return new samples from the currently
  175. * decoded signal in app->msg_info. The subghz subsystem aspects this function,
  176. * that is the data feeder, to return LevelDuration types (that is a structure
  177. * with level, that is pulse or gap, and duration in microseconds).
  178. *
  179. * The position into the transmission is stored in the context 'ctx', that
  180. * references a SendSignalCtx structure.
  181. *
  182. * In the SendSignalCtx structure 'ss' we remember at which bit of the
  183. * message we are, in ss->curoff. We also send a start and end gap in order
  184. * to make sure the transmission is clear.
  185. */
  186. LevelDuration radio_tx_feed_data(void *ctx) {
  187. SendSignalCtx *ss = ctx;
  188. /* Send start gap. */
  189. if (ss->state == SendSignalSendStartGap) {
  190. ss->state = SendSignalSendBits;
  191. return level_duration_make(0,ss->start_gap_dur);
  192. }
  193. /* Send data. */
  194. if (ss->state == SendSignalSendBits) {
  195. uint32_t dur = 0, j;
  196. uint32_t level = 0;
  197. /* Let's see how many consecutive bits we have with the same
  198. * level. */
  199. for (j = 0; ss->curpos+j < ss->app->msg_info->pulses_count; j++) {
  200. uint32_t l = bitmap_get(ss->app->msg_info->bits,
  201. ss->app->msg_info->bits_bytes,
  202. ss->curpos+j);
  203. if (j == 0) {
  204. /* At the first bit of this sequence, we store the
  205. * level of the sequence. */
  206. level = l;
  207. dur += ss->app->msg_info->short_pulse_dur;
  208. continue;
  209. }
  210. /* As long as the level is the same, we update the duration.
  211. * Otherwise stop the loop and return this sample. */
  212. if (l != level) break;
  213. dur += ss->app->msg_info->short_pulse_dur;
  214. }
  215. ss->curpos += j;
  216. /* If this was the last set of bits, change the state to
  217. * send the final gap. */
  218. if (ss->curpos >= ss->app->msg_info->pulses_count)
  219. ss->state = SendSignalSendEndGap;
  220. return level_duration_make(level, dur);
  221. }
  222. /* Send end gap. */
  223. if (ss->state == SendSignalSendEndGap) {
  224. ss->state = SendSignalEndTransmission;
  225. return level_duration_make(0,ss->end_gap_dur);
  226. }
  227. /* End transmission. Here state is guaranteed
  228. * to be SendSignalEndTransmission */
  229. return level_duration_reset();
  230. }
  231. /* Vibrate and produce a click sound when a signal is sent. */
  232. void notify_signal_sent(ProtoViewApp *app) {
  233. static const NotificationSequence sent_seq = {
  234. &message_blue_255,
  235. &message_vibro_on,
  236. &message_note_g1,
  237. &message_delay_10,
  238. &message_sound_off,
  239. &message_vibro_off,
  240. &message_blue_0,
  241. NULL
  242. };
  243. notification_message(app->notification, &sent_seq);
  244. }
  245. /* Handle input for the info view. */
  246. void process_input_info(ProtoViewApp *app, InputEvent input) {
  247. /* If we don't have a decoded signal, we don't allow to go up/down
  248. * in the subviews: they are only useful when a loaded signal. */
  249. if (app->signal_decoded &&
  250. ui_process_subview_updown(app,input,SubViewInfoLast)) return;
  251. InfoViewPrivData *privdata = app->view_privdata;
  252. int subview = ui_get_current_subview(app);
  253. /* Main subview. */
  254. if (subview == SubViewInfoMain) {
  255. if (input.type == InputTypeLong && input.key == InputKeyOk) {
  256. /* Reset the current sample to capture the next. */
  257. reset_current_signal(app);
  258. } else if (input.type == InputTypeShort && input.key == InputKeyOk) {
  259. /* Show next info page. */
  260. privdata->cur_info_page++;
  261. }
  262. } else if (subview == SubViewInfoSave) {
  263. /* Save subview. */
  264. if (input.type == InputTypePress && input.key == InputKeyRight) {
  265. privdata->signal_display_start_row++;
  266. } else if (input.type == InputTypePress && input.key == InputKeyLeft) {
  267. if (privdata->signal_display_start_row != 0)
  268. privdata->signal_display_start_row--;
  269. } else if (input.type == InputTypeLong && input.key == InputKeyOk)
  270. {
  271. // We have have the buffer already allocated, in case the
  272. // user aborted with BACK a previous saving.
  273. if (privdata->filename == NULL)
  274. privdata->filename = malloc(SAVE_FILENAME_LEN);
  275. set_signal_random_filename(app,privdata->filename,SAVE_FILENAME_LEN);
  276. ui_show_keyboard(app, privdata->filename, SAVE_FILENAME_LEN,
  277. text_input_done_callback);
  278. } else if (input.type == InputTypeShort && input.key == InputKeyOk) {
  279. SendSignalCtx send_state;
  280. send_signal_init(&send_state,app);
  281. radio_tx_signal(app,radio_tx_feed_data,&send_state);
  282. notify_signal_sent(app);
  283. }
  284. }
  285. }
  286. /* Called on view exit. */
  287. void view_exit_info(ProtoViewApp *app) {
  288. InfoViewPrivData *privdata = app->view_privdata;
  289. // When the user aborts the keyboard input, we are left with the
  290. // filename buffer allocated.
  291. if (privdata->filename) free(privdata->filename);
  292. }