infrared_decoder_rc6.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "infrared.h"
  2. #include <stdbool.h>
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. #include <furi.h>
  6. #include "../infrared_i.h"
  7. #include "../infrared_protocol_defs_i.h"
  8. typedef struct {
  9. InfraredCommonDecoder* common_decoder;
  10. bool toggle;
  11. } InfraredRc6Decoder;
  12. InfraredMessage* infrared_decoder_rc6_check_ready(void* ctx) {
  13. InfraredRc6Decoder* decoder_rc6 = ctx;
  14. return infrared_common_decoder_check_ready(decoder_rc6->common_decoder);
  15. }
  16. bool infrared_decoder_rc6_interpret(InfraredCommonDecoder* decoder) {
  17. furi_assert(decoder);
  18. bool result = false;
  19. uint32_t* data = (void*)&decoder->data[0];
  20. // MSB first
  21. uint8_t address = reverse((uint8_t)(*data >> 5));
  22. uint8_t command = reverse((uint8_t)(*data >> 13));
  23. bool start_bit = *data & 0x01;
  24. bool toggle = !!(*data & 0x10);
  25. uint8_t mode = (*data >> 1) & 0x7;
  26. if((start_bit == 1) && (mode == 0)) {
  27. InfraredMessage* message = &decoder->message;
  28. InfraredRc6Decoder* rc6_decoder = decoder->context;
  29. bool* prev_toggle = &rc6_decoder->toggle;
  30. if((message->address == address) && (message->command == command) &&
  31. (message->protocol == InfraredProtocolRC6)) {
  32. message->repeat = (toggle == *prev_toggle);
  33. } else {
  34. message->repeat = false;
  35. }
  36. *prev_toggle = toggle;
  37. message->command = command;
  38. message->address = address;
  39. message->protocol = InfraredProtocolRC6;
  40. result = true;
  41. }
  42. return result;
  43. }
  44. /*
  45. * RC6 Uses manchester encoding, but it has twice longer
  46. * 4-th bit (toggle bit) time quant, so we need to decode
  47. * it separately and than pass decoding for other bits to
  48. * common manchester decode function.
  49. */
  50. InfraredStatus infrared_decoder_rc6_decode_manchester(
  51. InfraredCommonDecoder* decoder,
  52. bool level,
  53. uint32_t timing) {
  54. // 4th bit lasts 2x times more
  55. InfraredStatus status = InfraredStatusError;
  56. uint16_t bit = decoder->protocol->timings.bit1_mark;
  57. uint16_t tolerance = decoder->protocol->timings.bit_tolerance;
  58. bool single_timing = MATCH_TIMING(timing, bit, tolerance);
  59. bool double_timing = MATCH_TIMING(timing, 2 * bit, tolerance);
  60. bool triple_timing = MATCH_TIMING(timing, 3 * bit, tolerance);
  61. if(decoder->databit_cnt == 4) {
  62. furi_assert(decoder->switch_detect == true);
  63. if(single_timing ^ triple_timing) {
  64. ++decoder->databit_cnt;
  65. decoder->data[0] |= (single_timing ? !level : level) << 4;
  66. status = InfraredStatusOk;
  67. }
  68. } else if(decoder->databit_cnt == 5) {
  69. if(single_timing || triple_timing) {
  70. if(triple_timing) timing = bit;
  71. decoder->switch_detect = false;
  72. status = infrared_common_decode_manchester(decoder, level, timing);
  73. } else if(double_timing) {
  74. status = InfraredStatusOk;
  75. }
  76. } else {
  77. status = infrared_common_decode_manchester(decoder, level, timing);
  78. }
  79. return status;
  80. }
  81. void* infrared_decoder_rc6_alloc(void) {
  82. InfraredRc6Decoder* decoder = malloc(sizeof(InfraredRc6Decoder));
  83. decoder->toggle = false;
  84. decoder->common_decoder = infrared_common_decoder_alloc(&protocol_rc6);
  85. decoder->common_decoder->context = decoder;
  86. return decoder;
  87. }
  88. InfraredMessage* infrared_decoder_rc6_decode(void* decoder, bool level, uint32_t duration) {
  89. InfraredRc6Decoder* decoder_rc6 = decoder;
  90. return infrared_common_decode(decoder_rc6->common_decoder, level, duration);
  91. }
  92. void infrared_decoder_rc6_free(void* decoder) {
  93. InfraredRc6Decoder* decoder_rc6 = decoder;
  94. infrared_common_decoder_free(decoder_rc6->common_decoder);
  95. free(decoder_rc6);
  96. }
  97. void infrared_decoder_rc6_reset(void* decoder) {
  98. InfraredRc6Decoder* decoder_rc6 = decoder;
  99. infrared_common_decoder_reset(decoder_rc6->common_decoder);
  100. }