infrared_common_encoder.c 5.9 KB

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