subghz_static.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. static const uint8_t subghz_static_keys[][4] = {
  8. {0x74, 0xBA, 0xDE, 0x80},
  9. {0x74, 0xBA, 0xDD, 0x80},
  10. {0x74, 0xBA, 0xDB, 0x80},
  11. };
  12. struct SubghzStatic {
  13. View* view;
  14. };
  15. typedef enum {
  16. SubghzStaticStatusRx,
  17. SubghzStaticStatusTx,
  18. } SubghzStaticStatus;
  19. typedef struct {
  20. uint8_t frequency;
  21. uint32_t real_frequency;
  22. ApiHalSubGhzPath path;
  23. uint8_t button;
  24. } SubghzStaticModel;
  25. void subghz_static_draw(Canvas* canvas, SubghzStaticModel* model) {
  26. char buffer[64];
  27. canvas_set_color(canvas, ColorBlack);
  28. canvas_set_font(canvas, FontPrimary);
  29. canvas_draw_str(canvas, 2, 12, "CC1101 Static");
  30. canvas_set_font(canvas, FontSecondary);
  31. // Frequency
  32. snprintf(
  33. buffer,
  34. sizeof(buffer),
  35. "Freq: %03ld.%03ld.%03ld Hz",
  36. model->real_frequency / 1000000 % 1000,
  37. model->real_frequency / 1000 % 1000,
  38. model->real_frequency % 1000);
  39. canvas_draw_str(canvas, 2, 24, buffer);
  40. // Path
  41. char* path_name = "Unknown";
  42. if(model->path == ApiHalSubGhzPathIsolate) {
  43. path_name = "isolate";
  44. } else if(model->path == ApiHalSubGhzPath1) {
  45. path_name = "433MHz";
  46. } else if(model->path == ApiHalSubGhzPath2) {
  47. path_name = "315MHz";
  48. } else if(model->path == ApiHalSubGhzPath3) {
  49. path_name = "868MHz";
  50. }
  51. snprintf(buffer, sizeof(buffer), "Path: %d - %s", model->path, path_name);
  52. canvas_draw_str(canvas, 2, 36, buffer);
  53. snprintf(buffer, sizeof(buffer), "Key: %d", model->button);
  54. canvas_draw_str(canvas, 2, 48, buffer);
  55. }
  56. bool subghz_static_input(InputEvent* event, void* context) {
  57. furi_assert(context);
  58. SubghzStatic* subghz_static = context;
  59. if(event->key == InputKeyBack) {
  60. return false;
  61. }
  62. with_view_model(
  63. subghz_static->view, (SubghzStaticModel * model) {
  64. bool reconfigure = false;
  65. if(event->type == InputTypeShort) {
  66. if(event->key == InputKeyLeft) {
  67. if(model->frequency > 0) model->frequency--;
  68. reconfigure = true;
  69. } else if(event->key == InputKeyRight) {
  70. if(model->frequency < subghz_frequencies_count - 1) model->frequency++;
  71. reconfigure = true;
  72. } else if(event->key == InputKeyDown) {
  73. if(model->button > 0) model->button--;
  74. } else if(event->key == InputKeyUp) {
  75. if(model->button < 2) model->button++;
  76. }
  77. model->path = subghz_frequencies_paths[model->frequency];
  78. }
  79. if(reconfigure) {
  80. api_hal_subghz_idle();
  81. model->real_frequency =
  82. api_hal_subghz_set_frequency(subghz_frequencies[model->frequency]);
  83. api_hal_subghz_set_path(model->path);
  84. api_hal_subghz_tx();
  85. }
  86. if(event->key == InputKeyOk) {
  87. if(event->type == InputTypePress) {
  88. const uint8_t* key = subghz_static_keys[model->button];
  89. api_hal_light_set(LightRed, 0xff);
  90. __disable_irq();
  91. gpio_write(&cc1101_g0_gpio, false);
  92. delay_us(136);
  93. gpio_write(&cc1101_g0_gpio, true);
  94. delay_us(10000);
  95. for(uint8_t r = 0; r < 8; r++) {
  96. for(uint8_t i = 0; i < 25; i++) {
  97. uint8_t byte = i / 8;
  98. uint8_t bit = i % 8;
  99. bool value = (key[byte] >> (7 - bit)) & 1;
  100. gpio_write(&cc1101_g0_gpio, false);
  101. if(value) {
  102. delay_us(360);
  103. } else {
  104. delay_us(1086);
  105. }
  106. gpio_write(&cc1101_g0_gpio, true);
  107. if(value) {
  108. delay_us(1086);
  109. } else {
  110. delay_us(360);
  111. }
  112. }
  113. delay_us(10000);
  114. }
  115. __enable_irq();
  116. api_hal_light_set(LightRed, 0x00);
  117. }
  118. }
  119. return true;
  120. });
  121. return true;
  122. }
  123. void subghz_static_enter(void* context) {
  124. furi_assert(context);
  125. SubghzStatic* subghz_static = context;
  126. api_hal_subghz_reset();
  127. api_hal_subghz_load_preset(ApiHalSubGhzPresetOokAsync);
  128. gpio_init(&cc1101_g0_gpio, GpioModeOutputPushPull);
  129. gpio_write(&cc1101_g0_gpio, true);
  130. with_view_model(
  131. subghz_static->view, (SubghzStaticModel * model) {
  132. model->frequency = 4;
  133. model->real_frequency =
  134. api_hal_subghz_set_frequency(subghz_frequencies[model->frequency]);
  135. model->path = subghz_frequencies_paths[model->frequency];
  136. model->button = 0;
  137. api_hal_subghz_set_path(model->path);
  138. return true;
  139. });
  140. api_hal_subghz_tx();
  141. }
  142. void subghz_static_exit(void* context) {
  143. furi_assert(context);
  144. // SubghzStatic* subghz_static = context;
  145. // Reinitialize IC to default state
  146. api_hal_subghz_init();
  147. }
  148. uint32_t subghz_static_back(void* context) {
  149. return SubGhzViewMenu;
  150. }
  151. SubghzStatic* subghz_static_alloc() {
  152. SubghzStatic* subghz_static = furi_alloc(sizeof(SubghzStatic));
  153. // View allocation and configuration
  154. subghz_static->view = view_alloc();
  155. view_allocate_model(subghz_static->view, ViewModelTypeLockFree, sizeof(SubghzStaticModel));
  156. view_set_context(subghz_static->view, subghz_static);
  157. view_set_draw_callback(subghz_static->view, (ViewDrawCallback)subghz_static_draw);
  158. view_set_input_callback(subghz_static->view, subghz_static_input);
  159. view_set_enter_callback(subghz_static->view, subghz_static_enter);
  160. view_set_exit_callback(subghz_static->view, subghz_static_exit);
  161. view_set_previous_callback(subghz_static->view, subghz_static_back);
  162. return subghz_static;
  163. }
  164. void subghz_static_free(SubghzStatic* subghz_static) {
  165. furi_assert(subghz_static);
  166. view_free(subghz_static->view);
  167. free(subghz_static);
  168. }
  169. View* subghz_static_get_view(SubghzStatic* subghz_static) {
  170. furi_assert(subghz_static);
  171. return subghz_static->view;
  172. }