view_raw_signal.c 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. /* Render the received signal.
  5. *
  6. * The screen of the flipper is 128 x 64. Even using 4 pixels per line
  7. * (where low level signal is one pixel high, high level is 4 pixels
  8. * high) and 4 pixels of spacing between the different lines, we can
  9. * plot comfortably 8 lines.
  10. *
  11. * The 'idx' argument is the first sample to render in the circular
  12. * buffer. */
  13. void render_signal(ProtoViewApp *app, Canvas *const canvas, RawSamplesBuffer *buf, uint32_t idx) {
  14. canvas_set_color(canvas, ColorBlack);
  15. int rows = 8;
  16. uint32_t time_per_pixel = app->us_scale;
  17. uint32_t start_idx = idx;
  18. bool level = 0;
  19. uint32_t dur = 0, sample_num = 0;
  20. for (int row = 0; row < rows ; row++) {
  21. for (int x = 0; x < 128; x++) {
  22. int y = 3 + row*8;
  23. if (dur < time_per_pixel/2) {
  24. /* Get more data. */
  25. raw_samples_get(buf, idx++, &level, &dur);
  26. sample_num++;
  27. }
  28. canvas_draw_line(canvas, x,y,x,y-(level*3));
  29. /* Write a small triangle under the last sample detected. */
  30. if (app->signal_bestlen != 0 &&
  31. sample_num+start_idx == app->signal_bestlen+1)
  32. {
  33. canvas_draw_dot(canvas,x,y+2);
  34. canvas_draw_dot(canvas,x-1,y+3);
  35. canvas_draw_dot(canvas,x,y+3);
  36. canvas_draw_dot(canvas,x+1,y+3);
  37. sample_num++; /* Make sure we don't mark the next, too. */
  38. }
  39. /* Remove from the current level duration the time we
  40. * just plot. */
  41. if (dur > time_per_pixel)
  42. dur -= time_per_pixel;
  43. else
  44. dur = 0;
  45. }
  46. }
  47. }
  48. /* Raw pulses rendering. This is our default view. */
  49. void render_view_raw_pulses(Canvas *const canvas, ProtoViewApp *app) {
  50. /* Show signal. */
  51. render_signal(app, canvas, DetectedSamples, app->signal_offset);
  52. /* Show signal information. */
  53. char buf[64];
  54. snprintf(buf,sizeof(buf),"%luus",
  55. (unsigned long)DetectedSamples->short_pulse_dur);
  56. canvas_set_font(canvas, FontSecondary);
  57. canvas_draw_str_with_border(canvas, 97, 63, buf, ColorWhite, ColorBlack);
  58. if (app->signal_decoded) {
  59. canvas_set_font(canvas, FontPrimary);
  60. canvas_draw_str_with_border(canvas, 1, 61, app->signal_info.name, ColorWhite, ColorBlack);
  61. }
  62. }
  63. /* Handle input for the raw pulses view. */
  64. void process_input_raw_pulses(ProtoViewApp *app, InputEvent input) {
  65. if (input.type == InputTypeRepeat) {
  66. /* Handle panning of the signal window. Long pressing
  67. * right will show successive samples, long pressing left
  68. * previous samples. */
  69. if (input.key == InputKeyRight) app->signal_offset++;
  70. else if (input.key == InputKeyLeft) app->signal_offset--;
  71. else if (input.key == InputKeyOk) {
  72. app->signal_offset = 0;
  73. app->us_scale = PROTOVIEW_RAW_VIEW_DEFAULT_SCALE;
  74. }
  75. } else if (input.type == InputTypeShort) {
  76. if (input.key == InputKeyOk) {
  77. /* Reset the current sample to capture the next. */
  78. reset_current_signal(app);
  79. } else if (input.key == InputKeyDown) {
  80. /* Rescaling. The set becomes finer under 50us per pixel. */
  81. uint32_t scale_step = app->us_scale >= 50 ? 50 : 10;
  82. if (app->us_scale < 500) app->us_scale += scale_step;
  83. } else if (input.key == InputKeyUp) {
  84. uint32_t scale_step = app->us_scale > 50 ? 50 : 10;
  85. if (app->us_scale > 10) app->us_scale -= scale_step;
  86. }
  87. }
  88. }