irda_decoder_rc6.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. message->protocol = IrdaProtocolRC6;
  37. result = true;
  38. }
  39. return result;
  40. }
  41. /*
  42. * RC6 Uses manchester encoding, but it has twice longer
  43. * 4-th bit (toggle bit) time quant, so we need to decode
  44. * it separately and than pass decoding for other bits to
  45. * common manchester decode function.
  46. */
  47. IrdaStatus irda_decoder_rc6_decode_manchester(IrdaCommonDecoder* decoder, bool level, uint32_t timing) {
  48. // 4th bit lasts 2x times more
  49. IrdaStatus status = IrdaStatusError;
  50. uint16_t bit = decoder->protocol->timings.bit1_mark;
  51. uint16_t tolerance = decoder->protocol->timings.bit_tolerance;
  52. bool single_timing = MATCH_TIMING(timing, bit, tolerance);
  53. bool double_timing = MATCH_TIMING(timing, 2*bit, tolerance);
  54. bool triple_timing = MATCH_TIMING(timing, 3*bit, tolerance);
  55. if (decoder->databit_cnt == 4) {
  56. furi_assert(decoder->switch_detect == true);
  57. if (single_timing ^ triple_timing) {
  58. ++decoder->databit_cnt;
  59. decoder->data[0] |= (single_timing ? !level : level) << 4;
  60. status = IrdaStatusOk;
  61. }
  62. } else if (decoder->databit_cnt == 5) {
  63. if (single_timing || triple_timing) {
  64. if (triple_timing)
  65. timing = bit;
  66. decoder->switch_detect = false;
  67. status = irda_common_decode_manchester(decoder, level, timing);
  68. } else if (double_timing) {
  69. status = IrdaStatusOk;
  70. }
  71. } else {
  72. status = irda_common_decode_manchester(decoder, level, timing);
  73. }
  74. return status;
  75. }
  76. void* irda_decoder_rc6_alloc(void) {
  77. IrdaRc6Decoder* decoder = furi_alloc(sizeof(IrdaRc6Decoder));
  78. decoder->toggle = false;
  79. decoder->common_decoder = irda_common_decoder_alloc(&protocol_rc6);
  80. decoder->common_decoder->context = decoder;
  81. return decoder;
  82. }
  83. IrdaMessage* irda_decoder_rc6_decode(void* decoder, bool level, uint32_t duration) {
  84. IrdaRc6Decoder* decoder_rc6 = decoder;
  85. return irda_common_decode(decoder_rc6->common_decoder, level, duration);
  86. }
  87. void irda_decoder_rc6_free(void* decoder) {
  88. IrdaRc6Decoder* decoder_rc6 = decoder;
  89. irda_common_decoder_free(decoder_rc6->common_decoder);
  90. free(decoder_rc6);
  91. }
  92. void irda_decoder_rc6_reset(void* decoder) {
  93. IrdaRc6Decoder* decoder_rc6 = decoder;
  94. irda_common_decoder_reset(decoder_rc6->common_decoder);
  95. }