subghz_protocol_came.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. void subghz_protocol_came_send_bit(SubGhzProtocolCame* instance, uint8_t bit) {
  25. if (bit) {
  26. //send bit 1
  27. SUBGHZ_TX_PIN_LOW();
  28. delay_us(instance->common.te_long);
  29. SUBGHZ_TX_PIN_HIGTH();
  30. delay_us(instance->common.te_shot);
  31. } else {
  32. //send bit 0
  33. SUBGHZ_TX_PIN_LOW();
  34. delay_us(instance->common.te_shot);
  35. SUBGHZ_TX_PIN_HIGTH();
  36. delay_us(instance->common.te_long);
  37. }
  38. }
  39. void subghz_protocol_came_send_key(SubGhzProtocolCame* instance, uint64_t key, uint8_t bit, uint8_t repeat) {
  40. while (repeat--) {
  41. //Send header
  42. SUBGHZ_TX_PIN_LOW();
  43. delay_us(instance->common.te_shot * 34); //+2 interval v bit 1
  44. //Send start bit
  45. subghz_protocol_came_send_bit(instance, 1);
  46. //Send key data
  47. for (uint8_t i = bit; i > 0; i--) {
  48. subghz_protocol_came_send_bit(instance, bit_read(key, i - 1));
  49. }
  50. }
  51. }
  52. void subghz_protocol_came_parse(SubGhzProtocolCame* instance, LevelPair data) {
  53. switch (instance->common.parser_step) {
  54. case 0:
  55. if ((data.level == ApiHalSubGhzCaptureLevelLow)
  56. && (DURATION_DIFF(data.duration,instance->common.te_shot * 51)< instance->common.te_delta * 51)) { //Need protocol 36 te_shot
  57. //Found header CAME
  58. instance->common.parser_step = 1;
  59. } else {
  60. instance->common.parser_step = 0;
  61. }
  62. break;
  63. case 1:
  64. if (data.level == ApiHalSubGhzCaptureLevelLow) {
  65. break;
  66. } else if (DURATION_DIFF(data.duration,instance->common.te_shot)< instance->common.te_delta) {
  67. //Found start bit CAME
  68. instance->common.parser_step = 2;
  69. instance->common.code_found = 0;
  70. instance->common.code_count_bit = 0;
  71. } else {
  72. instance->common.parser_step = 0;
  73. }
  74. break;
  75. case 2:
  76. if (data.level == ApiHalSubGhzCaptureLevelLow) { //save interval
  77. if (data.duration >= (instance->common.te_shot * 4)) {
  78. instance->common.parser_step = 1;
  79. if (instance->common.code_count_bit>= instance->common.code_min_count_bit_for_found) {
  80. //ToDo out data display
  81. if (instance->common.callback)
  82. instance->common.callback((SubGhzProtocolCommon*)instance, instance->common.context);
  83. }
  84. break;
  85. }
  86. instance->common.te_last = data.duration;
  87. instance->common.parser_step = 3;
  88. } else {
  89. instance->common.parser_step = 0;
  90. }
  91. break;
  92. case 3:
  93. if (data.level == ApiHalSubGhzCaptureLevelHigh) {
  94. if ((DURATION_DIFF(instance->common.te_last,instance->common.te_shot) < instance->common.te_delta)
  95. && (DURATION_DIFF(data.duration,instance->common.te_long)< instance->common.te_delta)) {
  96. subghz_protocol_common_add_bit(&instance->common, 0);
  97. instance->common.parser_step = 2;
  98. } else if ((DURATION_DIFF(instance->common.te_last,instance->common.te_long)< instance->common.te_delta)
  99. && (DURATION_DIFF(data.duration,instance->common.te_shot)< instance->common.te_delta)) {
  100. subghz_protocol_common_add_bit(&instance->common, 1);
  101. instance->common.parser_step = 2;
  102. } else
  103. instance->common.parser_step = 0;
  104. } else {
  105. instance->common.parser_step = 0;
  106. }
  107. break;
  108. }
  109. }