view_direct_sampling.c 4.8 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 <cc1101.h>
  5. #define CAPTURED_BITMAP_SIZE 128*64/8
  6. #define DEFAULT_USEC_PER_PIXEL 50
  7. #define USEC_PER_PIXEL_SMALL_CHANGE 5
  8. #define USEC_PER_PIXEL_LARGE_CHANGE 25
  9. #define USEC_PER_PIXEL_MIN 5
  10. #define USEC_PER_PIXEL_MAX 300
  11. typedef struct {
  12. uint8_t *captured; // Bitmap with the last captured screen.
  13. uint32_t usec_per_pixel; // Number of useconds a pixel should represent
  14. } DirectSamplingViewPrivData;
  15. /* Read directly from the G0 CC1101 pin, and draw a black or white
  16. * dot depending on the level. */
  17. void render_view_direct_sampling(Canvas *const canvas, ProtoViewApp *app) {
  18. DirectSamplingViewPrivData *privdata = app->view_privdata;
  19. if (!app->direct_sampling_enabled && privdata->captured == NULL) {
  20. canvas_set_font(canvas, FontSecondary);
  21. canvas_draw_str(canvas,2,9,"Direct sampling is a special");
  22. canvas_draw_str(canvas,2,18,"mode that displays the signal");
  23. canvas_draw_str(canvas,2,27,"captured in real time. Like in");
  24. canvas_draw_str(canvas,2,36,"a old CRT TV. It's very slow.");
  25. canvas_draw_str(canvas,2,45,"Can crash your Flipper.");
  26. canvas_set_font(canvas, FontPrimary);
  27. canvas_draw_str(canvas,14,60,"To enable press OK");
  28. return;
  29. }
  30. /* Allocate the bitmap only the first time. */
  31. if (privdata->captured == NULL)
  32. privdata->captured = malloc(CAPTURED_BITMAP_SIZE);
  33. /* Read from data from GPIO */
  34. if (app->direct_sampling_enabled) {
  35. for (int j = 0; j < CAPTURED_BITMAP_SIZE*8; j++) {
  36. uint32_t start_time = DWT->CYCCNT;
  37. bool level = furi_hal_gpio_read(&gpio_cc1101_g0);
  38. bitmap_set(privdata->captured,CAPTURED_BITMAP_SIZE,j,level);
  39. uint32_t period =
  40. furi_hal_cortex_instructions_per_microsecond() *
  41. privdata->usec_per_pixel;
  42. while(DWT->CYCCNT - start_time < period);
  43. }
  44. }
  45. /* Draw on screen. */
  46. int idx = 0;
  47. for (int y = 0; y < 64; y++) {
  48. for (int x = 0; x < 128; x++) {
  49. bool level = bitmap_get(privdata->captured,
  50. CAPTURED_BITMAP_SIZE,idx++);
  51. if (level) canvas_draw_dot(canvas,x,y);
  52. }
  53. }
  54. char buf[32];
  55. snprintf(buf,sizeof(buf),"%lu usec/px", privdata->usec_per_pixel);
  56. canvas_set_font(canvas, FontSecondary);
  57. canvas_draw_str_with_border(canvas,0,60,buf,ColorWhite,ColorBlack);
  58. }
  59. /* Handle input */
  60. void process_input_direct_sampling(ProtoViewApp *app, InputEvent input) {
  61. DirectSamplingViewPrivData *privdata = app->view_privdata;
  62. if (input.type == InputTypePress && input.key == InputKeyOk) {
  63. app->direct_sampling_enabled = !app->direct_sampling_enabled;
  64. }
  65. if ((input.key == InputKeyUp || input.key == InputKeyDown) &&
  66. (input.type == InputTypePress || input.type == InputTypeRepeat))
  67. {
  68. uint32_t change = input.type == InputTypePress ?
  69. USEC_PER_PIXEL_SMALL_CHANGE :
  70. USEC_PER_PIXEL_LARGE_CHANGE;
  71. if (input.key == InputKeyUp) change = -change;
  72. privdata->usec_per_pixel += change;
  73. if (privdata->usec_per_pixel < USEC_PER_PIXEL_MIN)
  74. privdata->usec_per_pixel = USEC_PER_PIXEL_MIN;
  75. else if (privdata->usec_per_pixel > USEC_PER_PIXEL_MAX)
  76. privdata->usec_per_pixel = USEC_PER_PIXEL_MAX;
  77. }
  78. }
  79. /* Enter view. Stop the subghz thread to prevent access as we read
  80. * the CC1101 data directly. */
  81. void view_enter_direct_sampling(ProtoViewApp *app) {
  82. /* Set view defaults. */
  83. DirectSamplingViewPrivData *privdata = app->view_privdata;
  84. privdata->usec_per_pixel = DEFAULT_USEC_PER_PIXEL;
  85. if (app->txrx->txrx_state == TxRxStateRx &&
  86. !app->txrx->debug_timer_sampling)
  87. {
  88. furi_hal_subghz_stop_async_rx();
  89. /* To read data asynchronously directly from the view, we need
  90. * to put the CC1101 back into reception mode (the previous call
  91. * to stop the async RX will put it into idle) and configure the
  92. * G0 pin for reading. */
  93. furi_hal_subghz_rx();
  94. furi_hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo,
  95. GpioSpeedLow);
  96. } else {
  97. raw_sampling_worker_stop(app);
  98. }
  99. }
  100. /* Exit view. Restore the subghz thread. */
  101. void view_exit_direct_sampling(ProtoViewApp *app) {
  102. DirectSamplingViewPrivData *privdata = app->view_privdata;
  103. if (privdata->captured) free(privdata->captured);
  104. app->direct_sampling_enabled = false;
  105. /* Restart normal data feeding. */
  106. if (app->txrx->txrx_state == TxRxStateRx &&
  107. !app->txrx->debug_timer_sampling)
  108. {
  109. furi_hal_subghz_start_async_rx(protoview_rx_callback, NULL);
  110. } else {
  111. raw_sampling_worker_start(app);
  112. }
  113. }