irda.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 ((uint32_t)38000)
  8. #define IRDA_COMMON_DUTY_CYCLE ((float)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. typedef enum {
  16. IrdaProtocolUnknown = -1,
  17. IrdaProtocolNEC = 0,
  18. IrdaProtocolNECext,
  19. IrdaProtocolNEC42,
  20. IrdaProtocolNEC42ext,
  21. IrdaProtocolSamsung32,
  22. IrdaProtocolRC6,
  23. IrdaProtocolRC5,
  24. IrdaProtocolRC5X,
  25. IrdaProtocolSIRC,
  26. IrdaProtocolSIRC15,
  27. IrdaProtocolSIRC20,
  28. IrdaProtocolMAX,
  29. } IrdaProtocol;
  30. typedef struct {
  31. IrdaProtocol protocol;
  32. uint32_t address;
  33. uint32_t command;
  34. bool repeat;
  35. } IrdaMessage;
  36. typedef enum {
  37. IrdaStatusError,
  38. IrdaStatusOk,
  39. IrdaStatusDone,
  40. IrdaStatusReady,
  41. } IrdaStatus;
  42. /**
  43. * Initialize decoder.
  44. *
  45. * \return returns pointer to IRDA decoder handler if success, otherwise - error.
  46. */
  47. IrdaDecoderHandler* irda_alloc_decoder(void);
  48. /**
  49. * Provide to decoder next timing.
  50. *
  51. * \param[in] handler - handler to IRDA decoders. Should be acquired with \c irda_alloc_decoder().
  52. * \param[in] level - high(true) or low(false) level of input signal to analyze.
  53. * it should alternate every call, otherwise it is an error case,
  54. * and decoder resets its state and start decoding from the start.
  55. * \param[in] duration - duration of steady high/low input signal.
  56. * \return if message is ready, returns pointer to decoded message, returns NULL.
  57. * Note: ownership of returned ptr belongs to handler. So pointer is valid
  58. * up to next irda_free_decoder(), irda_reset_decoder(),
  59. * irda_decode(), irda_check_decoder_ready() calls.
  60. */
  61. const IrdaMessage* irda_decode(IrdaDecoderHandler* handler, bool level, uint32_t duration);
  62. /**
  63. * Check whether decoder is ready.
  64. * Functionality is quite similar to irda_decode(), but with no timing providing.
  65. * Some protocols (e.g. Sony SIRC) has variable payload length, which means we
  66. * can't recognize end of message right after receiving last bit. That's why
  67. * application should call to irda_check_decoder_ready() after some timeout to
  68. * retrieve decoded message, if so.
  69. *
  70. * \param[in] handler - handler to IRDA decoders. Should be acquired with \c irda_alloc_decoder().
  71. * \return if message is ready, returns pointer to decoded message, returns NULL.
  72. * Note: ownership of returned ptr belongs to handler. So pointer is valid
  73. * up to next irda_free_decoder(), irda_reset_decoder(),
  74. * irda_decode(), irda_check_decoder_ready() calls.
  75. */
  76. const IrdaMessage* irda_check_decoder_ready(IrdaDecoderHandler* handler);
  77. /**
  78. * Deinitialize decoder and free allocated memory.
  79. *
  80. * \param[in] handler - handler to IRDA decoders. Should be acquired with \c irda_alloc_decoder().
  81. */
  82. void irda_free_decoder(IrdaDecoderHandler* handler);
  83. /**
  84. * Reset IRDA decoder.
  85. *
  86. * \param[in] handler - handler to IRDA decoders. Should be acquired with \c irda_alloc_decoder().
  87. */
  88. void irda_reset_decoder(IrdaDecoderHandler* handler);
  89. /**
  90. * Get protocol name by protocol enum.
  91. *
  92. * \param[in] protocol - protocol identifier.
  93. * \return string to protocol name.
  94. */
  95. const char* irda_get_protocol_name(IrdaProtocol protocol);
  96. /**
  97. * Get protocol enum by protocol name.
  98. *
  99. * \param[in] protocol_name - string to protocol name.
  100. * \return protocol identifier.
  101. */
  102. IrdaProtocol irda_get_protocol_by_name(const char* protocol_name);
  103. /**
  104. * Get address length by protocol enum.
  105. *
  106. * \param[in] protocol - protocol identifier.
  107. * \return length of address in bits.
  108. */
  109. uint8_t irda_get_protocol_address_length(IrdaProtocol protocol);
  110. /**
  111. * Get command length by protocol enum.
  112. *
  113. * \param[in] protocol - protocol identifier.
  114. * \return length of command in bits.
  115. */
  116. uint8_t irda_get_protocol_command_length(IrdaProtocol protocol);
  117. /**
  118. * Checks whether protocol valid.
  119. *
  120. * \param[in] protocol - protocol identifier.
  121. * \return true if protocol is valid, false otherwise.
  122. */
  123. bool irda_is_protocol_valid(IrdaProtocol protocol);
  124. /**
  125. * Allocate IRDA encoder.
  126. *
  127. * \return encoder handler.
  128. */
  129. IrdaEncoderHandler* irda_alloc_encoder(void);
  130. /**
  131. * Free encoder handler previously allocated with \c irda_alloc_encoder().
  132. *
  133. * \param[in] handler - handler to IRDA encoder. Should be acquired with \c irda_alloc_encoder().
  134. */
  135. void irda_free_encoder(IrdaEncoderHandler* handler);
  136. /**
  137. * Encode previously set IRDA message.
  138. * Usage:
  139. * 1) alloc with \c irda_alloc_encoder()
  140. * 2) set message to encode with \c irda_reset_encoder()
  141. * 3) call for \c irda_encode() to continuously get one at a time timings.
  142. * 4) when \c irda_encode() returns IrdaStatusDone, it means new message is fully encoded.
  143. * 5) to encode additional timings, just continue calling \c irda_encode().
  144. *
  145. * \param[in] handler - handler to IRDA encoder. Should be acquired with \c irda_alloc_encoder().
  146. * \param[out] duration - encoded timing.
  147. * \param[out] level - encoded level.
  148. *
  149. * \return status of encode operation.
  150. */
  151. IrdaStatus irda_encode(IrdaEncoderHandler* handler, uint32_t* duration, bool* level);
  152. /**
  153. * Reset IRDA encoder and set new message to encode. If it's not called after receiveing
  154. * IrdaStatusDone in \c irda_encode(), encoder will encode repeat messages
  155. * till the end of time.
  156. *
  157. * \param[in] handler - handler to IRDA encoder. Should be acquired with \c irda_alloc_encoder().
  158. * \param[in] message - message to encode.
  159. */
  160. void irda_reset_encoder(IrdaEncoderHandler* handler, const IrdaMessage* message);
  161. /**
  162. * Get PWM frequency value for selected protocol
  163. *
  164. * \param[in] protocol - protocol to get from PWM frequency
  165. *
  166. * \return frequency
  167. */
  168. uint32_t irda_get_protocol_frequency(IrdaProtocol protocol);
  169. /**
  170. * Get PWM duty cycle value for selected protocol
  171. *
  172. * \param[in] protocol - protocol to get from PWM duty cycle
  173. *
  174. * \return duty cycle
  175. */
  176. float irda_get_protocol_duty_cycle(IrdaProtocol protocol);
  177. #ifdef __cplusplus
  178. }
  179. #endif