subghz_static.c 5.8 KB

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