infrared_decoder_rc5.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "infrared_protocol_rc5_i.h"
  2. #include <stdlib.h>
  3. #include <core/check.h>
  4. typedef struct {
  5. InfraredCommonDecoder* common_decoder;
  6. bool toggle;
  7. } InfraredRc5Decoder;
  8. InfraredMessage* infrared_decoder_rc5_check_ready(void* ctx) {
  9. InfraredRc5Decoder* decoder = ctx;
  10. return infrared_common_decoder_check_ready(decoder->common_decoder);
  11. }
  12. bool infrared_decoder_rc5_interpret(InfraredCommonDecoder* 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. InfraredProtocol protocol = start_bit2 ? InfraredProtocolRC5 : InfraredProtocolRC5X;
  30. InfraredMessage* message = &decoder->message;
  31. InfraredRc5Decoder* rc5_decoder = decoder->context;
  32. bool* prev_toggle = &rc5_decoder->toggle;
  33. if((message->address == address) && (message->command == command) &&
  34. (message->protocol == protocol)) {
  35. message->repeat = (toggle == *prev_toggle);
  36. } else {
  37. message->repeat = false;
  38. }
  39. *prev_toggle = toggle;
  40. message->command = command;
  41. message->address = address;
  42. message->protocol = protocol;
  43. result = true;
  44. }
  45. return result;
  46. }
  47. void* infrared_decoder_rc5_alloc(void) {
  48. InfraredRc5Decoder* decoder = malloc(sizeof(InfraredRc5Decoder));
  49. decoder->toggle = false;
  50. decoder->common_decoder = infrared_common_decoder_alloc(&infrared_protocol_rc5);
  51. decoder->common_decoder->context = decoder;
  52. return decoder;
  53. }
  54. InfraredMessage* infrared_decoder_rc5_decode(void* decoder, bool level, uint32_t duration) {
  55. InfraredRc5Decoder* decoder_rc5 = decoder;
  56. return infrared_common_decode(decoder_rc5->common_decoder, level, duration);
  57. }
  58. void infrared_decoder_rc5_free(void* decoder) {
  59. InfraredRc5Decoder* decoder_rc5 = decoder;
  60. infrared_common_decoder_free(decoder_rc5->common_decoder);
  61. free(decoder_rc5);
  62. }
  63. void infrared_decoder_rc5_reset(void* decoder) {
  64. InfraredRc5Decoder* decoder_rc5 = decoder;
  65. infrared_common_decoder_reset(decoder_rc5->common_decoder);
  66. }