infrared.h 6.8 KB

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