subghz_test_static.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include "subghz_test_static.h"
  2. #include "../subghz_i.h"
  3. #include "../helpers/subghz_testing.h"
  4. #include <math.h>
  5. #include <furi.h>
  6. #include <furi_hal.h>
  7. #include <input/input.h>
  8. #include <notification/notification_messages.h>
  9. #include <lib/subghz/protocols/princeton_for_testing.h>
  10. #define TAG "SubGhzTestStatic"
  11. typedef enum {
  12. SubGhzTestStaticStatusIDLE,
  13. SubGhzTestStaticStatusTX,
  14. } SubGhzTestStaticStatus;
  15. static const uint32_t subghz_test_static_keys[] = {
  16. 0x0074BADE,
  17. 0x0074BADD,
  18. 0x0074BADB,
  19. 0x00E34A4E,
  20. };
  21. struct SubGhzTestStatic {
  22. View* view;
  23. SubGhzTestStaticStatus status_tx;
  24. SubGhzEncoderPrinceton* encoder;
  25. SubGhzTestStaticCallback callback;
  26. void* context;
  27. };
  28. typedef struct {
  29. uint8_t frequency;
  30. uint32_t real_frequency;
  31. uint8_t button;
  32. } SubGhzTestStaticModel;
  33. void subghz_test_static_set_callback(
  34. SubGhzTestStatic* subghz_test_static,
  35. SubGhzTestStaticCallback callback,
  36. void* context) {
  37. furi_assert(subghz_test_static);
  38. furi_assert(callback);
  39. subghz_test_static->callback = callback;
  40. subghz_test_static->context = context;
  41. }
  42. void subghz_test_static_draw(Canvas* canvas, SubGhzTestStaticModel* model) {
  43. char buffer[64];
  44. canvas_set_color(canvas, ColorBlack);
  45. canvas_set_font(canvas, FontPrimary);
  46. canvas_draw_str(canvas, 0, 8, "CC1101 Static");
  47. canvas_set_font(canvas, FontSecondary);
  48. // Frequency
  49. snprintf(
  50. buffer,
  51. sizeof(buffer),
  52. "Freq: %03ld.%03ld.%03ld Hz",
  53. model->real_frequency / 1000000 % 1000,
  54. model->real_frequency / 1000 % 1000,
  55. model->real_frequency % 1000);
  56. canvas_draw_str(canvas, 0, 20, buffer);
  57. snprintf(buffer, sizeof(buffer), "Key: %d", model->button);
  58. canvas_draw_str(canvas, 0, 31, buffer);
  59. }
  60. bool subghz_test_static_input(InputEvent* event, void* context) {
  61. furi_assert(context);
  62. SubGhzTestStatic* instance = context;
  63. if(event->key == InputKeyBack) {
  64. return false;
  65. }
  66. with_view_model(
  67. instance->view,
  68. SubGhzTestStaticModel * model,
  69. {
  70. if(event->type == InputTypeShort) {
  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_testing - 1) model->frequency++;
  75. } else if(event->key == InputKeyDown) {
  76. if(model->button > 0) model->button--;
  77. } else if(event->key == InputKeyUp) {
  78. if(model->button < 3) model->button++;
  79. }
  80. }
  81. model->real_frequency = subghz_frequencies_testing[model->frequency];
  82. if(event->key == InputKeyOk) {
  83. NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
  84. if(event->type == InputTypePress) {
  85. furi_hal_subghz_idle();
  86. furi_hal_subghz_set_frequency_and_path(
  87. subghz_frequencies_testing[model->frequency]);
  88. if(!furi_hal_subghz_tx()) {
  89. instance->callback(SubGhzTestStaticEventOnlyRx, instance->context);
  90. } else {
  91. notification_message_block(notification, &sequence_set_red_255);
  92. FURI_LOG_I(TAG, "TX Start");
  93. subghz_encoder_princeton_for_testing_set(
  94. instance->encoder,
  95. subghz_test_static_keys[model->button],
  96. 10000,
  97. subghz_frequencies_testing[model->frequency]);
  98. furi_hal_subghz_start_async_tx(
  99. subghz_encoder_princeton_for_testing_yield, instance->encoder);
  100. instance->status_tx = SubGhzTestStaticStatusTX;
  101. }
  102. } else if(event->type == InputTypeRelease) {
  103. if(instance->status_tx == SubGhzTestStaticStatusTX) {
  104. FURI_LOG_I(TAG, "TX Stop");
  105. subghz_encoder_princeton_for_testing_stop(
  106. instance->encoder, furi_get_tick());
  107. subghz_encoder_princeton_for_testing_print_log(instance->encoder);
  108. furi_hal_subghz_stop_async_tx();
  109. notification_message(notification, &sequence_reset_red);
  110. }
  111. instance->status_tx = SubGhzTestStaticStatusIDLE;
  112. }
  113. furi_record_close(RECORD_NOTIFICATION);
  114. }
  115. },
  116. true);
  117. return true;
  118. }
  119. void subghz_test_static_enter(void* context) {
  120. furi_assert(context);
  121. SubGhzTestStatic* instance = context;
  122. furi_hal_subghz_reset();
  123. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
  124. furi_hal_gpio_init(&gpio_cc1101_g0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  125. furi_hal_gpio_write(&gpio_cc1101_g0, false);
  126. instance->status_tx = SubGhzTestStaticStatusIDLE;
  127. with_view_model(
  128. instance->view,
  129. SubGhzTestStaticModel * model,
  130. {
  131. model->frequency = subghz_frequencies_433_92_testing;
  132. model->real_frequency = subghz_frequencies_testing[model->frequency];
  133. model->button = 0;
  134. },
  135. true);
  136. }
  137. void subghz_test_static_exit(void* context) {
  138. furi_assert(context);
  139. furi_hal_subghz_sleep();
  140. }
  141. SubGhzTestStatic* subghz_test_static_alloc() {
  142. SubGhzTestStatic* instance = malloc(sizeof(SubGhzTestStatic));
  143. // View allocation and configuration
  144. instance->view = view_alloc();
  145. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(SubGhzTestStaticModel));
  146. view_set_context(instance->view, instance);
  147. view_set_draw_callback(instance->view, (ViewDrawCallback)subghz_test_static_draw);
  148. view_set_input_callback(instance->view, subghz_test_static_input);
  149. view_set_enter_callback(instance->view, subghz_test_static_enter);
  150. view_set_exit_callback(instance->view, subghz_test_static_exit);
  151. instance->encoder = subghz_encoder_princeton_for_testing_alloc();
  152. return instance;
  153. }
  154. void subghz_test_static_free(SubGhzTestStatic* instance) {
  155. furi_assert(instance);
  156. subghz_encoder_princeton_for_testing_free(instance->encoder);
  157. view_free(instance->view);
  158. free(instance);
  159. }
  160. View* subghz_test_static_get_view(SubGhzTestStatic* instance) {
  161. furi_assert(instance);
  162. return instance->view;
  163. }