view_info.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_i.h>
  5. #include <lib/toolbox/random_name.h>
  6. enum {
  7. SubViewInfoMain,
  8. SubViewInfoSave,
  9. SubViewInfoLast, /* Just a sentinel. */
  10. };
  11. /* Our view private data. */
  12. #define SAVE_FILENAME_LEN 64
  13. typedef struct {
  14. /* Our save view displays an oscilloscope-alike resampled signal,
  15. * so that the user can see what they are saving. With left/right
  16. * you can move to next rows. Here we store where we are. */
  17. uint32_t signal_display_start_row;
  18. char *filename;
  19. } InfoViewPrivData;
  20. /* Render the view with the detected message information. */
  21. static void render_subview_main(Canvas *const canvas, ProtoViewApp *app) {
  22. /* Protocol name as title. */
  23. canvas_set_font(canvas, FontPrimary);
  24. uint8_t y = 8, lineheight = 10;
  25. canvas_draw_str(canvas, 0, y, app->msg_info->name);
  26. y += lineheight;
  27. /* Info fields. */
  28. char buf[128];
  29. canvas_set_font(canvas, FontSecondary);
  30. if (app->msg_info->raw[0]) {
  31. snprintf(buf,sizeof(buf),"Raw: %s", app->msg_info->raw);
  32. canvas_draw_str(canvas, 0, y, buf);
  33. y += lineheight;
  34. }
  35. canvas_draw_str(canvas, 0, y, app->msg_info->info1); y += lineheight;
  36. canvas_draw_str(canvas, 0, y, app->msg_info->info2); y += lineheight;
  37. canvas_draw_str(canvas, 0, y, app->msg_info->info3); y += lineheight;
  38. canvas_draw_str(canvas, 0, y, app->msg_info->info4); y += lineheight;
  39. }
  40. /* Render view with save option. */
  41. static void render_subview_save(Canvas *const canvas, ProtoViewApp *app) {
  42. InfoViewPrivData *privdata = app->view_privdata;
  43. /* Display our signal in digital form: here we don't show the
  44. * signal with the exact timing of the received samples, but as it
  45. * is in its logic form, in exact multiples of the short pulse length. */
  46. uint8_t rows = 6;
  47. uint8_t rowheight = 11;
  48. uint8_t bitwidth = 4;
  49. uint8_t bitheight = 5;
  50. uint32_t idx = privdata->signal_display_start_row * (128/4);
  51. bool prevbit = false;
  52. for (uint8_t y = bitheight+12; y <= rows*rowheight; y += rowheight) {
  53. for (uint8_t x = 0; x < 128; x += 4) {
  54. bool bit = bitmap_get(app->msg_info->bits,
  55. app->msg_info->bits_bytes,idx);
  56. uint8_t prevy = y + prevbit*(bitheight*-1) - 1;
  57. uint8_t thisy = y + bit*(bitheight*-1) - 1;
  58. canvas_draw_line(canvas,x,prevy,x,thisy);
  59. canvas_draw_line(canvas,x,thisy,x+bitwidth-1,thisy);
  60. prevbit = bit;
  61. if (idx >= app->msg_info->pulses_count) {
  62. canvas_set_color(canvas, ColorWhite);
  63. canvas_draw_dot(canvas, x+1,thisy);
  64. canvas_draw_dot(canvas, x+3,thisy);
  65. canvas_set_color(canvas, ColorBlack);
  66. }
  67. idx++; // Draw next bit
  68. }
  69. }
  70. canvas_set_font(canvas, FontSecondary);
  71. canvas_draw_str(canvas, 0, 6, "ok: save, < >: slide rows");
  72. }
  73. /* Render the selected subview of this view. */
  74. void render_view_info(Canvas *const canvas, ProtoViewApp *app) {
  75. if (app->signal_decoded == false) {
  76. canvas_set_font(canvas, FontSecondary);
  77. canvas_draw_str(canvas, 30,36,"No signal decoded");
  78. return;
  79. }
  80. show_available_subviews(canvas,app,SubViewInfoLast);
  81. switch(app->current_subview[app->current_view]) {
  82. case SubViewInfoMain: render_subview_main(canvas,app); break;
  83. case SubViewInfoSave: render_subview_save(canvas,app); break;
  84. }
  85. }
  86. /* The user typed the file name. Let's save it and remove the keyboard
  87. * view. */
  88. void text_input_done_callback(void* context) {
  89. ProtoViewApp *app = context;
  90. InfoViewPrivData *privdata = app->view_privdata;
  91. FuriString *save_path = furi_string_alloc_printf(
  92. "%s/%s.sub", EXT_PATH("subghz"), privdata->filename);
  93. save_signal(app, furi_string_get_cstr(save_path));
  94. furi_string_free(save_path);
  95. free(privdata->filename);
  96. dismiss_keyboard(app);
  97. }
  98. /* Replace all the occurrences of character c1 with c2 in the specified
  99. * string. */
  100. void str_replace(char *buf, char c1, char c2) {
  101. char *p = buf;
  102. while(*p) {
  103. if (*p == c1) *p = c2;
  104. p++;
  105. }
  106. }
  107. /* Set a random filename the user can edit. */
  108. void set_signal_random_filename(ProtoViewApp *app, char *buf, size_t buflen) {
  109. char suffix[6];
  110. set_random_name(suffix,sizeof(suffix));
  111. snprintf(buf,buflen,"%.10s-%s-%d",app->msg_info->name,suffix,rand()%1000);
  112. str_replace(buf,' ','_');
  113. str_replace(buf,'-','_');
  114. str_replace(buf,'/','_');
  115. }
  116. /* Handle input for the info view. */
  117. void process_input_info(ProtoViewApp *app, InputEvent input) {
  118. if (process_subview_updown(app,input,SubViewInfoLast)) return;
  119. InfoViewPrivData *privdata = app->view_privdata;
  120. int subview = get_current_subview(app);
  121. /* Main subview. */
  122. if (subview == SubViewInfoMain) {
  123. if (input.type == InputTypeShort && input.key == InputKeyOk) {
  124. /* Reset the current sample to capture the next. */
  125. reset_current_signal(app);
  126. }
  127. } else if (subview == SubViewInfoSave) {
  128. /* Save subview. */
  129. if (input.type == InputTypePress && input.key == InputKeyRight) {
  130. privdata->signal_display_start_row++;
  131. } else if (input.type == InputTypePress && input.key == InputKeyLeft) {
  132. if (privdata->signal_display_start_row != 0)
  133. privdata->signal_display_start_row--;
  134. } else if (input.type == InputTypePress && input.key == InputKeyOk) {
  135. privdata->filename = malloc(SAVE_FILENAME_LEN);
  136. set_signal_random_filename(app,privdata->filename,SAVE_FILENAME_LEN);
  137. show_keyboard(app, privdata->filename, SAVE_FILENAME_LEN,
  138. text_input_done_callback);
  139. }
  140. }
  141. }