infrared_decoder_rc5.c 2.6 KB

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