infrared_common_encoder.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. status = InfraredStatusOk;
  86. encoder->state = InfraredCommonEncoderStatePreamble;
  87. ++encoder->timings_encoded;
  88. encoder->timings_sum = 0;
  89. break;
  90. case InfraredCommonEncoderStatePreamble:
  91. if(timings->preamble_mark) {
  92. if(encoder->timings_encoded == 1) {
  93. *duration = timings->preamble_mark;
  94. *level = true;
  95. } else {
  96. *duration = timings->preamble_space;
  97. *level = false;
  98. encoder->state = InfraredCommonEncoderStateEncode;
  99. }
  100. ++encoder->timings_encoded;
  101. encoder->timings_sum += *duration;
  102. break;
  103. } else {
  104. encoder->state = InfraredCommonEncoderStateEncode;
  105. }
  106. /* FALLTHROUGH */
  107. case InfraredCommonEncoderStateEncode:
  108. status = infrared_common_encode_bits(encoder, duration, level);
  109. if(status == InfraredStatusDone) {
  110. if(encoder->protocol->encode_repeat) {
  111. encoder->state = InfraredCommonEncoderStateEncodeRepeat;
  112. } else {
  113. encoder->timings_encoded = 0;
  114. encoder->timings_sum = 0;
  115. encoder->bits_encoded = 0;
  116. encoder->switch_detect = 0;
  117. encoder->state = InfraredCommonEncoderStateSilence;
  118. }
  119. }
  120. break;
  121. case InfraredCommonEncoderStateEncodeRepeat:
  122. status = encoder->protocol->encode_repeat(encoder, duration, level);
  123. break;
  124. }
  125. return status;
  126. }
  127. void* infrared_common_encoder_alloc(const InfraredCommonProtocolSpec* protocol) {
  128. furi_assert(protocol);
  129. if(protocol->decode == infrared_common_decode_pdwm) {
  130. furi_assert(
  131. (protocol->timings.bit1_mark == protocol->timings.bit0_mark) ^
  132. (protocol->timings.bit1_space == protocol->timings.bit0_space));
  133. }
  134. /* protocol->databit_len[0] has to contain biggest value of bits that can be decoded */
  135. for(size_t i = 1; i < COUNT_OF(protocol->databit_len); ++i) {
  136. furi_assert(protocol->databit_len[i] <= protocol->databit_len[0]);
  137. }
  138. uint32_t alloc_size = sizeof(InfraredCommonDecoder) + protocol->databit_len[0] / 8 +
  139. !!(protocol->databit_len[0] % 8);
  140. InfraredCommonEncoder* encoder = malloc(alloc_size);
  141. memset(encoder, 0, alloc_size);
  142. encoder->protocol = protocol;
  143. return encoder;
  144. }
  145. void infrared_common_encoder_free(InfraredCommonEncoder* encoder) {
  146. furi_assert(encoder);
  147. free(encoder);
  148. }
  149. void infrared_common_encoder_reset(InfraredCommonEncoder* encoder) {
  150. furi_assert(encoder);
  151. encoder->timings_encoded = 0;
  152. encoder->timings_sum = 0;
  153. encoder->bits_encoded = 0;
  154. encoder->state = InfraredCommonEncoderStateSilence;
  155. encoder->switch_detect = 0;
  156. uint8_t max_databit_len = 0;
  157. for(size_t i = 0; i < COUNT_OF(encoder->protocol->databit_len); ++i) {
  158. max_databit_len = MAX(max_databit_len, encoder->protocol->databit_len[i]);
  159. }
  160. uint8_t bytes_to_clear = max_databit_len / 8 + !!(max_databit_len % 8);
  161. memset(encoder->data, 0, bytes_to_clear);
  162. }