irda_decoder_rc5.c 2.3 KB

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