encoder_cyfral.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "encoder_cyfral.h"
  2. #include <furi_hal.h>
  3. #define CYFRAL_DATA_SIZE sizeof(uint16_t)
  4. #define CYFRAL_PERIOD (125 * furi_hal_delay_instructions_per_microsecond())
  5. #define CYFRAL_0_LOW (CYFRAL_PERIOD * 0.66f)
  6. #define CYFRAL_0_HI (CYFRAL_PERIOD * 0.33f)
  7. #define CYFRAL_1_LOW (CYFRAL_PERIOD * 0.33f)
  8. #define CYFRAL_1_HI (CYFRAL_PERIOD * 0.66f)
  9. #define CYFRAL_SET_DATA(level, len) \
  10. *polarity = level; \
  11. *length = len;
  12. struct EncoderCyfral {
  13. uint32_t data;
  14. uint32_t index;
  15. };
  16. EncoderCyfral* encoder_cyfral_alloc() {
  17. EncoderCyfral* cyfral = malloc(sizeof(EncoderCyfral));
  18. encoder_cyfral_reset(cyfral);
  19. return cyfral;
  20. }
  21. void encoder_cyfral_free(EncoderCyfral* cyfral) {
  22. free(cyfral);
  23. }
  24. void encoder_cyfral_reset(EncoderCyfral* cyfral) {
  25. cyfral->data = 0;
  26. cyfral->index = 0;
  27. }
  28. uint32_t cyfral_encoder_encode(const uint16_t data) {
  29. uint32_t value = 0;
  30. for(int8_t i = 0; i <= 7; i++) {
  31. switch((data >> (i * 2)) & 0b00000011) {
  32. case 0b11:
  33. value = value << 4;
  34. value += 0b00000111;
  35. break;
  36. case 0b10:
  37. value = value << 4;
  38. value += 0b00001011;
  39. break;
  40. case 0b01:
  41. value = value << 4;
  42. value += 0b00001101;
  43. break;
  44. case 0b00:
  45. value = value << 4;
  46. value += 0b00001110;
  47. break;
  48. default:
  49. break;
  50. }
  51. }
  52. return value;
  53. }
  54. void encoder_cyfral_set_data(EncoderCyfral* cyfral, const uint8_t* data, size_t data_size) {
  55. furi_assert(cyfral);
  56. furi_check(data_size >= CYFRAL_DATA_SIZE);
  57. uint16_t intermediate;
  58. memcpy(&intermediate, data, CYFRAL_DATA_SIZE);
  59. cyfral->data = cyfral_encoder_encode(intermediate);
  60. }
  61. void encoder_cyfral_get_pulse(EncoderCyfral* cyfral, bool* polarity, uint32_t* length) {
  62. if(cyfral->index < 8) {
  63. // start word (0b0001)
  64. switch(cyfral->index) {
  65. case 0:
  66. CYFRAL_SET_DATA(false, CYFRAL_0_LOW);
  67. break;
  68. case 1:
  69. CYFRAL_SET_DATA(true, CYFRAL_0_HI);
  70. break;
  71. case 2:
  72. CYFRAL_SET_DATA(false, CYFRAL_0_LOW);
  73. break;
  74. case 3:
  75. CYFRAL_SET_DATA(true, CYFRAL_0_HI);
  76. break;
  77. case 4:
  78. CYFRAL_SET_DATA(false, CYFRAL_0_LOW);
  79. break;
  80. case 5:
  81. CYFRAL_SET_DATA(true, CYFRAL_0_HI);
  82. break;
  83. case 6:
  84. CYFRAL_SET_DATA(false, CYFRAL_1_LOW);
  85. break;
  86. case 7:
  87. CYFRAL_SET_DATA(true, CYFRAL_1_HI);
  88. break;
  89. }
  90. } else {
  91. // data
  92. uint8_t data_start_index = cyfral->index - 8;
  93. bool clock_polarity = (data_start_index) % 2;
  94. uint8_t bit_index = (data_start_index) / 2;
  95. bool bit_value = ((cyfral->data >> bit_index) & 1);
  96. if(!clock_polarity) {
  97. if(bit_value) {
  98. CYFRAL_SET_DATA(false, CYFRAL_1_LOW);
  99. } else {
  100. CYFRAL_SET_DATA(false, CYFRAL_0_LOW);
  101. }
  102. } else {
  103. if(bit_value) {
  104. CYFRAL_SET_DATA(true, CYFRAL_1_HI);
  105. } else {
  106. CYFRAL_SET_DATA(true, CYFRAL_0_HI);
  107. }
  108. }
  109. }
  110. cyfral->index++;
  111. if(cyfral->index >= (9 * 4 * 2)) {
  112. cyfral->index = 0;
  113. }
  114. }