subghz_test_carrier.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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) {
  66. return false;
  67. }
  68. with_view_model(
  69. subghz_test_carrier->view, (SubghzTestCarrierModel * model) {
  70. osTimerStop(subghz_test_carrier->timer);
  71. furi_hal_subghz_idle();
  72. if(event->type == InputTypeShort) {
  73. if(event->key == InputKeyLeft) {
  74. if(model->frequency > 0) model->frequency--;
  75. } else if(event->key == InputKeyRight) {
  76. if(model->frequency < subghz_frequencies_count - 1) model->frequency++;
  77. } else if(event->key == InputKeyDown) {
  78. if(model->path > 0) model->path--;
  79. } else if(event->key == InputKeyUp) {
  80. if(model->path < FuriHalSubGhzPath868) model->path++;
  81. } else if(event->key == InputKeyOk) {
  82. if(model->status == SubghzTestCarrierModelStatusTx) {
  83. model->status = SubghzTestCarrierModelStatusRx;
  84. } else {
  85. model->status = SubghzTestCarrierModelStatusTx;
  86. }
  87. }
  88. model->real_frequency =
  89. furi_hal_subghz_set_frequency(subghz_frequencies[model->frequency]);
  90. furi_hal_subghz_set_path(model->path);
  91. }
  92. if(model->status == SubghzTestCarrierModelStatusRx) {
  93. hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  94. furi_hal_subghz_rx();
  95. osTimerStart(subghz_test_carrier->timer, 1024 / 4);
  96. } else {
  97. hal_gpio_init(&gpio_cc1101_g0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  98. hal_gpio_write(&gpio_cc1101_g0, true);
  99. furi_hal_subghz_tx();
  100. }
  101. return true;
  102. });
  103. return true;
  104. }
  105. void subghz_test_carrier_enter(void* context) {
  106. furi_assert(context);
  107. SubghzTestCarrier* subghz_test_carrier = context;
  108. furi_hal_subghz_reset();
  109. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOokAsync);
  110. hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  111. with_view_model(
  112. subghz_test_carrier->view, (SubghzTestCarrierModel * model) {
  113. model->frequency = subghz_frequencies_433_92; // 433
  114. model->real_frequency =
  115. furi_hal_subghz_set_frequency(subghz_frequencies[model->frequency]);
  116. model->path = FuriHalSubGhzPathIsolate; // isolate
  117. model->rssi = 0.0f;
  118. model->status = SubghzTestCarrierModelStatusRx;
  119. return true;
  120. });
  121. furi_hal_subghz_rx();
  122. osTimerStart(subghz_test_carrier->timer, 1024 / 4);
  123. }
  124. void subghz_test_carrier_exit(void* context) {
  125. furi_assert(context);
  126. SubghzTestCarrier* subghz_test_carrier = context;
  127. osTimerStop(subghz_test_carrier->timer);
  128. // Reinitialize IC to default state
  129. furi_hal_subghz_sleep();
  130. }
  131. void subghz_test_carrier_rssi_timer_callback(void* context) {
  132. furi_assert(context);
  133. SubghzTestCarrier* subghz_test_carrier = context;
  134. with_view_model(
  135. subghz_test_carrier->view, (SubghzTestCarrierModel * model) {
  136. model->rssi = furi_hal_subghz_get_rssi();
  137. return true;
  138. });
  139. }
  140. SubghzTestCarrier* subghz_test_carrier_alloc() {
  141. SubghzTestCarrier* subghz_test_carrier = furi_alloc(sizeof(SubghzTestCarrier));
  142. // View allocation and configuration
  143. subghz_test_carrier->view = view_alloc();
  144. view_allocate_model(
  145. subghz_test_carrier->view, ViewModelTypeLockFree, sizeof(SubghzTestCarrierModel));
  146. view_set_context(subghz_test_carrier->view, subghz_test_carrier);
  147. view_set_draw_callback(subghz_test_carrier->view, (ViewDrawCallback)subghz_test_carrier_draw);
  148. view_set_input_callback(subghz_test_carrier->view, subghz_test_carrier_input);
  149. view_set_enter_callback(subghz_test_carrier->view, subghz_test_carrier_enter);
  150. view_set_exit_callback(subghz_test_carrier->view, subghz_test_carrier_exit);
  151. subghz_test_carrier->timer = osTimerNew(
  152. subghz_test_carrier_rssi_timer_callback, osTimerPeriodic, subghz_test_carrier, NULL);
  153. return subghz_test_carrier;
  154. }
  155. void subghz_test_carrier_free(SubghzTestCarrier* subghz_test_carrier) {
  156. furi_assert(subghz_test_carrier);
  157. osTimerDelete(subghz_test_carrier->timer);
  158. view_free(subghz_test_carrier->view);
  159. free(subghz_test_carrier);
  160. }
  161. View* subghz_test_carrier_get_view(SubghzTestCarrier* subghz_test_carrier) {
  162. furi_assert(subghz_test_carrier);
  163. return subghz_test_carrier->view;
  164. }