subghz_protocol_came.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "subghz_protocol_came.h"
  2. #include "subghz_protocol_common.h"
  3. /*
  4. * Help
  5. * https://phreakerclub.com/447
  6. *
  7. */
  8. struct SubGhzProtocolCame {
  9. SubGhzProtocolCommon common;
  10. };
  11. SubGhzProtocolCame* subghz_protocol_came_alloc() {
  12. SubGhzProtocolCame* instance = furi_alloc(sizeof(SubGhzProtocolCame));
  13. instance->common.name = "Came";
  14. instance->common.code_min_count_bit_for_found = 12;
  15. instance->common.te_shot = 320;
  16. instance->common.te_long = 640;
  17. instance->common.te_delta = 150;
  18. return instance;
  19. }
  20. void subghz_protocol_came_free(SubGhzProtocolCame* instance) {
  21. furi_assert(instance);
  22. free(instance);
  23. }
  24. /** Send bit
  25. *
  26. * @param instance - SubGhzProtocolCame instance
  27. * @param bit - bit
  28. */
  29. void subghz_protocol_came_send_bit(SubGhzProtocolCame* instance, uint8_t bit) {
  30. if (bit) {
  31. //send bit 1
  32. SUBGHZ_TX_PIN_LOW();
  33. delay_us(instance->common.te_long);
  34. SUBGHZ_TX_PIN_HIGTH();
  35. delay_us(instance->common.te_shot);
  36. } else {
  37. //send bit 0
  38. SUBGHZ_TX_PIN_LOW();
  39. delay_us(instance->common.te_shot);
  40. SUBGHZ_TX_PIN_HIGTH();
  41. delay_us(instance->common.te_long);
  42. }
  43. }
  44. void subghz_protocol_came_send_key(SubGhzProtocolCame* instance, uint64_t key, uint8_t bit, uint8_t repeat) {
  45. while (repeat--) {
  46. //Send header
  47. SUBGHZ_TX_PIN_LOW();
  48. delay_us(instance->common.te_shot * 34); //+2 interval v bit 1
  49. //Send start bit
  50. subghz_protocol_came_send_bit(instance, 1);
  51. //Send key data
  52. for (uint8_t i = bit; i > 0; i--) {
  53. subghz_protocol_came_send_bit(instance, bit_read(key, i - 1));
  54. }
  55. }
  56. }
  57. void subghz_protocol_came_parse(SubGhzProtocolCame* instance, LevelPair data) {
  58. switch (instance->common.parser_step) {
  59. case 0:
  60. if ((data.level == ApiHalSubGhzCaptureLevelLow)
  61. && (DURATION_DIFF(data.duration,instance->common.te_shot * 51)< instance->common.te_delta * 51)) { //Need protocol 36 te_shot
  62. //Found header CAME
  63. instance->common.parser_step = 1;
  64. } else {
  65. instance->common.parser_step = 0;
  66. }
  67. break;
  68. case 1:
  69. if (data.level == ApiHalSubGhzCaptureLevelLow) {
  70. break;
  71. } else if (DURATION_DIFF(data.duration,instance->common.te_shot)< instance->common.te_delta) {
  72. //Found start bit CAME
  73. instance->common.parser_step = 2;
  74. instance->common.code_found = 0;
  75. instance->common.code_count_bit = 0;
  76. } else {
  77. instance->common.parser_step = 0;
  78. }
  79. break;
  80. case 2:
  81. if (data.level == ApiHalSubGhzCaptureLevelLow) { //save interval
  82. if (data.duration >= (instance->common.te_shot * 4)) {
  83. instance->common.parser_step = 1;
  84. if (instance->common.code_count_bit>= instance->common.code_min_count_bit_for_found) {
  85. //ToDo out data display
  86. instance->common.serial = 0x0;
  87. instance->common.btn = 0x0;
  88. if (instance->common.callback)
  89. instance->common.callback((SubGhzProtocolCommon*)instance, instance->common.context);
  90. }
  91. break;
  92. }
  93. instance->common.te_last = data.duration;
  94. instance->common.parser_step = 3;
  95. } else {
  96. instance->common.parser_step = 0;
  97. }
  98. break;
  99. case 3:
  100. if (data.level == ApiHalSubGhzCaptureLevelHigh) {
  101. if ((DURATION_DIFF(instance->common.te_last,instance->common.te_shot) < instance->common.te_delta)
  102. && (DURATION_DIFF(data.duration,instance->common.te_long)< instance->common.te_delta)) {
  103. subghz_protocol_common_add_bit(&instance->common, 0);
  104. instance->common.parser_step = 2;
  105. } else if ((DURATION_DIFF(instance->common.te_last,instance->common.te_long)< instance->common.te_delta)
  106. && (DURATION_DIFF(data.duration,instance->common.te_shot)< instance->common.te_delta)) {
  107. subghz_protocol_common_add_bit(&instance->common, 1);
  108. instance->common.parser_step = 2;
  109. } else
  110. instance->common.parser_step = 0;
  111. } else {
  112. instance->common.parser_step = 0;
  113. }
  114. break;
  115. }
  116. }