infrared_encoder_nec.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "infrared_protocol_nec_i.h"
  2. #include <core/core_defines.h>
  3. #include <core/check.h>
  4. static const uint32_t repeat_timings[] = {
  5. INFRARED_NEC_REPEAT_PERIOD - INFRARED_NEC_REPEAT_MARK - INFRARED_NEC_REPEAT_SPACE -
  6. INFRARED_NEC_BIT1_MARK,
  7. INFRARED_NEC_REPEAT_MARK,
  8. INFRARED_NEC_REPEAT_SPACE,
  9. INFRARED_NEC_BIT1_MARK,
  10. };
  11. void infrared_encoder_nec_reset(void* encoder_ptr, const InfraredMessage* message) {
  12. furi_assert(encoder_ptr);
  13. furi_assert(message);
  14. InfraredCommonEncoder* encoder = encoder_ptr;
  15. infrared_common_encoder_reset(encoder);
  16. uint32_t* data1 = (void*)encoder->data;
  17. uint32_t* data2 = data1 + 1;
  18. if(message->protocol == InfraredProtocolNEC) {
  19. uint8_t address = message->address;
  20. uint8_t address_inverse = ~address;
  21. uint8_t command = message->command;
  22. uint8_t command_inverse = ~command;
  23. *data1 = address;
  24. *data1 |= address_inverse << 8;
  25. *data1 |= command << 16;
  26. *data1 |= command_inverse << 24;
  27. encoder->bits_to_encode = 32;
  28. } else if(message->protocol == InfraredProtocolNECext) {
  29. *data1 = (uint16_t)message->address;
  30. *data1 |= (message->command & 0xFFFF) << 16;
  31. encoder->bits_to_encode = 32;
  32. } else if(message->protocol == InfraredProtocolNEC42) {
  33. /* 13 address + 13 inverse address + 8 command + 8 inv command */
  34. *data1 = message->address & 0x1FFFUL;
  35. *data1 |= (~message->address & 0x1FFFUL) << 13;
  36. *data1 |= ((message->command & 0x3FUL) << 26);
  37. *data2 = (message->command & 0xC0UL) >> 6;
  38. *data2 |= (~message->command & 0xFFUL) << 2;
  39. encoder->bits_to_encode = 42;
  40. } else if(message->protocol == InfraredProtocolNEC42ext) {
  41. *data1 = message->address & 0x3FFFFFF;
  42. *data1 |= ((message->command & 0x3F) << 26);
  43. *data2 = (message->command & 0xFFC0) >> 6;
  44. encoder->bits_to_encode = 42;
  45. } else {
  46. furi_assert(0);
  47. }
  48. }
  49. InfraredStatus infrared_encoder_nec_encode_repeat(
  50. InfraredCommonEncoder* encoder,
  51. uint32_t* duration,
  52. bool* level) {
  53. furi_assert(encoder);
  54. /* space + 2 timings preambule + payload + stop bit */
  55. uint32_t timings_encoded_up_to_repeat = 1 + 2 + encoder->bits_to_encode * 2 + 1;
  56. uint32_t repeat_cnt = encoder->timings_encoded - timings_encoded_up_to_repeat;
  57. furi_assert(encoder->timings_encoded >= timings_encoded_up_to_repeat);
  58. if(repeat_cnt > 0) {
  59. *duration = repeat_timings[repeat_cnt % COUNT_OF(repeat_timings)];
  60. } else {
  61. *duration = INFRARED_NEC_REPEAT_PERIOD - encoder->timings_sum;
  62. }
  63. *level = repeat_cnt % 2;
  64. ++encoder->timings_encoded;
  65. bool done = (!((repeat_cnt + 1) % COUNT_OF(repeat_timings)));
  66. return done ? InfraredStatusDone : InfraredStatusOk;
  67. }
  68. void* infrared_encoder_nec_alloc(void) {
  69. return infrared_common_encoder_alloc(&infrared_protocol_nec);
  70. }
  71. void infrared_encoder_nec_free(void* encoder_ptr) {
  72. infrared_common_encoder_free(encoder_ptr);
  73. }
  74. InfraredStatus infrared_encoder_nec_encode(void* encoder_ptr, uint32_t* duration, bool* level) {
  75. return infrared_common_encode(encoder_ptr, duration, level);
  76. }