irda.h 6.3 KB

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