view_info.c 5.0 KB

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