irda_encoder_nec.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "furi/check.h"
  2. #include "irda.h"
  3. #include "common/irda_common_i.h"
  4. #include <stdint.h>
  5. #include "../irda_i.h"
  6. #include "irda_protocol_defs_i.h"
  7. #include <furi.h>
  8. static const uint32_t repeat_timings[] = {
  9. IRDA_NEC_REPEAT_PERIOD - IRDA_NEC_REPEAT_MARK - IRDA_NEC_REPEAT_SPACE - IRDA_NEC_BIT1_MARK,
  10. IRDA_NEC_REPEAT_MARK,
  11. IRDA_NEC_REPEAT_SPACE,
  12. IRDA_NEC_BIT1_MARK,
  13. };
  14. void irda_encoder_nec_reset(void* encoder_ptr, const IrdaMessage* message) {
  15. furi_assert(encoder_ptr);
  16. furi_assert(message);
  17. IrdaCommonEncoder* encoder = encoder_ptr;
  18. irda_common_encoder_reset(encoder);
  19. uint32_t* data1 = (void*) encoder->data;
  20. uint32_t* data2 = data1 + 1;
  21. if (message->protocol == IrdaProtocolNEC) {
  22. uint8_t address = message->address;
  23. uint8_t address_inverse = ~address;
  24. uint8_t command = message->command;
  25. uint8_t command_inverse = ~command;
  26. *data1 = address;
  27. *data1 |= address_inverse << 8;
  28. *data1 |= command << 16;
  29. *data1 |= command_inverse << 24;
  30. encoder->bits_to_encode = 32;
  31. } else if (message->protocol == IrdaProtocolNECext) {
  32. *data1 = (uint16_t) message->address;
  33. *data1 |= (message->command & 0xFFFF) << 16;
  34. encoder->bits_to_encode = 32;
  35. } else if (message->protocol == IrdaProtocolNEC42) {
  36. /* 13 address + 13 inverse address + 8 command + 8 inv command */
  37. *data1 = message->address & 0x1FFFUL;
  38. *data1 |= (~message->address & 0x1FFFUL) << 13;
  39. *data1 |= ((message->command & 0x3FUL) << 26);
  40. *data2 = (message->command & 0xC0UL) >> 6;
  41. *data2 |= (~message->command & 0xFFUL) << 2;
  42. encoder->bits_to_encode = 42;
  43. } else if (message->protocol == IrdaProtocolNEC42ext) {
  44. *data1 = message->address & 0x3FFFFFF;
  45. *data1 |= ((message->command & 0x3F) << 26);
  46. *data2 = (message->command & 0xFFC0) >> 6;
  47. encoder->bits_to_encode = 42;
  48. } else {
  49. furi_assert(0);
  50. }
  51. }
  52. IrdaStatus irda_encoder_nec_encode_repeat(IrdaCommonEncoder* encoder, uint32_t* duration, 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 = IRDA_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 ? IrdaStatusDone : IrdaStatusOk;
  67. }
  68. void* irda_encoder_nec_alloc(void) {
  69. return irda_common_encoder_alloc(&protocol_nec);
  70. }
  71. void irda_encoder_nec_free(void* encoder_ptr) {
  72. irda_common_encoder_free(encoder_ptr);
  73. }
  74. IrdaStatus irda_encoder_nec_encode(void* encoder_ptr, uint32_t* duration, bool* level) {
  75. return irda_common_encode(encoder_ptr, duration, level);
  76. }