subghz_test_carrier.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include "subghz_test_carrier.h"
  2. #include "../subghz_i.h"
  3. #include <math.h>
  4. #include <furi.h>
  5. #include <furi-hal.h>
  6. #include <input/input.h>
  7. struct SubghzTestCarrier {
  8. View* view;
  9. osTimerId timer;
  10. };
  11. typedef enum {
  12. SubghzTestCarrierModelStatusRx,
  13. SubghzTestCarrierModelStatusTx,
  14. } SubghzTestCarrierModelStatus;
  15. typedef struct {
  16. uint8_t frequency;
  17. uint32_t real_frequency;
  18. FuriHalSubGhzPath path;
  19. float rssi;
  20. SubghzTestCarrierModelStatus status;
  21. } SubghzTestCarrierModel;
  22. void subghz_test_carrier_draw(Canvas* canvas, SubghzTestCarrierModel* model) {
  23. char buffer[64];
  24. canvas_set_color(canvas, ColorBlack);
  25. canvas_set_font(canvas, FontPrimary);
  26. canvas_draw_str(canvas, 0, 8, "CC1101 Basic Test");
  27. canvas_set_font(canvas, FontSecondary);
  28. // Frequency
  29. snprintf(
  30. buffer,
  31. sizeof(buffer),
  32. "Freq: %03ld.%03ld.%03ld Hz",
  33. model->real_frequency / 1000000 % 1000,
  34. model->real_frequency / 1000 % 1000,
  35. model->real_frequency % 1000);
  36. canvas_draw_str(canvas, 0, 20, buffer);
  37. // Path
  38. char* path_name = "Unknown";
  39. if(model->path == FuriHalSubGhzPathIsolate) {
  40. path_name = "isolate";
  41. } else if(model->path == FuriHalSubGhzPath433) {
  42. path_name = "433MHz";
  43. } else if(model->path == FuriHalSubGhzPath315) {
  44. path_name = "315MHz";
  45. } else if(model->path == FuriHalSubGhzPath868) {
  46. path_name = "868MHz";
  47. }
  48. snprintf(buffer, sizeof(buffer), "Path: %d - %s", model->path, path_name);
  49. canvas_draw_str(canvas, 0, 31, buffer);
  50. if(model->status == SubghzTestCarrierModelStatusRx) {
  51. snprintf(
  52. buffer,
  53. sizeof(buffer),
  54. "RSSI: %ld.%ld dBm",
  55. (int32_t)(model->rssi),
  56. (int32_t)fabs(model->rssi * 10) % 10);
  57. canvas_draw_str(canvas, 0, 42, buffer);
  58. } else {
  59. canvas_draw_str(canvas, 0, 42, "TX");
  60. }
  61. }
  62. bool subghz_test_carrier_input(InputEvent* event, void* context) {
  63. furi_assert(context);
  64. SubghzTestCarrier* subghz_test_carrier = context;
  65. if(event->key == InputKeyBack || event->type != InputTypeShort) {
  66. return false;
  67. }
  68. with_view_model(
  69. subghz_test_carrier->view, (SubghzTestCarrierModel * model) {
  70. furi_hal_subghz_idle();
  71. if(event->key == InputKeyLeft) {
  72. if(model->frequency > 0) model->frequency--;
  73. } else if(event->key == InputKeyRight) {
  74. if(model->frequency < subghz_frequencies_count - 1) model->frequency++;
  75. } else if(event->key == InputKeyDown) {
  76. if(model->path > 0) model->path--;
  77. } else if(event->key == InputKeyUp) {
  78. if(model->path < FuriHalSubGhzPath868) model->path++;
  79. } else if(event->key == InputKeyOk) {
  80. if(model->status == SubghzTestCarrierModelStatusTx) {
  81. model->status = SubghzTestCarrierModelStatusRx;
  82. } else {
  83. model->status = SubghzTestCarrierModelStatusTx;
  84. }
  85. }
  86. model->real_frequency =
  87. furi_hal_subghz_set_frequency(subghz_frequencies[model->frequency]);
  88. furi_hal_subghz_set_path(model->path);
  89. if(model->status == SubghzTestCarrierModelStatusRx) {
  90. hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  91. furi_hal_subghz_rx();
  92. } else {
  93. hal_gpio_init(&gpio_cc1101_g0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  94. hal_gpio_write(&gpio_cc1101_g0, true);
  95. furi_hal_subghz_tx();
  96. }
  97. return true;
  98. });
  99. return true;
  100. }
  101. void subghz_test_carrier_enter(void* context) {
  102. furi_assert(context);
  103. SubghzTestCarrier* subghz_test_carrier = context;
  104. furi_hal_subghz_reset();
  105. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
  106. hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  107. with_view_model(
  108. subghz_test_carrier->view, (SubghzTestCarrierModel * model) {
  109. model->frequency = subghz_frequencies_433_92; // 433
  110. model->real_frequency =
  111. furi_hal_subghz_set_frequency(subghz_frequencies[model->frequency]);
  112. model->path = FuriHalSubGhzPathIsolate; // isolate
  113. model->rssi = 0.0f;
  114. model->status = SubghzTestCarrierModelStatusRx;
  115. return true;
  116. });
  117. furi_hal_subghz_rx();
  118. osTimerStart(subghz_test_carrier->timer, 1024 / 4);
  119. }
  120. void subghz_test_carrier_exit(void* context) {
  121. furi_assert(context);
  122. SubghzTestCarrier* subghz_test_carrier = context;
  123. osTimerStop(subghz_test_carrier->timer);
  124. // Reinitialize IC to default state
  125. furi_hal_subghz_sleep();
  126. }
  127. void subghz_test_carrier_rssi_timer_callback(void* context) {
  128. furi_assert(context);
  129. SubghzTestCarrier* subghz_test_carrier = context;
  130. with_view_model(
  131. subghz_test_carrier->view, (SubghzTestCarrierModel * model) {
  132. if(model->status == SubghzTestCarrierModelStatusRx) {
  133. model->rssi = furi_hal_subghz_get_rssi();
  134. return true;
  135. }
  136. return false;
  137. });
  138. }
  139. SubghzTestCarrier* subghz_test_carrier_alloc() {
  140. SubghzTestCarrier* subghz_test_carrier = furi_alloc(sizeof(SubghzTestCarrier));
  141. // View allocation and configuration
  142. subghz_test_carrier->view = view_alloc();
  143. view_allocate_model(
  144. subghz_test_carrier->view, ViewModelTypeLocking, sizeof(SubghzTestCarrierModel));
  145. view_set_context(subghz_test_carrier->view, subghz_test_carrier);
  146. view_set_draw_callback(subghz_test_carrier->view, (ViewDrawCallback)subghz_test_carrier_draw);
  147. view_set_input_callback(subghz_test_carrier->view, subghz_test_carrier_input);
  148. view_set_enter_callback(subghz_test_carrier->view, subghz_test_carrier_enter);
  149. view_set_exit_callback(subghz_test_carrier->view, subghz_test_carrier_exit);
  150. subghz_test_carrier->timer = osTimerNew(
  151. subghz_test_carrier_rssi_timer_callback, osTimerPeriodic, subghz_test_carrier, NULL);
  152. return subghz_test_carrier;
  153. }
  154. void subghz_test_carrier_free(SubghzTestCarrier* subghz_test_carrier) {
  155. furi_assert(subghz_test_carrier);
  156. osTimerDelete(subghz_test_carrier->timer);
  157. view_free(subghz_test_carrier->view);
  158. free(subghz_test_carrier);
  159. }
  160. View* subghz_test_carrier_get_view(SubghzTestCarrier* subghz_test_carrier) {
  161. furi_assert(subghz_test_carrier);
  162. return subghz_test_carrier->view;
  163. }