infrared_common_encoder.c 6.0 KB

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