subghz_test_static.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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, subghz_test_static_keys[model->button], 10000);
  91. furi_hal_subghz_start_async_tx(
  92. subghz_encoder_princeton_yield, instance->encoder);
  93. instance->satus_tx = SubghzTestStaticStatusTX;
  94. }
  95. } else if(event->type == InputTypeRelease) {
  96. if(instance->satus_tx == SubghzTestStaticStatusTX) {
  97. FURI_LOG_I(TAG, "TX Stop");
  98. subghz_encoder_princeton_print_log(instance->encoder);
  99. furi_hal_subghz_stop_async_tx();
  100. notification_message(notification, &sequence_reset_red);
  101. }
  102. instance->satus_tx = SubghzTestStaticStatusIDLE;
  103. }
  104. furi_record_close("notification");
  105. }
  106. return true;
  107. });
  108. return true;
  109. }
  110. void subghz_test_static_enter(void* context) {
  111. furi_assert(context);
  112. SubghzTestStatic* instance = context;
  113. furi_hal_subghz_reset();
  114. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
  115. hal_gpio_init(&gpio_cc1101_g0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  116. hal_gpio_write(&gpio_cc1101_g0, false);
  117. instance->satus_tx = SubghzTestStaticStatusIDLE;
  118. with_view_model(
  119. instance->view, (SubghzTestStaticModel * model) {
  120. model->frequency = subghz_frequencies_433_92;
  121. model->real_frequency = subghz_frequencies[model->frequency];
  122. model->button = 0;
  123. return true;
  124. });
  125. }
  126. void subghz_test_static_exit(void* context) {
  127. furi_assert(context);
  128. furi_hal_subghz_sleep();
  129. }
  130. SubghzTestStatic* subghz_test_static_alloc() {
  131. SubghzTestStatic* instance = furi_alloc(sizeof(SubghzTestStatic));
  132. // View allocation and configuration
  133. instance->view = view_alloc();
  134. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(SubghzTestStaticModel));
  135. view_set_context(instance->view, instance);
  136. view_set_draw_callback(instance->view, (ViewDrawCallback)subghz_test_static_draw);
  137. view_set_input_callback(instance->view, subghz_test_static_input);
  138. view_set_enter_callback(instance->view, subghz_test_static_enter);
  139. view_set_exit_callback(instance->view, subghz_test_static_exit);
  140. instance->encoder = subghz_encoder_princeton_alloc();
  141. return instance;
  142. }
  143. void subghz_test_static_free(SubghzTestStatic* instance) {
  144. furi_assert(instance);
  145. subghz_encoder_princeton_free(instance->encoder);
  146. view_free(instance->view);
  147. free(instance);
  148. }
  149. View* subghz_test_static_get_view(SubghzTestStatic* instance) {
  150. furi_assert(instance);
  151. return instance->view;
  152. }