irda_decoder_rc5.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. } IrdaRc5Decoder;
  12. IrdaMessage* irda_decoder_rc5_check_ready(void* ctx) {
  13. IrdaRc5Decoder* decoder = ctx;
  14. return irda_common_decoder_check_ready(decoder->common_decoder);
  15. }
  16. bool irda_decoder_rc5_interpret(IrdaCommonDecoder* decoder) {
  17. furi_assert(decoder);
  18. bool result = false;
  19. uint32_t* data = (void*) &decoder->data[0];
  20. /* Manchester (inverse):
  21. * 0->1 : 1
  22. * 1->0 : 0
  23. */
  24. decoder->data[0] = ~decoder->data[0];
  25. decoder->data[1] = ~decoder->data[1];
  26. // MSB first
  27. uint8_t address = reverse((uint8_t) decoder->data[0]) & 0x1F;
  28. uint8_t command = (reverse((uint8_t) decoder->data[1]) >> 2) & 0x3F;
  29. bool start_bit1 = *data & 0x01;
  30. bool start_bit2 = *data & 0x02;
  31. bool toggle = !!(*data & 0x04);
  32. if (start_bit1 == 1) {
  33. IrdaProtocol protocol = start_bit2 ? IrdaProtocolRC5 : IrdaProtocolRC5X;
  34. IrdaMessage* message = &decoder->message;
  35. IrdaRc5Decoder *rc5_decoder = decoder->context;
  36. bool *prev_toggle = &rc5_decoder->toggle;
  37. if ((message->address == address)
  38. && (message->command == command)
  39. && (message->protocol == protocol)) {
  40. message->repeat = (toggle == *prev_toggle);
  41. } else {
  42. message->repeat = false;
  43. }
  44. *prev_toggle = toggle;
  45. message->command = command;
  46. message->address = address;
  47. message->protocol = protocol;
  48. result = true;
  49. }
  50. return result;
  51. }
  52. void* irda_decoder_rc5_alloc(void) {
  53. IrdaRc5Decoder* decoder = furi_alloc(sizeof(IrdaRc5Decoder));
  54. decoder->toggle = false;
  55. decoder->common_decoder = irda_common_decoder_alloc(&protocol_rc5);
  56. decoder->common_decoder->context = decoder;
  57. return decoder;
  58. }
  59. IrdaMessage* irda_decoder_rc5_decode(void* decoder, bool level, uint32_t duration) {
  60. IrdaRc5Decoder* decoder_rc5 = decoder;
  61. return irda_common_decode(decoder_rc5->common_decoder, level, duration);
  62. }
  63. void irda_decoder_rc5_free(void* decoder) {
  64. IrdaRc5Decoder* decoder_rc5 = decoder;
  65. irda_common_decoder_free(decoder_rc5->common_decoder);
  66. free(decoder_rc5);
  67. }
  68. void irda_decoder_rc5_reset(void* decoder) {
  69. IrdaRc5Decoder* decoder_rc5 = decoder;
  70. irda_common_decoder_reset(decoder_rc5->common_decoder);
  71. }