irda_decoder_rc6.c 3.7 KB

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