irda_common_encoder.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /*
  8. *
  9. * 3:
  10. * even_timing = 0
  11. * level = 0 ^ 1 = 1
  12. * 4:
  13. * even_timing = 1
  14. * level = 1 ^ 1 = 0
  15. * ++timing;
  16. *
  17. *
  18. * 0 1 2 | 3 4 |
  19. * _____-------_____---___
  20. */
  21. IrdaStatus irda_common_encode_manchester(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level) {
  22. furi_assert(encoder);
  23. furi_assert(duration);
  24. furi_assert(level);
  25. const IrdaTimings* timings = &encoder->protocol->timings;
  26. uint8_t index = encoder->bits_encoded / 8;
  27. uint8_t shift = encoder->bits_encoded % 8; // LSB first
  28. bool logic_value = !!(encoder->data[index] & (0x01 << shift));
  29. bool even_timing = !(encoder->timings_encoded % 2);
  30. *level = even_timing ^ logic_value;
  31. *duration = timings->bit1_mark;
  32. if (even_timing) /* start encoding from space */
  33. ++encoder->bits_encoded;
  34. ++encoder->timings_encoded;
  35. bool finish = (encoder->bits_encoded == encoder->protocol->databit_len);
  36. finish |= (encoder->bits_encoded == (encoder->protocol->databit_len-1)) && *level && !even_timing;
  37. return finish ? IrdaStatusDone : IrdaStatusOk;
  38. }
  39. IrdaStatus irda_common_encode_pdwm(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level) {
  40. furi_assert(encoder);
  41. furi_assert(duration);
  42. furi_assert(level);
  43. const IrdaTimings* timings = &encoder->protocol->timings;
  44. uint8_t index = encoder->bits_encoded / 8;
  45. uint8_t shift = encoder->bits_encoded % 8; // LSB first
  46. bool logic_value = !!(encoder->data[index] & (0x01 << shift));
  47. // stop bit
  48. if (encoder->bits_encoded == encoder->protocol->databit_len) {
  49. *duration = timings->bit1_mark;
  50. *level = true;
  51. ++encoder->timings_encoded;
  52. return IrdaStatusDone;
  53. }
  54. if (encoder->timings_encoded % 2) { /* start encoding from space */
  55. *duration = logic_value ? timings->bit1_mark : timings->bit0_mark;
  56. *level = true;
  57. } else {
  58. *duration = logic_value ? timings->bit1_space : timings->bit0_space;
  59. *level = false;
  60. ++encoder->bits_encoded;
  61. }
  62. ++encoder->timings_encoded;
  63. return IrdaStatusOk;
  64. }
  65. IrdaStatus irda_common_encode(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level) {
  66. furi_assert(encoder);
  67. furi_assert(duration);
  68. furi_assert(level);
  69. IrdaStatus status = IrdaStatusOk;
  70. const IrdaTimings* timings = &encoder->protocol->timings;
  71. switch (encoder->state) {
  72. case IrdaCommonEncoderStateSpace:
  73. *duration = encoder->protocol->timings.silence_time;
  74. *level = false;
  75. status = IrdaStatusOk;
  76. encoder->state = IrdaCommonEncoderStatePreamble;
  77. ++encoder->timings_encoded;
  78. break;
  79. case IrdaCommonEncoderStatePreamble:
  80. if (timings->preamble_mark) {
  81. if (encoder->timings_encoded == 1) {
  82. *duration = timings->preamble_mark;
  83. *level = true;
  84. } else {
  85. *duration = timings->preamble_space;
  86. *level = false;
  87. encoder->state = IrdaCommonEncoderStateEncode;
  88. }
  89. ++encoder->timings_encoded;
  90. break;
  91. } else {
  92. encoder->state = IrdaCommonEncoderStateEncode;
  93. }
  94. /* FALLTHROUGH */
  95. case IrdaCommonEncoderStateEncode:
  96. status = encoder->protocol->encode(encoder, duration, level);
  97. if (status == IrdaStatusDone) {
  98. if (encoder->protocol->encode_repeat) {
  99. encoder->state = IrdaCommonEncoderStateEncodeRepeat;
  100. } else {
  101. encoder->timings_encoded = 0;
  102. encoder->bits_encoded = 0;
  103. encoder->switch_detect = 0;
  104. encoder->state = IrdaCommonEncoderStateSpace;
  105. }
  106. }
  107. break;
  108. case IrdaCommonEncoderStateEncodeRepeat:
  109. status = encoder->protocol->encode_repeat(encoder, duration, level);
  110. break;
  111. }
  112. return status;
  113. }
  114. void* irda_common_encoder_alloc(const IrdaCommonProtocolSpec* protocol) {
  115. furi_assert(protocol);
  116. uint32_t alloc_size = sizeof(IrdaCommonEncoder)
  117. + protocol->databit_len / 8
  118. + !!(protocol->databit_len % 8);
  119. IrdaCommonEncoder* encoder = furi_alloc(alloc_size);
  120. memset(encoder, 0, alloc_size);
  121. encoder->protocol = protocol;
  122. return encoder;
  123. }
  124. void irda_common_encoder_free(IrdaCommonEncoder* encoder) {
  125. furi_assert(encoder);
  126. free(encoder);
  127. }
  128. void irda_common_encoder_reset(IrdaCommonEncoder* encoder) {
  129. furi_assert(encoder);
  130. encoder->timings_encoded = 0;
  131. encoder->bits_encoded = 0;
  132. encoder->state = IrdaCommonEncoderStateSpace;
  133. encoder->switch_detect = 0;
  134. uint8_t bytes_to_clear = encoder->protocol->databit_len / 8
  135. + !!(encoder->protocol->databit_len % 8);
  136. memset(encoder->data, 0, bytes_to_clear);
  137. }
  138. void irda_common_encoder_set_context(void* decoder, void* context) {
  139. IrdaCommonEncoder* common_encoder = decoder;
  140. common_encoder->context = context;
  141. }