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. 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) && (message->command == command) &&
  31. (message->protocol == IrdaProtocolRC6)) {
  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 = IrdaProtocolRC6;
  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. IrdaStatus
  51. 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) timing = bit;
  69. decoder->switch_detect = false;
  70. status = irda_common_decode_manchester(decoder, level, timing);
  71. } else if(double_timing) {
  72. status = IrdaStatusOk;
  73. }
  74. } else {
  75. status = irda_common_decode_manchester(decoder, level, timing);
  76. }
  77. return status;
  78. }
  79. void* irda_decoder_rc6_alloc(void) {
  80. IrdaRc6Decoder* decoder = malloc(sizeof(IrdaRc6Decoder));
  81. decoder->toggle = false;
  82. decoder->common_decoder = irda_common_decoder_alloc(&protocol_rc6);
  83. decoder->common_decoder->context = 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. }