subghz_static.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include "subghz_static.h"
  2. #include "../subghz_i.h"
  3. #include <math.h>
  4. #include <furi.h>
  5. #include <api-hal.h>
  6. #include <input/input.h>
  7. #include <notification/notification-messages.h>
  8. static const uint8_t subghz_static_keys[][4] = {
  9. {0x74, 0xBA, 0xDE},
  10. {0x74, 0xBA, 0xDD},
  11. {0x74, 0xBA, 0xDB},
  12. {0xE3, 0x4A, 0x4E},
  13. };
  14. #define SUBGHZ_PT_SHORT 376
  15. #define SUBGHZ_PT_LONG (SUBGHZ_PT_SHORT * 3)
  16. #define SUBGHZ_PT_GUARD 10600
  17. struct SubghzStatic {
  18. View* view;
  19. };
  20. typedef enum {
  21. SubghzStaticStatusRx,
  22. SubghzStaticStatusTx,
  23. } SubghzStaticStatus;
  24. typedef struct {
  25. uint8_t frequency;
  26. uint32_t real_frequency;
  27. uint8_t button;
  28. } SubghzStaticModel;
  29. void subghz_static_draw(Canvas* canvas, SubghzStaticModel* model) {
  30. char buffer[64];
  31. canvas_set_color(canvas, ColorBlack);
  32. canvas_set_font(canvas, FontPrimary);
  33. canvas_draw_str(canvas, 0, 8, "CC1101 Static");
  34. canvas_set_font(canvas, FontSecondary);
  35. // Frequency
  36. snprintf(
  37. buffer,
  38. sizeof(buffer),
  39. "Freq: %03ld.%03ld.%03ld Hz",
  40. model->real_frequency / 1000000 % 1000,
  41. model->real_frequency / 1000 % 1000,
  42. model->real_frequency % 1000);
  43. canvas_draw_str(canvas, 0, 20, buffer);
  44. snprintf(buffer, sizeof(buffer), "Key: %d", model->button);
  45. canvas_draw_str(canvas, 0, 31, buffer);
  46. }
  47. bool subghz_static_input(InputEvent* event, void* context) {
  48. furi_assert(context);
  49. SubghzStatic* subghz_static = context;
  50. if(event->key == InputKeyBack) {
  51. return false;
  52. }
  53. with_view_model(
  54. subghz_static->view, (SubghzStaticModel * model) {
  55. bool reconfigure = false;
  56. if(event->type == InputTypeShort) {
  57. if(event->key == InputKeyLeft) {
  58. if(model->frequency > 0) model->frequency--;
  59. reconfigure = true;
  60. } else if(event->key == InputKeyRight) {
  61. if(model->frequency < subghz_frequencies_count - 1) model->frequency++;
  62. reconfigure = true;
  63. } else if(event->key == InputKeyDown) {
  64. if(model->button > 0) model->button--;
  65. } else if(event->key == InputKeyUp) {
  66. if(model->button < 3) model->button++;
  67. }
  68. }
  69. if(reconfigure) {
  70. api_hal_subghz_idle();
  71. model->real_frequency =
  72. api_hal_subghz_set_frequency_and_path(subghz_frequencies[model->frequency]);
  73. api_hal_subghz_tx();
  74. }
  75. if(event->key == InputKeyOk) {
  76. if(event->type == InputTypePress) {
  77. const uint8_t* key = subghz_static_keys[model->button];
  78. NotificationApp* notification = furi_record_open("notification");
  79. notification_message_block(notification, &sequence_set_red_255);
  80. __disable_irq();
  81. for(uint8_t r = 0; r < 20; r++) {
  82. //Payload
  83. for(uint8_t i = 0; i < 24; i++) {
  84. uint8_t byte = i / 8;
  85. uint8_t bit = i % 8;
  86. bool value = (key[byte] >> (7 - bit)) & 1;
  87. // Payload send
  88. hal_gpio_write(&gpio_cc1101_g0, true);
  89. delay_us(value ? SUBGHZ_PT_SHORT : SUBGHZ_PT_LONG);
  90. hal_gpio_write(&gpio_cc1101_g0, false);
  91. delay_us(value ? SUBGHZ_PT_LONG : SUBGHZ_PT_SHORT);
  92. }
  93. // Last bit
  94. hal_gpio_write(&gpio_cc1101_g0, true);
  95. delay_us(SUBGHZ_PT_SHORT);
  96. hal_gpio_write(&gpio_cc1101_g0, false);
  97. // Guard time
  98. delay_us(10600);
  99. }
  100. __enable_irq();
  101. notification_message(notification, &sequence_reset_red);
  102. furi_record_close("notification");
  103. }
  104. }
  105. return true;
  106. });
  107. return true;
  108. }
  109. void subghz_static_enter(void* context) {
  110. furi_assert(context);
  111. SubghzStatic* subghz_static = context;
  112. api_hal_subghz_reset();
  113. api_hal_subghz_load_preset(ApiHalSubGhzPresetOokAsync);
  114. hal_gpio_init(&gpio_cc1101_g0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  115. hal_gpio_write(&gpio_cc1101_g0, false);
  116. with_view_model(
  117. subghz_static->view, (SubghzStaticModel * model) {
  118. model->frequency = subghz_frequencies_433_92;
  119. model->real_frequency =
  120. api_hal_subghz_set_frequency_and_path(subghz_frequencies[model->frequency]);
  121. model->button = 0;
  122. return true;
  123. });
  124. api_hal_subghz_tx();
  125. }
  126. void subghz_static_exit(void* context) {
  127. furi_assert(context);
  128. // SubghzStatic* subghz_static = context;
  129. // Reinitialize IC to default state
  130. api_hal_subghz_sleep();
  131. }
  132. uint32_t subghz_static_back(void* context) {
  133. return SubGhzViewMenu;
  134. }
  135. SubghzStatic* subghz_static_alloc() {
  136. SubghzStatic* subghz_static = furi_alloc(sizeof(SubghzStatic));
  137. // View allocation and configuration
  138. subghz_static->view = view_alloc();
  139. view_allocate_model(subghz_static->view, ViewModelTypeLockFree, sizeof(SubghzStaticModel));
  140. view_set_context(subghz_static->view, subghz_static);
  141. view_set_draw_callback(subghz_static->view, (ViewDrawCallback)subghz_static_draw);
  142. view_set_input_callback(subghz_static->view, subghz_static_input);
  143. view_set_enter_callback(subghz_static->view, subghz_static_enter);
  144. view_set_exit_callback(subghz_static->view, subghz_static_exit);
  145. view_set_previous_callback(subghz_static->view, subghz_static_back);
  146. return subghz_static;
  147. }
  148. void subghz_static_free(SubghzStatic* subghz_static) {
  149. furi_assert(subghz_static);
  150. view_free(subghz_static->view);
  151. free(subghz_static);
  152. }
  153. View* subghz_static_get_view(SubghzStatic* subghz_static) {
  154. furi_assert(subghz_static);
  155. return subghz_static->view;
  156. }