subghz_protocol_gate_tx.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #include "subghz_protocol_gate_tx.h"
  2. struct SubGhzProtocolGateTX {
  3. SubGhzProtocolCommon common;
  4. };
  5. SubGhzProtocolGateTX* subghz_protocol_gate_tx_alloc(void) {
  6. SubGhzProtocolGateTX* instance = furi_alloc(sizeof(SubGhzProtocolGateTX));
  7. instance->common.name = "GateTX";
  8. instance->common.code_min_count_bit_for_found = 24;
  9. instance->common.te_shot = 350;
  10. instance->common.te_long = 700;
  11. instance->common.te_delta = 100;
  12. instance->common.to_string = (SubGhzProtocolCommonToStr)subghz_protocol_gate_tx_to_str;
  13. instance->common.to_save_string =
  14. (SubGhzProtocolCommonGetStrSave)subghz_protocol_gate_tx_to_save_str;
  15. instance->common.to_load_protocol=
  16. (SubGhzProtocolCommonLoad)subghz_protocol_gate_tx_to_load_protocol;
  17. return instance;
  18. }
  19. void subghz_protocol_gate_tx_free(SubGhzProtocolGateTX* instance) {
  20. furi_assert(instance);
  21. free(instance);
  22. }
  23. /** Send bit
  24. *
  25. * @param instance - SubGhzProtocolGateTX instance
  26. * @param bit - bit
  27. */
  28. void subghz_protocol_gate_tx_send_bit(SubGhzProtocolGateTX* instance, uint8_t bit) {
  29. if (bit) {
  30. //send bit 1
  31. SUBGHZ_TX_PIN_LOW();
  32. delay_us(instance->common.te_long);
  33. SUBGHZ_TX_PIN_HIGTH();
  34. delay_us(instance->common.te_shot);
  35. } else {
  36. //send bit 0
  37. SUBGHZ_TX_PIN_LOW();
  38. delay_us(instance->common.te_shot);
  39. SUBGHZ_TX_PIN_HIGTH();
  40. delay_us(instance->common.te_long);
  41. }
  42. }
  43. void subghz_protocol_gate_tx_send_key(SubGhzProtocolGateTX* instance, uint64_t key, uint8_t bit,uint8_t repeat) {
  44. while (repeat--) {
  45. //Send header
  46. SUBGHZ_TX_PIN_LOW();
  47. delay_us(instance->common.te_shot * 47); //+2 interval v bit 1
  48. //Send start bit
  49. SUBGHZ_TX_PIN_HIGTH();
  50. delay_us(instance->common.te_long);
  51. //Send key data
  52. for (uint8_t i = bit; i > 0; i--) {
  53. subghz_protocol_gate_tx_send_bit(instance, bit_read(key, i - 1));
  54. }
  55. }
  56. }
  57. void subghz_protocol_gate_tx_reset(SubGhzProtocolGateTX* instance) {
  58. instance->common.parser_step = 0;
  59. }
  60. /** Analysis of received data
  61. *
  62. * @param instance SubGhzProtocolFaacSLH instance
  63. */
  64. void subghz_protocol_gate_tx_check_remote_controller(SubGhzProtocolGateTX* instance) {
  65. uint32_t code_found_reverse = subghz_protocol_common_reverse_key(instance->common.code_last_found, instance->common.code_last_count_bit);
  66. instance->common.serial = (code_found_reverse & 0xFF) << 12 | ((code_found_reverse >>8) & 0xFF) << 4 | ((code_found_reverse >>20) & 0x0F) ;
  67. instance->common.btn = ((code_found_reverse >> 16) & 0x0F);
  68. }
  69. void subghz_protocol_gate_tx_parse(SubGhzProtocolGateTX* instance, bool level, uint32_t duration) {
  70. switch (instance->common.parser_step) {
  71. case 0:
  72. if ((!level)
  73. && (DURATION_DIFF(duration,instance->common.te_shot * 47)< instance->common.te_delta * 47)) {
  74. //Found Preambula
  75. instance->common.parser_step = 1;
  76. } else {
  77. instance->common.parser_step = 0;
  78. }
  79. break;
  80. case 1:
  81. if (level && ((DURATION_DIFF(duration,instance->common.te_long)< instance->common.te_delta*3))){
  82. //Found start bit
  83. instance->common.parser_step = 2;
  84. instance->common.code_found = 0;
  85. instance->common.code_count_bit = 0;
  86. } else {
  87. instance->common.parser_step = 0;
  88. }
  89. break;
  90. case 2:
  91. if (!level) {
  92. if (duration >= (instance->common.te_shot * 10 + instance->common.te_delta)) {
  93. instance->common.parser_step = 1;
  94. if (instance->common.code_count_bit>= instance->common.code_min_count_bit_for_found) {
  95. instance->common.code_last_found = instance->common.code_found;
  96. instance->common.code_last_count_bit = instance->common.code_count_bit;
  97. if (instance->common.callback) instance->common.callback((SubGhzProtocolCommon*)instance, instance->common.context);
  98. }
  99. instance->common.code_found = 0;
  100. instance->common.code_count_bit = 0;
  101. break;
  102. } else {
  103. instance->common.te_last = duration;
  104. instance->common.parser_step = 3;
  105. }
  106. }
  107. break;
  108. case 3:
  109. if(level){
  110. if ((DURATION_DIFF(instance->common.te_last,instance->common.te_shot)< instance->common.te_delta)
  111. && (DURATION_DIFF(duration,instance->common.te_long)< instance->common.te_delta*3)) {
  112. subghz_protocol_common_add_bit(&instance->common, 0);
  113. instance->common.parser_step = 2;
  114. } else if ((DURATION_DIFF(instance->common.te_last,instance->common.te_long)< instance->common.te_delta*3)
  115. && (DURATION_DIFF(duration,instance->common.te_shot)< instance->common.te_delta)) {
  116. subghz_protocol_common_add_bit(&instance->common, 1);
  117. instance->common.parser_step = 2;
  118. } else {
  119. instance->common.parser_step = 0;
  120. }
  121. }else{
  122. instance->common.parser_step = 0;
  123. }
  124. break;
  125. }
  126. }
  127. void subghz_protocol_gate_tx_to_str(SubGhzProtocolGateTX* instance, string_t output) {
  128. subghz_protocol_gate_tx_check_remote_controller(instance);
  129. string_cat_printf(output,
  130. "%s, %d Bit\r\n"
  131. " KEY:%06lX\r\n"
  132. " SN:%05lX BTN:%lX\r\n",
  133. instance->common.name,
  134. instance->common.code_last_count_bit,
  135. (uint32_t)(instance->common.code_last_found & 0xFFFFFF),
  136. instance->common.serial,
  137. instance->common.btn
  138. );
  139. }
  140. void subghz_protocol_gate_tx_to_save_str(SubGhzProtocolGateTX* instance, string_t output) {
  141. string_printf(
  142. output,
  143. "Protocol: %s\n"
  144. "Bit: %d\n"
  145. "Key: %08lX\n",
  146. instance->common.name,
  147. instance->common.code_last_count_bit,
  148. (uint32_t)(instance->common.code_last_found & 0x00000000ffffffff));
  149. }
  150. bool subghz_protocol_gate_tx_to_load_protocol(FileWorker* file_worker, SubGhzProtocolGateTX* instance){
  151. bool loaded = false;
  152. string_t temp_str;
  153. string_init(temp_str);
  154. int res = 0;
  155. int data = 0;
  156. do {
  157. // Read and parse bit data from 2nd line
  158. if(!file_worker_read_until(file_worker, temp_str, '\n')) {
  159. break;
  160. }
  161. res = sscanf(string_get_cstr(temp_str), "Bit: %d\n", &data);
  162. if(res != 1) {
  163. break;
  164. }
  165. instance->common.code_last_count_bit = (uint8_t)data;
  166. // Read and parse key data from 3nd line
  167. if(!file_worker_read_until(file_worker, temp_str, '\n')) {
  168. break;
  169. }
  170. uint32_t temp_key = 0;
  171. res = sscanf(string_get_cstr(temp_str), "Key: %08lX\n", &temp_key);
  172. if(res != 1) {
  173. break;
  174. }
  175. instance->common.code_last_found = (uint64_t)temp_key;
  176. subghz_protocol_gate_tx_check_remote_controller(instance);
  177. loaded = true;
  178. } while(0);
  179. string_clear(temp_str);
  180. return loaded;
  181. }