subghz_test_static.c 6.2 KB

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