infrared.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. #define INFRARED_COMMON_CARRIER_FREQUENCY ((uint32_t)38000)
  8. #define INFRARED_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 INFRARED_RAW_RX_TIMING_DELAY_US 150000
  12. #define INFRARED_RAW_TX_TIMING_DELAY_US 180000
  13. typedef struct InfraredDecoderHandler InfraredDecoderHandler;
  14. typedef struct InfraredEncoderHandler InfraredEncoderHandler;
  15. typedef enum {
  16. InfraredProtocolUnknown = -1,
  17. InfraredProtocolNEC = 0,
  18. InfraredProtocolNECext,
  19. InfraredProtocolNEC42,
  20. InfraredProtocolNEC42ext,
  21. InfraredProtocolSamsung32,
  22. InfraredProtocolRC6,
  23. InfraredProtocolRC5,
  24. InfraredProtocolRC5X,
  25. InfraredProtocolSIRC,
  26. InfraredProtocolSIRC15,
  27. InfraredProtocolSIRC20,
  28. InfraredProtocolMAX,
  29. } InfraredProtocol;
  30. typedef struct {
  31. InfraredProtocol protocol;
  32. uint32_t address;
  33. uint32_t command;
  34. bool repeat;
  35. } InfraredMessage;
  36. typedef enum {
  37. InfraredStatusError,
  38. InfraredStatusOk,
  39. InfraredStatusDone,
  40. InfraredStatusReady,
  41. } InfraredStatus;
  42. /**
  43. * Initialize decoder.
  44. *
  45. * \return returns pointer to INFRARED decoder handler if success, otherwise - error.
  46. */
  47. InfraredDecoderHandler* infrared_alloc_decoder(void);
  48. /**
  49. * Provide to decoder next timing.
  50. *
  51. * \param[in] handler - handler to INFRARED decoders. Should be acquired with \c infrared_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 infrared_free_decoder(), infrared_reset_decoder(),
  59. * infrared_decode(), infrared_check_decoder_ready() calls.
  60. */
  61. const InfraredMessage*
  62. infrared_decode(InfraredDecoderHandler* handler, bool level, uint32_t duration);
  63. /**
  64. * Check whether decoder is ready.
  65. * Functionality is quite similar to infrared_decode(), but with no timing providing.
  66. * Some protocols (e.g. Sony SIRC) has variable payload length, which means we
  67. * can't recognize end of message right after receiving last bit. That's why
  68. * application should call to infrared_check_decoder_ready() after some timeout to
  69. * retrieve decoded message, if so.
  70. *
  71. * \param[in] handler - handler to INFRARED decoders. Should be acquired with \c infrared_alloc_decoder().
  72. * \return if message is ready, returns pointer to decoded message, returns NULL.
  73. * Note: ownership of returned ptr belongs to handler. So pointer is valid
  74. * up to next infrared_free_decoder(), infrared_reset_decoder(),
  75. * infrared_decode(), infrared_check_decoder_ready() calls.
  76. */
  77. const InfraredMessage* infrared_check_decoder_ready(InfraredDecoderHandler* handler);
  78. /**
  79. * Deinitialize decoder and free allocated memory.
  80. *
  81. * \param[in] handler - handler to INFRARED decoders. Should be acquired with \c infrared_alloc_decoder().
  82. */
  83. void infrared_free_decoder(InfraredDecoderHandler* handler);
  84. /**
  85. * Reset INFRARED decoder.
  86. *
  87. * \param[in] handler - handler to INFRARED decoders. Should be acquired with \c infrared_alloc_decoder().
  88. */
  89. void infrared_reset_decoder(InfraredDecoderHandler* handler);
  90. /**
  91. * Get protocol name by protocol enum.
  92. *
  93. * \param[in] protocol - protocol identifier.
  94. * \return string to protocol name.
  95. */
  96. const char* infrared_get_protocol_name(InfraredProtocol protocol);
  97. /**
  98. * Get protocol enum by protocol name.
  99. *
  100. * \param[in] protocol_name - string to protocol name.
  101. * \return protocol identifier.
  102. */
  103. InfraredProtocol infrared_get_protocol_by_name(const char* protocol_name);
  104. /**
  105. * Get address length by protocol enum.
  106. *
  107. * \param[in] protocol - protocol identifier.
  108. * \return length of address in bits.
  109. */
  110. uint8_t infrared_get_protocol_address_length(InfraredProtocol protocol);
  111. /**
  112. * Get command length by protocol enum.
  113. *
  114. * \param[in] protocol - protocol identifier.
  115. * \return length of command in bits.
  116. */
  117. uint8_t infrared_get_protocol_command_length(InfraredProtocol protocol);
  118. /**
  119. * Checks whether protocol valid.
  120. *
  121. * \param[in] protocol - protocol identifier.
  122. * \return true if protocol is valid, false otherwise.
  123. */
  124. bool infrared_is_protocol_valid(InfraredProtocol protocol);
  125. /**
  126. * Allocate INFRARED encoder.
  127. *
  128. * \return encoder handler.
  129. */
  130. InfraredEncoderHandler* infrared_alloc_encoder(void);
  131. /**
  132. * Free encoder handler previously allocated with \c infrared_alloc_encoder().
  133. *
  134. * \param[in] handler - handler to INFRARED encoder. Should be acquired with \c infrared_alloc_encoder().
  135. */
  136. void infrared_free_encoder(InfraredEncoderHandler* handler);
  137. /**
  138. * Encode previously set INFRARED message.
  139. * Usage:
  140. * 1) alloc with \c infrared_alloc_encoder()
  141. * 2) set message to encode with \c infrared_reset_encoder()
  142. * 3) call for \c infrared_encode() to continuously get one at a time timings.
  143. * 4) when \c infrared_encode() returns InfraredStatusDone, it means new message is fully encoded.
  144. * 5) to encode additional timings, just continue calling \c infrared_encode().
  145. *
  146. * \param[in] handler - handler to INFRARED encoder. Should be acquired with \c infrared_alloc_encoder().
  147. * \param[out] duration - encoded timing.
  148. * \param[out] level - encoded level.
  149. *
  150. * \return status of encode operation.
  151. */
  152. InfraredStatus infrared_encode(InfraredEncoderHandler* handler, uint32_t* duration, bool* level);
  153. /**
  154. * Reset INFRARED encoder and set new message to encode. If it's not called after receiveing
  155. * InfraredStatusDone in \c infrared_encode(), encoder will encode repeat messages
  156. * till the end of time.
  157. *
  158. * \param[in] handler - handler to INFRARED encoder. Should be acquired with \c infrared_alloc_encoder().
  159. * \param[in] message - message to encode.
  160. */
  161. void infrared_reset_encoder(InfraredEncoderHandler* handler, const InfraredMessage* message);
  162. /**
  163. * Get PWM frequency value for selected protocol
  164. *
  165. * \param[in] protocol - protocol to get from PWM frequency
  166. *
  167. * \return frequency
  168. */
  169. uint32_t infrared_get_protocol_frequency(InfraredProtocol protocol);
  170. /**
  171. * Get PWM duty cycle value for selected protocol
  172. *
  173. * \param[in] protocol - protocol to get from PWM duty cycle
  174. *
  175. * \return duty cycle
  176. */
  177. float infrared_get_protocol_duty_cycle(InfraredProtocol protocol);
  178. #ifdef __cplusplus
  179. }
  180. #endif