irda.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. #define IRDA_COMMON_CARRIER_FREQUENCY 38000
  8. #define IRDA_COMMON_DUTY_CYCLE 0.33
  9. /* if we want to see splitted raw signals during brutforce,
  10. * we have to have RX raw timing delay less than TX */
  11. #define IRDA_RAW_RX_TIMING_DELAY_US 150000
  12. #define IRDA_RAW_TX_TIMING_DELAY_US 180000
  13. typedef struct IrdaDecoderHandler IrdaDecoderHandler;
  14. typedef struct IrdaEncoderHandler IrdaEncoderHandler;
  15. // Do not change protocol order, as it can be saved into memory and fw update can be performed!
  16. typedef enum {
  17. IrdaProtocolUnknown = -1,
  18. IrdaProtocolNEC = 0,
  19. IrdaProtocolNECext = 1,
  20. IrdaProtocolSamsung32 = 2,
  21. IrdaProtocolRC6 = 3,
  22. } IrdaProtocol;
  23. typedef struct {
  24. IrdaProtocol protocol;
  25. uint32_t address;
  26. uint32_t command;
  27. bool repeat;
  28. } IrdaMessage;
  29. typedef enum {
  30. IrdaStatusError,
  31. IrdaStatusOk,
  32. IrdaStatusDone,
  33. IrdaStatusReady,
  34. } IrdaStatus;
  35. /**
  36. * Initialize decoder.
  37. *
  38. * \return returns pointer to IRDA decoder handler if success, otherwise - error.
  39. */
  40. IrdaDecoderHandler* irda_alloc_decoder(void);
  41. /**
  42. * Provide to decoder next timing.
  43. *
  44. * \param[in] handler - handler to IRDA decoders. Should be acquired with \c irda_alloc_decoder().
  45. * \param[in] level - high(true) or low(false) level of input signal to analyze.
  46. * it should alternate every call, otherwise it is an error case,
  47. * and decoder resets its state and start decoding from the start.
  48. * \param[in] duration - duration of steady high/low input signal.
  49. * \return if message is ready, returns pointer to decoded message, returns NULL.
  50. */
  51. const IrdaMessage* irda_decode(IrdaDecoderHandler* handler, bool level, uint32_t duration);
  52. /**
  53. * Deinitialize decoder and free allocated memory.
  54. *
  55. * \param[in] handler - handler to IRDA decoders. Should be acquired with \c irda_alloc_decoder().
  56. */
  57. void irda_free_decoder(IrdaDecoderHandler* handler);
  58. /**
  59. * Reset IRDA decoder.
  60. *
  61. * \param[in] handler - handler to IRDA decoders. Should be acquired with \c irda_alloc_decoder().
  62. */
  63. void irda_reset_decoder(IrdaDecoderHandler* handler);
  64. /**
  65. * Get protocol name by protocol enum.
  66. *
  67. * \param[in] protocol - protocol identifier.
  68. * \return string to protocol name.
  69. */
  70. const char* irda_get_protocol_name(IrdaProtocol protocol);
  71. /**
  72. * Get protocol enum by protocol name.
  73. *
  74. * \param[in] protocol_name - string to protocol name.
  75. * \return protocol identifier.
  76. */
  77. IrdaProtocol irda_get_protocol_by_name(const char* protocol_name);
  78. /**
  79. * Get address length by protocol enum.
  80. *
  81. * \param[in] protocol - protocol identifier.
  82. * \return length of address in nibbles.
  83. */
  84. uint8_t irda_get_protocol_address_length(IrdaProtocol protocol);
  85. /**
  86. * Get command length by protocol enum.
  87. *
  88. * \param[in] protocol - protocol identifier.
  89. * \return length of command in nibbles.
  90. */
  91. uint8_t irda_get_protocol_command_length(IrdaProtocol protocol);
  92. /**
  93. * Checks whether protocol valid.
  94. *
  95. * \param[in] protocol - protocol identifier.
  96. * \return true if protocol is valid, false otherwise.
  97. */
  98. bool irda_is_protocol_valid(IrdaProtocol protocol);
  99. /**
  100. * Allocate IRDA encoder.
  101. *
  102. * \return encoder handler.
  103. */
  104. IrdaEncoderHandler* irda_alloc_encoder(void);
  105. /**
  106. * Free encoder handler previously allocated with \c irda_alloc_encoder().
  107. *
  108. * \param[in] handler - handler to IRDA encoder. Should be acquired with \c irda_alloc_encoder().
  109. */
  110. void irda_free_encoder(IrdaEncoderHandler* handler);
  111. /**
  112. * Encode previously set IRDA message.
  113. * Usage:
  114. * 1) alloc with \c irda_alloc_encoder()
  115. * 2) set message to encode with \c irda_reset_encoder()
  116. * 3) call for \c irda_encode() to continuously get one at a time timings.
  117. * 4) when \c irda_encode() returns IrdaStatusDone, it means new message is fully encoded.
  118. * 5) to encode additional timings, just continue calling \c irda_encode().
  119. *
  120. * \param[in] handler - handler to IRDA encoder. Should be acquired with \c irda_alloc_encoder().
  121. * \param[out] duration - encoded timing.
  122. * \param[out] level - encoded level.
  123. *
  124. * \return status of encode operation.
  125. */
  126. IrdaStatus irda_encode(IrdaEncoderHandler* handler, uint32_t* duration, bool* level);
  127. /**
  128. * Reset IRDA encoder and set new message to encode. If it's not called after receiveing
  129. * IrdaStatusDone in \c irda_encode(), encoder will encode repeat messages
  130. * till the end of time.
  131. *
  132. * \param[in] handler - handler to IRDA encoder. Should be acquired with \c irda_alloc_encoder().
  133. * \param[in] message - message to encode.
  134. */
  135. void irda_reset_encoder(IrdaEncoderHandler* handler, const IrdaMessage* message);
  136. /**
  137. * Get PWM frequency value for selected protocol
  138. *
  139. * \param[in] protocol - protocol to get from PWM frequency
  140. *
  141. * \return frequency
  142. */
  143. uint32_t irda_get_protocol_frequency(IrdaProtocol protocol);
  144. /**
  145. * Get PWM duty cycle value for selected protocol
  146. *
  147. * \param[in] protocol - protocol to get from PWM duty cycle
  148. *
  149. * \return duty cycle
  150. */
  151. float irda_get_protocol_duty_cycle(IrdaProtocol protocol);
  152. #ifdef __cplusplus
  153. }
  154. #endif