subghz_test_static.c 6.0 KB

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