irda_decoder_rc6.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "irda.h"
  2. #include <stdbool.h>
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. #include <furi.h>
  6. #include "../irda_i.h"
  7. #include "../irda_protocol_defs_i.h"
  8. typedef struct {
  9. IrdaCommonDecoder* common_decoder;
  10. bool toggle;
  11. } IrdaRc6Decoder;
  12. bool irda_decoder_rc6_interpret(IrdaCommonDecoder* decoder) {
  13. furi_assert(decoder);
  14. bool result = false;
  15. uint32_t* data = (void*) &decoder->data[0];
  16. // MSB first
  17. uint8_t address = reverse((uint8_t) (*data >> 5));
  18. uint8_t command = reverse((uint8_t) (*data >> 13));
  19. bool start_bit = *data & 0x01;
  20. bool toggle = !!(*data & 0x10);
  21. uint8_t mode = (*data >> 1) & 0x7;
  22. if ((start_bit == 1) && (mode == 0)) {
  23. IrdaMessage* message = &decoder->message;
  24. IrdaRc6Decoder *rc6_decoder = decoder->context;
  25. bool *prev_toggle = &rc6_decoder->toggle;
  26. if ((message->address == address)
  27. && (message->command == command)
  28. && (message->protocol == IrdaProtocolRC6)) {
  29. message->repeat = (toggle == *prev_toggle);
  30. } else {
  31. message->repeat = false;
  32. }
  33. *prev_toggle = toggle;
  34. message->command = command;
  35. message->address = address;
  36. result = true;
  37. }
  38. return result;
  39. }
  40. /*
  41. * RC6 Uses manchester encoding, but it has twice longer
  42. * 4-th bit (toggle bit) time quant, so we need to decode
  43. * it separately and than pass decoding for other bits to
  44. * common manchester decode function.
  45. */
  46. IrdaStatus irda_decoder_rc6_decode_manchester(IrdaCommonDecoder* decoder) {
  47. // 4th bit lasts 2x times more
  48. IrdaStatus status = IrdaStatusError;
  49. uint16_t bit = decoder->protocol->timings.bit1_mark;
  50. uint16_t tolerance = decoder->protocol->timings.bit_tolerance;
  51. uint16_t timing = decoder->timings[0];
  52. bool single_timing = MATCH_BIT_TIMING(timing, bit, tolerance);
  53. bool double_timing = MATCH_BIT_TIMING(timing, 2*bit, tolerance);
  54. bool triple_timing = MATCH_BIT_TIMING(timing, 3*bit, tolerance);
  55. if (decoder->databit_cnt == 4) {
  56. furi_assert(decoder->timings_cnt == 1);
  57. furi_assert(decoder->switch_detect == true);
  58. if (single_timing ^ triple_timing) {
  59. --decoder->timings_cnt;
  60. ++decoder->databit_cnt;
  61. decoder->data[0] |= (single_timing ? !decoder->level : decoder->level) << 4;
  62. status = IrdaStatusOk;
  63. }
  64. } else if (decoder->databit_cnt == 5) {
  65. if (single_timing || triple_timing) {
  66. if (triple_timing)
  67. decoder->timings[0] = bit;
  68. decoder->switch_detect = false;
  69. status = irda_common_decode_manchester(decoder);
  70. } else if (double_timing) {
  71. --decoder->timings_cnt;
  72. status = IrdaStatusOk;
  73. }
  74. } else {
  75. status = irda_common_decode_manchester(decoder);
  76. }
  77. return status;
  78. }
  79. void* irda_decoder_rc6_alloc(void) {
  80. IrdaRc6Decoder* decoder = furi_alloc(sizeof(IrdaRc6Decoder));
  81. decoder->toggle = false;
  82. decoder->common_decoder = irda_common_decoder_alloc(&protocol_rc6);
  83. irda_common_decoder_set_context(decoder->common_decoder, decoder);
  84. return decoder;
  85. }
  86. IrdaMessage* irda_decoder_rc6_decode(void* decoder, bool level, uint32_t duration) {
  87. IrdaRc6Decoder* decoder_rc6 = decoder;
  88. return irda_common_decode(decoder_rc6->common_decoder, level, duration);
  89. }
  90. void irda_decoder_rc6_free(void* decoder) {
  91. IrdaRc6Decoder* decoder_rc6 = decoder;
  92. irda_common_decoder_free(decoder_rc6->common_decoder);
  93. free(decoder_rc6);
  94. }
  95. void irda_decoder_rc6_reset(void* decoder) {
  96. IrdaRc6Decoder* decoder_rc6 = decoder;
  97. irda_common_decoder_reset(decoder_rc6->common_decoder);
  98. }