irda_common_encoder.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "furi/check.h"
  2. #include "irda.h"
  3. #include "irda_common_i.h"
  4. #include <stdbool.h>
  5. #include <furi.h>
  6. #include "irda_i.h"
  7. #include <stdint.h>
  8. static IrdaStatus irda_common_encode_bits(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level) {
  9. IrdaStatus status = encoder->protocol->encode(encoder, duration, level);
  10. furi_assert(status == IrdaStatusOk);
  11. ++encoder->timings_encoded;
  12. encoder->timings_sum += *duration;
  13. if ((encoder->bits_encoded == encoder->bits_to_encode) && *level) {
  14. status = IrdaStatusDone;
  15. }
  16. return status;
  17. }
  18. /*
  19. *
  20. * 3:
  21. * even_timing = 0
  22. * level = 0 ^ 1 = 1
  23. * 4:
  24. * even_timing = 1
  25. * level = 1 ^ 1 = 0
  26. * ++timing;
  27. *
  28. *
  29. * 0 1 2 | 3 4 |
  30. * _____-------_____---___
  31. */
  32. IrdaStatus irda_common_encode_manchester(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level) {
  33. furi_assert(encoder);
  34. furi_assert(duration);
  35. furi_assert(level);
  36. const IrdaTimings* timings = &encoder->protocol->timings;
  37. uint8_t index = encoder->bits_encoded / 8;
  38. uint8_t shift = encoder->bits_encoded % 8; // LSB first
  39. bool logic_value = !!(encoder->data[index] & (0x01 << shift));
  40. bool even_timing = !(encoder->timings_encoded % 2);
  41. *level = even_timing ^ logic_value;
  42. *duration = timings->bit1_mark;
  43. if (even_timing)
  44. ++encoder->bits_encoded;
  45. else if (*level && (encoder->bits_encoded + 1 == encoder->bits_to_encode))
  46. ++encoder->bits_encoded; /* don't encode last space */
  47. return IrdaStatusOk;
  48. }
  49. IrdaStatus irda_common_encode_pdwm(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level) {
  50. furi_assert(encoder);
  51. furi_assert(duration);
  52. furi_assert(level);
  53. const IrdaTimings* timings = &encoder->protocol->timings;
  54. uint8_t index = encoder->bits_encoded / 8;
  55. uint8_t shift = encoder->bits_encoded % 8; // LSB first
  56. bool logic_value = !!(encoder->data[index] & (0x01 << shift));
  57. bool pwm = timings->bit1_space == timings->bit0_space;
  58. if (encoder->timings_encoded % 2) { /* start encoding from space */
  59. *duration = logic_value ? timings->bit1_mark : timings->bit0_mark;
  60. *level = true;
  61. if (pwm)
  62. ++encoder->bits_encoded;
  63. } else {
  64. *duration = logic_value ? timings->bit1_space : timings->bit0_space;
  65. *level = false;
  66. if (!pwm)
  67. ++encoder->bits_encoded;
  68. }
  69. return IrdaStatusOk;
  70. }
  71. IrdaStatus irda_common_encode(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level) {
  72. furi_assert(encoder);
  73. furi_assert(duration);
  74. furi_assert(level);
  75. IrdaStatus status = IrdaStatusOk;
  76. const IrdaTimings* timings = &encoder->protocol->timings;
  77. switch (encoder->state) {
  78. case IrdaCommonEncoderStateSilence:
  79. *duration = encoder->protocol->timings.silence_time;
  80. *level = false;
  81. status = IrdaStatusOk;
  82. encoder->state = IrdaCommonEncoderStatePreamble;
  83. ++encoder->timings_encoded;
  84. encoder->timings_sum = 0;
  85. break;
  86. case IrdaCommonEncoderStatePreamble:
  87. if (timings->preamble_mark) {
  88. if (encoder->timings_encoded == 1) {
  89. *duration = timings->preamble_mark;
  90. *level = true;
  91. } else {
  92. *duration = timings->preamble_space;
  93. *level = false;
  94. encoder->state = IrdaCommonEncoderStateEncode;
  95. }
  96. ++encoder->timings_encoded;
  97. encoder->timings_sum += *duration;
  98. break;
  99. } else {
  100. encoder->state = IrdaCommonEncoderStateEncode;
  101. }
  102. /* FALLTHROUGH */
  103. case IrdaCommonEncoderStateEncode:
  104. status = irda_common_encode_bits(encoder, duration, level);
  105. if (status == IrdaStatusDone) {
  106. if (encoder->protocol->encode_repeat) {
  107. encoder->state = IrdaCommonEncoderStateEncodeRepeat;
  108. } else {
  109. encoder->timings_encoded = 0;
  110. encoder->timings_sum = 0;
  111. encoder->bits_encoded = 0;
  112. encoder->switch_detect = 0;
  113. encoder->state = IrdaCommonEncoderStateSilence;
  114. }
  115. }
  116. break;
  117. case IrdaCommonEncoderStateEncodeRepeat:
  118. status = encoder->protocol->encode_repeat(encoder, duration, level);
  119. break;
  120. }
  121. return status;
  122. }
  123. void* irda_common_encoder_alloc(const IrdaCommonProtocolSpec* protocol) {
  124. furi_assert(protocol);
  125. if (protocol->decode == irda_common_decode_pdwm) {
  126. furi_assert((protocol->timings.bit1_mark == protocol->timings.bit0_mark) ^ (protocol->timings.bit1_space == protocol->timings.bit0_space));
  127. }
  128. /* protocol->databit_len[0] has to contain biggest value of bits that can be decoded */
  129. for (int i = 1; i < COUNT_OF(protocol->databit_len); ++i) {
  130. furi_assert(protocol->databit_len[i] <= protocol->databit_len[0]);
  131. }
  132. uint32_t alloc_size = sizeof(IrdaCommonDecoder)
  133. + protocol->databit_len[0] / 8
  134. + !!(protocol->databit_len[0] % 8);
  135. IrdaCommonEncoder* encoder = furi_alloc(alloc_size);
  136. memset(encoder, 0, alloc_size);
  137. encoder->protocol = protocol;
  138. return encoder;
  139. }
  140. void irda_common_encoder_free(IrdaCommonEncoder* encoder) {
  141. furi_assert(encoder);
  142. free(encoder);
  143. }
  144. void irda_common_encoder_reset(IrdaCommonEncoder* encoder) {
  145. furi_assert(encoder);
  146. encoder->timings_encoded = 0;
  147. encoder->timings_sum = 0;
  148. encoder->bits_encoded = 0;
  149. encoder->state = IrdaCommonEncoderStateSilence;
  150. encoder->switch_detect = 0;
  151. uint8_t max_databit_len = 0;
  152. for (int i = 0; i < COUNT_OF(encoder->protocol->databit_len); ++i) {
  153. max_databit_len = MAX(max_databit_len, encoder->protocol->databit_len[i]);
  154. }
  155. uint8_t bytes_to_clear = max_databit_len / 8
  156. + !!(max_databit_len % 8);
  157. memset(encoder->data, 0, bytes_to_clear);
  158. }